@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,397 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { checkAdrs } from "../adr.js";
|
|
5
|
+
import { behaviorDirs, loadCatalog, renderIndex } from "../catalog.js";
|
|
6
|
+
import { checkCatalog, checkFixCatalog, checkFixMarkers, checkIndexes, checkOrphans, checkPlaceholders, checkStrayCatalog, } from "../check.js";
|
|
7
|
+
import { loadConfig, resolveComponents } from "../config.js";
|
|
8
|
+
import { checkProfile, E2E_PROFILE } from "../e2e.js";
|
|
9
|
+
import { checkEpic, parseEpic } from "../epic.js";
|
|
10
|
+
import { git, originDefaultBranch } from "../git.js";
|
|
11
|
+
import { repoRoot } from "../repo.js";
|
|
12
|
+
import { checkRunbook } from "../runbook.js";
|
|
13
|
+
import { writeInside } from "../safe-write.js";
|
|
14
|
+
import { buildTrace, findAllMarkers, readReports, } from "../trace.js";
|
|
15
|
+
import { fsTree, gitTree } from "../tree.js";
|
|
16
|
+
import { findSpecDocument } from "../work.js";
|
|
17
|
+
import { checkoutDirs, identifyRepo, workspace } from "../workspace.js";
|
|
18
|
+
/**
|
|
19
|
+
* `cupola trace [<area>] [--index]`: the map requirement → tests → latest
|
|
20
|
+
* result, unverified entries, and orphaned tags. `--index`
|
|
21
|
+
* regenerates each behavior directory's README.md.
|
|
22
|
+
*/
|
|
23
|
+
export async function trace(io, area, opts) {
|
|
24
|
+
const root = await repoRoot(io.cwd);
|
|
25
|
+
const config = await loadConfig(root);
|
|
26
|
+
const repo = await repoHere(io, root, config);
|
|
27
|
+
const areas = await loadCatalog(root, config, repo);
|
|
28
|
+
if (opts.index) {
|
|
29
|
+
const written = [];
|
|
30
|
+
for (const { dir } of behaviorDirs(config, repo)) {
|
|
31
|
+
const own = areas.filter((a) => a.dir === dir);
|
|
32
|
+
if (own.length === 0)
|
|
33
|
+
continue;
|
|
34
|
+
await writeInside(root, join(dir, "README.md"), renderIndex(own));
|
|
35
|
+
written.push(join(dir, "README.md"));
|
|
36
|
+
}
|
|
37
|
+
io.out(opts.json
|
|
38
|
+
? JSON.stringify({ written })
|
|
39
|
+
: written.length
|
|
40
|
+
? `wrote ${written.join(", ")}`
|
|
41
|
+
: "no areas yet");
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
const selected = area ? areas.filter((a) => a.area === area) : areas;
|
|
45
|
+
if (area && selected.length === 0)
|
|
46
|
+
throw new Error(`no area ${area} in the catalog`);
|
|
47
|
+
// In the hub, the other repositories are cloned inside it: their reports are theirs.
|
|
48
|
+
const ws = await workspace({ ...io, cwd: root }).catch(() => undefined);
|
|
49
|
+
const named = ws?.here.isHub === true ? await checkoutDirs(ws.hub.main) : new Map();
|
|
50
|
+
const clones = ws?.here.isHub === true
|
|
51
|
+
? ws.slugs
|
|
52
|
+
.filter((x) => x !== ws.hub.slug)
|
|
53
|
+
.map((x) => named.get(x.toLowerCase()) ?? x.split("/")[1] ?? x)
|
|
54
|
+
: [];
|
|
55
|
+
const reports = await readReports(root, config.verification.test_reports, [
|
|
56
|
+
...clones,
|
|
57
|
+
...clones.map((c) => `${c}.worktrees`),
|
|
58
|
+
]);
|
|
59
|
+
const t = buildTrace(selected, await findAllMarkers(root, await codeSources(root, config, ws), behaviorDirs(config, repo).map((d) => d.dir)), reports.results, reports.warnings, await epicExamples(root, config));
|
|
60
|
+
if (area)
|
|
61
|
+
t.orphans = [];
|
|
62
|
+
if (opts.json) {
|
|
63
|
+
io.out(JSON.stringify(t));
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
const count = (s) => t.entries.filter((e) => e.status === s).length;
|
|
67
|
+
io.out(`${t.entries.length} entries: ${count("passing")} passing, ${count("failing")} failing, ` +
|
|
68
|
+
`${count("no-result")} tested without a result, ${count("unverified")} unverified, ` +
|
|
69
|
+
`${count("manual") + count("untestable")} manual or untestable`);
|
|
70
|
+
for (const e of t.entries.filter((e) => e.status === "failing" || e.status === "unverified")) {
|
|
71
|
+
io.out(` ${e.status}: ${e.id} (${e.path})`);
|
|
72
|
+
}
|
|
73
|
+
for (const o of t.orphans)
|
|
74
|
+
io.out(` orphaned: @req ${o.ref} at ${o.file}:${o.line}`);
|
|
75
|
+
for (const w of t.warnings)
|
|
76
|
+
io.out(` warning: ${w}`);
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
79
|
+
/** `cupola check`: self-checks a change before it is opened. */
|
|
80
|
+
export async function check(io, opts) {
|
|
81
|
+
const root = await repoRoot(io.cwd);
|
|
82
|
+
const config = await loadConfig(root);
|
|
83
|
+
const repo = await repoHere(io, root, config);
|
|
84
|
+
const areas = await loadCatalog(root, config, repo);
|
|
85
|
+
const ws = config.documents === "hub"
|
|
86
|
+
? await workspace({ ...io, cwd: root }).catch(() => undefined)
|
|
87
|
+
: undefined;
|
|
88
|
+
const t = buildTrace(areas, await findAllMarkers(root, config.documents === "hub" ? await codeSources(root, config, ws) : [], behaviorDirs(config, repo).map((d) => d.dir)), []);
|
|
89
|
+
const problems = [
|
|
90
|
+
...checkCatalog(areas, config, repo),
|
|
91
|
+
...checkOrphans(t.orphans),
|
|
92
|
+
...(await checkIndexes(root, areas, config, repo)),
|
|
93
|
+
...(await checkStrayCatalog(root, config, repo)),
|
|
94
|
+
...(await checkPlaceholders(root, areas)),
|
|
95
|
+
...(await checkWork(root, config)),
|
|
96
|
+
...(await checkEpics(root, config)),
|
|
97
|
+
...(await checkRunbooks(root, config)),
|
|
98
|
+
...(await checkEpicRefs(root, config, t.epicRefs)),
|
|
99
|
+
...(await checkAdrs(root, config.paths.adr)),
|
|
100
|
+
...(await checkE2eProfile(root, config)),
|
|
101
|
+
];
|
|
102
|
+
if (opts.json)
|
|
103
|
+
io.out(JSON.stringify({ ok: problems.length === 0, problems }));
|
|
104
|
+
else if (problems.length === 0)
|
|
105
|
+
io.out("No problems found.");
|
|
106
|
+
else
|
|
107
|
+
for (const p of problems)
|
|
108
|
+
io.out(`${p.file}${p.line ? `:${p.line}` : ""}: ${p.message}`);
|
|
109
|
+
return problems.length === 0 ? 0 : 1;
|
|
110
|
+
}
|
|
111
|
+
/** The examples each requirement of every epic rests on (its `examples` field). */
|
|
112
|
+
async function epicExamples(root, config) {
|
|
113
|
+
const epics = config.paths.epics;
|
|
114
|
+
const out = new Map();
|
|
115
|
+
// Outside the hub, the hub's agreed epics.
|
|
116
|
+
const ws = await workspace({ out: () => { }, err: () => { }, cwd: root }).catch(() => undefined);
|
|
117
|
+
let files;
|
|
118
|
+
if (ws && !ws.here.isHub) {
|
|
119
|
+
const base = await originDefaultBranch(ws.hub.main);
|
|
120
|
+
const tree = gitTree(ws.hub.main, base ? `refs/remotes/origin/${base}` : "HEAD");
|
|
121
|
+
files = (await tree.list(epics)).map((f) => ({
|
|
122
|
+
name: f,
|
|
123
|
+
read: () => tree.read(`${epics}/${f}`),
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
const dir = join(root, epics);
|
|
128
|
+
if (!existsSync(dir))
|
|
129
|
+
return out;
|
|
130
|
+
files = (await readdir(dir)).map((f) => ({
|
|
131
|
+
name: f,
|
|
132
|
+
read: () => readFile(join(dir, f), "utf8"),
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
for (const f of files.filter((x) => x.name.endsWith(".md"))) {
|
|
136
|
+
const byId = new Map();
|
|
137
|
+
for (const e of parseEpic((await f.read()) ?? "").entries) {
|
|
138
|
+
const examples = e.fields.find((x) => x.key === "examples")?.value ?? "";
|
|
139
|
+
byId.set(e.id, examples.split(/[,\s]+/).filter((x) => /^X\d+$/.test(x)));
|
|
140
|
+
}
|
|
141
|
+
out.set(f.name.slice(0, -3), byId);
|
|
142
|
+
}
|
|
143
|
+
return out;
|
|
144
|
+
}
|
|
145
|
+
/** The e2e profile, when the repository has one. */
|
|
146
|
+
async function checkE2eProfile(root, config) {
|
|
147
|
+
const path = join(root, E2E_PROFILE);
|
|
148
|
+
if (!existsSync(path))
|
|
149
|
+
return [];
|
|
150
|
+
const all = resolveComponents(config);
|
|
151
|
+
const here = await repoHere({ out: () => { }, err: () => { }, cwd: root }, root, config);
|
|
152
|
+
const own = all.filter((c) => c.repo.toLowerCase() === here.toLowerCase());
|
|
153
|
+
return checkProfile(await readFile(path, "utf8"), all.map((c) => c.name), own.map((c) => c.name));
|
|
154
|
+
}
|
|
155
|
+
/** Every epic in the hub. */
|
|
156
|
+
async function checkEpics(root, config) {
|
|
157
|
+
const { epics } = config.paths;
|
|
158
|
+
const dir = join(root, epics);
|
|
159
|
+
// Outside the hub, the epics are copies of the hub's agreed ones: checked in the hub.
|
|
160
|
+
if (!existsSync(dir) || (await hubEpics(root, config)))
|
|
161
|
+
return [];
|
|
162
|
+
const components = resolveComponents(config).map((c) => c.name);
|
|
163
|
+
const books = join(root, config.paths.runbooks);
|
|
164
|
+
const runbooks = new Set(existsSync(books)
|
|
165
|
+
? (await readdir(books)).filter((f) => f.endsWith(".md")).map((f) => f.slice(0, -3))
|
|
166
|
+
: []);
|
|
167
|
+
const problems = [];
|
|
168
|
+
for (const f of (await readdir(dir)).filter((f) => f.endsWith(".md")).sort()) {
|
|
169
|
+
problems.push(...checkEpic(`${epics}/${f}`, await readFile(join(dir, f), "utf8"), components, runbooks));
|
|
170
|
+
}
|
|
171
|
+
return problems;
|
|
172
|
+
}
|
|
173
|
+
/** Every runbook in the hub. */
|
|
174
|
+
async function checkRunbooks(root, config) {
|
|
175
|
+
const { runbooks } = config.paths;
|
|
176
|
+
const dir = join(root, runbooks);
|
|
177
|
+
if (!existsSync(dir) || (await hubEpics(root, config)))
|
|
178
|
+
return [];
|
|
179
|
+
const components = resolveComponents(config).map((c) => c.name);
|
|
180
|
+
const problems = [];
|
|
181
|
+
for (const f of (await readdir(dir)).filter((f) => f.endsWith(".md")).sort()) {
|
|
182
|
+
problems.push(...checkRunbook(`${runbooks}/${f}`, await readFile(join(dir, f), "utf8"), components));
|
|
183
|
+
}
|
|
184
|
+
return problems;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Tests tagged with an epic's IDs (`@req <epic>#X1`, `#E2`, `#C1.g1`) before
|
|
188
|
+
* catalog entries exist: the epic and the entry must exist. A
|
|
189
|
+
* repository without the epics directory is not the hub, and cannot tell.
|
|
190
|
+
*/
|
|
191
|
+
async function checkEpicRefs(root, config, refs) {
|
|
192
|
+
if (refs.length === 0)
|
|
193
|
+
return [];
|
|
194
|
+
const hub = await hubEpics(root, config);
|
|
195
|
+
if (hub)
|
|
196
|
+
return checkRefsIn(refs, config, hub);
|
|
197
|
+
const dir = join(root, config.paths.epics);
|
|
198
|
+
if (!existsSync(dir))
|
|
199
|
+
return [];
|
|
200
|
+
return checkRefsIn(refs, config, async (epicId) => {
|
|
201
|
+
const path = join(dir, `${epicId}.md`);
|
|
202
|
+
return existsSync(path) ? readFile(path, "utf8") : undefined;
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Outside the hub, the epics are the hub's agreed ones: its origin's
|
|
207
|
+
* default branch, read through its checkout. Undefined in the hub itself.
|
|
208
|
+
*/
|
|
209
|
+
async function hubEpics(root, config) {
|
|
210
|
+
const ws = await workspace({ out: () => { }, err: () => { }, cwd: root }).catch(() => undefined);
|
|
211
|
+
if (!ws || ws.here.isHub)
|
|
212
|
+
return undefined;
|
|
213
|
+
const base = await originDefaultBranch(ws.hub.main);
|
|
214
|
+
const tree = gitTree(ws.hub.main, base ? `refs/remotes/origin/${base}` : "HEAD");
|
|
215
|
+
return (epic) => tree.read(`${config.paths.epics}/${epic}.md`);
|
|
216
|
+
}
|
|
217
|
+
async function checkRefsIn(refs, config, read) {
|
|
218
|
+
const epics = new Map();
|
|
219
|
+
const problems = [];
|
|
220
|
+
for (const m of refs) {
|
|
221
|
+
const [epicId = "", id = ""] = m.ref.split("#");
|
|
222
|
+
if (!epics.has(epicId)) {
|
|
223
|
+
const text = await read(epicId);
|
|
224
|
+
if (text === undefined)
|
|
225
|
+
epics.set(epicId, undefined);
|
|
226
|
+
else {
|
|
227
|
+
const ids = new Set();
|
|
228
|
+
for (const e of parseEpic(text).entries) {
|
|
229
|
+
ids.add(e.id);
|
|
230
|
+
if (e.type === "C")
|
|
231
|
+
for (const f of e.fields)
|
|
232
|
+
ids.add(`${e.id}.${f.key}`);
|
|
233
|
+
}
|
|
234
|
+
epics.set(epicId, ids);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const ids = epics.get(epicId);
|
|
238
|
+
if (!ids)
|
|
239
|
+
problems.push({
|
|
240
|
+
file: m.file,
|
|
241
|
+
line: m.line,
|
|
242
|
+
message: `@req ${m.ref}: no ${config.paths.epics}/${epicId}.md`,
|
|
243
|
+
});
|
|
244
|
+
else if (!ids.has(id)) {
|
|
245
|
+
problems.push({
|
|
246
|
+
file: m.file,
|
|
247
|
+
line: m.line,
|
|
248
|
+
message: `@req ${m.ref}: epic ${epicId} has no ${id}`,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return problems;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Checks of the work item this checkout is on, by its branch `cupola/<id>`.
|
|
256
|
+
* Only a fix has its own: it is measured against where it left origin's
|
|
257
|
+
* default branch.
|
|
258
|
+
*/
|
|
259
|
+
async function checkWork(root, config) {
|
|
260
|
+
let branch;
|
|
261
|
+
try {
|
|
262
|
+
branch = await git(root, ["symbolic-ref", "--quiet", "--short", "HEAD"]);
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
return [];
|
|
266
|
+
}
|
|
267
|
+
const id = /^cupola\/(.+)$/.exec(branch)?.[1];
|
|
268
|
+
if (!id)
|
|
269
|
+
return [];
|
|
270
|
+
let doc;
|
|
271
|
+
try {
|
|
272
|
+
doc = await findSpecDocument(fsTree(root), config, id);
|
|
273
|
+
}
|
|
274
|
+
catch {
|
|
275
|
+
return [];
|
|
276
|
+
}
|
|
277
|
+
if (doc.kind !== "fix")
|
|
278
|
+
return [];
|
|
279
|
+
const own = resolveComponents(config).find((c) => c.name === doc.component)?.code?.dir;
|
|
280
|
+
const target = await originDefaultBranch(root);
|
|
281
|
+
if (!target) {
|
|
282
|
+
return [
|
|
283
|
+
{ file: ".", message: "cannot tell origin's default branch, so the fix cannot be checked" },
|
|
284
|
+
];
|
|
285
|
+
}
|
|
286
|
+
// Measured from where the fix left origin's default branch as it is now, so that main
|
|
287
|
+
// merged into the fix's branch is not taken for the fix's own change.
|
|
288
|
+
let base;
|
|
289
|
+
try {
|
|
290
|
+
await git(root, ["fetch", "--quiet", "origin", target]);
|
|
291
|
+
base = await git(root, ["merge-base", "HEAD", "FETCH_HEAD"]);
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
return [
|
|
295
|
+
{ file: ".", message: `cannot fetch origin's ${target}, so the fix cannot be checked` },
|
|
296
|
+
];
|
|
297
|
+
}
|
|
298
|
+
const dirs = behaviorDirs(config, await repoHere({ out: () => { }, err: () => { }, cwd: root }, root, config)).map((d) => d.dir);
|
|
299
|
+
// Code mounted here is measured the same way, from where its branch left its own default branch.
|
|
300
|
+
const codeAt = async (dir) => {
|
|
301
|
+
const there = join(root, dir);
|
|
302
|
+
const own = await originDefaultBranch(there);
|
|
303
|
+
if (!own)
|
|
304
|
+
return undefined;
|
|
305
|
+
try {
|
|
306
|
+
await git(there, ["fetch", "--quiet", "origin", own]);
|
|
307
|
+
return await git(there, ["merge-base", "HEAD", "FETCH_HEAD"]);
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
// A repository whose base cannot be told is left out of both sides, and said so.
|
|
314
|
+
const measured = [];
|
|
315
|
+
const bases = new Map();
|
|
316
|
+
const unmeasured = [];
|
|
317
|
+
for (const dir of codeDirs(config)) {
|
|
318
|
+
if (!existsSync(join(root, dir, ".git")))
|
|
319
|
+
continue;
|
|
320
|
+
const at = await codeAt(dir);
|
|
321
|
+
if (at) {
|
|
322
|
+
measured.push(dir);
|
|
323
|
+
bases.set(dir, at);
|
|
324
|
+
}
|
|
325
|
+
else if (dir === own)
|
|
326
|
+
// Only the fix's own code matters: another repository that cannot be measured is left out.
|
|
327
|
+
unmeasured.push({
|
|
328
|
+
file: dir,
|
|
329
|
+
message: `cannot tell where ${dir} left its default branch, so the fix's code there cannot be checked`,
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
const before = await findAllMarkers(root, measured, dirs, {
|
|
333
|
+
hub: base,
|
|
334
|
+
codeAt: async (dir) => bases.get(dir),
|
|
335
|
+
});
|
|
336
|
+
const after = await findAllMarkers(root, measured, dirs);
|
|
337
|
+
const diff = await git(root, ["diff", "-U0", base, "--", ...dirs.map((d) => `${d}/*.md`)]);
|
|
338
|
+
return [...unmeasured, ...checkFixMarkers(before, after), ...checkFixCatalog(diff)];
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Where each code repository's markers are read: a work's own
|
|
342
|
+
* mounted checkout, on its work branch, as it is; otherwise — not mounted
|
|
343
|
+
* here, or a checkout on its default branch that is never pulled — the
|
|
344
|
+
* repository as merged, at its origin's default branch.
|
|
345
|
+
*/
|
|
346
|
+
async function codeSources(root, config, ws) {
|
|
347
|
+
const out = [];
|
|
348
|
+
const seen = new Set();
|
|
349
|
+
for (const c of resolveComponents(config)) {
|
|
350
|
+
if (!c.code || seen.has(c.code.dir))
|
|
351
|
+
continue;
|
|
352
|
+
seen.add(c.code.dir);
|
|
353
|
+
const { dir, repo } = c.code;
|
|
354
|
+
const mounted = join(root, dir);
|
|
355
|
+
let path = mounted;
|
|
356
|
+
if (existsSync(join(mounted, ".git"))) {
|
|
357
|
+
const branch = await git(mounted, ["symbolic-ref", "--quiet", "--short", "HEAD"]).catch(() => "");
|
|
358
|
+
const own = await originDefaultBranch(mounted);
|
|
359
|
+
if (branch !== "" && branch !== own) {
|
|
360
|
+
out.push({ dir, path: mounted });
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
try {
|
|
366
|
+
path = ws ? ws.checkout(repo).main : mounted;
|
|
367
|
+
}
|
|
368
|
+
catch {
|
|
369
|
+
continue; // not cloned: nothing to read
|
|
370
|
+
}
|
|
371
|
+
if (!existsSync(join(path, ".git")))
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
const own = await originDefaultBranch(path);
|
|
375
|
+
if (!own) {
|
|
376
|
+
out.push({ dir, path });
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
await git(path, [
|
|
380
|
+
"fetch",
|
|
381
|
+
"--quiet",
|
|
382
|
+
"origin",
|
|
383
|
+
`+refs/heads/${own}:refs/remotes/origin/${own}`,
|
|
384
|
+
]).catch(() => "");
|
|
385
|
+
out.push({ dir, path, ref: `refs/remotes/origin/${own}` });
|
|
386
|
+
}
|
|
387
|
+
return out;
|
|
388
|
+
}
|
|
389
|
+
/** Where code in repositories of its own is mounted in the hub's tree. */
|
|
390
|
+
function codeDirs(config) {
|
|
391
|
+
return [...new Set(resolveComponents(config).flatMap((c) => (c.code ? [c.code.dir] : [])))];
|
|
392
|
+
}
|
|
393
|
+
/** The repository this checkout is, for its catalog; the hub when it cannot tell. */
|
|
394
|
+
async function repoHere(io, root, config) {
|
|
395
|
+
const ws = await workspace({ ...io, cwd: root }).catch(() => undefined);
|
|
396
|
+
return ws?.here.slug ?? (await identifyRepo(root, config)) ?? config.hub;
|
|
397
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { forgetRemotes } from "../git.js";
|
|
2
|
+
import { MissingWorkError } from "../work.js";
|
|
3
|
+
import { workspace } from "../workspace.js";
|
|
4
|
+
import { caseStatus, describe, workStatus } from "./status.js";
|
|
5
|
+
/** The gates of an epic's lifecycle a session waits on: the human merges, then the work goes on. */
|
|
6
|
+
const WAITED = ["G1", "G3", "G4"];
|
|
7
|
+
const INTERVAL_S = 60;
|
|
8
|
+
/** Readings that fail in a row before the wait gives up: the code host is gone, not slow. */
|
|
9
|
+
const FAILURES = 10;
|
|
10
|
+
function seconds(value, flag) {
|
|
11
|
+
if (value === undefined)
|
|
12
|
+
return undefined;
|
|
13
|
+
const n = Number(value);
|
|
14
|
+
if (!Number.isFinite(n) || n <= 0)
|
|
15
|
+
throw new Error(`${flag} takes seconds, not ${value}`);
|
|
16
|
+
return n;
|
|
17
|
+
}
|
|
18
|
+
/** What a change of status is: where it stands and what comes next. */
|
|
19
|
+
function key(r) {
|
|
20
|
+
return JSON.stringify([r.status, r.gate, r.next.kind, r.next.command ?? null]);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* `cupola wait <work> [--timeout <s>] [--interval <s>]`: waits, without a
|
|
24
|
+
* model running, until a work item at a gate of its epic's lifecycle (G1, G3,
|
|
25
|
+
* G4) moves on — the human merged, sent it back, or closed it — and prints
|
|
26
|
+
* its new status. A work at no such gate is not waited on: running,
|
|
27
|
+
* done, at G2 (answered in the session), or a case (G5 is the case's own,
|
|
28
|
+
* judged later).
|
|
29
|
+
*/
|
|
30
|
+
export async function wait(io, id, opts) {
|
|
31
|
+
const timeout = seconds(opts.timeout, "--timeout");
|
|
32
|
+
const interval = seconds(opts.interval, "--interval") ?? INTERVAL_S;
|
|
33
|
+
const ws = await workspace(io);
|
|
34
|
+
const root = ws.here.isHub ? ws.root : ws.hub.main;
|
|
35
|
+
const read = () => {
|
|
36
|
+
// Each reading asks origin again: what one command learned is not kept across readings.
|
|
37
|
+
forgetRemotes();
|
|
38
|
+
return workStatus(io, root, ws.config, id);
|
|
39
|
+
};
|
|
40
|
+
let first;
|
|
41
|
+
try {
|
|
42
|
+
first = await read();
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (!(error instanceof MissingWorkError))
|
|
46
|
+
throw error;
|
|
47
|
+
// A case, not a work item: its G5 is the case's own lifecycle, judged later.
|
|
48
|
+
const found = await caseStatus(io, root, ws.config, id);
|
|
49
|
+
if (!found)
|
|
50
|
+
throw error;
|
|
51
|
+
io.out(opts.json
|
|
52
|
+
? JSON.stringify({ waited: false, changed: false, next: found.next, status: found })
|
|
53
|
+
: `nothing to wait for; a case's G5 is judged later. ${describe(found)}`);
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
const out = (result, s, next = s.next) => io.out(opts.json
|
|
57
|
+
? JSON.stringify({ ...result, next, status: s })
|
|
58
|
+
: `${result.changed ? "moved on" : result.timedOut ? "timed out; still" : "nothing to wait for;"} ${describe(s)}${next !== s.next ? `\nnext: ${next.command}` : ""}`);
|
|
59
|
+
if (first.turn !== "human" || !WAITED.includes(first.gate ?? "")) {
|
|
60
|
+
out({ waited: false, changed: false }, first);
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
const now = () => (io.now?.() ?? new Date()).getTime();
|
|
64
|
+
const sleep = io.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
65
|
+
const deadline = timeout === undefined ? Number.POSITIVE_INFINITY : now() + timeout * 1000;
|
|
66
|
+
let failures = 0;
|
|
67
|
+
for (;;) {
|
|
68
|
+
if (now() >= deadline) {
|
|
69
|
+
out({ waited: true, changed: false, timedOut: true }, first);
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
await sleep(Math.min(interval * 1000, Math.max(0, deadline - now())));
|
|
73
|
+
let current;
|
|
74
|
+
try {
|
|
75
|
+
current = await read();
|
|
76
|
+
failures = 0;
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
// A work that is gone is not coming back: only a reading that could not be made is retried.
|
|
80
|
+
if (error instanceof MissingWorkError)
|
|
81
|
+
throw error;
|
|
82
|
+
failures += 1;
|
|
83
|
+
if (failures >= FAILURES)
|
|
84
|
+
throw error;
|
|
85
|
+
io.err(`warning: could not read the status (${error.message}); trying again`);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (key(current) !== key(first)) {
|
|
89
|
+
// Merged at G3 or G4: what follows is the gate's own command (merge it, then run it),
|
|
90
|
+
// though the status, derived from the merge alone, says there is nothing left.
|
|
91
|
+
const merged = first.gate !== "G1" && current.change?.state === "merged" && first.next.command
|
|
92
|
+
? {
|
|
93
|
+
kind: "run",
|
|
94
|
+
text: `Merged: ${first.next.command}.`,
|
|
95
|
+
command: first.next.command,
|
|
96
|
+
}
|
|
97
|
+
: current.next;
|
|
98
|
+
out({ waited: true, changed: true }, current, merged);
|
|
99
|
+
return 0;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { loadConfig } from "../config.js";
|
|
4
|
+
import { today } from "../day.js";
|
|
5
|
+
import { parseFrontMatter } from "../frontmatter.js";
|
|
6
|
+
import { repoRoot } from "../repo.js";
|
|
7
|
+
import { trackerFor } from "../tracker/index.js";
|
|
8
|
+
import { fromDraft } from "../tracker/tracker.js";
|
|
9
|
+
import { workspace } from "../workspace.js";
|
|
10
|
+
import { caseStatus, notSolvedMarker } from "./status.js";
|
|
11
|
+
export async function trackerOf(io) {
|
|
12
|
+
// Cases live in the hub: from another repository, through the hub's checkout.
|
|
13
|
+
const ws = await workspace(io);
|
|
14
|
+
const root = ws.here.isHub ? ws.root : ws.hub.main;
|
|
15
|
+
return io.tracker ? io.tracker(root) : await trackerFor(root, ws.config);
|
|
16
|
+
}
|
|
17
|
+
/** `cupola work list`: every case, open and closed, so that duplicates can be looked for. */
|
|
18
|
+
export async function workList(io, opts) {
|
|
19
|
+
const cases = await (await trackerOf(io)).list();
|
|
20
|
+
if (opts.json)
|
|
21
|
+
io.out(JSON.stringify({ cases }));
|
|
22
|
+
else
|
|
23
|
+
for (const c of cases)
|
|
24
|
+
io.out(`${c.open ? "open " : "closed"} ${c.id} — ${c.title}`);
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
/** `cupola work show <case>`: the case document, as Markdown with front matter. */
|
|
28
|
+
export async function workShow(io, id, opts) {
|
|
29
|
+
const found = await (await trackerOf(io)).show(id);
|
|
30
|
+
if (!found)
|
|
31
|
+
throw new Error(`no case ${id}`);
|
|
32
|
+
io.out(opts.json ? JSON.stringify(found) : found.text.trimEnd());
|
|
33
|
+
return 0;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* `cupola work file --body-file <case.md>`: files a case. A case draft from a
|
|
37
|
+
* run is accepted as it is: its draft keys are dropped and `filed` is set.
|
|
38
|
+
*/
|
|
39
|
+
export async function workFile(io, opts) {
|
|
40
|
+
if (!opts.bodyFile)
|
|
41
|
+
throw new Error("give the case: --body-file <file>");
|
|
42
|
+
const text = await readFile(resolve(io.cwd, opts.bodyFile), "utf8");
|
|
43
|
+
const doc = parseFrontMatter(text).data.draft === true ? fromDraft(text, await today(io)) : text;
|
|
44
|
+
const filed = await (await trackerOf(io)).file(doc);
|
|
45
|
+
io.out(opts.json ? JSON.stringify(filed) : `filed ${filed.id}: ${filed.url}`);
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
async function oneLine(io, opts) {
|
|
49
|
+
if (!opts.bodyFile)
|
|
50
|
+
throw new Error("give the line: --body-file <file>");
|
|
51
|
+
const line = (await readFile(resolve(io.cwd, opts.bodyFile), "utf8")).trim();
|
|
52
|
+
if (line === "" || line.includes("\n"))
|
|
53
|
+
throw new Error("a Log entry is one line");
|
|
54
|
+
if (line.includes("{{"))
|
|
55
|
+
throw new Error("the Log entry still has template placeholders ({{…}})");
|
|
56
|
+
return line;
|
|
57
|
+
}
|
|
58
|
+
/** `cupola work log <case> --body-file <f>`: appends one dated line to the case's Log. */
|
|
59
|
+
export async function workLog(io, id, opts) {
|
|
60
|
+
const line = await oneLine(io, opts);
|
|
61
|
+
await (await trackerOf(io)).log(id, `${await today(io)} ${line}`);
|
|
62
|
+
io.out(opts.json ? JSON.stringify({ logged: id }) : `logged on ${id}`);
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* `cupola work close <case> --body-file <f>`: G5 found the problem gone. The
|
|
67
|
+
* human's words go to the Log, and the case is closed.
|
|
68
|
+
*/
|
|
69
|
+
export async function workClose(io, id, opts) {
|
|
70
|
+
const line = await oneLine(io, opts);
|
|
71
|
+
const root = await repoRoot(io.cwd);
|
|
72
|
+
const found = await caseStatus(io, root, await loadConfig(root), id);
|
|
73
|
+
if (!found)
|
|
74
|
+
throw new Error(`no case ${id}`);
|
|
75
|
+
const caseId = found.work.id;
|
|
76
|
+
const ready = opts.withoutEpic ? "open" : "awaiting-resolution";
|
|
77
|
+
if (found.status !== "resolved" && found.status !== ready) {
|
|
78
|
+
throw new Error(opts.withoutEpic
|
|
79
|
+
? `${caseId} is ${found.status}: only a case no agreed epic works on is closed without one`
|
|
80
|
+
: `${caseId} is ${found.status}: a case is closed at G5, once its epics are done (one that needs no work: --without-epic)`);
|
|
81
|
+
}
|
|
82
|
+
const day = await today(io);
|
|
83
|
+
const closed = found.status === "resolved"
|
|
84
|
+
? false
|
|
85
|
+
: await (await trackerOf(io)).close(caseId, `${day} resolved${opts.withoutEpic ? " without an epic" : ""}: ${line}`, day);
|
|
86
|
+
io.out(opts.json
|
|
87
|
+
? JSON.stringify({ closed: caseId, already: !closed })
|
|
88
|
+
: closed
|
|
89
|
+
? `closed ${caseId}`
|
|
90
|
+
: `${caseId} was closed already`);
|
|
91
|
+
return 0;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* `cupola work reopen <case> --body-file <f>`: G5 found the problem not gone.
|
|
95
|
+
* The reason goes to the Log with the epics judged, which then no longer
|
|
96
|
+
* count, so the case is open again.
|
|
97
|
+
*/
|
|
98
|
+
export async function workReopen(io, id, opts) {
|
|
99
|
+
const line = await oneLine(io, opts);
|
|
100
|
+
const root = await repoRoot(io.cwd);
|
|
101
|
+
const found = await caseStatus(io, root, await loadConfig(root), id);
|
|
102
|
+
if (!found)
|
|
103
|
+
throw new Error(`no case ${id}`);
|
|
104
|
+
const caseId = found.work.id;
|
|
105
|
+
const epics = found.epics ?? [];
|
|
106
|
+
// Run again after a lost answer: every done epic is judged, and the case is open.
|
|
107
|
+
if (found.status === "open" && epics.some((e) => e.status === "done" && e.judged)) {
|
|
108
|
+
const left = epics.filter((e) => e.status === "done" && !e.judged);
|
|
109
|
+
if (left.length === 0) {
|
|
110
|
+
io.out(opts.json ? JSON.stringify({ reopened: caseId, epics: [] }) : `${caseId} is open`);
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (found.status !== "awaiting-resolution") {
|
|
115
|
+
throw new Error(`${caseId} is ${found.status}: a case is judged at G5, once all its epics are done`);
|
|
116
|
+
}
|
|
117
|
+
const judged = epics.filter((e) => e.status === "done" && !e.judged).map((e) => e.id);
|
|
118
|
+
await (await trackerOf(io)).log(caseId, `${await today(io)} not solved yet after ${judged.join(", ")}: ${line} ${notSolvedMarker(judged)}`);
|
|
119
|
+
io.out(opts.json ? JSON.stringify({ reopened: caseId, epics: judged }) : `reopened ${caseId}`);
|
|
120
|
+
return 0;
|
|
121
|
+
}
|