@kyuki3rain/cupola 0.0.0-stage → 0.2.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.
- package/CHANGELOG.md +184 -0
- package/LICENSE +202 -0
- package/README.md +41 -2
- package/dist/adr.js +106 -0
- package/dist/branch-store.js +73 -0
- package/dist/catalog.js +137 -0
- package/dist/check.js +192 -0
- package/dist/cli.js +8 -0
- package/dist/code.js +216 -0
- package/dist/commands/change.js +728 -0
- package/dist/commands/epic.js +311 -0
- package/dist/commands/execution.js +482 -0
- package/dist/commands/install.js +109 -0
- package/dist/commands/locks.js +243 -0
- package/dist/commands/reaction.js +185 -0
- package/dist/commands/revision.js +139 -0
- package/dist/commands/run.js +1021 -0
- package/dist/commands/setup.js +414 -0
- package/dist/commands/spec.js +80 -0
- package/dist/commands/status.js +1457 -0
- package/dist/commands/trace.js +474 -0
- package/dist/commands/wait.js +105 -0
- package/dist/commands/work.js +126 -0
- package/dist/commands/workspace.js +137 -0
- package/dist/commands/worktree.js +422 -0
- package/dist/config.js +353 -0
- package/dist/day.js +29 -0
- package/dist/e2e-project.js +62 -0
- package/dist/e2e.js +207 -0
- package/dist/engine/cc-sdd.js +164 -0
- package/dist/epic.js +379 -0
- package/dist/execution.js +272 -0
- package/dist/feedback.js +20 -0
- package/dist/frontmatter.js +20 -0
- package/dist/git.js +365 -0
- package/dist/host/git.js +224 -0
- package/dist/host/github.js +387 -0
- package/dist/host/host.js +3 -0
- package/dist/host/index.js +63 -0
- package/dist/install.js +182 -0
- package/dist/io.js +46 -0
- package/dist/main.js +866 -0
- package/dist/method/v1.js +56 -0
- package/dist/reaction.js +110 -0
- package/dist/record.js +507 -0
- package/dist/release.js +84 -0
- package/dist/repo.js +13 -0
- package/dist/runbook.js +132 -0
- package/dist/safe-write.js +80 -0
- package/dist/spec.js +171 -0
- package/dist/status.js +321 -0
- package/dist/templates.js +50 -0
- package/dist/trace.js +275 -0
- package/dist/tracker/files.js +99 -0
- package/dist/tracker/github-issues.js +217 -0
- package/dist/tracker/index.js +34 -0
- package/dist/tracker/tracker.js +69 -0
- package/dist/tree.js +59 -0
- package/dist/trust.js +13 -0
- package/dist/version.js +5 -0
- package/dist/work.js +170 -0
- package/dist/workspace.js +324 -0
- package/npm-shrinkwrap.json +2014 -0
- package/package.json +41 -4
- package/skills/cupola-abort/SKILL.md +51 -0
- package/skills/cupola-adr/SKILL.md +54 -0
- package/skills/cupola-ask/SKILL.md +42 -0
- package/skills/cupola-catalog/SKILL.md +41 -0
- package/skills/cupola-catalog-draft/SKILL.md +116 -0
- package/skills/cupola-complete/SKILL.md +107 -0
- package/skills/cupola-decide/SKILL.md +99 -0
- package/skills/cupola-doc/SKILL.md +52 -0
- package/skills/cupola-e2e-setup/SKILL.md +78 -0
- package/skills/cupola-e2e-test/SKILL.md +115 -0
- package/skills/cupola-epic-accept/SKILL.md +166 -0
- package/skills/cupola-epic-create/SKILL.md +160 -0
- package/skills/cupola-epic-revise/SKILL.md +92 -0
- package/skills/cupola-epic-run/SKILL.md +166 -0
- package/skills/cupola-fix/SKILL.md +117 -0
- package/skills/cupola-init/SKILL.md +143 -0
- package/skills/cupola-intake/SKILL.md +95 -0
- package/skills/cupola-operate/SKILL.md +81 -0
- package/skills/cupola-resolve/SKILL.md +93 -0
- package/skills/cupola-respond/SKILL.md +73 -0
- package/skills/cupola-review/SKILL.md +131 -0
- package/skills/cupola-spec-design/SKILL.md +169 -0
- package/skills/cupola-spec-impl/SKILL.md +137 -0
- package/skills/cupola-spec-run/SKILL.md +137 -0
- package/skills/cupola-steering/SKILL.md +53 -0
- package/skills/cupola-try/SKILL.md +38 -0
- package/templates/install/.kiro/steering/e2e.md +48 -0
- package/templates/install/.kiro/steering/operations.md +109 -0
- package/templates/install/.kiro/steering/principles.md +25 -0
- package/templates/install/CLAUDE.md +19 -0
- package/templates/install/adr/TEMPLATE.md +38 -0
- package/templates/install/cc-sdd/patches.json +31 -0
- package/templates/method-1/behavior-area.md +43 -0
- package/templates/method-1/behavior-index.md +16 -0
- package/templates/method-1/card.md +49 -0
- package/templates/method-1/case-draft.md +25 -0
- package/templates/method-1/case.md +38 -0
- package/templates/method-1/catalog-spec.md +33 -0
- package/templates/method-1/changes/adr.md +4 -0
- package/templates/method-1/changes/catalog.md +29 -0
- package/templates/method-1/changes/doc.md +5 -0
- package/templates/method-1/changes/e2e.md +7 -0
- package/templates/method-1/changes/epic.md +30 -0
- package/templates/method-1/changes/fix.md +21 -0
- package/templates/method-1/changes/release.md +23 -0
- package/templates/method-1/changes/spec.md +55 -0
- package/templates/method-1/changes/system.md +13 -0
- package/templates/method-1/cupola-issue-draft.md +17 -0
- package/templates/method-1/epic.md +147 -0
- package/templates/method-1/execution.md +55 -0
- package/templates/method-1/fix-spec.md +34 -0
- package/templates/method-1/fix.md +39 -0
- package/templates/method-1/lock.json +13 -0
- package/templates/method-1/no-go.md +28 -0
- package/templates/method-1/phase-outcome.json +29 -0
- package/templates/method-1/reaction.md +17 -0
- package/templates/method-1/record.json +63 -0
- package/templates/method-1/report.md +27 -0
- package/templates/method-1/runbook.md +45 -0
- package/templates/method-1/runner-result.json +6 -0
- package/templates/methods.json +13 -0
|
@@ -0,0 +1,1457 @@
|
|
|
1
|
+
import { codeChanges } from "../code.js";
|
|
2
|
+
import { epicCardsDir, epicPath, resolveComponents } from "../config.js";
|
|
3
|
+
import { E2E_PROFILE } from "../e2e.js";
|
|
4
|
+
import { projectProfile } from "../e2e-project.js";
|
|
5
|
+
import { EpicFront, parseEpic } from "../epic.js";
|
|
6
|
+
import { git, originDefaultBranch } from "../git.js";
|
|
7
|
+
import { codeHost } from "../host/index.js";
|
|
8
|
+
import { hostReactions, pendingOf, sessionReactions, triesOf } from "../reaction.js";
|
|
9
|
+
import { endingCard, parseRecord, redoFromAcceptance, } from "../record.js";
|
|
10
|
+
import { releaseChanges, releasesOf } from "../release.js";
|
|
11
|
+
import { epicBranchName, specName, specNamesOf } from "../spec.js";
|
|
12
|
+
import { ago, askOf, blockedBy, deriveStatus, withReactions, } from "../status.js";
|
|
13
|
+
import { gitTree } from "../tree.js";
|
|
14
|
+
import { findSpecDocument, isWorkId, MissingWorkError, readCards, readCardsIn, runDir, WORK_BRANCH_PREFIX, workBranchName, } from "../work.js";
|
|
15
|
+
import { locateWork, workspace } from "../workspace.js";
|
|
16
|
+
import { recordsAbort } from "./epic.js";
|
|
17
|
+
import { executionStatus, executionsFor } from "./execution.js";
|
|
18
|
+
import { environmentsInUse } from "./locks.js";
|
|
19
|
+
import { carriedOut, droppedBecause, pendingPlan, redesignToTakeIn } from "./revision.js";
|
|
20
|
+
import { trackerOf } from "./work.js";
|
|
21
|
+
/**
|
|
22
|
+
* Fetches a branch into its tracking ref. Runners of one epic fetch the same
|
|
23
|
+
* refs at once, and git refuses a ref another fetch has locked: the tracking
|
|
24
|
+
* ref that fetch is writing is then as fresh as this one would be.
|
|
25
|
+
*/
|
|
26
|
+
export async function fetchOrKeep(root, branch, tracking) {
|
|
27
|
+
try {
|
|
28
|
+
await git(root, ["fetch", "--quiet", "origin", `+${branch}:${tracking}`]);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (!(await hasRef(root, tracking)))
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function hasRef(root, ref) {
|
|
36
|
+
try {
|
|
37
|
+
await git(root, ["rev-parse", "--verify", "--quiet", ref]);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Where a work item's files are: its branch — origin's copy, or the local one
|
|
46
|
+
* when that holds everything origin has and more (a push that failed) — else
|
|
47
|
+
* the current commit, for a merged item whose branch is gone.
|
|
48
|
+
*/
|
|
49
|
+
export async function workTree(root, branch) {
|
|
50
|
+
const local = `refs/heads/${branch}`;
|
|
51
|
+
const tracking = `refs/remotes/origin/${branch}`;
|
|
52
|
+
const remotes = (await git(root, ["remote"])).split("\n");
|
|
53
|
+
let onOrigin = false;
|
|
54
|
+
if (remotes.includes("origin")) {
|
|
55
|
+
try {
|
|
56
|
+
await git(root, ["ls-remote", "--exit-code", "--heads", "origin", local]);
|
|
57
|
+
onOrigin = true;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
// Exit code 2: origin is reachable and the branch is not on it.
|
|
61
|
+
if (error.code !== 2) {
|
|
62
|
+
throw new Error(`cannot reach origin to read ${branch}; try again when it is reachable`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (onOrigin)
|
|
66
|
+
await fetchOrKeep(root, local, tracking);
|
|
67
|
+
}
|
|
68
|
+
const hasLocal = await hasRef(root, local);
|
|
69
|
+
if (onOrigin) {
|
|
70
|
+
if (hasLocal) {
|
|
71
|
+
try {
|
|
72
|
+
await git(root, ["merge-base", "--is-ancestor", tracking, local]);
|
|
73
|
+
return { tree: gitTree(root, local), ref: local };
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// the local branch lacks origin's commits: origin is the newer account
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { tree: gitTree(root, tracking), ref: tracking };
|
|
80
|
+
}
|
|
81
|
+
if (hasLocal)
|
|
82
|
+
return { tree: gitTree(root, local), ref: local };
|
|
83
|
+
return { tree: gitTree(root, "HEAD"), ref: "HEAD" };
|
|
84
|
+
}
|
|
85
|
+
export async function status(io, id, json,
|
|
86
|
+
/** Only a case: its name may also be a work item's. */
|
|
87
|
+
onlyCase = false) {
|
|
88
|
+
// Cases and epics are the hub's: from another repository, read through it.
|
|
89
|
+
const ws = await workspace(io);
|
|
90
|
+
const root = ws.here.isHub ? ws.root : ws.hub.main;
|
|
91
|
+
const config = ws.config;
|
|
92
|
+
let result;
|
|
93
|
+
if (onlyCase) {
|
|
94
|
+
const found = await caseStatus(io, root, config, id);
|
|
95
|
+
if (!found)
|
|
96
|
+
throw new MissingWorkError(`no case ${id} in the tracker`);
|
|
97
|
+
io.out(json ? JSON.stringify(found) : describe(found));
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
result = await workStatus(io, root, config, id);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
if (!(error instanceof MissingWorkError))
|
|
105
|
+
throw error;
|
|
106
|
+
const found = await caseStatus(io, root, config, id);
|
|
107
|
+
if (!found)
|
|
108
|
+
throw new MissingWorkError(`${error.message}, and no case ${id} in the tracker`);
|
|
109
|
+
result = found;
|
|
110
|
+
}
|
|
111
|
+
io.out(json ? JSON.stringify(result) : describe(result));
|
|
112
|
+
return 0;
|
|
113
|
+
}
|
|
114
|
+
/** Marks a Log line of a case found not solved at resolution, naming the epics judged. */
|
|
115
|
+
const NOT_SOLVED = /<!--\s*cupola:not-solved\s+(\{.*?\})\s*-->/g;
|
|
116
|
+
/** The epics a case's Log says resolution already judged not enough. */
|
|
117
|
+
export function judgedEpics(caseText) {
|
|
118
|
+
const judged = new Set();
|
|
119
|
+
for (const m of caseText.matchAll(NOT_SOLVED)) {
|
|
120
|
+
try {
|
|
121
|
+
const epics = JSON.parse(m[1] ?? "{}").epics;
|
|
122
|
+
if (Array.isArray(epics))
|
|
123
|
+
for (const e of epics)
|
|
124
|
+
judged.add(String(e));
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
// a marker someone edited by hand: it names nothing
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return judged;
|
|
131
|
+
}
|
|
132
|
+
/** The marker that `cupola work reopen` appends to the Log line. */
|
|
133
|
+
export function notSolvedMarker(epics) {
|
|
134
|
+
return `<!-- cupola:not-solved ${JSON.stringify({ epics })} -->`;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* A case's epics with their statuses: the agreed ones — on origin's default
|
|
138
|
+
* branch — that list it, and those under agreement, whose epic change is open
|
|
139
|
+
* and whose draft on its branch lists it. Undefined when the tracker has no
|
|
140
|
+
* such case.
|
|
141
|
+
*/
|
|
142
|
+
export async function caseEpics(io, root, config, id) {
|
|
143
|
+
const doc = await (await trackerOf(io)).show(id);
|
|
144
|
+
if (!doc)
|
|
145
|
+
return undefined;
|
|
146
|
+
// The tracker may find it by another name (an issue number): the epics list the case's id.
|
|
147
|
+
const caseId = doc.id;
|
|
148
|
+
const base = await originDefaultBranch(root);
|
|
149
|
+
if (!base)
|
|
150
|
+
throw new Error("cannot tell origin's default branch, where agreed epics are");
|
|
151
|
+
const ref = `refs/remotes/origin/${base}`;
|
|
152
|
+
await git(root, ["fetch", "--quiet", "origin", `+refs/heads/${base}:${ref}`]);
|
|
153
|
+
const agreed = gitTree(root, ref);
|
|
154
|
+
const judged = judgedEpics(doc.text);
|
|
155
|
+
const epics = [];
|
|
156
|
+
for (const epic of (await agreed.dirs(config.paths.epics)).sort()) {
|
|
157
|
+
const raw = await agreed.read(epicPath(config.paths, epic));
|
|
158
|
+
if (raw === undefined)
|
|
159
|
+
continue;
|
|
160
|
+
const front = EpicFront.safeParse(parseEpic(raw).front);
|
|
161
|
+
if (!front.success || !front.data.cases.includes(caseId))
|
|
162
|
+
continue;
|
|
163
|
+
const of = await workStatus(io, root, config, epic);
|
|
164
|
+
const pending = (of.executions ?? []).filter((e) => e.id === null).map((e) => e.runbook);
|
|
165
|
+
epics.push({
|
|
166
|
+
id: epic,
|
|
167
|
+
status: of.status,
|
|
168
|
+
judged: judged.has(epic),
|
|
169
|
+
...(pending.length > 0 ? { pending } : {}),
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
for (const epic of await epicsUnderAgreement(io, root, config, caseId)) {
|
|
173
|
+
if (epics.some((e) => e.id === epic))
|
|
174
|
+
continue;
|
|
175
|
+
const of = await workStatus(io, root, config, epic);
|
|
176
|
+
epics.push({ id: epic, status: of.status, judged: judged.has(epic) });
|
|
177
|
+
}
|
|
178
|
+
return { doc, epics };
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The epics under agreement that list a case: a draft on its work branch on
|
|
182
|
+
* origin whose change is open. An epic whose change was closed is abandoned;
|
|
183
|
+
* one whose change was merged is agreed, and read from the default branch.
|
|
184
|
+
*/
|
|
185
|
+
async function epicsUnderAgreement(io, root, config, caseId) {
|
|
186
|
+
const prefix = WORK_BRANCH_PREFIX;
|
|
187
|
+
const heads = await git(root, ["ls-remote", "--heads", "origin", `refs/heads/${prefix}*`]);
|
|
188
|
+
const ids = heads
|
|
189
|
+
.split("\n")
|
|
190
|
+
.map((line) => line.split("\t")[1] ?? "")
|
|
191
|
+
.filter((ref) => ref.startsWith(`refs/heads/${prefix}`))
|
|
192
|
+
.map((ref) => ref.slice(`refs/heads/${prefix}`.length))
|
|
193
|
+
.filter(isWorkId);
|
|
194
|
+
if (ids.length === 0)
|
|
195
|
+
return [];
|
|
196
|
+
try {
|
|
197
|
+
await git(root, [
|
|
198
|
+
"fetch",
|
|
199
|
+
"--quiet",
|
|
200
|
+
"origin",
|
|
201
|
+
`+refs/heads/${prefix}*:refs/remotes/origin/${prefix}*`,
|
|
202
|
+
]);
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
// another fetch holds a ref: the tracking refs it writes are as fresh
|
|
206
|
+
}
|
|
207
|
+
const host = (io.host ?? codeHost)(root);
|
|
208
|
+
const found = [];
|
|
209
|
+
for (const id of ids.sort()) {
|
|
210
|
+
const branch = workBranchName(id);
|
|
211
|
+
const ref = `refs/remotes/origin/${branch}`;
|
|
212
|
+
if (!(await hasRef(root, ref)))
|
|
213
|
+
continue;
|
|
214
|
+
const raw = await gitTree(root, ref).read(epicPath(config.paths, id));
|
|
215
|
+
if (raw === undefined)
|
|
216
|
+
continue;
|
|
217
|
+
const front = EpicFront.safeParse(parseEpic(raw).front);
|
|
218
|
+
if (!front.success || !front.data.cases.includes(caseId))
|
|
219
|
+
continue;
|
|
220
|
+
if ((await host.findChange(branch))?.state === "open")
|
|
221
|
+
found.push(id);
|
|
222
|
+
}
|
|
223
|
+
return found;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* The status of a case: open until an epic lists it — agreed, or under
|
|
227
|
+
* agreement with its change open — or an execution works on it; in progress
|
|
228
|
+
* while one is not done, awaiting resolution when all are, and resolved
|
|
229
|
+
* once closed. Work resolution found not enough, aborted epics, and dropped executions
|
|
230
|
+
* no longer count, so the case is open again. Its Log is read for nothing else
|
|
231
|
+
* than what resolution judged.
|
|
232
|
+
*/
|
|
233
|
+
export async function caseStatus(io, root, config, id) {
|
|
234
|
+
const found = await caseEpics(io, root, config, id);
|
|
235
|
+
if (!found)
|
|
236
|
+
return undefined;
|
|
237
|
+
const { doc, epics } = found;
|
|
238
|
+
id = doc.id;
|
|
239
|
+
const judged = judgedEpics(doc.text);
|
|
240
|
+
const executions = (await executionsFor(io, root, config, id)).map((e) => ({
|
|
241
|
+
id: e.id,
|
|
242
|
+
runbook: e.runbook,
|
|
243
|
+
status: e.status.status,
|
|
244
|
+
}));
|
|
245
|
+
const base = {
|
|
246
|
+
work: { kind: "case", id, component: "" },
|
|
247
|
+
change: null,
|
|
248
|
+
cards: [],
|
|
249
|
+
run: null,
|
|
250
|
+
epics,
|
|
251
|
+
executions,
|
|
252
|
+
};
|
|
253
|
+
if (!doc.open) {
|
|
254
|
+
return {
|
|
255
|
+
...base,
|
|
256
|
+
status: "resolved",
|
|
257
|
+
turn: "none",
|
|
258
|
+
gate: null,
|
|
259
|
+
next: { kind: "nothing", text: "Resolved." },
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
const counted = [
|
|
263
|
+
...epics.filter((e) => !e.judged && e.status !== "aborted" && e.status !== "dropped"),
|
|
264
|
+
...executions.filter((e) => !judged.has(e.id) && e.status !== "dropped"),
|
|
265
|
+
];
|
|
266
|
+
if (counted.length === 0) {
|
|
267
|
+
return {
|
|
268
|
+
...base,
|
|
269
|
+
status: "open",
|
|
270
|
+
turn: "none",
|
|
271
|
+
gate: null,
|
|
272
|
+
next: {
|
|
273
|
+
kind: "nothing",
|
|
274
|
+
text: "No epic (agreed or under agreement) or execution works on it; turn it into an epic when you choose.",
|
|
275
|
+
command: `cupola-epic-create ${id}`,
|
|
276
|
+
},
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
const running = counted.filter((e) => e.status !== "done" || (e.pending?.length ?? 0) > 0);
|
|
280
|
+
if (running.length > 0) {
|
|
281
|
+
return {
|
|
282
|
+
...base,
|
|
283
|
+
status: "in-progress",
|
|
284
|
+
turn: "none",
|
|
285
|
+
gate: null,
|
|
286
|
+
next: {
|
|
287
|
+
kind: "nothing",
|
|
288
|
+
text: `It follows its ${executions.length > 0 ? "work" : "epics"}: ${running.map((e) => `${e.id} (${e.status === "done" ? `released; ${e.pending?.join(", ")} not started` : e.status})`).join(", ")}.`,
|
|
289
|
+
},
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
return {
|
|
293
|
+
...base,
|
|
294
|
+
status: "awaiting-resolution",
|
|
295
|
+
turn: "human",
|
|
296
|
+
gate: "resolution",
|
|
297
|
+
next: {
|
|
298
|
+
kind: "gate",
|
|
299
|
+
text: `Its ${executions.length > 0 ? "work is" : "epics are"} done (${counted.map((e) => e.id).join(", ")}); judge whether the problem is gone.`,
|
|
300
|
+
command: `cupola-resolve ${id}`,
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* With every document in the hub, a work's documents and code are
|
|
306
|
+
* merged together, the code first. What is left when they are not: the
|
|
307
|
+
* change to act on and what to do, or undefined when nothing is.
|
|
308
|
+
*/
|
|
309
|
+
async function codeLeft(io, ws, branch, component, hub) {
|
|
310
|
+
if (component === "system" || (hub?.state !== "merged" && hub?.state !== "closed"))
|
|
311
|
+
return undefined;
|
|
312
|
+
const codes = (await codeChanges(io, ws, branch, [component])).flatMap((c) => c.change ? [c.change] : []);
|
|
313
|
+
if (hub.state === "closed") {
|
|
314
|
+
// The code reached main while its documents did not: they have to follow it.
|
|
315
|
+
const merged = codes.find((c) => c.state === "merged");
|
|
316
|
+
return merged
|
|
317
|
+
? {
|
|
318
|
+
change: hub,
|
|
319
|
+
text: `has its code change merged (${merged.url}), but its documents' change was closed (${hub.url}): reopen and merge it, then run the command.`,
|
|
320
|
+
}
|
|
321
|
+
: undefined;
|
|
322
|
+
}
|
|
323
|
+
const draft = codes.find((c) => c.state === "open" && c.draft);
|
|
324
|
+
if (draft)
|
|
325
|
+
return {
|
|
326
|
+
change: draft,
|
|
327
|
+
text: `is merged; its code change is still a draft (${draft.url}): mark it ready and merge it, then run the command.`,
|
|
328
|
+
};
|
|
329
|
+
const open = codes.find((c) => c.state === "open");
|
|
330
|
+
if (open)
|
|
331
|
+
return {
|
|
332
|
+
change: open,
|
|
333
|
+
text: `is merged; merge its code change too (${open.url}), then run the command.`,
|
|
334
|
+
};
|
|
335
|
+
const closed = codes.find((c) => c.state === "closed");
|
|
336
|
+
if (closed)
|
|
337
|
+
return {
|
|
338
|
+
change: closed,
|
|
339
|
+
text: `is merged, but its code change was closed without merging (${closed.url}): the documents describe code that is not there; reopen and merge it, then run the command.`,
|
|
340
|
+
};
|
|
341
|
+
return undefined;
|
|
342
|
+
}
|
|
343
|
+
/** The status of any work item: a spec, fix, or catalog spec, or else an epic. */
|
|
344
|
+
export async function workStatus(io, hubRoot, config, id) {
|
|
345
|
+
// An execution of a runbook lives in the hub; its status comes from its own record.
|
|
346
|
+
const execution = await executionStatus(io, hubRoot, config, id);
|
|
347
|
+
if (execution)
|
|
348
|
+
return execution;
|
|
349
|
+
const branch = workBranchName(id);
|
|
350
|
+
// The work lives in its component's repository; epics, cases, and revisions in the hub.
|
|
351
|
+
const ws = await workspace({ ...io, cwd: hubRoot });
|
|
352
|
+
const hub = hubRoot;
|
|
353
|
+
const located = await locateWork(ws, id, { branch });
|
|
354
|
+
const root = located.slug === ws.here.slug ? hubRoot : located.main;
|
|
355
|
+
const rootOf = (component) => {
|
|
356
|
+
const slug = ws.repoOf(component);
|
|
357
|
+
return slug === located.slug ? root : ws.checkout(slug).main;
|
|
358
|
+
};
|
|
359
|
+
const { tree, ref } = await workTree(root, branch);
|
|
360
|
+
let doc;
|
|
361
|
+
try {
|
|
362
|
+
doc = await findSpecDocument(tree, config, id, located.slug);
|
|
363
|
+
}
|
|
364
|
+
catch (error) {
|
|
365
|
+
if (!(error instanceof MissingWorkError))
|
|
366
|
+
throw error;
|
|
367
|
+
const epic = await epicStatus(io, hub, config, root === hub ? tree : gitTree(hub, "HEAD"), id);
|
|
368
|
+
if (epic)
|
|
369
|
+
return epic;
|
|
370
|
+
// Only a document missing from the branch sends us to the current commit.
|
|
371
|
+
const missing = (e) => e instanceof MissingWorkError
|
|
372
|
+
? new MissingWorkError(`${e.message}, and no ${epicPath(config.paths, id)}`)
|
|
373
|
+
: e;
|
|
374
|
+
// A merged item whose branch is gone is on origin's default branch, which the main
|
|
375
|
+
// checkout (the human's, never pulled by Cupola) may not have caught up with.
|
|
376
|
+
const trees = ref === "HEAD" ? [] : [gitTree(root, "HEAD")];
|
|
377
|
+
const base = await originDefaultBranch(root);
|
|
378
|
+
if (base) {
|
|
379
|
+
try {
|
|
380
|
+
await git(root, [
|
|
381
|
+
"fetch",
|
|
382
|
+
"--quiet",
|
|
383
|
+
"origin",
|
|
384
|
+
`+refs/heads/${base}:refs/remotes/origin/${base}`,
|
|
385
|
+
]);
|
|
386
|
+
trees.push(gitTree(root, `refs/remotes/origin/${base}`));
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
// origin unreachable: the current commit is all there is
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
// A spec of an epic with two or more specs is merged into its epic branch.
|
|
393
|
+
trees.push(...(await epicBranchTrees(root, id)));
|
|
394
|
+
let found;
|
|
395
|
+
let last = error;
|
|
396
|
+
for (const t of trees) {
|
|
397
|
+
try {
|
|
398
|
+
found = await findSpecDocument(t, config, id);
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
catch (e) {
|
|
402
|
+
if (!(e instanceof MissingWorkError))
|
|
403
|
+
throw e;
|
|
404
|
+
last = e;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
if (!found)
|
|
408
|
+
throw missing(last);
|
|
409
|
+
doc = found;
|
|
410
|
+
}
|
|
411
|
+
const raw = await tree.read(`${runDir(id)}/record.json`);
|
|
412
|
+
const record = raw === undefined ? undefined : parseRecord(raw);
|
|
413
|
+
const read = await readCards(tree, id);
|
|
414
|
+
const inEpic = doc.kind === "spec" && doc.epic ? doc.epic : undefined;
|
|
415
|
+
// The epic whose run runs it: a spec's, or a fix spec's its acceptance check added.
|
|
416
|
+
const runBy = (doc.kind === "spec" || doc.kind === "fix-spec") && doc.epic ? doc.epic : undefined;
|
|
417
|
+
const workOf = {
|
|
418
|
+
kind: doc.kind,
|
|
419
|
+
id,
|
|
420
|
+
component: doc.component,
|
|
421
|
+
...(runBy ? { epic: runBy } : {}),
|
|
422
|
+
};
|
|
423
|
+
const cards = inEpic ? await carriedOut(io, hub, id, inEpic, read) : read;
|
|
424
|
+
const host = (io.host ?? codeHost)(root);
|
|
425
|
+
const change = await host.findChange(branch);
|
|
426
|
+
// The only spec of an epic outside the hub is released with its `system` change:
|
|
427
|
+
// merged alone, it is not done until that one is merged too.
|
|
428
|
+
if (inEpic && change?.state === "merged" && located.slug !== ws.hub.slug) {
|
|
429
|
+
const system = await (io.host ?? codeHost)(hub).findChange(workBranchName(`${inEpic}-system`));
|
|
430
|
+
if (system?.state === "open") {
|
|
431
|
+
return {
|
|
432
|
+
work: workOf,
|
|
433
|
+
change: system,
|
|
434
|
+
cards: [],
|
|
435
|
+
run: null,
|
|
436
|
+
status: "awaiting-acceptance",
|
|
437
|
+
turn: "human",
|
|
438
|
+
gate: "acceptance",
|
|
439
|
+
next: {
|
|
440
|
+
kind: "gate",
|
|
441
|
+
text: `${id} is merged; merge its system change too (${system.url}), then run the command.`,
|
|
442
|
+
command: `cupola-complete ${id}`,
|
|
443
|
+
},
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
// With every document in the hub, its code change is merged with it.
|
|
448
|
+
const code = ws.config.documents === "hub"
|
|
449
|
+
? await codeLeft(io, ws, branch, doc.component, change)
|
|
450
|
+
: undefined;
|
|
451
|
+
if (code) {
|
|
452
|
+
return {
|
|
453
|
+
work: workOf,
|
|
454
|
+
change: code.change,
|
|
455
|
+
cards: [],
|
|
456
|
+
run: null,
|
|
457
|
+
status: "awaiting-acceptance",
|
|
458
|
+
turn: "human",
|
|
459
|
+
gate: "acceptance",
|
|
460
|
+
next: {
|
|
461
|
+
kind: "gate",
|
|
462
|
+
text: `${id} ${code.text}`,
|
|
463
|
+
command: `cupola-complete ${id}`,
|
|
464
|
+
},
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
const subject = ref === "HEAD" ? "" : await git(root, ["log", "-1", "--format=%s", ref]);
|
|
468
|
+
// Outside the hub, the epic is read from the hub: the spec's branch has no epics.
|
|
469
|
+
const waitsFor = inEpic
|
|
470
|
+
? await waitingFor(rootOf, ws.config.paths, root === hub ? tree : await hubAgreed(hub), inEpic, doc.component)
|
|
471
|
+
: [];
|
|
472
|
+
const dropped = inEpic ? await droppedBecause(io, hub, inEpic, doc.component) : undefined;
|
|
473
|
+
const redesign = inEpic ? await redesignToTakeIn(hub, tree, id) : undefined;
|
|
474
|
+
// The run takes an agreed redesign in before anything runs: shown as it will run, from the
|
|
475
|
+
// acceptance tests, not as the record says before it is taken in. A phase running now is shown
|
|
476
|
+
// as it is.
|
|
477
|
+
const retaken = redesign !== undefined && record !== undefined && !record.active;
|
|
478
|
+
const shown = retaken ? structuredClone(record) : record;
|
|
479
|
+
if (retaken && shown)
|
|
480
|
+
redoFromAcceptance(shown);
|
|
481
|
+
const pending = inEpic && change?.state !== "merged" && change?.state !== "closed"
|
|
482
|
+
? await pendingPlan(io, hub, inEpic, doc.component)
|
|
483
|
+
: undefined;
|
|
484
|
+
const environment = await environmentWait(root, tree, shown, id);
|
|
485
|
+
// Reactions are read at acceptance only (ready, no run going), so that a run in progress costs nothing.
|
|
486
|
+
const gate = change?.state === "open" && !change.draft && !record
|
|
487
|
+
? await gateReactions(root, host, change, tree, id, ws.config)
|
|
488
|
+
: undefined;
|
|
489
|
+
// A change of an epic's spec closed with nothing recording why: not dropped, but the human's.
|
|
490
|
+
const closedByHand = runBy &&
|
|
491
|
+
change?.state === "closed" &&
|
|
492
|
+
!endingCard(record, cards) &&
|
|
493
|
+
!(await droppedBecause(io, hub, runBy, doc.component))
|
|
494
|
+
? runBy
|
|
495
|
+
: undefined;
|
|
496
|
+
const left = change?.state === "merged" && (await untidy(root, branch));
|
|
497
|
+
const derived = deriveStatus({
|
|
498
|
+
work: workOf,
|
|
499
|
+
change,
|
|
500
|
+
...(change ? { reach: await host.reach(change, id) } : {}),
|
|
501
|
+
record: shown,
|
|
502
|
+
cards,
|
|
503
|
+
...(gate ? { gate } : {}),
|
|
504
|
+
...(closedByHand ? { closedByHand } : {}),
|
|
505
|
+
...(left ? { untidy: true } : {}),
|
|
506
|
+
...(environment ? { environment } : {}),
|
|
507
|
+
// A finish followed by a redesign is not an interrupted finish: the run starts again.
|
|
508
|
+
finished: subject === `cupola: ${id}: finish` && redesign === undefined,
|
|
509
|
+
...(doc.epic && waitsFor.length > 0
|
|
510
|
+
? { waiting: { for: waitsFor, resume: `cupola-epic-run ${doc.epic}` } }
|
|
511
|
+
: {}),
|
|
512
|
+
...(inEpic && change?.state !== "merged" && dropped ? { dropped } : {}),
|
|
513
|
+
...(inEpic && redesign && change?.state === "open" && !change.draft && !record
|
|
514
|
+
? {
|
|
515
|
+
retake: {
|
|
516
|
+
text: `R${redesign} of ${inEpic}, agreed, redesigns it: the epic's run sends its change back and runs it again.`,
|
|
517
|
+
command: `cupola-epic-run ${inEpic}`,
|
|
518
|
+
},
|
|
519
|
+
}
|
|
520
|
+
: {}),
|
|
521
|
+
...(inEpic && pending
|
|
522
|
+
? {
|
|
523
|
+
revision: {
|
|
524
|
+
text: `R${pending.revision} of ${inEpic} plans to ${pending.plan} it; it waits until the revision is agreed (its start gate)`,
|
|
525
|
+
epic: inEpic,
|
|
526
|
+
url: (await (io.host ?? codeHost)(hub).findChange(workBranchName(inEpic)))?.url,
|
|
527
|
+
},
|
|
528
|
+
}
|
|
529
|
+
: {}),
|
|
530
|
+
});
|
|
531
|
+
if (retaken && derived.next.kind === "run") {
|
|
532
|
+
return {
|
|
533
|
+
...derived,
|
|
534
|
+
next: {
|
|
535
|
+
...derived.next,
|
|
536
|
+
text: `${derived.next.text} R${redesign} of ${inEpic}, agreed, redesigns it: the run takes it in first, so the record still shows the phases before it.`,
|
|
537
|
+
},
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
// At acceptance with every document in the hub, its code change is merged first.
|
|
541
|
+
if (derived.gate === "acceptance" && change?.state === "open" && ws.config.documents === "hub") {
|
|
542
|
+
const codes = (await codeChanges(io, ws, branch, [doc.component])).flatMap((c) => c.change?.state === "open" ? [c.change.url] : []);
|
|
543
|
+
if (codes.length > 0)
|
|
544
|
+
return {
|
|
545
|
+
...derived,
|
|
546
|
+
next: {
|
|
547
|
+
...derived.next,
|
|
548
|
+
text: `Its code change goes first: ${codes.join(", ")}. ${derived.next.text.replace(/merge it,/, "merge both, the code change first,")}`,
|
|
549
|
+
},
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
return derived;
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Why a run whose next phase needs shared e2e environments cannot take them
|
|
556
|
+
* now; undefined when it can, or when no phase needs any.
|
|
557
|
+
*/
|
|
558
|
+
async function environmentWait(root, tree, record, id) {
|
|
559
|
+
if (!record || record.active)
|
|
560
|
+
return undefined;
|
|
561
|
+
const phase = record.phases.find((p) => p.state === "running") ??
|
|
562
|
+
record.phases.find((p) => p.state === "pending" || p.state === "redo");
|
|
563
|
+
if (!phase?.needs?.length)
|
|
564
|
+
return undefined;
|
|
565
|
+
const ws = await workspace({ out: () => { }, err: () => { }, cwd: root });
|
|
566
|
+
const project = await projectProfile(ws, root, await tree.read(E2E_PROFILE));
|
|
567
|
+
if (!project.profile)
|
|
568
|
+
return undefined;
|
|
569
|
+
return environmentsInUse(project.rootOf, project.profile, phase.needs, id);
|
|
570
|
+
}
|
|
571
|
+
/** Trees of the epic branches on origin whose epic a spec named `id` may belong to. */
|
|
572
|
+
async function epicBranchTrees(root, id) {
|
|
573
|
+
let out;
|
|
574
|
+
try {
|
|
575
|
+
out = await git(root, ["ls-remote", "--heads", "origin", "refs/heads/epic/*"]);
|
|
576
|
+
}
|
|
577
|
+
catch {
|
|
578
|
+
return [];
|
|
579
|
+
}
|
|
580
|
+
const trees = [];
|
|
581
|
+
for (const line of out.split("\n")) {
|
|
582
|
+
const epic = /refs\/heads\/epic\/(\S+)$/.exec(line)?.[1];
|
|
583
|
+
if (!epic || !id.startsWith(`${epic}-`))
|
|
584
|
+
continue;
|
|
585
|
+
const ref = `refs/remotes/origin/${epicBranchName(epic)}`;
|
|
586
|
+
await fetchOrKeep(root, `refs/heads/${epicBranchName(epic)}`, ref);
|
|
587
|
+
trees.push(gitTree(root, ref));
|
|
588
|
+
}
|
|
589
|
+
return trees;
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* The fix specs an epic's acceptance check added (`<epic>-<component>-fix-<n>`), found by their
|
|
593
|
+
* branches on origin. Merged ones are done and
|
|
594
|
+
* no longer have a branch.
|
|
595
|
+
*/
|
|
596
|
+
export async function fixSpecsOf(root, config, epic, known) {
|
|
597
|
+
const ws = await workspace({ out: () => { }, err: () => { }, cwd: root });
|
|
598
|
+
if (ws.slugs.length === 1)
|
|
599
|
+
return fixSpecsIn(root, config, epic, known);
|
|
600
|
+
const found = [];
|
|
601
|
+
for (const slug of ws.slugs) {
|
|
602
|
+
let main;
|
|
603
|
+
try {
|
|
604
|
+
main = ws.checkout(slug).main;
|
|
605
|
+
}
|
|
606
|
+
catch {
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
found.push(...(await fixSpecsIn(main, config, epic, [...known, ...found])));
|
|
610
|
+
}
|
|
611
|
+
return found;
|
|
612
|
+
}
|
|
613
|
+
async function fixSpecsIn(root, config, epic, known) {
|
|
614
|
+
let out;
|
|
615
|
+
try {
|
|
616
|
+
out = await git(root, ["ls-remote", "--heads", "origin", `refs/heads/cupola/${epic}-*`]);
|
|
617
|
+
}
|
|
618
|
+
catch {
|
|
619
|
+
return [];
|
|
620
|
+
}
|
|
621
|
+
const found = [];
|
|
622
|
+
for (const line of out.split("\n")) {
|
|
623
|
+
const id = /refs\/heads\/cupola\/(\S+)$/.exec(line)?.[1];
|
|
624
|
+
if (!id || known.includes(id) || !/-fix-\d+$/.test(id))
|
|
625
|
+
continue;
|
|
626
|
+
try {
|
|
627
|
+
// Another epic's name may start the same way: the document says whose it is.
|
|
628
|
+
const doc = await findSpecDocument((await workTree(root, workBranchName(id))).tree, config, id);
|
|
629
|
+
if (doc.kind === "fix-spec" && doc.epic === epic)
|
|
630
|
+
found.push(id);
|
|
631
|
+
}
|
|
632
|
+
catch (error) {
|
|
633
|
+
if (error instanceof MissingWorkError)
|
|
634
|
+
continue;
|
|
635
|
+
// A broken document named like another epic's fix spec is that epic's to report, not this
|
|
636
|
+
// one's.
|
|
637
|
+
const component = id.slice(epic.length + 1).replace(/-fix-\d+$/, "");
|
|
638
|
+
if (config.components.some((c) => c.name === component))
|
|
639
|
+
throw error;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
return found;
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* An epic whose specs are all done: *accepting* while its own
|
|
646
|
+
* record on the epic branch runs the acceptance check and opens the release,
|
|
647
|
+
* *awaiting release* once the release change is ready, *done* once it is
|
|
648
|
+
* merged.
|
|
649
|
+
*/
|
|
650
|
+
async function acceptance(io, root, config, id, specs, result) {
|
|
651
|
+
const branch = epicBranchName(id);
|
|
652
|
+
// With several repositories, one release per repository, merged in order.
|
|
653
|
+
const ws = await workspace({ ...io, cwd: root });
|
|
654
|
+
const each = ws.repos.length > 1 ? await releaseChanges(io, ws, id, releasesOf(ws, specs)) : undefined;
|
|
655
|
+
const pendingRelease = each?.find((r) => r.change?.state !== "merged");
|
|
656
|
+
const release = each
|
|
657
|
+
? ((pendingRelease ?? each.at(-1))?.change ?? null)
|
|
658
|
+
: ((await (io.host ?? codeHost)(root).findChange(branch)) ?? null);
|
|
659
|
+
const releases = each?.map((r) => ({
|
|
660
|
+
repo: r.repo.slug,
|
|
661
|
+
order: r.order,
|
|
662
|
+
of: r.of,
|
|
663
|
+
change: r.change,
|
|
664
|
+
}));
|
|
665
|
+
const withRelease = (r) => ({
|
|
666
|
+
...r,
|
|
667
|
+
change: release,
|
|
668
|
+
...(releases ? { releases } : {}),
|
|
669
|
+
});
|
|
670
|
+
if (release?.state === "merged") {
|
|
671
|
+
// cupola-complete deletes the epic branch: while it is on origin, complete has not run.
|
|
672
|
+
const complete = `cupola-complete ${id}`;
|
|
673
|
+
const untidied = await git(root, [
|
|
674
|
+
"ls-remote",
|
|
675
|
+
"--heads",
|
|
676
|
+
"origin",
|
|
677
|
+
`refs/heads/${branch}`,
|
|
678
|
+
]).then((out) => out.trim() !== "", () => false);
|
|
679
|
+
return withRelease(result({
|
|
680
|
+
status: "done",
|
|
681
|
+
turn: untidied ? "human" : "none",
|
|
682
|
+
gate: null,
|
|
683
|
+
next: untidied
|
|
684
|
+
? {
|
|
685
|
+
kind: "run",
|
|
686
|
+
text: `Released: \`${complete}\` tidies up and moves on.`,
|
|
687
|
+
command: complete,
|
|
688
|
+
}
|
|
689
|
+
: { kind: "nothing", text: "Released." },
|
|
690
|
+
}));
|
|
691
|
+
}
|
|
692
|
+
const { tree } = await workTree(root, branch);
|
|
693
|
+
const raw = await tree.read(`${runDir(id)}/record.json`);
|
|
694
|
+
const record = raw === undefined ? undefined : parseRecord(raw);
|
|
695
|
+
// A wrong composition stops the check with a card: the human's turn: a decision.
|
|
696
|
+
const cards = await carriedOut(io, root, id, id, await readCards(tree, id));
|
|
697
|
+
const unanswered = cards.filter((c) => !c.answered).map((c) => c.card);
|
|
698
|
+
if (unanswered.length > 0) {
|
|
699
|
+
return {
|
|
700
|
+
...withRelease(result({
|
|
701
|
+
status: "awaiting-decision",
|
|
702
|
+
turn: "human",
|
|
703
|
+
gate: null,
|
|
704
|
+
next: {
|
|
705
|
+
kind: "gate",
|
|
706
|
+
text: `Answer cards ${unanswered.join(", ")} of the acceptance check.`,
|
|
707
|
+
command: `cupola-decide ${id}`,
|
|
708
|
+
},
|
|
709
|
+
})),
|
|
710
|
+
cards: unanswered,
|
|
711
|
+
...askOf(cards, unanswered),
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
const ending = endingCard(record, cards);
|
|
715
|
+
if (ending) {
|
|
716
|
+
return withRelease(result({
|
|
717
|
+
status: "accepting",
|
|
718
|
+
turn: "ai",
|
|
719
|
+
gate: null,
|
|
720
|
+
next: {
|
|
721
|
+
kind: "run",
|
|
722
|
+
text: `Card ${ending.card} chose ${ending.effect}, which is not carried out yet.`,
|
|
723
|
+
command: `cupola-decide ${id}`,
|
|
724
|
+
},
|
|
725
|
+
}));
|
|
726
|
+
}
|
|
727
|
+
if (release?.state === "closed" && !record) {
|
|
728
|
+
return withRelease(result({
|
|
729
|
+
status: "in-progress",
|
|
730
|
+
turn: "human",
|
|
731
|
+
gate: null,
|
|
732
|
+
next: {
|
|
733
|
+
kind: "nothing",
|
|
734
|
+
text: `The release change ${release.url} was closed without merging. Reopen it to release the epic, or bring the reason to cupola-intake.`,
|
|
735
|
+
},
|
|
736
|
+
}));
|
|
737
|
+
}
|
|
738
|
+
if (release?.state === "open" && !release.draft && !record) {
|
|
739
|
+
const merged = each?.filter((r) => r.change?.state === "merged").length ?? 0;
|
|
740
|
+
const k = pendingRelease
|
|
741
|
+
? `${pendingRelease.order} of ${pendingRelease.of} (${pendingRelease.repo.slug})`
|
|
742
|
+
: "";
|
|
743
|
+
const releaseHost = pendingRelease?.host ?? (io.host ?? codeHost)(root);
|
|
744
|
+
const { read, merge } = await releaseHost.reach(release, id, { release: true });
|
|
745
|
+
const reactions = await gateReactions(root, releaseHost, release, tree, id, config);
|
|
746
|
+
return withReactions(withRelease(result({
|
|
747
|
+
status: "awaiting-release",
|
|
748
|
+
turn: "human",
|
|
749
|
+
gate: "release",
|
|
750
|
+
next: {
|
|
751
|
+
kind: "gate",
|
|
752
|
+
text: `${each
|
|
753
|
+
? `Read "Release" in ${read} and ${merge}, release ${k}${merged > 0 ? `, after the ${merged} merged before it are deployed` : ""}; merge the rest in order, then run the command.`
|
|
754
|
+
: `Read "Release" in ${read} and ${merge} to release the epic, then run the command.`} Or comment, or give a No-Go (it revises the epic) on the change or with \`cupola-decide ${id}\`.`,
|
|
755
|
+
command: `cupola-complete ${id}`,
|
|
756
|
+
},
|
|
757
|
+
})), id, reactions);
|
|
758
|
+
}
|
|
759
|
+
const active = record?.active;
|
|
760
|
+
const blocked = blockedBy(record);
|
|
761
|
+
if (blocked) {
|
|
762
|
+
return {
|
|
763
|
+
...withRelease(result({
|
|
764
|
+
status: "blocked",
|
|
765
|
+
turn: "human",
|
|
766
|
+
gate: null,
|
|
767
|
+
next: {
|
|
768
|
+
kind: "run",
|
|
769
|
+
text: `The acceptance check is blocked: ${blocked.blocker}. Fix that, then resume it.`,
|
|
770
|
+
command: `cupola-epic-run ${id}`,
|
|
771
|
+
},
|
|
772
|
+
})),
|
|
773
|
+
blocked,
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
const environment = await environmentWait(root, tree, record, id);
|
|
777
|
+
if (environment) {
|
|
778
|
+
return withRelease(result({
|
|
779
|
+
status: "accepting",
|
|
780
|
+
turn: "ai",
|
|
781
|
+
gate: null,
|
|
782
|
+
next: {
|
|
783
|
+
kind: "nothing",
|
|
784
|
+
text: `The acceptance check waits: ${environment}. It resumes when that is released.`,
|
|
785
|
+
command: `cupola-epic-run ${id}`,
|
|
786
|
+
},
|
|
787
|
+
}));
|
|
788
|
+
}
|
|
789
|
+
return withRelease(result({
|
|
790
|
+
status: "accepting",
|
|
791
|
+
turn: "ai",
|
|
792
|
+
gate: null,
|
|
793
|
+
next: active
|
|
794
|
+
? {
|
|
795
|
+
kind: "nothing",
|
|
796
|
+
text: `The acceptance check is running (${active.who}, session ${active.session}, ${ago(active.since) ?? `since ${active.since}`}). If that session has ended, take it over with \`cupola-epic-run ${id} --take-over ${active.session}\`.`,
|
|
797
|
+
command: `cupola-epic-run ${id} --take-over ${active.session}`,
|
|
798
|
+
}
|
|
799
|
+
: {
|
|
800
|
+
kind: "run",
|
|
801
|
+
text: "Every spec is done; run the acceptance check across specs and open the release.",
|
|
802
|
+
command: `cupola-epic-run ${id}`,
|
|
803
|
+
},
|
|
804
|
+
}));
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
807
|
+
* Whether a spec's design is done (a consumer's design waits for
|
|
808
|
+
* its provider's design, not its merge): its tasks are approved in the
|
|
809
|
+
* engine's spec.json, on its branch or wherever it was merged.
|
|
810
|
+
*/
|
|
811
|
+
export async function specDesignDone(root, spec) {
|
|
812
|
+
const path = `.kiro/specs/${spec}/spec.json`;
|
|
813
|
+
const own = (await workTree(root, workBranchName(spec))).tree;
|
|
814
|
+
// A design that ended with decision cards is settled only once they are answered; cards from
|
|
815
|
+
// later phases do not reopen it.
|
|
816
|
+
const raw = await own.read(`${runDir(spec)}/record.json`);
|
|
817
|
+
if (raw !== undefined) {
|
|
818
|
+
const fromDesign = new Set(parseRecord(raw)
|
|
819
|
+
.events.filter((e) => e.phase === "design" && e.what === "card")
|
|
820
|
+
.flatMap((e) => (e.note ?? "").split(",").map((f) => f.trim())));
|
|
821
|
+
const cards = await readCards(own, spec);
|
|
822
|
+
if (cards.some((c) => !c.answered && fromDesign.has(c.file)))
|
|
823
|
+
return false;
|
|
824
|
+
}
|
|
825
|
+
const trees = [own, ...(await epicBranchTrees(root, spec))];
|
|
826
|
+
for (const t of trees) {
|
|
827
|
+
const raw = await t.read(path);
|
|
828
|
+
if (raw === undefined)
|
|
829
|
+
continue;
|
|
830
|
+
try {
|
|
831
|
+
const json = JSON.parse(raw);
|
|
832
|
+
if (json.approvals?.tasks?.approved === true)
|
|
833
|
+
return true;
|
|
834
|
+
}
|
|
835
|
+
catch {
|
|
836
|
+
// a spec.json being rewritten: not done
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
return false;
|
|
840
|
+
}
|
|
841
|
+
/** The specs whose design a spec of `epic` in `component` still waits for (its `after`). */
|
|
842
|
+
export async function waitingFor(rootOf,
|
|
843
|
+
/** The hub's paths: the epic is read from the hub's tree. */
|
|
844
|
+
paths, tree, epic, component) {
|
|
845
|
+
const raw = await tree.read(epicPath(paths, epic));
|
|
846
|
+
if (raw === undefined)
|
|
847
|
+
return [];
|
|
848
|
+
const front = EpicFront.safeParse(parseEpic(raw).front);
|
|
849
|
+
if (!front.success)
|
|
850
|
+
return [];
|
|
851
|
+
const after = front.data.specs.find((s) => s.component === component)?.after ?? [];
|
|
852
|
+
const waits = [];
|
|
853
|
+
for (const dep of after) {
|
|
854
|
+
const spec = specName(epic, dep);
|
|
855
|
+
if (!(await specDesignDone(rootOf(dep), spec)))
|
|
856
|
+
waits.push(spec);
|
|
857
|
+
}
|
|
858
|
+
return waits;
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* The session of an epic's run, read from the markers of the runners it has running: the epic's
|
|
862
|
+
* run names its session `cupola-<epic>-<unix seconds>` and each runner `<session>-<component>`.
|
|
863
|
+
* A spec run on its own (`cupola-<spec>-<unix seconds>`) is not the epic's run.
|
|
864
|
+
*/
|
|
865
|
+
export function epicRunSession(epic, specs) {
|
|
866
|
+
const pattern = new RegExp(`^cupola-${epic}-(\\d+)-[a-z0-9]`);
|
|
867
|
+
const runs = specs.flatMap((s) => {
|
|
868
|
+
const started = s.active ? pattern.exec(s.active.session)?.[1] : undefined;
|
|
869
|
+
return started ? [{ spec: s.id, started: Number(started) }] : [];
|
|
870
|
+
});
|
|
871
|
+
if (runs.length === 0)
|
|
872
|
+
return undefined;
|
|
873
|
+
// Two at once is not expected; the one started last is named.
|
|
874
|
+
const started = Math.max(...runs.map((r) => r.started));
|
|
875
|
+
return {
|
|
876
|
+
session: `cupola-${epic}-${started}`,
|
|
877
|
+
specs: runs.filter((r) => r.started === started).map((r) => r.spec),
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* Each spec of an epic with two or more specs, then the fix specs its
|
|
882
|
+
* acceptance check added, with its status and whether the epic's run can
|
|
883
|
+
* start it now.
|
|
884
|
+
*/
|
|
885
|
+
async function specsOfEpic(io, root, config, id, specs) {
|
|
886
|
+
const each = [];
|
|
887
|
+
const named = specNamesOf(id, specs);
|
|
888
|
+
const names = named.map((s) => s.name);
|
|
889
|
+
const fixes = await fixSpecsOf(root, config, id, names);
|
|
890
|
+
const all = [...names, ...fixes];
|
|
891
|
+
// Each spec's status needs calls to its repository's origin and code host: the repositories
|
|
892
|
+
// are asked at once, and within one repository one spec after another (its refs are shared).
|
|
893
|
+
const repoOf = new Map(resolveComponents(config).map((c) => [c.name, c.repo.toLowerCase()]));
|
|
894
|
+
const componentOf = (spec, i) => named[i]?.component ?? spec.slice(id.length + 1).replace(/-fix-\d+$/, "");
|
|
895
|
+
const lanes = new Map();
|
|
896
|
+
const statuses = all.map((spec, i) => {
|
|
897
|
+
const lane = repoOf.get(componentOf(spec, i)) ?? "";
|
|
898
|
+
const run = (lanes.get(lane) ?? Promise.resolve()).then(() => workStatus(io, root, config, spec));
|
|
899
|
+
lanes.set(lane, run.catch(() => undefined));
|
|
900
|
+
return run.then((of) => ({ of }), (error) => ({ error }));
|
|
901
|
+
});
|
|
902
|
+
for (const [i, spec] of all.entries()) {
|
|
903
|
+
try {
|
|
904
|
+
const got = await statuses[i];
|
|
905
|
+
if (!got)
|
|
906
|
+
throw new Error(`no status for ${spec}`);
|
|
907
|
+
if ("error" in got)
|
|
908
|
+
throw got.error;
|
|
909
|
+
const of = got.of;
|
|
910
|
+
const active = of.run?.active
|
|
911
|
+
? { id: of.run.active.id, session: of.run.active.session }
|
|
912
|
+
: null;
|
|
913
|
+
// An agreed revision the epic's run takes in before the spec runs: a dropped spec's change
|
|
914
|
+
// still open is closed; a redesigned spec's change at acceptance goes back to draft (its status
|
|
915
|
+
// then sends it to the epic's run instead of its own).
|
|
916
|
+
const takeIn = of.status === "dropped" && of.change?.state === "open"
|
|
917
|
+
? "close"
|
|
918
|
+
: of.status === "ai-working" &&
|
|
919
|
+
of.change?.state === "open" &&
|
|
920
|
+
!of.change.draft &&
|
|
921
|
+
of.next.command === `cupola-epic-run ${id}`
|
|
922
|
+
? "draft"
|
|
923
|
+
: undefined;
|
|
924
|
+
each.push({
|
|
925
|
+
id: spec,
|
|
926
|
+
status: of.status,
|
|
927
|
+
gate: of.gate,
|
|
928
|
+
active,
|
|
929
|
+
// A blocked spec waits for a human to fix what stopped it.
|
|
930
|
+
movable: of.status === "ai-working" && active === null && !takeIn,
|
|
931
|
+
...(takeIn ? { takeIn } : {}),
|
|
932
|
+
...(of.blocked ? { blocked: of.blocked } : {}),
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
catch (error) {
|
|
936
|
+
if (!(error instanceof MissingWorkError))
|
|
937
|
+
throw error;
|
|
938
|
+
const component = named[i]?.component ?? "";
|
|
939
|
+
// Not created: a dropped one never is; a follow-up waits for the earlier spec of its
|
|
940
|
+
// component.
|
|
941
|
+
const dropped = await droppedBecause(io, root, id, component);
|
|
942
|
+
// Nor is one a revision under agreement is about to change.
|
|
943
|
+
const pending = dropped ? undefined : await pendingPlan(io, root, id, component);
|
|
944
|
+
const earlier = named
|
|
945
|
+
.slice(0, i)
|
|
946
|
+
.filter((s) => s.component === component)
|
|
947
|
+
.at(-1);
|
|
948
|
+
const before = earlier ? each.find((e) => e.id === earlier.name) : undefined;
|
|
949
|
+
const ready = !before || before.status === "done" || before.status === "dropped";
|
|
950
|
+
each.push({
|
|
951
|
+
id: spec,
|
|
952
|
+
status: dropped ? "dropped" : "not-created",
|
|
953
|
+
gate: null,
|
|
954
|
+
active: null,
|
|
955
|
+
movable: !dropped && !pending && ready,
|
|
956
|
+
});
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
return each;
|
|
960
|
+
}
|
|
961
|
+
/**
|
|
962
|
+
* The status of an epic, and, once it is agreed, the cards it was merged with
|
|
963
|
+
* still open. Merging with cards open stops nothing: the runs take each as
|
|
964
|
+
* settled on its recommendation, and the status keeps listing them so they are
|
|
965
|
+
* not forgotten — information only, with no effect on status, turn, or next.
|
|
966
|
+
*/
|
|
967
|
+
async function epicStatus(io, root, config, tree, id) {
|
|
968
|
+
const r = await epicStatusOf(io, root, config, tree, id);
|
|
969
|
+
if (!r || r.gate === "start" || r.status === "draft" || r.status === "aborted")
|
|
970
|
+
return r;
|
|
971
|
+
const unanswered = await neverAnswered(root, config, id);
|
|
972
|
+
return unanswered.length > 0 ? { ...r, unanswered } : r;
|
|
973
|
+
}
|
|
974
|
+
/** An agreed epic's cards never answered, as merged (origin's default branch, or this commit). */
|
|
975
|
+
async function neverAnswered(root, config, id) {
|
|
976
|
+
try {
|
|
977
|
+
const cards = await readCardsIn(await hubAgreed(root), epicCardsDir(config.paths, id));
|
|
978
|
+
return cards
|
|
979
|
+
.filter((c) => !c.answered)
|
|
980
|
+
.map((c) => ({ card: c.card, recommended: c.recommended ?? null, ask: c.ask ?? [] }));
|
|
981
|
+
}
|
|
982
|
+
catch {
|
|
983
|
+
// A card that cannot be read leaves out what is only information.
|
|
984
|
+
return [];
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
* The status of an epic: awaiting start while its `epic`
|
|
989
|
+
* change is open; once merged, an epic with one spec takes its spec's
|
|
990
|
+
* statuses, and the run that creates the spec is `cupola-epic-run`.
|
|
991
|
+
*/
|
|
992
|
+
async function epicStatusOf(io, root, config, tree, id) {
|
|
993
|
+
const path = epicPath(config.paths, id);
|
|
994
|
+
const base = await originDefaultBranch(root);
|
|
995
|
+
let agreed;
|
|
996
|
+
if (base) {
|
|
997
|
+
try {
|
|
998
|
+
await git(root, [
|
|
999
|
+
"fetch",
|
|
1000
|
+
"--quiet",
|
|
1001
|
+
"origin",
|
|
1002
|
+
`+refs/heads/${base}:refs/remotes/origin/${base}`,
|
|
1003
|
+
]);
|
|
1004
|
+
agreed = await gitTree(root, `refs/remotes/origin/${base}`).read(path);
|
|
1005
|
+
}
|
|
1006
|
+
catch {
|
|
1007
|
+
agreed = undefined;
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
const draft = agreed ?? (await tree.read(path)) ?? (await gitTree(root, "HEAD").read(path));
|
|
1011
|
+
if (draft === undefined)
|
|
1012
|
+
return undefined;
|
|
1013
|
+
const front = EpicFront.safeParse(parseEpic(draft).front);
|
|
1014
|
+
if (!front.success)
|
|
1015
|
+
throw new Error(`${path}: its front matter is not an epic's (cupola check says why)`);
|
|
1016
|
+
const specs = front.data.specs;
|
|
1017
|
+
const procedures = front.data.procedures.map((r) => r.runbook);
|
|
1018
|
+
// After the start gate, the instruction that moves the epic: its specs' run, or the executions of its
|
|
1019
|
+
// procedures, which cupola-complete creates.
|
|
1020
|
+
const start = specs.length > 0 ? `cupola-epic-run ${id}` : `cupola-complete ${id}`;
|
|
1021
|
+
const work = {
|
|
1022
|
+
kind: "epic",
|
|
1023
|
+
id,
|
|
1024
|
+
component: specs.length === 1 ? (specs[0]?.component ?? "") : specs.map((s) => s.component).join(", "),
|
|
1025
|
+
};
|
|
1026
|
+
const change = (await (io.host ?? codeHost)(root).findChange(workBranchName(id))) ?? null;
|
|
1027
|
+
const result = (r) => ({
|
|
1028
|
+
work,
|
|
1029
|
+
change,
|
|
1030
|
+
cards: [],
|
|
1031
|
+
run: null,
|
|
1032
|
+
...r,
|
|
1033
|
+
});
|
|
1034
|
+
// An open epic change — the first one, or a revision of an agreed epic — is the start gate.
|
|
1035
|
+
if (change?.state === "open") {
|
|
1036
|
+
const host = (io.host ?? codeHost)(root);
|
|
1037
|
+
const reactions = await gateReactions(root, host, change, tree, id, config);
|
|
1038
|
+
const g1 = (r) => withReactions(r, id, reactions);
|
|
1039
|
+
// Its cards are on its branch: those still open are what the start gate has to settle first.
|
|
1040
|
+
const all = await readCardsIn(tree, epicCardsDir(config.paths, id));
|
|
1041
|
+
const open = all.filter((c) => !c.answered).map((c) => c.card);
|
|
1042
|
+
const skill = agreed === undefined ? "cupola-epic-create" : "cupola-epic-revise";
|
|
1043
|
+
if (open.length > 0) {
|
|
1044
|
+
return g1({
|
|
1045
|
+
...result({
|
|
1046
|
+
status: "awaiting-start",
|
|
1047
|
+
turn: "human",
|
|
1048
|
+
gate: "start",
|
|
1049
|
+
next: {
|
|
1050
|
+
kind: "gate",
|
|
1051
|
+
text: `Answer cards ${open.join(", ")} with cupola-decide ${id}, which has ${skill} reflect them in the epic (${change.url}); merge the change once no card is open.`,
|
|
1052
|
+
command: start,
|
|
1053
|
+
},
|
|
1054
|
+
}),
|
|
1055
|
+
cards: open,
|
|
1056
|
+
...askOf(all, open),
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
if (agreed === undefined) {
|
|
1060
|
+
return g1(result({
|
|
1061
|
+
status: "awaiting-start",
|
|
1062
|
+
turn: "human",
|
|
1063
|
+
gate: "start",
|
|
1064
|
+
next: {
|
|
1065
|
+
kind: "gate",
|
|
1066
|
+
text: `Read the epic and merge its change to agree (${change.url}); to change it, comment, ask for a try, or give a No-Go on the change or with cupola-decide ${id}.`,
|
|
1067
|
+
command: start,
|
|
1068
|
+
},
|
|
1069
|
+
}));
|
|
1070
|
+
}
|
|
1071
|
+
// A revision of a running epic: the start gate again, while the specs it leaves alone go on.
|
|
1072
|
+
const running = specs.length >= 2 ? await specsOfEpic(io, root, config, id, specs) : undefined;
|
|
1073
|
+
return g1({
|
|
1074
|
+
...result({
|
|
1075
|
+
status: "awaiting-start",
|
|
1076
|
+
turn: "human",
|
|
1077
|
+
gate: "start",
|
|
1078
|
+
next: {
|
|
1079
|
+
kind: "gate",
|
|
1080
|
+
text: `Read the revision and merge its change to agree (${change.url}); to change it, comment, ask for a try, or give a No-Go on the change or with cupola-decide ${id}. Specs it leaves alone go on with the command.`,
|
|
1081
|
+
command: start,
|
|
1082
|
+
},
|
|
1083
|
+
}),
|
|
1084
|
+
...(running ? { specs: running } : {}),
|
|
1085
|
+
});
|
|
1086
|
+
}
|
|
1087
|
+
// A merged change is agreement even when origin's default branch could not be read.
|
|
1088
|
+
if (agreed === undefined && change?.state !== "merged") {
|
|
1089
|
+
if (change?.state === "closed") {
|
|
1090
|
+
return result({
|
|
1091
|
+
status: "aborted",
|
|
1092
|
+
turn: "none",
|
|
1093
|
+
gate: null,
|
|
1094
|
+
next: { kind: "nothing", text: "Aborted." },
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
// Written on its branch, but the dialogue has not opened its change yet.
|
|
1098
|
+
return result({
|
|
1099
|
+
status: "draft",
|
|
1100
|
+
turn: "human",
|
|
1101
|
+
gate: null,
|
|
1102
|
+
next: {
|
|
1103
|
+
kind: "run",
|
|
1104
|
+
text: "The epic is a draft whose change is not open yet; finish the dialogue.",
|
|
1105
|
+
command: `cupola-epic-create ${id}`,
|
|
1106
|
+
},
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
// Aborted after its start: recorded on its cases, where its status comes from.
|
|
1110
|
+
const tracker = await trackerOf(io);
|
|
1111
|
+
for (const c of front.data.cases) {
|
|
1112
|
+
const shown = await tracker.show(c);
|
|
1113
|
+
if (shown && recordsAbort(shown.text, id)) {
|
|
1114
|
+
return result({
|
|
1115
|
+
status: "aborted",
|
|
1116
|
+
turn: "none",
|
|
1117
|
+
gate: null,
|
|
1118
|
+
next: { kind: "nothing", text: "Aborted." },
|
|
1119
|
+
});
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
// Released, or with no specs agreed: cupola-complete tidies up and creates one execution per
|
|
1123
|
+
// procedure, the last thing it does; the epic is operating until every execution is done.
|
|
1124
|
+
const afterRelease = async (released) => {
|
|
1125
|
+
if (procedures.length === 0)
|
|
1126
|
+
return released;
|
|
1127
|
+
const found = await executionsFor(io, root, config, id, procedures);
|
|
1128
|
+
const each = procedures.map((runbook) => {
|
|
1129
|
+
const live = found.filter((e) => e.runbook === runbook && e.status.status !== "dropped");
|
|
1130
|
+
return { runbook, of: live.at(-1) };
|
|
1131
|
+
});
|
|
1132
|
+
const executions = each.map((e) => ({
|
|
1133
|
+
id: e.of?.id ?? null,
|
|
1134
|
+
runbook: e.runbook,
|
|
1135
|
+
status: e.of?.status.status ?? "not-created",
|
|
1136
|
+
}));
|
|
1137
|
+
const missing = each.filter((e) => !e.of).map((e) => e.runbook);
|
|
1138
|
+
if (released.next.command?.startsWith("cupola-complete "))
|
|
1139
|
+
return { ...released, executions };
|
|
1140
|
+
if (missing.length > 0) {
|
|
1141
|
+
const complete = `cupola-complete ${id}`;
|
|
1142
|
+
return {
|
|
1143
|
+
...released,
|
|
1144
|
+
status: "done",
|
|
1145
|
+
turn: "human",
|
|
1146
|
+
gate: null,
|
|
1147
|
+
executions,
|
|
1148
|
+
next: {
|
|
1149
|
+
kind: "run",
|
|
1150
|
+
text: `${specs.length > 0 ? "Released" : "Agreed"}: \`${complete}\` tidies up and creates the executions of ${missing.join(", ")}.`,
|
|
1151
|
+
command: complete,
|
|
1152
|
+
},
|
|
1153
|
+
};
|
|
1154
|
+
}
|
|
1155
|
+
const current = each.find((e) => e.of && e.of.status.status !== "done");
|
|
1156
|
+
if (!current?.of)
|
|
1157
|
+
return { ...released, executions };
|
|
1158
|
+
const of = current.of.status;
|
|
1159
|
+
return {
|
|
1160
|
+
...released,
|
|
1161
|
+
status: "operating",
|
|
1162
|
+
turn: of.turn,
|
|
1163
|
+
gate: of.gate,
|
|
1164
|
+
executions,
|
|
1165
|
+
next: { ...of.next, text: `${current.of.id} (${of.status}): ${of.next.text}` },
|
|
1166
|
+
};
|
|
1167
|
+
};
|
|
1168
|
+
if (specs.length === 0) {
|
|
1169
|
+
return afterRelease(result({
|
|
1170
|
+
status: "done",
|
|
1171
|
+
turn: "none",
|
|
1172
|
+
gate: null,
|
|
1173
|
+
next: { kind: "nothing", text: "Done." },
|
|
1174
|
+
}));
|
|
1175
|
+
}
|
|
1176
|
+
// Agreed. An epic with one spec takes its spec's statuses, once the spec exists.
|
|
1177
|
+
const only = specs.length === 1 ? specs[0] : undefined;
|
|
1178
|
+
if (only) {
|
|
1179
|
+
const spec = specName(id, only.component);
|
|
1180
|
+
// Released: the spec's change is merged, and its branch may be gone.
|
|
1181
|
+
// The spec's change is in its component's repository.
|
|
1182
|
+
const ws = await workspace({ ...io, cwd: root });
|
|
1183
|
+
const slug = ws.repoOf(only.component);
|
|
1184
|
+
const specRoot = slug === ws.here.slug ? root : ws.checkout(slug).main;
|
|
1185
|
+
const specChange = await (io.host ?? codeHost)(specRoot).findChange(workBranchName(spec));
|
|
1186
|
+
// Outside the hub, its system catalog entries came in a `system` change merged with it.
|
|
1187
|
+
const system = specChange?.state === "merged" && slug !== ws.hub.slug
|
|
1188
|
+
? await (io.host ?? codeHost)(root).findChange(workBranchName(`${id}-system`))
|
|
1189
|
+
: undefined;
|
|
1190
|
+
if (system?.state === "open") {
|
|
1191
|
+
return {
|
|
1192
|
+
work,
|
|
1193
|
+
spec,
|
|
1194
|
+
change: system,
|
|
1195
|
+
cards: [],
|
|
1196
|
+
run: null,
|
|
1197
|
+
status: "awaiting-acceptance",
|
|
1198
|
+
turn: "human",
|
|
1199
|
+
gate: "acceptance",
|
|
1200
|
+
next: {
|
|
1201
|
+
kind: "gate",
|
|
1202
|
+
text: `${spec} is merged; merge its system change too (${system.url}), then run the command.`,
|
|
1203
|
+
command: `cupola-complete ${spec}`,
|
|
1204
|
+
},
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
const code = ws.config.documents === "hub"
|
|
1208
|
+
? await codeLeft(io, ws, workBranchName(spec), only.component, specChange ?? undefined)
|
|
1209
|
+
: undefined;
|
|
1210
|
+
if (code) {
|
|
1211
|
+
return {
|
|
1212
|
+
work,
|
|
1213
|
+
spec,
|
|
1214
|
+
change: code.change,
|
|
1215
|
+
cards: [],
|
|
1216
|
+
run: null,
|
|
1217
|
+
status: "awaiting-acceptance",
|
|
1218
|
+
turn: "human",
|
|
1219
|
+
gate: "acceptance",
|
|
1220
|
+
next: {
|
|
1221
|
+
kind: "gate",
|
|
1222
|
+
text: `${spec} ${code.text}`,
|
|
1223
|
+
command: `cupola-complete ${spec}`,
|
|
1224
|
+
},
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
1227
|
+
if (specChange?.state === "merged") {
|
|
1228
|
+
// Merging it released the epic; `cupola-complete <spec>` tidies up and moves the cases on.
|
|
1229
|
+
if (await untidy(ws.config.documents === "hub" ? root : specRoot, workBranchName(spec))) {
|
|
1230
|
+
const complete = `cupola-complete ${spec}`;
|
|
1231
|
+
return afterRelease({
|
|
1232
|
+
work,
|
|
1233
|
+
spec,
|
|
1234
|
+
change: specChange,
|
|
1235
|
+
cards: [],
|
|
1236
|
+
run: null,
|
|
1237
|
+
status: "done",
|
|
1238
|
+
turn: "human",
|
|
1239
|
+
gate: null,
|
|
1240
|
+
next: {
|
|
1241
|
+
kind: "run",
|
|
1242
|
+
text: `Merged: \`${complete}\` tidies up and moves on.`,
|
|
1243
|
+
command: complete,
|
|
1244
|
+
},
|
|
1245
|
+
});
|
|
1246
|
+
}
|
|
1247
|
+
return afterRelease({
|
|
1248
|
+
work,
|
|
1249
|
+
spec,
|
|
1250
|
+
change: specChange,
|
|
1251
|
+
cards: [],
|
|
1252
|
+
run: null,
|
|
1253
|
+
status: "done",
|
|
1254
|
+
turn: "none",
|
|
1255
|
+
gate: null,
|
|
1256
|
+
next: { kind: "nothing", text: "Done." },
|
|
1257
|
+
});
|
|
1258
|
+
}
|
|
1259
|
+
try {
|
|
1260
|
+
const of = await workStatus(io, root, config, spec);
|
|
1261
|
+
return { ...of, work, spec };
|
|
1262
|
+
}
|
|
1263
|
+
catch (error) {
|
|
1264
|
+
if (!(error instanceof MissingWorkError))
|
|
1265
|
+
throw error;
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
if (!only) {
|
|
1269
|
+
// Two or more specs: in progress, per spec.
|
|
1270
|
+
const each = await specsOfEpic(io, root, config, id, specs);
|
|
1271
|
+
const movable = each.filter((s) => s.movable);
|
|
1272
|
+
const gates = each.filter((s) => s.gate).map((s) => `${s.id} at ${s.gate}`);
|
|
1273
|
+
// A blocked spec is the human's to fix, as a gate is theirs to judge.
|
|
1274
|
+
const blocked = each
|
|
1275
|
+
.filter((s) => s.status === "blocked")
|
|
1276
|
+
.map((s) => `${s.id} blocked (${s.blocked?.blocker ?? "no reason was given"})`);
|
|
1277
|
+
const yours = [...gates, ...blocked];
|
|
1278
|
+
const running = each.filter((s) => s.active);
|
|
1279
|
+
const finished = each.every((s) => s.status === "done" || s.status === "dropped");
|
|
1280
|
+
if (finished) {
|
|
1281
|
+
const accepted = await acceptance(io, root, config, id, specs, result);
|
|
1282
|
+
return {
|
|
1283
|
+
...(accepted.status === "done" ? await afterRelease(accepted) : accepted),
|
|
1284
|
+
specs: each,
|
|
1285
|
+
};
|
|
1286
|
+
}
|
|
1287
|
+
let next;
|
|
1288
|
+
let turn;
|
|
1289
|
+
// A revision to take in is the epic's run's work, as a spec to start is.
|
|
1290
|
+
const takeIn = each.filter((s) => s.takeIn);
|
|
1291
|
+
if (movable.length > 0 || takeIn.length > 0) {
|
|
1292
|
+
const work = [
|
|
1293
|
+
...takeIn.map((s) => `${s.takeIn} the change of ${s.id}`),
|
|
1294
|
+
...(movable.length > 0 ? [`run ${movable.map((s) => s.id).join(", ")}`] : []),
|
|
1295
|
+
].join(", then ");
|
|
1296
|
+
turn = "ai";
|
|
1297
|
+
next = {
|
|
1298
|
+
kind: "run",
|
|
1299
|
+
text: `${work.charAt(0).toUpperCase()}${work.slice(1)}${yours.length > 0 ? `; waiting for you: ${yours.join(", ")}` : ""}.`,
|
|
1300
|
+
command: `cupola-epic-run ${id}`,
|
|
1301
|
+
};
|
|
1302
|
+
}
|
|
1303
|
+
else if (gates.length === 0 && blocked.length > 0) {
|
|
1304
|
+
turn = "human";
|
|
1305
|
+
next = {
|
|
1306
|
+
kind: "run",
|
|
1307
|
+
text: `Waiting for you: ${blocked.join(", ")}. Fix that, then resume the epic's run.`,
|
|
1308
|
+
command: `cupola-epic-run ${id}`,
|
|
1309
|
+
};
|
|
1310
|
+
}
|
|
1311
|
+
else if (gates.length > 0) {
|
|
1312
|
+
turn = "human";
|
|
1313
|
+
next = {
|
|
1314
|
+
kind: "gate",
|
|
1315
|
+
text: `Waiting for you: ${yours.join(", ")}; see each spec's status.`,
|
|
1316
|
+
};
|
|
1317
|
+
}
|
|
1318
|
+
else if (running.length > 0) {
|
|
1319
|
+
turn = "ai";
|
|
1320
|
+
// The epic's run's session takes over all its runners at once; a spec run by hand, its own.
|
|
1321
|
+
const ran = epicRunSession(id, running);
|
|
1322
|
+
const sessions = ran
|
|
1323
|
+
? [ran.session]
|
|
1324
|
+
: [...new Set(running.map((s) => s.active?.session ?? ""))].filter(Boolean);
|
|
1325
|
+
const takeOver = sessions.map((s) => `\`cupola-epic-run ${id} --take-over ${s}\``);
|
|
1326
|
+
next = {
|
|
1327
|
+
kind: "nothing",
|
|
1328
|
+
text: `Running: ${running.map((s) => `${s.id} (session ${s.active?.session})`).join(", ")}. If that session has ended, take it over with ${takeOver.join(" or ")}.`,
|
|
1329
|
+
};
|
|
1330
|
+
}
|
|
1331
|
+
else if (each.some((s) => s.status === "waiting")) {
|
|
1332
|
+
turn = "ai";
|
|
1333
|
+
next = {
|
|
1334
|
+
kind: "nothing",
|
|
1335
|
+
text: "Its specs wait: for other specs' designs, or for a shared e2e environment; they move once that is done or released.",
|
|
1336
|
+
command: `cupola-epic-run ${id}`,
|
|
1337
|
+
};
|
|
1338
|
+
}
|
|
1339
|
+
else {
|
|
1340
|
+
turn = "none";
|
|
1341
|
+
next = {
|
|
1342
|
+
kind: "nothing",
|
|
1343
|
+
text: `Nothing can move: ${each.map((s) => `${s.id} (${s.status})`).join(", ")}.`,
|
|
1344
|
+
};
|
|
1345
|
+
}
|
|
1346
|
+
const session = epicRunSession(id, running);
|
|
1347
|
+
return {
|
|
1348
|
+
...result({ status: "in-progress", turn, gate: null, next }),
|
|
1349
|
+
specs: each,
|
|
1350
|
+
...(session ? { running: session } : {}),
|
|
1351
|
+
};
|
|
1352
|
+
}
|
|
1353
|
+
// Only an epic with one spec gets here (two or more return above): its spec is not created yet.
|
|
1354
|
+
return result({
|
|
1355
|
+
status: "ai-working",
|
|
1356
|
+
turn: "ai",
|
|
1357
|
+
gate: null,
|
|
1358
|
+
next: {
|
|
1359
|
+
kind: "run",
|
|
1360
|
+
text: "Agreed; create its spec and run it.",
|
|
1361
|
+
command: `cupola-epic-run ${id}`,
|
|
1362
|
+
},
|
|
1363
|
+
});
|
|
1364
|
+
}
|
|
1365
|
+
const LABELS = {
|
|
1366
|
+
accepting: "accepting",
|
|
1367
|
+
"awaiting-release": "awaiting release",
|
|
1368
|
+
waiting: "waiting",
|
|
1369
|
+
open: "open",
|
|
1370
|
+
"awaiting-resolution": "awaiting resolution",
|
|
1371
|
+
resolved: "resolved",
|
|
1372
|
+
"awaiting-start": "awaiting start",
|
|
1373
|
+
"in-progress": "in progress",
|
|
1374
|
+
draft: "draft",
|
|
1375
|
+
aborted: "aborted",
|
|
1376
|
+
"ai-working": "AI working",
|
|
1377
|
+
blocked: "blocked",
|
|
1378
|
+
"awaiting-decision": "awaiting decision",
|
|
1379
|
+
"awaiting-acceptance": "awaiting acceptance",
|
|
1380
|
+
operating: "operating",
|
|
1381
|
+
done: "done",
|
|
1382
|
+
dropped: "dropped",
|
|
1383
|
+
};
|
|
1384
|
+
export function describe(r) {
|
|
1385
|
+
const whose = r.turn === "human"
|
|
1386
|
+
? r.gate
|
|
1387
|
+
? `your turn, at the ${r.gate} gate`
|
|
1388
|
+
: "your turn"
|
|
1389
|
+
: r.turn === "ai"
|
|
1390
|
+
? "the AI's turn"
|
|
1391
|
+
: "nobody's turn";
|
|
1392
|
+
const lines = [
|
|
1393
|
+
`${r.work.kind} ${r.work.id}${r.work.component ? ` (${r.work.component})` : ""}: ${LABELS[r.status]} — ${whose}`,
|
|
1394
|
+
];
|
|
1395
|
+
if (r.spec)
|
|
1396
|
+
lines.push(` spec: ${r.spec}`);
|
|
1397
|
+
for (const e of r.epics ?? [])
|
|
1398
|
+
lines.push(` epic: ${e.id} (${LABELS[e.status]}${e.judged ? ", judged not enough at resolution" : ""})`);
|
|
1399
|
+
if (r.execution)
|
|
1400
|
+
lines.push(` execution of ${r.execution.runbook} for ${r.execution.for}: ${r.execution.stopped ? `stopped at ${r.execution.stopped}` : r.execution.done.length > 0 ? `done ${r.execution.done.join(", ")}` : "no step done"}${r.execution.result ? `, ${r.execution.result}` : ""}`);
|
|
1401
|
+
for (const e of r.executions ?? [])
|
|
1402
|
+
lines.push(` execution: ${e.id ?? `${e.runbook} (not created)`}${e.id ? ` (${e.status === "not-created" ? "not created" : LABELS[e.status]})` : ""}`);
|
|
1403
|
+
if (r.change)
|
|
1404
|
+
lines.push(` change: ${r.change.url}${r.change.draft ? " (draft)" : ""}`);
|
|
1405
|
+
for (const a of r.ask ?? [])
|
|
1406
|
+
lines.push(` card ${a.card}: ask ${a.ask.join(", ")}`);
|
|
1407
|
+
if (r.unanswered?.length)
|
|
1408
|
+
lines.push(` never answered (taken as recommended): ${r.unanswered
|
|
1409
|
+
.map((u) => `card ${u.card}${u.recommended ? ` → ${u.recommended}` : ""}${u.ask.length > 0 ? ` (ask ${u.ask.join(", ")})` : ""}`)
|
|
1410
|
+
.join(", ")}`);
|
|
1411
|
+
for (const p of r.reactions ?? [])
|
|
1412
|
+
lines.push(` reaction not processed: ${p.kind} ${p.id} by ${p.who} (${p.at})`);
|
|
1413
|
+
if (r.tries?.length)
|
|
1414
|
+
lines.push(` tries: ${r.tries.map((n) => `try-${n}`).join(", ")}`);
|
|
1415
|
+
if (r.unread)
|
|
1416
|
+
lines.push(` reactions not read: ${r.unread}`);
|
|
1417
|
+
if (r.run?.phase)
|
|
1418
|
+
lines.push(` phase: ${r.run.phase}${r.run.active ? ` (running, ${r.run.active.who})` : ""}`);
|
|
1419
|
+
lines.push(` next: ${r.next.text}${r.next.command ? ` → ${r.next.command}` : ""}`);
|
|
1420
|
+
return lines.join("\n");
|
|
1421
|
+
}
|
|
1422
|
+
/** The hub's agreed epics: origin's default branch, or this commit when it cannot be read. */
|
|
1423
|
+
async function hubAgreed(hub) {
|
|
1424
|
+
const base = await originDefaultBranch(hub);
|
|
1425
|
+
return base ? gitTree(hub, `refs/remotes/origin/${base}`) : gitTree(hub, "HEAD");
|
|
1426
|
+
}
|
|
1427
|
+
/**
|
|
1428
|
+
* The reactions of a work at its gate: those on its open change that people
|
|
1429
|
+
* the configuration trusts gave, and those given in a session (its files on
|
|
1430
|
+
* `tree`); the ones not processed yet, the tries built, and why the change's
|
|
1431
|
+
* could not be read, if they could not — then none of them is processed, and
|
|
1432
|
+
* the status says so rather than showing none.
|
|
1433
|
+
*/
|
|
1434
|
+
export async function gateReactions(root, host, change, tree, id, config) {
|
|
1435
|
+
const fromHost = change?.state === "open" ? await hostReactions(host, change, config) : { reactions: [] };
|
|
1436
|
+
const all = [...fromHost.reactions, ...(await sessionReactions(tree, id))].sort((a, b) => a.at.localeCompare(b.at));
|
|
1437
|
+
return {
|
|
1438
|
+
reactions: pendingOf(all),
|
|
1439
|
+
tries: await triesOf(root, id),
|
|
1440
|
+
...(fromHost.unread ? { unread: fromHost.unread } : {}),
|
|
1441
|
+
};
|
|
1442
|
+
}
|
|
1443
|
+
/**
|
|
1444
|
+
* Whether a merged work's own worktree is still here: `cupola-complete` removes
|
|
1445
|
+
* it last, so one left means it has not run. The main checkout is not the work's,
|
|
1446
|
+
* even with the branch checked out there.
|
|
1447
|
+
*/
|
|
1448
|
+
export async function untidy(cwd, branch) {
|
|
1449
|
+
try {
|
|
1450
|
+
await git(cwd, ["worktree", "prune"]);
|
|
1451
|
+
const blocks = (await git(cwd, ["worktree", "list", "--porcelain"])).split("\n\n").slice(1);
|
|
1452
|
+
return blocks.some((b) => b.split("\n").includes(`branch refs/heads/${branch}`));
|
|
1453
|
+
}
|
|
1454
|
+
catch {
|
|
1455
|
+
return false;
|
|
1456
|
+
}
|
|
1457
|
+
}
|