@kyuki3rain/cupola 0.0.0-stage → 0.1.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 +26 -0
- package/LICENSE +202 -0
- package/README.md +40 -2
- package/dist/adr.js +106 -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 +522 -0
- package/dist/commands/epic.js +311 -0
- package/dist/commands/install.js +109 -0
- package/dist/commands/locks.js +175 -0
- package/dist/commands/procedure.js +92 -0
- package/dist/commands/revision.js +139 -0
- package/dist/commands/run.js +1015 -0
- package/dist/commands/setup.js +382 -0
- package/dist/commands/spec.js +80 -0
- package/dist/commands/status.js +1158 -0
- package/dist/commands/trace.js +397 -0
- package/dist/commands/wait.js +102 -0
- package/dist/commands/work.js +121 -0
- package/dist/commands/workspace.js +126 -0
- package/dist/commands/worktree.js +422 -0
- package/dist/config.js +289 -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 +360 -0
- package/dist/feedback.js +34 -0
- package/dist/frontmatter.js +20 -0
- package/dist/git.js +365 -0
- package/dist/host/github.js +290 -0
- package/dist/host/host.js +1 -0
- package/dist/install.js +182 -0
- package/dist/io.js +46 -0
- package/dist/main.js +771 -0
- package/dist/method/v1.js +56 -0
- package/dist/procedure.js +60 -0
- package/dist/record.js +473 -0
- package/dist/release.js +84 -0
- package/dist/repo.js +13 -0
- package/dist/runbook.js +111 -0
- package/dist/safe-write.js +80 -0
- package/dist/spec.js +171 -0
- package/dist/status.js +190 -0
- package/dist/templates.js +49 -0
- package/dist/trace.js +275 -0
- package/dist/tracker/files.js +156 -0
- package/dist/tracker/github-issues.js +217 -0
- package/dist/tracker/index.js +27 -0
- package/dist/tracker/tracker.js +69 -0
- package/dist/tree.js +42 -0
- package/dist/trust.js +13 -0
- package/dist/version.js +5 -0
- package/dist/work.js +149 -0
- package/dist/workspace.js +299 -0
- package/npm-shrinkwrap.json +2014 -0
- package/package.json +41 -4
- package/skills/cupola-abort/SKILL.md +45 -0
- package/skills/cupola-adr/SKILL.md +41 -0
- package/skills/cupola-ask/SKILL.md +42 -0
- package/skills/cupola-catalog/SKILL.md +41 -0
- package/skills/cupola-catalog-draft/SKILL.md +114 -0
- package/skills/cupola-complete/SKILL.md +87 -0
- package/skills/cupola-decide/SKILL.md +60 -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 +162 -0
- package/skills/cupola-epic-create/SKILL.md +120 -0
- package/skills/cupola-epic-revise/SKILL.md +71 -0
- package/skills/cupola-epic-run/SKILL.md +127 -0
- package/skills/cupola-fix/SKILL.md +115 -0
- package/skills/cupola-init/SKILL.md +88 -0
- package/skills/cupola-intake/SKILL.md +78 -0
- package/skills/cupola-operate/SKILL.md +64 -0
- package/skills/cupola-resolve/SKILL.md +86 -0
- package/skills/cupola-review/SKILL.md +132 -0
- package/skills/cupola-spec-design/SKILL.md +164 -0
- package/skills/cupola-spec-impl/SKILL.md +135 -0
- package/skills/cupola-spec-run/SKILL.md +125 -0
- package/skills/cupola-steering/SKILL.md +46 -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 +41 -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 +29 -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 +136 -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/phase-outcome.json +29 -0
- package/templates/method-1/record.json +63 -0
- package/templates/method-1/report.md +27 -0
- package/templates/method-1/runbook.md +39 -0
- package/templates/method-1/runner-result.json +6 -0
- package/templates/method-1/send-back.md +23 -0
- package/templates/methods.json +13 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
4
|
+
import { epicPath, loadConfig, pathsAt } from "../config.js";
|
|
5
|
+
import { today } from "../day.js";
|
|
6
|
+
import { parseFrontMatter } from "../frontmatter.js";
|
|
7
|
+
import { git, originDefaultBranch } from "../git.js";
|
|
8
|
+
import { github } from "../host/github.js";
|
|
9
|
+
import { parseRecord } from "../record.js";
|
|
10
|
+
import { epicBranchName, epicFront, specNamesOf } from "../spec.js";
|
|
11
|
+
import { gitTree } from "../tree.js";
|
|
12
|
+
import { assertWorkId, runDir, workBranchName } from "../work.js";
|
|
13
|
+
import { epicRepos, mainCheckout, workspace } from "../workspace.js";
|
|
14
|
+
import { fileCases } from "./run.js";
|
|
15
|
+
import { fixSpecsOf, workTree } from "./status.js";
|
|
16
|
+
import { trackerOf } from "./work.js";
|
|
17
|
+
async function fetchBranch(root, branch) {
|
|
18
|
+
try {
|
|
19
|
+
await git(root, ["ls-remote", "--exit-code", "--heads", "origin", `refs/heads/${branch}`]);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (error.code === 2)
|
|
23
|
+
return false;
|
|
24
|
+
throw new Error(`cannot reach origin to look for ${branch}; try again when it is reachable`);
|
|
25
|
+
}
|
|
26
|
+
await git(root, [
|
|
27
|
+
"fetch",
|
|
28
|
+
"--quiet",
|
|
29
|
+
"origin",
|
|
30
|
+
`+refs/heads/${branch}:refs/remotes/origin/${branch}`,
|
|
31
|
+
]);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
/** The agreed epic, read from origin's default branch, and that branch's name. */
|
|
35
|
+
async function agreedEpic(root, id) {
|
|
36
|
+
assertWorkId(id);
|
|
37
|
+
const base = await originDefaultBranch(root);
|
|
38
|
+
if (!base)
|
|
39
|
+
throw new Error("cannot tell origin's default branch, where agreed epics are");
|
|
40
|
+
await fetchBranch(root, base);
|
|
41
|
+
const path = epicPath(await pathsAt(root), id);
|
|
42
|
+
const raw = await gitTree(root, `refs/remotes/origin/${base}`).read(path);
|
|
43
|
+
if (raw === undefined)
|
|
44
|
+
throw new Error(`${path} is not on origin's ${base}: the epic is not agreed`);
|
|
45
|
+
return { base, ...epicFront(path, raw) };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* `cupola epic branch <epic>`: the epic branch `epic/<id>` of an agreed epic
|
|
49
|
+
* with two or more specs, created from origin's default branch
|
|
50
|
+
* and pushed when it does not exist yet. Running it again changes nothing.
|
|
51
|
+
*/
|
|
52
|
+
export async function epicBranch(io, id, opts) {
|
|
53
|
+
const { hub, repos } = await epicPlaces(io, id);
|
|
54
|
+
const { front } = await agreedEpic(hub, id);
|
|
55
|
+
if (front.specs.length < 2) {
|
|
56
|
+
throw new Error(`${id} has one spec: it runs on its spec's branch, without an epic branch`);
|
|
57
|
+
}
|
|
58
|
+
const branch = epicBranchName(id);
|
|
59
|
+
// One epic branch per repository the epic touches, the hub's included.
|
|
60
|
+
// The repositories are independent: each is reached at once, not one after another.
|
|
61
|
+
const settled = await Promise.allSettled(repos.map(async (repo) => {
|
|
62
|
+
const base = await originDefaultBranch(repo.main);
|
|
63
|
+
if (!base)
|
|
64
|
+
throw new Error(`cannot tell origin's default branch of ${repo.slug}`);
|
|
65
|
+
// Within one repository, one fetch at a time: two at once contend for its refs.
|
|
66
|
+
await fetchBranch(repo.main, base);
|
|
67
|
+
const exists = await fetchBranch(repo.main, branch);
|
|
68
|
+
if (!exists) {
|
|
69
|
+
await git(repo.main, [
|
|
70
|
+
"push",
|
|
71
|
+
"--quiet",
|
|
72
|
+
"origin",
|
|
73
|
+
`refs/remotes/origin/${base}:refs/heads/${branch}`,
|
|
74
|
+
]);
|
|
75
|
+
await fetchBranch(repo.main, branch);
|
|
76
|
+
}
|
|
77
|
+
return { repo: repo.slug, created: !exists };
|
|
78
|
+
}));
|
|
79
|
+
// Every repository is tried; then every failure is told, not only the first.
|
|
80
|
+
const failed = settled.flatMap((r, i) => r.status === "rejected"
|
|
81
|
+
? [`${repos[i]?.slug}: ${r.reason instanceof Error ? r.reason.message : String(r.reason)}`]
|
|
82
|
+
: []);
|
|
83
|
+
if (failed.length > 0)
|
|
84
|
+
throw new Error(failed.join("\n"));
|
|
85
|
+
const branches = settled.flatMap((r) => (r.status === "fulfilled" ? [r.value] : []));
|
|
86
|
+
const created = branches.some((b) => b.created);
|
|
87
|
+
const result = {
|
|
88
|
+
epic: id,
|
|
89
|
+
branch,
|
|
90
|
+
created,
|
|
91
|
+
...(repos.length > 1 ? { branches } : {}),
|
|
92
|
+
};
|
|
93
|
+
io.out(opts.json ? JSON.stringify(result) : `${branch}${created ? " (created)" : ""}`);
|
|
94
|
+
return 0;
|
|
95
|
+
}
|
|
96
|
+
/** The hub's checkout and the repositories an epic touches. */
|
|
97
|
+
async function epicPlaces(io, id) {
|
|
98
|
+
// Checked here, not only in agreedEpic, whose failure below is taken as "not agreed".
|
|
99
|
+
assertWorkId(id);
|
|
100
|
+
const ws = await workspace(io);
|
|
101
|
+
const hub = ws.here.isHub ? ws.root : ws.hub.main;
|
|
102
|
+
if (ws.repos.length === 1)
|
|
103
|
+
return { hub, repos: [{ ...ws.hub, main: ws.root }] };
|
|
104
|
+
const { front } = await agreedEpic(hub, id).catch(async () => {
|
|
105
|
+
// Not agreed: an abort of a draft touches the hub alone.
|
|
106
|
+
return { front: { specs: [] } };
|
|
107
|
+
});
|
|
108
|
+
return {
|
|
109
|
+
hub,
|
|
110
|
+
repos: epicRepos(ws, front.specs.map((s) => s.component)),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* `cupola epic sync <epic>`: merges origin's default branch into the epic
|
|
115
|
+
* branch (after every release and fix), in a worktree of its
|
|
116
|
+
* own. On a conflict the worktree is left with the merge in progress, for the
|
|
117
|
+
* AI to resolve, commit, and push; nothing is pushed until then.
|
|
118
|
+
*/
|
|
119
|
+
export async function epicSync(io, id, opts) {
|
|
120
|
+
const { repos } = await epicPlaces(io, id);
|
|
121
|
+
// Each repository's epic branch takes in its own default branch; the first conflict stops.
|
|
122
|
+
let merged = false;
|
|
123
|
+
for (const repo of repos) {
|
|
124
|
+
const lines = [];
|
|
125
|
+
const code = await syncOne({ ...io, out: (l) => lines.push(l) }, repo.main, id, { json: true });
|
|
126
|
+
const r = JSON.parse(lines.join("\n"));
|
|
127
|
+
merged ||= r.merged;
|
|
128
|
+
if (code !== 0 || r.conflicts.length > 0) {
|
|
129
|
+
const result = { ...r, ...(repos.length > 1 ? { repo: repo.slug } : {}) };
|
|
130
|
+
io.out(opts.json ? JSON.stringify(result) : `conflicts in ${repo.slug}: ${r.conflicts.join(", ")}`);
|
|
131
|
+
return 1;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const branch = epicBranchName(id);
|
|
135
|
+
io.out(opts.json
|
|
136
|
+
? JSON.stringify({ epic: id, branch, merged, conflicts: [] })
|
|
137
|
+
: merged
|
|
138
|
+
? `merged into ${branch}`
|
|
139
|
+
: `${branch} is up to date`);
|
|
140
|
+
return 0;
|
|
141
|
+
}
|
|
142
|
+
/** Merges one repository's default branch into its epic branch. */
|
|
143
|
+
async function syncOne(io, root, id, opts) {
|
|
144
|
+
const base = await originDefaultBranch(root);
|
|
145
|
+
if (!base)
|
|
146
|
+
throw new Error(`cannot tell origin's default branch in ${root}`);
|
|
147
|
+
await fetchBranch(root, base);
|
|
148
|
+
const branch = epicBranchName(id);
|
|
149
|
+
if (!(await fetchBranch(root, branch)))
|
|
150
|
+
throw new Error(`${branch} is not on origin`);
|
|
151
|
+
const remoteBase = `refs/remotes/origin/${base}`;
|
|
152
|
+
const remoteEpic = `refs/remotes/origin/${branch}`;
|
|
153
|
+
const out = (r, text) => io.out(opts.json ? JSON.stringify(r) : text);
|
|
154
|
+
try {
|
|
155
|
+
await git(root, ["merge-base", "--is-ancestor", remoteBase, remoteEpic]);
|
|
156
|
+
out({ epic: id, branch, merged: false, conflicts: [] }, `${branch} already has ${base}`);
|
|
157
|
+
return 0;
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
// behind: merge below
|
|
161
|
+
}
|
|
162
|
+
const main = await mainCheckout(root);
|
|
163
|
+
const path = join(dirname(main), `${basename(main)}.worktrees`, `epic-${id}-sync`);
|
|
164
|
+
if (existsSync(path)) {
|
|
165
|
+
throw new Error(`${path} exists: an earlier sync stopped on a conflict. Resolve it there, commit, push to ${branch}, remove the worktree, and run this again`);
|
|
166
|
+
}
|
|
167
|
+
await git(main, ["worktree", "add", "--quiet", "--detach", path, remoteEpic]);
|
|
168
|
+
// Never hides the error that made the sync stop.
|
|
169
|
+
const drop = () => git(main, ["worktree", "remove", "--force", path]).catch(() => "");
|
|
170
|
+
try {
|
|
171
|
+
await git(path, ["merge", "--quiet", "--no-edit", `origin/${base}`]);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
const conflicts = (await git(path, ["diff", "--name-only", "--diff-filter=U"]))
|
|
175
|
+
.split("\n")
|
|
176
|
+
.filter(Boolean);
|
|
177
|
+
if (conflicts.length === 0) {
|
|
178
|
+
// Not a conflict: nothing is left behind for a later sync to trip on.
|
|
179
|
+
await drop();
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
out({ epic: id, branch, merged: false, conflicts, path }, `conflicts merging ${base} into ${branch} in ${path}: ${conflicts.join(", ")}`);
|
|
183
|
+
return 1;
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
await git(path, ["push", "--quiet", "origin", `HEAD:refs/heads/${branch}`]);
|
|
187
|
+
}
|
|
188
|
+
finally {
|
|
189
|
+
// Pushed, or refused (a spec merged into the epic branch meanwhile): run again either way.
|
|
190
|
+
await drop();
|
|
191
|
+
}
|
|
192
|
+
await fetchBranch(root, branch);
|
|
193
|
+
out({ epic: id, branch, merged: true, conflicts: [] }, `merged ${base} into ${branch}`);
|
|
194
|
+
return 0;
|
|
195
|
+
}
|
|
196
|
+
/** The Log marker of an epic aborted after G1, on each of its cases (its status derives from it). */
|
|
197
|
+
export function abortedMarker(epic) {
|
|
198
|
+
return `<!-- cupola:aborted ${JSON.stringify({ epic })} -->`;
|
|
199
|
+
}
|
|
200
|
+
/** Whether a case's text records the epic as aborted. */
|
|
201
|
+
export function recordsAbort(caseText, epic) {
|
|
202
|
+
for (const m of caseText.matchAll(/<!--\s*cupola:aborted\s+(\{.*?\})\s*-->/g)) {
|
|
203
|
+
try {
|
|
204
|
+
if (JSON.parse(m[1] ?? "{}").epic === epic)
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
// a marker edited by hand names nothing
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* `cupola epic abort <epic> --body-file <reason>`: carries out an abort a
|
|
215
|
+
* human decided. Every open change of the epic is closed — its
|
|
216
|
+
* `epic` change, its specs' and fix specs', its release — with their branches
|
|
217
|
+
* kept for reference; an agreed epic's cases record the abort and its reason.
|
|
218
|
+
* Running it again changes nothing more.
|
|
219
|
+
*/
|
|
220
|
+
export async function epicAbort(io, id, opts) {
|
|
221
|
+
if (!opts.bodyFile)
|
|
222
|
+
throw new Error("give the reason, one line: --body-file <file>");
|
|
223
|
+
const reason = (await readFile(resolve(io.cwd, opts.bodyFile), "utf8")).trim();
|
|
224
|
+
if (reason === "" || reason.includes("\n"))
|
|
225
|
+
throw new Error("the reason is one line");
|
|
226
|
+
assertWorkId(id);
|
|
227
|
+
const { hub: root, repos } = await epicPlaces(io, id);
|
|
228
|
+
const base = await originDefaultBranch(root);
|
|
229
|
+
if (base)
|
|
230
|
+
await fetchBranch(root, base);
|
|
231
|
+
const path = epicPath(await pathsAt(root), id);
|
|
232
|
+
const agreedText = base
|
|
233
|
+
? await gitTree(root, `refs/remotes/origin/${base}`).read(path)
|
|
234
|
+
: undefined;
|
|
235
|
+
let text = agreedText;
|
|
236
|
+
if (text === undefined && (await fetchBranch(root, workBranchName(id)))) {
|
|
237
|
+
text = await gitTree(root, `refs/remotes/origin/${workBranchName(id)}`).read(path);
|
|
238
|
+
}
|
|
239
|
+
if (text === undefined)
|
|
240
|
+
throw new Error(`no epic ${id}: no ${path} agreed or drafted`);
|
|
241
|
+
const { front } = epicFront(path, text);
|
|
242
|
+
// The `system` change of an only spec outside the hub goes with them.
|
|
243
|
+
const branches = [workBranchName(id), epicBranchName(id), workBranchName(`${id}-system`)];
|
|
244
|
+
const names = specNamesOf(id, front.specs).map((s) => s.name);
|
|
245
|
+
// Fix specs are matched by their document: another epic's name may start the same way.
|
|
246
|
+
names.push(...(await fixSpecsOf(root, await loadConfig(root), id, names)));
|
|
247
|
+
branches.push(...names.map(workBranchName));
|
|
248
|
+
// What the runs split off is filed either way; Cupola issue drafts are
|
|
249
|
+
// listed for a human. Only runs that did not reach their gate still hold drafts.
|
|
250
|
+
const drafts = [];
|
|
251
|
+
const cupolaIssues = [];
|
|
252
|
+
for (const [work, branch] of [
|
|
253
|
+
...names.map((n) => [n, workBranchName(n)]),
|
|
254
|
+
[id, epicBranchName(id)],
|
|
255
|
+
]) {
|
|
256
|
+
// A spec's run is in its component's repository: the first that has its branch.
|
|
257
|
+
let raw;
|
|
258
|
+
let tree;
|
|
259
|
+
for (const repo of repos) {
|
|
260
|
+
const t = (await workTree(repo.main, branch)).tree;
|
|
261
|
+
raw = await t.read(`${runDir(work)}/record.json`);
|
|
262
|
+
if (raw !== undefined) {
|
|
263
|
+
tree = t;
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (raw === undefined || !tree)
|
|
268
|
+
continue;
|
|
269
|
+
for (const d of parseRecord(raw).drafts) {
|
|
270
|
+
const path = `${runDir(work)}/drafts/${d}`;
|
|
271
|
+
const text = await tree.read(path);
|
|
272
|
+
if (text === undefined)
|
|
273
|
+
continue;
|
|
274
|
+
if (parseFrontMatter(text).data.kind === "case")
|
|
275
|
+
drafts.push(text);
|
|
276
|
+
else
|
|
277
|
+
cupolaIssues.push(`${path} on ${branch}`);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
const filed = (await fileCases(io, drafts)).map((c) => c.id);
|
|
281
|
+
const comment = `Aborted: ${reason}\n\nThe branch is kept for reference.`;
|
|
282
|
+
const closed = [];
|
|
283
|
+
// Every repository the epic touches: its epic branch's change and its specs'.
|
|
284
|
+
for (const repo of repos) {
|
|
285
|
+
const host = (io.host ?? github)(repo.main);
|
|
286
|
+
for (const branch of branches) {
|
|
287
|
+
const change = await host.findChange(branch);
|
|
288
|
+
if (change?.state !== "open")
|
|
289
|
+
continue;
|
|
290
|
+
await host.closeChange(change.number, comment);
|
|
291
|
+
closed.push(change.url);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
const logged = [];
|
|
295
|
+
if (agreedText !== undefined) {
|
|
296
|
+
const tracker = await trackerOf(io);
|
|
297
|
+
const day = await today(io);
|
|
298
|
+
for (const c of front.cases) {
|
|
299
|
+
const shown = await tracker.show(c);
|
|
300
|
+
if (!shown || recordsAbort(shown.text, id))
|
|
301
|
+
continue;
|
|
302
|
+
await tracker.log(c, `${day} epic ${id} aborted: ${reason} ${abortedMarker(id)}`);
|
|
303
|
+
logged.push(c);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
const result = { epic: id, closed, logged, filed, cupolaIssues };
|
|
307
|
+
io.out(opts.json
|
|
308
|
+
? JSON.stringify(result)
|
|
309
|
+
: `aborted ${id}: closed ${closed.length} changes, logged on ${logged.join(", ") || "no case"}`);
|
|
310
|
+
return 0;
|
|
311
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { basename, join, resolve } from "node:path";
|
|
4
|
+
import { CONFIG_PATH, loadConfig, parseConfig } from "../config.js";
|
|
5
|
+
import { ENGINE_LANGUAGE } from "../engine/cc-sdd.js";
|
|
6
|
+
import { git } from "../git.js";
|
|
7
|
+
import { ownerOf } from "../host/github.js";
|
|
8
|
+
import { update } from "../install.js";
|
|
9
|
+
import { repoRoot } from "../repo.js";
|
|
10
|
+
import { writeInside } from "../safe-write.js";
|
|
11
|
+
import { identifyRepo } from "../workspace.js";
|
|
12
|
+
/** Whether this checkout is the hub's; one that cannot be told apart counts as the hub. */
|
|
13
|
+
async function isHub(root, config) {
|
|
14
|
+
const slug = await identifyRepo(root, config).catch(() => undefined);
|
|
15
|
+
return !slug || slug.toLowerCase() === config.hub.toLowerCase();
|
|
16
|
+
}
|
|
17
|
+
/** After init: the dialogue decides the components and fills the rest. */
|
|
18
|
+
const INIT_NEXT = "in Claude Code, run `cupola-init`: it decides the components with you, sets up the tools, and writes the steering";
|
|
19
|
+
function report(io, json, r, next) {
|
|
20
|
+
if (json)
|
|
21
|
+
io.out(JSON.stringify(next ? { ...r, next } : r));
|
|
22
|
+
else {
|
|
23
|
+
if (r.written.length)
|
|
24
|
+
io.out(`wrote: ${r.written.join(", ")}`);
|
|
25
|
+
if (r.kept.length)
|
|
26
|
+
io.out(`kept: ${r.kept.join(", ")}`);
|
|
27
|
+
for (const c of r.conflicts) {
|
|
28
|
+
io.out(`conflict: ${c} was edited here and changed in Cupola; rerun with --keep ${c} or --take-new ${c}`);
|
|
29
|
+
}
|
|
30
|
+
if (next)
|
|
31
|
+
io.out(`next: ${next}`);
|
|
32
|
+
}
|
|
33
|
+
return r.conflicts.length > 0 ? 1 : 0;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* `cupola update [--keep <path>] [--take-new <path>]`: installs or updates the
|
|
37
|
+
* files Cupola manages (skills, cc-sdd's skills and settings with Cupola's
|
|
38
|
+
* patches, the CLAUDE.md section) and creates the
|
|
39
|
+
* project's steering defaults once. Nothing is committed: the human reviews
|
|
40
|
+
* the working tree and commits it.
|
|
41
|
+
*/
|
|
42
|
+
export async function updateCommand(io, opts) {
|
|
43
|
+
const root = await repoRoot(io.cwd);
|
|
44
|
+
if (!existsSync(join(root, CONFIG_PATH)))
|
|
45
|
+
throw new Error(`${CONFIG_PATH} not found; run \`cupola init\` first`);
|
|
46
|
+
const config = await loadConfig(root);
|
|
47
|
+
return report(io, opts.json, await update(root, { keep: opts.keep ?? [], takeNew: opts.takeNew ?? [] }, { language: ENGINE_LANGUAGE, ...(io.engine ? { generate: io.engine } : {}) }, (await isHub(root, config)) ? config.paths.adr : null));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* `cupola init [--hub owner/repo] [--component name[:path[:role]]]… [--language en]
|
|
51
|
+
* [--tracker files|github-issues] [--reviewer copilot|subagent]`: writes
|
|
52
|
+
* `.cupola/config.json` and installs Cupola. The hub defaults to origin's
|
|
53
|
+
* repository, and a single component to the whole repository.
|
|
54
|
+
*/
|
|
55
|
+
export async function initCommand(io, opts) {
|
|
56
|
+
const root = await repoRoot(io.cwd);
|
|
57
|
+
if (existsSync(join(root, CONFIG_PATH))) {
|
|
58
|
+
throw new Error(`${CONFIG_PATH} exists; edit it, or run \`cupola update\` to install the files`);
|
|
59
|
+
}
|
|
60
|
+
if (opts.configFile) {
|
|
61
|
+
if (opts.hub || opts.components?.length || opts.language || opts.tracker || opts.reviewer)
|
|
62
|
+
throw new Error("--config-file holds the whole configuration; give --hub, --component, --language, --tracker, and --reviewer in it instead");
|
|
63
|
+
const text = await readFile(resolve(io.cwd, opts.configFile), "utf8");
|
|
64
|
+
const config = parseConfig(text); // refuse to write what Cupola would not read
|
|
65
|
+
await writeInside(root, CONFIG_PATH, `${JSON.stringify(JSON.parse(text), null, 2)}\n`);
|
|
66
|
+
if (!opts.json)
|
|
67
|
+
io.out(`wrote ${CONFIG_PATH}`);
|
|
68
|
+
return report(io, opts.json, await update(root, {}, { language: ENGINE_LANGUAGE, ...(io.engine ? { generate: io.engine } : {}) }, (await isHub(root, config)) ? config.paths.adr : null), INIT_NEXT);
|
|
69
|
+
}
|
|
70
|
+
let hub = opts.hub;
|
|
71
|
+
if (!hub) {
|
|
72
|
+
const url = await git(root, ["remote", "get-url", "origin"]).catch(() => "");
|
|
73
|
+
const name = /[:/][^/:]+\/([^/]+?)(?:\.git)?\/?$/.exec(url)?.[1];
|
|
74
|
+
const owner = ownerOf(url);
|
|
75
|
+
if (!owner || !name)
|
|
76
|
+
throw new Error("cannot tell the hub from origin; give --hub <owner>/<repo>");
|
|
77
|
+
hub = `${owner}/${name}`;
|
|
78
|
+
}
|
|
79
|
+
// Without --component, the whole repository is one component; cupola-init splits it.
|
|
80
|
+
const components = (opts.components?.length ? opts.components : [basename(root)]).map((spec) => {
|
|
81
|
+
const [name = "", path, ...role] = spec.split(":");
|
|
82
|
+
return {
|
|
83
|
+
name: name
|
|
84
|
+
.toLowerCase()
|
|
85
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
86
|
+
.replace(/^-+|-+$/g, ""),
|
|
87
|
+
...(path ? { path } : {}),
|
|
88
|
+
role: role.join(":") || (path ? `the code under ${path}` : "the whole repository"),
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
const config = {
|
|
92
|
+
version: 1,
|
|
93
|
+
language: opts.language ?? "en",
|
|
94
|
+
// Records are dated in one zone whoever writes them: this machine's, to confirm in cupola-init.
|
|
95
|
+
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
96
|
+
hub,
|
|
97
|
+
components,
|
|
98
|
+
host: { kind: "github" },
|
|
99
|
+
tracker: { kind: opts.tracker ?? "github-issues" },
|
|
100
|
+
engine: { kind: "cc-sdd" },
|
|
101
|
+
review: { reviewer: opts.reviewer ?? "copilot", max_rounds: 5, confirm_mode: false },
|
|
102
|
+
};
|
|
103
|
+
const text = `${JSON.stringify(config, null, 2)}\n`;
|
|
104
|
+
parseConfig(text); // refuse to write what Cupola would not read
|
|
105
|
+
await writeInside(root, CONFIG_PATH, text);
|
|
106
|
+
if (!opts.json)
|
|
107
|
+
io.out(`wrote ${CONFIG_PATH}`);
|
|
108
|
+
return report(io, opts.json, await update(root, {}, { language: ENGINE_LANGUAGE, ...(io.engine ? { generate: io.engine } : {}) }, (await isHub(root, parseConfig(text))) ? parseConfig(text).paths.adr : null), INIT_NEXT);
|
|
109
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { candidatesOf, pushLock, readLocks, removeLock, } from "../e2e.js";
|
|
2
|
+
function inUse(env, held) {
|
|
3
|
+
const l = held.lock;
|
|
4
|
+
return (`the shared e2e environment ${env} is in use by ${l.work} (${l.who}, session ${l.session}, since ${l.since}); ` +
|
|
5
|
+
`if that session has ended, run again with --take-over ${l.id}`);
|
|
6
|
+
}
|
|
7
|
+
/** The needs the profile does not know: neither an environment nor a pool of shared ones. */
|
|
8
|
+
export function unknownNeeds(profile, needs) {
|
|
9
|
+
return needs.filter((n) => candidatesOf(profile, n) === undefined);
|
|
10
|
+
}
|
|
11
|
+
function needed(profile, need) {
|
|
12
|
+
const candidates = candidatesOf(profile, need);
|
|
13
|
+
if (!candidates) {
|
|
14
|
+
throw new Error(`${need} is neither an environment nor a pool of shared environments in the e2e profile`);
|
|
15
|
+
}
|
|
16
|
+
return candidates;
|
|
17
|
+
}
|
|
18
|
+
function placeOf(where) {
|
|
19
|
+
return typeof where === "string" ? () => where : where;
|
|
20
|
+
}
|
|
21
|
+
/** The locks of the environments, read from each root they live in. */
|
|
22
|
+
async function readAll(rootOf, environments) {
|
|
23
|
+
const roots = [...new Set(environments.map(rootOf))];
|
|
24
|
+
const out = new Map();
|
|
25
|
+
for (const root of roots) {
|
|
26
|
+
for (const [env, held] of await readLocks(root))
|
|
27
|
+
if (rootOf(env) === root)
|
|
28
|
+
out.set(env, held);
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
/** Every environment a set of needs may take. */
|
|
33
|
+
function envsOf(profile, needs) {
|
|
34
|
+
return needs.flatMap((n) => (candidatesOf(profile, n) ?? []).map((c) => c.id));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Takes a shared environment for each need; when one is in use, takes none —
|
|
38
|
+
* giving back what it took, and what this work held already — and says why.
|
|
39
|
+
*/
|
|
40
|
+
export async function claimEnvironments(where, profile, claim) {
|
|
41
|
+
const rootOf = placeOf(where);
|
|
42
|
+
const locks = await readAll(rootOf, envsOf(profile, claim.needs));
|
|
43
|
+
const taken = [];
|
|
44
|
+
const tookOver = [];
|
|
45
|
+
// Every lock this call counts as this work's, with its commit: given back unless all are taken.
|
|
46
|
+
const pushed = [];
|
|
47
|
+
const lockFor = (environment) => ({
|
|
48
|
+
kind: "lock",
|
|
49
|
+
method: 1,
|
|
50
|
+
id: claim.marker,
|
|
51
|
+
environment,
|
|
52
|
+
work: claim.work,
|
|
53
|
+
phase: claim.phase,
|
|
54
|
+
who: claim.runner.who,
|
|
55
|
+
session: claim.runner.session,
|
|
56
|
+
since: claim.runner.now,
|
|
57
|
+
cli: claim.runner.cli,
|
|
58
|
+
});
|
|
59
|
+
const take = async (env, replacing) => {
|
|
60
|
+
if (replacing)
|
|
61
|
+
await removeLock(rootOf(env), env, replacing.sha);
|
|
62
|
+
const sha = await pushLock(rootOf(env), lockFor(env));
|
|
63
|
+
if (!sha)
|
|
64
|
+
return false;
|
|
65
|
+
pushed.push({ env, sha });
|
|
66
|
+
taken.push(env);
|
|
67
|
+
return true;
|
|
68
|
+
};
|
|
69
|
+
const giveBack = async () => {
|
|
70
|
+
for (const p of pushed)
|
|
71
|
+
await removeLock(rootOf(p.env), p.env, p.sha);
|
|
72
|
+
};
|
|
73
|
+
const choices = [];
|
|
74
|
+
const chosen = (id) => choices.some((c) => c.env === id);
|
|
75
|
+
for (const need of claim.needs) {
|
|
76
|
+
const all = needed(profile, need);
|
|
77
|
+
if (all.length === 0)
|
|
78
|
+
continue; // no lock needed
|
|
79
|
+
const candidates = all.filter((c) => !chosen(c.id));
|
|
80
|
+
// Already this work's (a run resumed, perhaps in another session), then a free one, then a
|
|
81
|
+
// take-over.
|
|
82
|
+
const mine = candidates.find((c) => locks.get(c.id)?.lock.work === claim.work);
|
|
83
|
+
const free = candidates.find((c) => !locks.has(c.id));
|
|
84
|
+
const over = candidates.find((c) => locks.get(c.id)?.lock.id === claim.runner.takeOver);
|
|
85
|
+
if (mine) {
|
|
86
|
+
const held = locks.get(mine.id);
|
|
87
|
+
const same = held.lock.session === claim.runner.session && held.lock.id === claim.marker;
|
|
88
|
+
choices.push({ env: mine.id, held, how: same ? "keep" : "retake" });
|
|
89
|
+
}
|
|
90
|
+
else if (free)
|
|
91
|
+
choices.push({ env: free.id, how: "free" });
|
|
92
|
+
else if (over)
|
|
93
|
+
choices.push({ env: over.id, held: locks.get(over.id), how: "over" });
|
|
94
|
+
else {
|
|
95
|
+
// It waits holding nothing: what this work held already is given back (all or none).
|
|
96
|
+
for (const [env, held] of locks) {
|
|
97
|
+
if (held.lock.work === claim.work)
|
|
98
|
+
await removeLock(rootOf(env), env, held.sha);
|
|
99
|
+
}
|
|
100
|
+
const busy = candidates.map((c) => [c.id, locks.get(c.id)]).find(([, h]) => h);
|
|
101
|
+
return {
|
|
102
|
+
waiting: busy
|
|
103
|
+
? inUse(busy[0], busy[1])
|
|
104
|
+
: `every shared e2e environment for ${need} is taken by this run's other needs`,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
for (const c of choices) {
|
|
110
|
+
if (c.how === "keep" && c.held) {
|
|
111
|
+
pushed.push({ env: c.env, sha: c.held.sha });
|
|
112
|
+
taken.push(c.env);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (await take(c.env, c.how === "free" ? undefined : c.held)) {
|
|
116
|
+
if (c.how === "over" && c.held) {
|
|
117
|
+
const l = c.held.lock;
|
|
118
|
+
tookOver.push(`${c.env} from ${l.work} (${l.who}, session ${l.session})`);
|
|
119
|
+
}
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
// Someone pushed first, just now.
|
|
123
|
+
await giveBack();
|
|
124
|
+
const now = await readAll(rootOf, [c.env]);
|
|
125
|
+
const held = now.get(c.env);
|
|
126
|
+
return {
|
|
127
|
+
waiting: held
|
|
128
|
+
? inUse(c.env, held)
|
|
129
|
+
: `the shared e2e environment ${c.env} was taken just now; run again`,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
// All or none: a failure halfway gives back what this call took.
|
|
135
|
+
await giveBack().catch(() => { });
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
return { taken, tookOver };
|
|
139
|
+
}
|
|
140
|
+
/** Releases this work's locks on the environments. */
|
|
141
|
+
export async function releaseEnvironments(where, environments, work) {
|
|
142
|
+
if (environments.length === 0)
|
|
143
|
+
return;
|
|
144
|
+
const rootOf = placeOf(where);
|
|
145
|
+
const locks = await readAll(rootOf, environments);
|
|
146
|
+
for (const env of environments) {
|
|
147
|
+
const held = locks.get(env);
|
|
148
|
+
if (held && held.lock.work === work)
|
|
149
|
+
await removeLock(rootOf(env), env, held.sha);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** Why the needs cannot be met now, without taking anything; undefined when they can. */
|
|
153
|
+
export async function environmentsInUse(where, profile, needs, work) {
|
|
154
|
+
let locks;
|
|
155
|
+
try {
|
|
156
|
+
locks = await readAll(placeOf(where), envsOf(profile, needs));
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
return undefined; // origin unreachable: the run itself will find out
|
|
160
|
+
}
|
|
161
|
+
for (const need of needs) {
|
|
162
|
+
const candidates = candidatesOf(profile, need) ?? [];
|
|
163
|
+
if (candidates.length === 0)
|
|
164
|
+
continue;
|
|
165
|
+
const open = candidates.some((c) => {
|
|
166
|
+
const held = locks.get(c.id);
|
|
167
|
+
return !held || held.lock.work === work;
|
|
168
|
+
});
|
|
169
|
+
if (!open) {
|
|
170
|
+
const [env, held] = candidates.map((c) => [c.id, locks.get(c.id)])[0];
|
|
171
|
+
return inUse(env, held);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|