@kyuki3rain/cupola 0.1.0 → 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 +158 -0
- package/README.md +3 -2
- package/dist/branch-store.js +73 -0
- package/dist/commands/change.js +245 -39
- package/dist/commands/epic.js +3 -3
- package/dist/commands/execution.js +482 -0
- package/dist/commands/locks.js +69 -1
- package/dist/commands/reaction.js +185 -0
- package/dist/commands/revision.js +4 -4
- package/dist/commands/run.js +68 -62
- package/dist/commands/setup.js +54 -22
- package/dist/commands/spec.js +1 -1
- package/dist/commands/status.js +437 -138
- package/dist/commands/trace.js +106 -29
- package/dist/commands/wait.js +14 -11
- package/dist/commands/work.js +12 -7
- package/dist/commands/workspace.js +16 -5
- package/dist/commands/worktree.js +3 -3
- package/dist/config.js +66 -2
- package/dist/epic.js +24 -5
- package/dist/execution.js +272 -0
- package/dist/feedback.js +3 -17
- package/dist/host/git.js +224 -0
- package/dist/host/github.js +145 -48
- package/dist/host/host.js +3 -1
- package/dist/host/index.js +63 -0
- package/dist/main.js +143 -48
- package/dist/reaction.js +110 -0
- package/dist/record.js +52 -18
- package/dist/runbook.js +26 -5
- package/dist/status.js +158 -27
- package/dist/templates.js +2 -1
- package/dist/tracker/files.js +9 -66
- package/dist/tracker/index.js +8 -1
- package/dist/tree.js +17 -0
- package/dist/work.js +27 -6
- package/dist/workspace.js +37 -12
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/skills/cupola-abort/SKILL.md +11 -5
- package/skills/cupola-adr/SKILL.md +23 -10
- package/skills/cupola-catalog/SKILL.md +1 -1
- package/skills/cupola-catalog-draft/SKILL.md +6 -4
- package/skills/cupola-complete/SKILL.md +35 -15
- package/skills/cupola-decide/SKILL.md +64 -25
- package/skills/cupola-e2e-setup/SKILL.md +1 -1
- package/skills/cupola-epic-accept/SKILL.md +14 -10
- package/skills/cupola-epic-create/SKILL.md +67 -27
- package/skills/cupola-epic-revise/SKILL.md +39 -18
- package/skills/cupola-epic-run/SKILL.md +81 -42
- package/skills/cupola-fix/SKILL.md +7 -5
- package/skills/cupola-init/SKILL.md +79 -24
- package/skills/cupola-intake/SKILL.md +19 -2
- package/skills/cupola-operate/SKILL.md +62 -45
- package/skills/cupola-resolve/SKILL.md +21 -14
- package/skills/cupola-respond/SKILL.md +73 -0
- package/skills/cupola-review/SKILL.md +14 -15
- package/skills/cupola-spec-design/SKILL.md +15 -10
- package/skills/cupola-spec-impl/SKILL.md +6 -4
- package/skills/cupola-spec-run/SKILL.md +29 -17
- package/skills/cupola-steering/SKILL.md +13 -6
- package/skills/cupola-try/SKILL.md +38 -0
- package/templates/install/.kiro/steering/operations.md +2 -2
- package/templates/install/.kiro/steering/principles.md +1 -1
- package/templates/install/cc-sdd/patches.json +1 -1
- package/templates/method-1/card.md +11 -3
- package/templates/method-1/case.md +2 -2
- package/templates/method-1/changes/catalog.md +1 -1
- package/templates/method-1/changes/epic.md +3 -2
- package/templates/method-1/changes/fix.md +1 -1
- package/templates/method-1/changes/release.md +1 -1
- package/templates/method-1/changes/spec.md +1 -1
- package/templates/method-1/cupola-issue-draft.md +1 -1
- package/templates/method-1/epic.md +17 -6
- package/templates/method-1/execution.md +55 -0
- package/templates/method-1/no-go.md +28 -0
- package/templates/method-1/reaction.md +17 -0
- package/templates/method-1/record.json +3 -3
- package/templates/method-1/report.md +3 -3
- package/templates/method-1/runbook.md +8 -2
- package/templates/method-1/runner-result.json +1 -1
- package/dist/commands/procedure.js +0 -92
- package/dist/procedure.js +0 -60
- package/templates/method-1/send-back.md +0 -23
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
4
|
+
import { epicPath } from "../config.js";
|
|
5
|
+
import { today } from "../day.js";
|
|
6
|
+
import { EpicFront, parseEpic } from "../epic.js";
|
|
7
|
+
import { arrangedSteps, deriveExecution, ExecutionFront, executionState, NEXT_STEP, parseExecution, RESULT_MARK, } from "../execution.js";
|
|
8
|
+
import { commitPaths, git, originDefaultBranch } from "../git.js";
|
|
9
|
+
import { codeHost } from "../host/index.js";
|
|
10
|
+
import { executionPath, executionsDir, parseRunbook, RunbookFront, runbookPath, } from "../runbook.js";
|
|
11
|
+
import { templatePath } from "../templates.js";
|
|
12
|
+
import { gitTree } from "../tree.js";
|
|
13
|
+
import { isWorkId, MissingWorkError, workBranchName } from "../work.js";
|
|
14
|
+
import { workspace } from "../workspace.js";
|
|
15
|
+
import { gateReactions, untidy, workStatus, workTree } from "./status.js";
|
|
16
|
+
import { trackerOf } from "./work.js";
|
|
17
|
+
import { addWorktree, existingWorktree } from "./worktree.js";
|
|
18
|
+
const NUMBERED = /^(.+)-(\d+)$/;
|
|
19
|
+
/** Origin's default branch of the hub, fetched: where agreed runbooks and finished executions are. */
|
|
20
|
+
async function agreed(root) {
|
|
21
|
+
const base = await originDefaultBranch(root);
|
|
22
|
+
if (!base)
|
|
23
|
+
return { tree: gitTree(root, "HEAD"), ref: "HEAD" };
|
|
24
|
+
const ref = `refs/remotes/origin/${base}`;
|
|
25
|
+
try {
|
|
26
|
+
await git(root, ["fetch", "--quiet", "origin", `+refs/heads/${base}:${ref}`]);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// origin unreachable: as last fetched
|
|
30
|
+
}
|
|
31
|
+
return { tree: gitTree(root, ref), ref };
|
|
32
|
+
}
|
|
33
|
+
/** The runbooks agreed in the hub. */
|
|
34
|
+
async function runbookIds(tree, config) {
|
|
35
|
+
const out = [];
|
|
36
|
+
for (const id of await tree.dirs(config.paths.runbooks))
|
|
37
|
+
if ((await tree.read(runbookPath(config.paths, id))) !== undefined)
|
|
38
|
+
out.push(id);
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
/** Branches `cupola/<name>` on origin, or locally with `local`. */
|
|
42
|
+
async function cupolaBranches(root, local = false) {
|
|
43
|
+
const names = [];
|
|
44
|
+
try {
|
|
45
|
+
const out = await git(root, ["ls-remote", "--heads", "origin", "refs/heads/cupola/*"]);
|
|
46
|
+
for (const line of out.split("\n")) {
|
|
47
|
+
const m = /refs\/heads\/cupola\/(\S+)$/.exec(line);
|
|
48
|
+
if (m?.[1])
|
|
49
|
+
names.push(m[1]);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
// no origin, or unreachable
|
|
54
|
+
}
|
|
55
|
+
if (local) {
|
|
56
|
+
const out = await git(root, [
|
|
57
|
+
"for-each-ref",
|
|
58
|
+
"--format=%(refname:strip=3)",
|
|
59
|
+
"refs/heads/cupola/",
|
|
60
|
+
]).catch(() => "");
|
|
61
|
+
names.push(...out.split("\n").filter(Boolean));
|
|
62
|
+
}
|
|
63
|
+
return names;
|
|
64
|
+
}
|
|
65
|
+
function refOf(name, runbooks) {
|
|
66
|
+
const m = NUMBERED.exec(name);
|
|
67
|
+
if (!m?.[1] || !runbooks.includes(m[1]))
|
|
68
|
+
return undefined;
|
|
69
|
+
return { id: name, runbook: m[1], n: Number(m[2]) };
|
|
70
|
+
}
|
|
71
|
+
/** Every execution of the given runbooks: merged ones on origin's default branch, open ones by their branch. */
|
|
72
|
+
export async function executionRefs(root, config, tree, runbooks) {
|
|
73
|
+
const seen = new Map();
|
|
74
|
+
for (const rb of runbooks) {
|
|
75
|
+
for (const f of await tree.list(executionsDir(config.paths, rb))) {
|
|
76
|
+
const m = /^(\d+)\.md$/.exec(f);
|
|
77
|
+
if (m)
|
|
78
|
+
seen.set(`${rb}-${m[1]}`, { id: `${rb}-${m[1]}`, runbook: rb, n: Number(m[1]) });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
for (const name of await cupolaBranches(root)) {
|
|
82
|
+
const ref = refOf(name, runbooks);
|
|
83
|
+
if (ref && !seen.has(ref.id))
|
|
84
|
+
seen.set(ref.id, ref);
|
|
85
|
+
}
|
|
86
|
+
return [...seen.values()].sort((a, b) => a.runbook === b.runbook ? a.n - b.n : a.runbook < b.runbook ? -1 : 1);
|
|
87
|
+
}
|
|
88
|
+
/** The hub's root for reading: this checkout when it is the hub's, else the hub's main checkout. */
|
|
89
|
+
async function hubRoot(io) {
|
|
90
|
+
const ws = await workspace(io);
|
|
91
|
+
return { root: ws.here.isHub ? ws.root : ws.hub.main, main: ws.hub.main, config: ws.config };
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Reads an execution and derives its status: its document on its branch
|
|
95
|
+
* (origin's, or the local one when newer), else on origin's default branch once merged.
|
|
96
|
+
*/
|
|
97
|
+
async function load(io, root, config, ref, agreedTree) {
|
|
98
|
+
const branch = workBranchName(ref.id);
|
|
99
|
+
const path = executionPath(config.paths, ref.runbook, ref.n);
|
|
100
|
+
const own = await workTree(root, branch);
|
|
101
|
+
const raw = (own.ref === "HEAD" ? undefined : await own.tree.read(path)) ?? (await agreedTree.read(path));
|
|
102
|
+
if (raw === undefined)
|
|
103
|
+
throw new MissingWorkError(`no execution ${ref.id}: no ${path}`);
|
|
104
|
+
const doc = parseExecution(raw);
|
|
105
|
+
const parsed = ExecutionFront.safeParse(doc.front);
|
|
106
|
+
if (!parsed.success)
|
|
107
|
+
throw new Error(`${path}: its front matter is not an execution's (cupola check says why)`);
|
|
108
|
+
const front = parsed.data;
|
|
109
|
+
const book = await agreedTree.read(runbookPath(config.paths, ref.runbook));
|
|
110
|
+
const runbookSteps = book === undefined ? [] : parseRunbook(book).steps.map((s) => s.id);
|
|
111
|
+
const { steps } = arrangedSteps(runbookSteps, doc.arrangement);
|
|
112
|
+
const state = executionState(steps, doc.records);
|
|
113
|
+
const host = (io.host ?? codeHost)(root);
|
|
114
|
+
const change = await host.findChange(branch);
|
|
115
|
+
const gate = front.result && change?.state === "open" && !change.draft
|
|
116
|
+
? await gateReactions(root, host, change, own.tree, ref.id, config)
|
|
117
|
+
: undefined;
|
|
118
|
+
const waits = await waitsFor(io, root, config, agreedTree, front);
|
|
119
|
+
const reach = front.result && change?.state === "open" && !change.draft
|
|
120
|
+
? await host.reach(change, ref.id)
|
|
121
|
+
: undefined;
|
|
122
|
+
const status = deriveExecution({
|
|
123
|
+
id: ref.id,
|
|
124
|
+
front,
|
|
125
|
+
state,
|
|
126
|
+
change,
|
|
127
|
+
...(gate ? { gate } : {}),
|
|
128
|
+
...(change?.state === "merged" && (await untidy(root, branch)) ? { untidy: true } : {}),
|
|
129
|
+
...(waits.length > 0 ? { waits } : {}),
|
|
130
|
+
...(reach ? { reach } : {}),
|
|
131
|
+
});
|
|
132
|
+
return { ref, front, status };
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* An execution for an epic waits for the executions of the runbooks before
|
|
136
|
+
* its own in the epic's procedures: each must have one whose change is merged.
|
|
137
|
+
*/
|
|
138
|
+
async function waitsFor(io, root, config, tree, front) {
|
|
139
|
+
const raw = await tree.read(epicPath(config.paths, front.for));
|
|
140
|
+
if (raw === undefined)
|
|
141
|
+
return [];
|
|
142
|
+
const epic = EpicFront.safeParse(parseEpic(raw).front);
|
|
143
|
+
if (!epic.success)
|
|
144
|
+
return [];
|
|
145
|
+
const order = epic.data.procedures.map((p) => p.runbook);
|
|
146
|
+
const at = order.indexOf(front.runbook);
|
|
147
|
+
if (at <= 0)
|
|
148
|
+
return [];
|
|
149
|
+
const waits = [];
|
|
150
|
+
const host = (io.host ?? codeHost)(root);
|
|
151
|
+
for (const rb of order.slice(0, at)) {
|
|
152
|
+
const refs = await executionRefs(root, config, tree, [rb]);
|
|
153
|
+
let merged = false;
|
|
154
|
+
let open;
|
|
155
|
+
for (const r of refs) {
|
|
156
|
+
const path = executionPath(config.paths, r.runbook, r.n);
|
|
157
|
+
const own = await workTree(root, workBranchName(r.id));
|
|
158
|
+
const text = (own.ref === "HEAD" ? undefined : await own.tree.read(path)) ?? (await tree.read(path));
|
|
159
|
+
const of = text === undefined ? undefined : ExecutionFront.safeParse(parseExecution(text).front);
|
|
160
|
+
if (of?.data?.for !== front.for)
|
|
161
|
+
continue;
|
|
162
|
+
const change = await host.findChange(workBranchName(r.id));
|
|
163
|
+
if (change?.state === "merged")
|
|
164
|
+
merged = true;
|
|
165
|
+
else if (change?.state !== "closed")
|
|
166
|
+
open = r.id;
|
|
167
|
+
}
|
|
168
|
+
if (!merged)
|
|
169
|
+
waits.push(open ?? rb);
|
|
170
|
+
}
|
|
171
|
+
return waits;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The status of an execution, or undefined when `id` is not one: an
|
|
175
|
+
* execution's id is `<runbook>-<n>`, of a runbook agreed in the hub.
|
|
176
|
+
*/
|
|
177
|
+
export async function executionStatus(io, root, config, id) {
|
|
178
|
+
const m = NUMBERED.exec(id);
|
|
179
|
+
if (!m?.[1] || !isWorkId(id))
|
|
180
|
+
return undefined;
|
|
181
|
+
const { tree } = await agreed(root);
|
|
182
|
+
const rb = m[1];
|
|
183
|
+
if ((await tree.read(runbookPath(config.paths, rb))) === undefined)
|
|
184
|
+
return undefined;
|
|
185
|
+
return (await load(io, root, config, { id, runbook: rb, n: Number(m[2]) }, tree)).status;
|
|
186
|
+
}
|
|
187
|
+
/** The executions carried out for an epic or a case, of the given runbooks (all, when not given). */
|
|
188
|
+
export async function executionsFor(io, root, config, target, runbooks) {
|
|
189
|
+
const { tree } = await agreed(root);
|
|
190
|
+
const books = runbooks ?? (await runbookIds(tree, config));
|
|
191
|
+
const out = [];
|
|
192
|
+
for (const ref of await executionRefs(root, config, tree, books)) {
|
|
193
|
+
let loaded;
|
|
194
|
+
try {
|
|
195
|
+
loaded = await load(io, root, config, ref, tree);
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
if (error instanceof MissingWorkError)
|
|
199
|
+
continue;
|
|
200
|
+
throw error;
|
|
201
|
+
}
|
|
202
|
+
if (loaded.front.for === target)
|
|
203
|
+
out.push({ id: ref.id, runbook: ref.runbook, status: loaded.status });
|
|
204
|
+
}
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
function parseParams(given) {
|
|
208
|
+
const out = {};
|
|
209
|
+
for (const p of given) {
|
|
210
|
+
const at = p.indexOf("=");
|
|
211
|
+
if (at <= 0)
|
|
212
|
+
throw new Error(`--param takes <name>=<value>, not ${JSON.stringify(p)}`);
|
|
213
|
+
const name = p.slice(0, at).trim();
|
|
214
|
+
if (name in out)
|
|
215
|
+
throw new Error(`--param ${name} is given twice`);
|
|
216
|
+
out[name] = p.slice(at + 1).trim();
|
|
217
|
+
}
|
|
218
|
+
return out;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* `cupola execution start <runbook> --for <epic|case> [--param <name>=<value>…]`:
|
|
222
|
+
* starts an execution of an agreed runbook — its record from the template, on
|
|
223
|
+
* its own branch, with a draft change open — and prints it. For an epic, the
|
|
224
|
+
* runbook must be one of its procedures and the epic released (or, with no
|
|
225
|
+
* specs, agreed); its params come from the epic. Run again for an epic, it
|
|
226
|
+
* gives the execution there is.
|
|
227
|
+
*/
|
|
228
|
+
export async function executionStart(io, runbook, opts) {
|
|
229
|
+
const target = opts.for;
|
|
230
|
+
if (!target || !isWorkId(target))
|
|
231
|
+
throw new Error("give what it is for: --for <epic|case>");
|
|
232
|
+
if (!isWorkId(runbook))
|
|
233
|
+
throw new Error(`${JSON.stringify(runbook)} is not a runbook id`);
|
|
234
|
+
const { root, main, config } = await hubRoot(io);
|
|
235
|
+
const { tree, ref: agreedRef } = await agreed(root);
|
|
236
|
+
const bookFile = runbookPath(config.paths, runbook);
|
|
237
|
+
const raw = await tree.read(bookFile);
|
|
238
|
+
if (raw === undefined)
|
|
239
|
+
throw new Error(`no ${bookFile} on origin's default branch: no agreed runbook ${runbook}`);
|
|
240
|
+
const book = RunbookFront.safeParse(parseRunbook(raw).front);
|
|
241
|
+
if (!book.success)
|
|
242
|
+
throw new Error(`${bookFile}: its front matter is not a runbook's (cupola check says why)`);
|
|
243
|
+
let params = parseParams(opts.params ?? []);
|
|
244
|
+
const epicRaw = await tree.read(epicPath(config.paths, target));
|
|
245
|
+
if (epicRaw !== undefined) {
|
|
246
|
+
const epic = EpicFront.safeParse(parseEpic(epicRaw).front);
|
|
247
|
+
if (!epic.success)
|
|
248
|
+
throw new Error(`${epicPath(config.paths, target)}: not an epic's front matter`);
|
|
249
|
+
const procedure = epic.data.procedures.find((p) => p.runbook === runbook);
|
|
250
|
+
if (!procedure)
|
|
251
|
+
throw new Error(`${runbook} is not one of ${target}'s procedures`);
|
|
252
|
+
if (Object.keys(params).length > 0)
|
|
253
|
+
throw new Error(`an epic's execution takes its params from the epic (${target}'s procedures)`);
|
|
254
|
+
params = Object.fromEntries(Object.entries(procedure.params ?? {}).map(([k, v]) => [k, String(v)]));
|
|
255
|
+
// Run again (cupola-complete interrupted): the execution there is.
|
|
256
|
+
const there = (await executionsFor(io, root, config, target, [runbook])).find((e) => e.status.status !== "dropped");
|
|
257
|
+
if (there) {
|
|
258
|
+
io.out(opts.json
|
|
259
|
+
? JSON.stringify({
|
|
260
|
+
id: there.id,
|
|
261
|
+
existing: true,
|
|
262
|
+
status: there.status.status,
|
|
263
|
+
change: there.status.change,
|
|
264
|
+
})
|
|
265
|
+
: `${there.id} exists (${there.status.status})`);
|
|
266
|
+
return 0;
|
|
267
|
+
}
|
|
268
|
+
const now = await workStatus(io, root, config, target);
|
|
269
|
+
if (now.status !== "done" && now.status !== "operating")
|
|
270
|
+
throw new Error(`${target} is ${now.status}: an epic's executions start once it is released (with no specs, agreed)`);
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
const found = await (await trackerOf(io)).show(target);
|
|
274
|
+
if (!found)
|
|
275
|
+
throw new Error(`no epic ${target} agreed, and no case ${target} in the tracker`);
|
|
276
|
+
if (!found.open)
|
|
277
|
+
throw new Error(`case ${target} is closed`);
|
|
278
|
+
}
|
|
279
|
+
const declared = Object.keys(book.data.params);
|
|
280
|
+
const missing = declared.filter((k) => !(k in params) || params[k] === "");
|
|
281
|
+
if (missing.length > 0)
|
|
282
|
+
throw new Error(`${runbook} needs ${missing.map((k) => `--param ${k}=<${book.data.params[k]}>`).join(" ")}`);
|
|
283
|
+
const unknown = Object.keys(params).filter((k) => !declared.includes(k));
|
|
284
|
+
if (unknown.length > 0)
|
|
285
|
+
throw new Error(`${runbook} has no param ${unknown.join(", ")}`);
|
|
286
|
+
// n counts this runbook's executions, never reused: merged, on origin, or only here.
|
|
287
|
+
const taken = [
|
|
288
|
+
...(await executionRefs(root, config, tree, [runbook])).map((r) => r.n),
|
|
289
|
+
...(await cupolaBranches(main, true)).flatMap((b) => refOf(b, [runbook])?.n ?? []),
|
|
290
|
+
];
|
|
291
|
+
const n = Math.max(0, ...taken) + 1;
|
|
292
|
+
const id = `${runbook}-${n}`;
|
|
293
|
+
const branch = workBranchName(id);
|
|
294
|
+
const version = await git(root, ["log", "-1", "--format=%H", agreedRef, "--", bookFile]);
|
|
295
|
+
const path = (await existingWorktree(main, branch)) ??
|
|
296
|
+
join(dirname(main), `${basename(main)}.worktrees`, id);
|
|
297
|
+
if (!(await existingWorktree(main, branch)))
|
|
298
|
+
await addWorktree(main, path, branch, {});
|
|
299
|
+
const doc = executionPath(config.paths, runbook, n);
|
|
300
|
+
const list = Object.entries(params);
|
|
301
|
+
const text = readFileSync(templatePath("execution"), "utf8")
|
|
302
|
+
.replaceAll("{{execution-id}}", id)
|
|
303
|
+
.replaceAll("{{runbook-id}}", runbook)
|
|
304
|
+
.replaceAll("{{runbook-title}}", book.data.title)
|
|
305
|
+
.replaceAll("{{commit}}", version)
|
|
306
|
+
.replaceAll("{{epic-or-case}}", target)
|
|
307
|
+
.replaceAll("{{params}}", JSON.stringify(params))
|
|
308
|
+
.replaceAll("{{params-list}}", list.length > 0 ? list.map(([k, v]) => `${k} = ${v}`).join(", ") : "none")
|
|
309
|
+
.replaceAll("{{YYYY-MM-DD}}", await today(io));
|
|
310
|
+
await mkdir(join(path, executionsDir(config.paths, runbook)), { recursive: true });
|
|
311
|
+
await writeFile(join(path, doc), text);
|
|
312
|
+
await commitPaths(path, branch, [doc], `cupola: ${id}: start`);
|
|
313
|
+
const change = await ensureChange(io, main, branch, {
|
|
314
|
+
title: `${book.data.title} (${id})`,
|
|
315
|
+
body: changeBody(id, runbook, target),
|
|
316
|
+
draft: true,
|
|
317
|
+
});
|
|
318
|
+
io.out(opts.json
|
|
319
|
+
? JSON.stringify({ id, runbook, for: target, params, path, document: doc, change })
|
|
320
|
+
: `${id}: ${change.url} (${path})`);
|
|
321
|
+
return 0;
|
|
322
|
+
}
|
|
323
|
+
function changeBody(id, runbook, target) {
|
|
324
|
+
return [
|
|
325
|
+
`<!-- cupola:change ${JSON.stringify({ kind: "execution", work: id, method: 1 })} -->`,
|
|
326
|
+
`Execution \`${id}\` of the runbook \`${runbook}\`, for ${target}. Each step it records is a commit; merging this change accepts the record (execution acceptance), which stays as it is.`,
|
|
327
|
+
"",
|
|
328
|
+
].join("\n");
|
|
329
|
+
}
|
|
330
|
+
async function ensureChange(io, main, branch, opts) {
|
|
331
|
+
const host = (io.host ?? codeHost)(main);
|
|
332
|
+
const found = await host.findChange(branch);
|
|
333
|
+
if (found?.state === "open")
|
|
334
|
+
return found;
|
|
335
|
+
const base = await originDefaultBranch(main);
|
|
336
|
+
if (!base)
|
|
337
|
+
throw new Error("cannot tell origin's default branch");
|
|
338
|
+
return host.openChange({ branch, base, ...opts });
|
|
339
|
+
}
|
|
340
|
+
const STEP_RESULTS = ["done", "stopped"];
|
|
341
|
+
const END_RESULTS = ["finished", "given-up"];
|
|
342
|
+
/**
|
|
343
|
+
* `cupola execution record <execution> --step <P#|X#> --result done|stopped --body-file <f>`,
|
|
344
|
+
* or `--result finished|given-up --body-file <f>`: records one step of an
|
|
345
|
+
* execution in the human's words, or its end, as one commit on its branch,
|
|
346
|
+
* pushed. The end marks its change ready for acceptance and tells the cases.
|
|
347
|
+
*/
|
|
348
|
+
export async function executionRecord(io, id, opts) {
|
|
349
|
+
const result = opts.result ?? "";
|
|
350
|
+
const step = opts.step;
|
|
351
|
+
if (step !== undefined ? !STEP_RESULTS.includes(result) : !END_RESULTS.includes(result))
|
|
352
|
+
throw new Error(step !== undefined
|
|
353
|
+
? "a step is recorded --result done or stopped"
|
|
354
|
+
: "give --step <P#> --result done|stopped, or --result finished|given-up");
|
|
355
|
+
if (!opts.bodyFile)
|
|
356
|
+
throw new Error("give the human's words, one line: --body-file <file>");
|
|
357
|
+
const line = (await readFile(resolve(io.cwd, opts.bodyFile), "utf8")).trim();
|
|
358
|
+
if (line === "" || line.includes("\n"))
|
|
359
|
+
throw new Error("a record is one line");
|
|
360
|
+
if (line.includes("{{"))
|
|
361
|
+
throw new Error("the record still has template placeholders ({{…}})");
|
|
362
|
+
const { root, main, config } = await hubRoot(io);
|
|
363
|
+
const now = await executionStatus(io, root, config, id);
|
|
364
|
+
if (!now?.execution)
|
|
365
|
+
throw new MissingWorkError(`no execution ${id}`);
|
|
366
|
+
const ex = now.execution;
|
|
367
|
+
const ended = ex.result !== null;
|
|
368
|
+
if (now.status === "done" || now.status === "dropped")
|
|
369
|
+
throw new Error(`${id} is ${now.status}: nothing more is recorded`);
|
|
370
|
+
if (now.status === "awaiting-acceptance")
|
|
371
|
+
throw new Error(`${id} is awaiting acceptance: its record changes only after a No-Go sends it back to a step`);
|
|
372
|
+
if (now.status === "waiting" && result !== "given-up")
|
|
373
|
+
throw new Error(`${id} is waiting: ${now.next.text}`);
|
|
374
|
+
if (step !== undefined) {
|
|
375
|
+
if (!ex.steps.includes(step))
|
|
376
|
+
throw new Error(`${id} has no step ${step} (${ex.steps.join(", ")})`);
|
|
377
|
+
// Sent back after its end: any step may be done again. Otherwise one at a time, in order.
|
|
378
|
+
if (!ended && step !== ex.next)
|
|
379
|
+
throw new Error(ex.next
|
|
380
|
+
? `${id} goes on at ${ex.next}, not ${step}: steps are recorded in their order`
|
|
381
|
+
: `every step of ${id} is done: record its end (--result finished)`);
|
|
382
|
+
}
|
|
383
|
+
else if (result === "finished" && ex.result !== "finished" && (ex.next || ex.stopped)) {
|
|
384
|
+
throw new Error(`${id} is not done: ${ex.next ?? ex.stopped} is next`);
|
|
385
|
+
}
|
|
386
|
+
const branch = workBranchName(id);
|
|
387
|
+
let path = await existingWorktree(main, branch);
|
|
388
|
+
if (!path) {
|
|
389
|
+
path = join(dirname(main), `${basename(main)}.worktrees`, id);
|
|
390
|
+
await addWorktree(main, path, branch, {
|
|
391
|
+
mustExist: `an execution's branch is made by \`cupola execution start\``,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
// Someone else may have recorded from another checkout: take theirs in first.
|
|
396
|
+
try {
|
|
397
|
+
await git(path, [
|
|
398
|
+
"fetch",
|
|
399
|
+
"--quiet",
|
|
400
|
+
"origin",
|
|
401
|
+
`+refs/heads/${branch}:refs/remotes/origin/${branch}`,
|
|
402
|
+
]);
|
|
403
|
+
await git(path, ["merge", "--ff-only", "--quiet", `origin/${branch}`]);
|
|
404
|
+
}
|
|
405
|
+
catch {
|
|
406
|
+
throw new Error(`${path} and origin's ${branch} have both moved on, or it has changes in the way; merge origin/${branch} into it, push, and run again`);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
const doc = executionPath(config.paths, ex.runbook, Number(NUMBERED.exec(id)?.[2]));
|
|
410
|
+
const file = join(path, doc);
|
|
411
|
+
let text = await readFile(file, "utf8");
|
|
412
|
+
const day = await today(io);
|
|
413
|
+
const who = await git(path, ["config", "user.name"]).catch(() => "unknown");
|
|
414
|
+
const again = ex.result === result && step === undefined;
|
|
415
|
+
if (step !== undefined) {
|
|
416
|
+
text = insertBefore(text, NEXT_STEP, `- ${step} ${result} (${day}, ${who}): ${line}`);
|
|
417
|
+
if (ended)
|
|
418
|
+
text = setResult(text, null);
|
|
419
|
+
}
|
|
420
|
+
else if (!again) {
|
|
421
|
+
text = insertBefore(text, RESULT_MARK, `- ${result} (${day}, ${who}): ${line}`);
|
|
422
|
+
text = setResult(text, result);
|
|
423
|
+
}
|
|
424
|
+
await writeFile(file, text);
|
|
425
|
+
await commitPaths(path, branch, [doc], `cupola: ${id}: ${step ? `${step} ` : ""}${result}`);
|
|
426
|
+
// The change: ready for acceptance at the end, a draft again when a step is done over after it.
|
|
427
|
+
const host = (io.host ?? codeHost)(main);
|
|
428
|
+
const change = await ensureChange(io, main, branch, {
|
|
429
|
+
title: id,
|
|
430
|
+
body: changeBody(id, ex.runbook, ex.for),
|
|
431
|
+
draft: step !== undefined,
|
|
432
|
+
});
|
|
433
|
+
if (step === undefined && change.draft)
|
|
434
|
+
await host.markReady(change.number);
|
|
435
|
+
if (step !== undefined && ended && !change.draft)
|
|
436
|
+
await host.markDraft(change.number);
|
|
437
|
+
const logged = [];
|
|
438
|
+
if (step === undefined) {
|
|
439
|
+
// People following the cases are told; the case's status never comes from this line.
|
|
440
|
+
const tracker = await trackerOf(io);
|
|
441
|
+
const epicText = await (await agreed(root)).tree.read(epicPath(config.paths, ex.for));
|
|
442
|
+
const epic = epicText === undefined ? undefined : EpicFront.safeParse(parseEpic(epicText).front);
|
|
443
|
+
const cases = epic?.success ? epic.data.cases : [ex.for];
|
|
444
|
+
const what = `execution ${id} ${result === "given-up" ? "given up" : "finished"}`;
|
|
445
|
+
for (const c of cases) {
|
|
446
|
+
const shown = await tracker.show(c);
|
|
447
|
+
if (!shown || shown.text.includes(`${what}:`))
|
|
448
|
+
continue;
|
|
449
|
+
await tracker.log(c, `${day} ${what}: ${line}`);
|
|
450
|
+
logged.push(c);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
const after = await executionStatus(io, root, config, id);
|
|
454
|
+
io.out(opts.json
|
|
455
|
+
? JSON.stringify({
|
|
456
|
+
id,
|
|
457
|
+
...(step ? { step } : {}),
|
|
458
|
+
result,
|
|
459
|
+
logged,
|
|
460
|
+
status: after?.status,
|
|
461
|
+
next: after?.next,
|
|
462
|
+
})
|
|
463
|
+
: `${id}: ${step ? `${step} ` : ""}${result}; ${after?.status ?? ""}`);
|
|
464
|
+
return 0;
|
|
465
|
+
}
|
|
466
|
+
function insertBefore(text, marker, entry) {
|
|
467
|
+
const at = text.indexOf(marker);
|
|
468
|
+
if (at < 0)
|
|
469
|
+
return `${text.trimEnd()}\n${entry}\n`;
|
|
470
|
+
// Entries follow each other, one per line, right above the marker.
|
|
471
|
+
const before = text.slice(0, at).replace(/\n*$/, "\n");
|
|
472
|
+
const sep = /\n- [^\n]*\n$/.test(before) ? "" : "\n";
|
|
473
|
+
return `${before}${sep}${entry}\n\n${text.slice(at)}`;
|
|
474
|
+
}
|
|
475
|
+
function setResult(text, value) {
|
|
476
|
+
const fm = /^---\n([\s\S]*?)\n---/.exec(text);
|
|
477
|
+
if (!fm)
|
|
478
|
+
return text;
|
|
479
|
+
const lines = (fm[1] ?? "").split("\n").filter((l) => !/^result:/.test(l));
|
|
480
|
+
lines.push(value ? `result: ${value}` : "result:");
|
|
481
|
+
return `---\n${lines.join("\n")}\n---${text.slice(fm[0].length)}`;
|
|
482
|
+
}
|
package/dist/commands/locks.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { candidatesOf, pushLock, readLocks, removeLock, } from "../e2e.js";
|
|
2
|
+
import { takeOverNames } from "../record.js";
|
|
3
|
+
import { ago } from "../status.js";
|
|
4
|
+
import { workspace } from "../workspace.js";
|
|
2
5
|
function inUse(env, held) {
|
|
3
6
|
const l = held.lock;
|
|
4
7
|
return (`the shared e2e environment ${env} is in use by ${l.work} (${l.who}, session ${l.session}, since ${l.since}); ` +
|
|
@@ -81,7 +84,10 @@ export async function claimEnvironments(where, profile, claim) {
|
|
|
81
84
|
// take-over.
|
|
82
85
|
const mine = candidates.find((c) => locks.get(c.id)?.lock.work === claim.work);
|
|
83
86
|
const free = candidates.find((c) => !locks.has(c.id));
|
|
84
|
-
const over = candidates.find((c) =>
|
|
87
|
+
const over = candidates.find((c) => {
|
|
88
|
+
const held = locks.get(c.id);
|
|
89
|
+
return held !== undefined && takeOverNames(claim.runner.takeOver, held.lock);
|
|
90
|
+
});
|
|
85
91
|
if (mine) {
|
|
86
92
|
const held = locks.get(mine.id);
|
|
87
93
|
const same = held.lock.session === claim.runner.session && held.lock.id === claim.marker;
|
|
@@ -173,3 +179,65 @@ export async function environmentsInUse(where, profile, needs, work) {
|
|
|
173
179
|
}
|
|
174
180
|
return undefined;
|
|
175
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* `cupola locks [<component>]`: the locks on shared e2e environments, read from origin of each
|
|
184
|
+
* repository that keeps a component's work. Read only: nothing is taken or released.
|
|
185
|
+
*/
|
|
186
|
+
export async function listLocks(io, component, opts) {
|
|
187
|
+
const ws = await workspace(io);
|
|
188
|
+
if (component !== undefined && !ws.config.components.some((c) => c.name === component))
|
|
189
|
+
throw new Error(`${component} is not a component of the configuration`);
|
|
190
|
+
const now = io.now?.() ?? new Date();
|
|
191
|
+
const locks = [];
|
|
192
|
+
const unreachable = [];
|
|
193
|
+
for (const slug of ws.slugs) {
|
|
194
|
+
let root;
|
|
195
|
+
try {
|
|
196
|
+
root = slug === ws.here.slug ? ws.root : ws.checkout(slug).main;
|
|
197
|
+
}
|
|
198
|
+
catch {
|
|
199
|
+
unreachable.push(`${slug}: not cloned (\`cupola workspace setup\`)`);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
let held;
|
|
203
|
+
try {
|
|
204
|
+
held = await readLocks(root);
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
unreachable.push(`${slug}: its origin could not be read`);
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
for (const [environment, { lock }] of held) {
|
|
211
|
+
// An environment is named `<component>.<name>`.
|
|
212
|
+
const of = environment.split(".")[0] ?? "";
|
|
213
|
+
if (component !== undefined && of !== component)
|
|
214
|
+
continue;
|
|
215
|
+
locks.push({
|
|
216
|
+
environment,
|
|
217
|
+
component: of,
|
|
218
|
+
repo: slug,
|
|
219
|
+
work: lock.work,
|
|
220
|
+
phase: lock.phase,
|
|
221
|
+
who: lock.who,
|
|
222
|
+
session: lock.session,
|
|
223
|
+
since: lock.since,
|
|
224
|
+
ago: ago(lock.since, now) ?? null,
|
|
225
|
+
id: lock.id,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
locks.sort((a, b) => a.environment.localeCompare(b.environment));
|
|
230
|
+
if (opts.json) {
|
|
231
|
+
io.out(JSON.stringify({ locks, ...(unreachable.length > 0 ? { unreachable } : {}) }));
|
|
232
|
+
return 0;
|
|
233
|
+
}
|
|
234
|
+
if (locks.length === 0)
|
|
235
|
+
io.out("No shared e2e environment is locked.");
|
|
236
|
+
for (const l of locks) {
|
|
237
|
+
io.out(`${l.environment}: ${l.work} (${l.phase}), ${l.who}, session ${l.session}, since ${l.since}` +
|
|
238
|
+
`${l.ago ? ` (${l.ago})` : ""}; if that session has ended, its run takes it over with --take-over ${l.id}`);
|
|
239
|
+
}
|
|
240
|
+
for (const u of unreachable)
|
|
241
|
+
io.out(`not read: ${u}`);
|
|
242
|
+
return 0;
|
|
243
|
+
}
|