@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
package/dist/config.js
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join, posix } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { ASSOCIATIONS, DEFAULT_ASSOCIATIONS } from "./trust.js";
|
|
5
|
+
/** Path of the configuration file, relative to a repository root. */
|
|
6
|
+
export const CONFIG_PATH = join(".cupola", "config.json");
|
|
7
|
+
const kebab = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
8
|
+
const repoSlugPattern = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
|
|
9
|
+
/** `owner/name`, where neither part could name a directory other than a clone's own. */
|
|
10
|
+
const repoSlug = z
|
|
11
|
+
.string()
|
|
12
|
+
.regex(repoSlugPattern, "must be owner/name")
|
|
13
|
+
.refine((s) => s.split("/").every((p) => p !== "." && p !== ".." && p !== ".git" && !p.startsWith("-")), "must be owner/name, neither part being ., .., .git, or starting with -");
|
|
14
|
+
const component = z.strictObject({
|
|
15
|
+
name: z
|
|
16
|
+
.string()
|
|
17
|
+
.regex(kebab, "must be kebab-case")
|
|
18
|
+
.refine((n) => n !== "system", "system is the catalog's level of the whole, not a component"),
|
|
19
|
+
repo: repoSlug.optional(),
|
|
20
|
+
path: z
|
|
21
|
+
.string()
|
|
22
|
+
.min(1)
|
|
23
|
+
.refine((p) => !escapesRepository(p), "must be a relative path inside the repository")
|
|
24
|
+
.optional(),
|
|
25
|
+
role: z.string().min(1),
|
|
26
|
+
});
|
|
27
|
+
const board = z.strictObject({
|
|
28
|
+
kind: z.literal("github-projects"),
|
|
29
|
+
owner: z.string().min(1),
|
|
30
|
+
project_number: z.int().positive(),
|
|
31
|
+
field: z.string().min(1),
|
|
32
|
+
});
|
|
33
|
+
/**
|
|
34
|
+
* Where Cupola keeps its documents, relative to the repository root; each
|
|
35
|
+
* repository has one `.cupola/`, and the catalog holds a directory per
|
|
36
|
+
* component there.
|
|
37
|
+
*/
|
|
38
|
+
export const DEFAULT_PATHS = {
|
|
39
|
+
epics: ".cupola/epics",
|
|
40
|
+
runbooks: ".cupola/runbooks",
|
|
41
|
+
adr: ".cupola/adr",
|
|
42
|
+
acceptance: ".cupola/acceptance",
|
|
43
|
+
behavior: ".cupola/behavior",
|
|
44
|
+
};
|
|
45
|
+
const insidePath = z
|
|
46
|
+
.string()
|
|
47
|
+
.min(1)
|
|
48
|
+
.refine((p) => !escapesRepository(p) && normalizePath(p) !== ".", "must be a relative path inside the repository")
|
|
49
|
+
.transform((p) => normalizePath(p));
|
|
50
|
+
/** A glob of test reports, relative to the repository root and never above it. */
|
|
51
|
+
const reportGlob = z
|
|
52
|
+
.string()
|
|
53
|
+
.min(1)
|
|
54
|
+
.refine((g) => !g.includes("\\") &&
|
|
55
|
+
!posix.isAbsolute(g) &&
|
|
56
|
+
!/^[A-Za-z]:/.test(g) &&
|
|
57
|
+
!g.split("/").includes(".."), "must be a relative glob inside the repository");
|
|
58
|
+
const paths = z.strictObject({
|
|
59
|
+
epics: insidePath.default(DEFAULT_PATHS.epics),
|
|
60
|
+
runbooks: insidePath.default(DEFAULT_PATHS.runbooks),
|
|
61
|
+
adr: insidePath.default(DEFAULT_PATHS.adr),
|
|
62
|
+
acceptance: insidePath.default(DEFAULT_PATHS.acceptance),
|
|
63
|
+
behavior: insidePath.default(DEFAULT_PATHS.behavior),
|
|
64
|
+
});
|
|
65
|
+
/** Whether a time zone name is one this runtime knows (`Asia/Tokyo`, `UTC`). */
|
|
66
|
+
function knownTimeZone(zone) {
|
|
67
|
+
try {
|
|
68
|
+
new Intl.DateTimeFormat("en", { timeZone: zone });
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export const configSchema = z
|
|
76
|
+
.strictObject({
|
|
77
|
+
version: z.literal(1),
|
|
78
|
+
language: z.string().min(2),
|
|
79
|
+
/** The zone records are dated in (`Asia/Tokyo`); this machine's without one. */
|
|
80
|
+
timezone: z
|
|
81
|
+
.string()
|
|
82
|
+
.refine((zone) => knownTimeZone(zone), {
|
|
83
|
+
message: "is not a time zone name, such as Asia/Tokyo",
|
|
84
|
+
})
|
|
85
|
+
.optional(),
|
|
86
|
+
hub: repoSlug,
|
|
87
|
+
components: z.array(component).min(1),
|
|
88
|
+
host: z.strictObject({ kind: z.literal("github") }),
|
|
89
|
+
tracker: z.strictObject({ kind: z.enum(["github-issues", "files"]) }),
|
|
90
|
+
engine: z.strictObject({ kind: z.literal("cc-sdd") }),
|
|
91
|
+
board: board.optional(),
|
|
92
|
+
review: z.strictObject({
|
|
93
|
+
reviewer: z.string().min(1),
|
|
94
|
+
max_rounds: z.int().positive().default(5),
|
|
95
|
+
confirm_mode: z.boolean().default(false),
|
|
96
|
+
}),
|
|
97
|
+
run: z
|
|
98
|
+
.strictObject({ max_parallel: z.int().positive().default(5) })
|
|
99
|
+
.default({ max_parallel: 5 }),
|
|
100
|
+
verification: z
|
|
101
|
+
.strictObject({ test_reports: z.array(reportGlob).default([]) })
|
|
102
|
+
.default({ test_reports: [] }),
|
|
103
|
+
models: z.strictObject({ subagent: z.string().min(1).optional() }).default({}),
|
|
104
|
+
/** Whose words on the code host and tracker may steer the work (src/trust.ts). */
|
|
105
|
+
trust: z
|
|
106
|
+
.strictObject({
|
|
107
|
+
associations: z.array(z.enum(ASSOCIATIONS)).default([...DEFAULT_ASSOCIATIONS]),
|
|
108
|
+
logins: z.array(z.string().min(1)).default([]),
|
|
109
|
+
})
|
|
110
|
+
.default({ associations: [...DEFAULT_ASSOCIATIONS], logins: [] }),
|
|
111
|
+
paths: paths.default({ ...DEFAULT_PATHS }),
|
|
112
|
+
/**
|
|
113
|
+
* Where the documents of a component in another repository live:
|
|
114
|
+
* in that repository, or all in the hub, the other repositories holding only code.
|
|
115
|
+
*/
|
|
116
|
+
documents: z.enum(["repositories", "hub"]).default("repositories"),
|
|
117
|
+
})
|
|
118
|
+
.superRefine((config, ctx) => {
|
|
119
|
+
// Each place holds one kind of document, apart from the others and from Cupola's records.
|
|
120
|
+
const reserved = [".cupola/runs", ".cupola/work", ".cupola/specs", ".kiro", ".claude"];
|
|
121
|
+
const within = (a, b) => a === b || a.startsWith(`${b}/`);
|
|
122
|
+
const entries = Object.entries(config.paths);
|
|
123
|
+
entries.forEach(([key, p], i) => {
|
|
124
|
+
const clash = reserved.find((r) => within(p, r) || within(r, p)) ??
|
|
125
|
+
entries.slice(0, i).find(([, q]) => within(p, q) || within(q, p))?.[0];
|
|
126
|
+
if (clash)
|
|
127
|
+
ctx.addIssue({
|
|
128
|
+
code: "custom",
|
|
129
|
+
path: ["paths", key],
|
|
130
|
+
message: `overlaps ${reserved.includes(clash) ? clash : `paths.${clash}`}: each place holds its own documents`,
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
const names = new Set();
|
|
134
|
+
const places = new Set();
|
|
135
|
+
config.components.forEach((c, i) => {
|
|
136
|
+
if (names.has(c.name)) {
|
|
137
|
+
ctx.addIssue({
|
|
138
|
+
code: "custom",
|
|
139
|
+
path: ["components", i, "name"],
|
|
140
|
+
message: `component "${c.name}" is declared twice`,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
names.add(c.name);
|
|
144
|
+
const place = `${c.repo ?? config.hub}:${normalizePath(c.path)}`;
|
|
145
|
+
if (places.has(place)) {
|
|
146
|
+
ctx.addIssue({
|
|
147
|
+
code: "custom",
|
|
148
|
+
path: ["components", i, "path"],
|
|
149
|
+
message: `another component already lives at ${place}`,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
places.add(place);
|
|
153
|
+
});
|
|
154
|
+
if (config.documents !== "hub")
|
|
155
|
+
return;
|
|
156
|
+
// Code repositories are mounted in the hub's tree by their names: each name is its own place
|
|
157
|
+
// there.
|
|
158
|
+
const hub = config.hub.toLowerCase();
|
|
159
|
+
const mounted = new Map();
|
|
160
|
+
const taken = [
|
|
161
|
+
".cupola",
|
|
162
|
+
".kiro",
|
|
163
|
+
".claude",
|
|
164
|
+
...Object.values(config.paths).map((p) => p.split("/")[0] ?? p),
|
|
165
|
+
...config.components
|
|
166
|
+
.filter((c) => (c.repo ?? config.hub).toLowerCase() === hub)
|
|
167
|
+
.map((c) => normalizePath(c.path).split("/")[0] ?? "."),
|
|
168
|
+
];
|
|
169
|
+
config.components.forEach((c, i) => {
|
|
170
|
+
const repo = c.repo ?? config.hub;
|
|
171
|
+
if (repo.toLowerCase() === hub)
|
|
172
|
+
return;
|
|
173
|
+
const dir = cloneName(repo);
|
|
174
|
+
const other = mounted.get(dir);
|
|
175
|
+
if (other !== undefined && other.toLowerCase() !== repo.toLowerCase())
|
|
176
|
+
ctx.addIssue({
|
|
177
|
+
code: "custom",
|
|
178
|
+
path: ["components", i, "repo"],
|
|
179
|
+
message: `${repo} and ${other} would both be mounted at ${dir}/ in the hub`,
|
|
180
|
+
});
|
|
181
|
+
else if (taken.includes(dir))
|
|
182
|
+
ctx.addIssue({
|
|
183
|
+
code: "custom",
|
|
184
|
+
path: ["components", i, "repo"],
|
|
185
|
+
message: `${repo} would be mounted at ${dir}/ in the hub, which the hub uses already`,
|
|
186
|
+
});
|
|
187
|
+
mounted.set(dir, repo);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
export function resolveComponents(config) {
|
|
191
|
+
return config.components.map((c) => {
|
|
192
|
+
const repo = c.repo ?? config.hub;
|
|
193
|
+
const path = normalizePath(c.path);
|
|
194
|
+
if (config.documents !== "hub" || repo.toLowerCase() === config.hub.toLowerCase()) {
|
|
195
|
+
return { name: c.name, role: c.role, repo, path };
|
|
196
|
+
}
|
|
197
|
+
const dir = cloneName(repo);
|
|
198
|
+
return {
|
|
199
|
+
name: c.name,
|
|
200
|
+
role: c.role,
|
|
201
|
+
repo: config.hub,
|
|
202
|
+
path: path === "." ? dir : `${dir}/${path}`,
|
|
203
|
+
code: { repo, dir, path },
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
/** The directory a repository is cloned into under the hub: its last path segment. */
|
|
208
|
+
export function cloneName(slug) {
|
|
209
|
+
return slug.split("/")[1] ?? slug;
|
|
210
|
+
}
|
|
211
|
+
/** Normalizes a component path to a repository-relative POSIX path; the root is ".". */
|
|
212
|
+
function normalizePath(path) {
|
|
213
|
+
const normalized = posix.normalize(path ?? ".").replace(/\/+$/, "");
|
|
214
|
+
return normalized === "" ? "." : normalized;
|
|
215
|
+
}
|
|
216
|
+
function escapesRepository(path) {
|
|
217
|
+
if (path.includes("\\") || posix.isAbsolute(path) || /^[A-Za-z]:/.test(path))
|
|
218
|
+
return true;
|
|
219
|
+
const normalized = normalizePath(path);
|
|
220
|
+
return normalized === ".." || normalized.startsWith("../");
|
|
221
|
+
}
|
|
222
|
+
export class ConfigError extends Error {
|
|
223
|
+
problems;
|
|
224
|
+
constructor(message, problems = []) {
|
|
225
|
+
super(message);
|
|
226
|
+
this.problems = problems;
|
|
227
|
+
this.name = "ConfigError";
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
export function parseConfig(raw) {
|
|
231
|
+
let json;
|
|
232
|
+
try {
|
|
233
|
+
json = JSON.parse(raw);
|
|
234
|
+
}
|
|
235
|
+
catch (error) {
|
|
236
|
+
throw new ConfigError(`${CONFIG_PATH} is not valid JSON: ${error.message}`);
|
|
237
|
+
}
|
|
238
|
+
const result = configSchema.safeParse(json);
|
|
239
|
+
if (!result.success) {
|
|
240
|
+
const problems = result.error.issues.map((issue) => `${issue.path.join(".") || "(root)"}: ${issue.message}`);
|
|
241
|
+
throw new ConfigError(`${CONFIG_PATH} is invalid`, problems);
|
|
242
|
+
}
|
|
243
|
+
return result.data;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Loads the configuration from a repository root. A missing file is an error:
|
|
247
|
+
* Cupola never falls back to defaults, to avoid writing to the wrong tracker
|
|
248
|
+
* or repository.
|
|
249
|
+
*/
|
|
250
|
+
export async function loadConfig(root) {
|
|
251
|
+
let raw;
|
|
252
|
+
try {
|
|
253
|
+
raw = await readFile(join(root, CONFIG_PATH), "utf8");
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
if (error.code === "ENOENT") {
|
|
257
|
+
throw new ConfigError(`${CONFIG_PATH} not found in ${root}; run \`cupola init\` first`);
|
|
258
|
+
}
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
return parseConfig(raw);
|
|
262
|
+
}
|
|
263
|
+
const pathsCache = new Map();
|
|
264
|
+
/**
|
|
265
|
+
* Where Cupola keeps its documents in the repository at `root`: its
|
|
266
|
+
* configuration's `paths`, or the defaults when it has no configuration. An
|
|
267
|
+
* invalid one is an error, never read as the defaults.
|
|
268
|
+
*/
|
|
269
|
+
export async function pathsAt(root) {
|
|
270
|
+
const known = pathsCache.get(root);
|
|
271
|
+
if (known)
|
|
272
|
+
return known;
|
|
273
|
+
try {
|
|
274
|
+
const { paths } = await loadConfig(root);
|
|
275
|
+
pathsCache.set(root, paths);
|
|
276
|
+
return paths;
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
if (error instanceof ConfigError &&
|
|
280
|
+
error.problems.length === 0 &&
|
|
281
|
+
/not found/.test(error.message))
|
|
282
|
+
return { ...DEFAULT_PATHS };
|
|
283
|
+
throw error;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/** The path of an epic's document, relative to the hub's root. */
|
|
287
|
+
export function epicPath(paths, epic) {
|
|
288
|
+
return `${paths.epics}/${epic}.md`;
|
|
289
|
+
}
|
package/dist/day.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ConfigError, loadConfig } from "./config.js";
|
|
2
|
+
import { repoRoot } from "./repo.js";
|
|
3
|
+
/** The day of `date` as `YYYY-MM-DD` in `timeZone`, or in this machine's zone without one. */
|
|
4
|
+
export function dayIn(date, timeZone) {
|
|
5
|
+
return new Intl.DateTimeFormat("en-CA", {
|
|
6
|
+
timeZone,
|
|
7
|
+
year: "numeric",
|
|
8
|
+
month: "2-digit",
|
|
9
|
+
day: "2-digit",
|
|
10
|
+
}).format(date);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Today, in the configured `timezone` so that every machine dates a record the
|
|
14
|
+
* same; in this machine's zone when none is configured or no configuration is found.
|
|
15
|
+
*/
|
|
16
|
+
export async function today(io) {
|
|
17
|
+
let zone;
|
|
18
|
+
const root = await repoRoot(io.cwd).catch(() => undefined);
|
|
19
|
+
try {
|
|
20
|
+
if (root)
|
|
21
|
+
zone = (await loadConfig(root)).timezone;
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
// Only a missing configuration falls back; an invalid one is not read past.
|
|
25
|
+
if (!(error instanceof ConfigError && /not found/.test(error.message)))
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
return dayIn(io.now?.() ?? new Date(), zone);
|
|
29
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { E2E_PROFILE, parseProfile } from "./e2e.js";
|
|
4
|
+
import { git, originDefaultBranch } from "./git.js";
|
|
5
|
+
export async function projectProfile(ws, root, own) {
|
|
6
|
+
const mine = own === undefined ? undefined : parseProfile(own);
|
|
7
|
+
const environments = [...(mine?.environments ?? [])];
|
|
8
|
+
const others = [];
|
|
9
|
+
for (const slug of ws.slugs) {
|
|
10
|
+
if (slug === ws.here.slug)
|
|
11
|
+
continue;
|
|
12
|
+
let main;
|
|
13
|
+
try {
|
|
14
|
+
main = ws.checkout(slug).main;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const base = await originDefaultBranch(main);
|
|
20
|
+
let raw;
|
|
21
|
+
try {
|
|
22
|
+
raw = await git(main, ["show", `refs/remotes/origin/${base ?? "main"}:${E2E_PROFILE}`]);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const theirs = parseProfile(raw);
|
|
28
|
+
// Only the environments of components that live there: each repository speaks for its own.
|
|
29
|
+
const added = theirs.environments.filter((e) => !environments.some((x) => x.id === e.id) && safeRepo(ws, e.component) === slug);
|
|
30
|
+
if (added.length === 0)
|
|
31
|
+
continue;
|
|
32
|
+
environments.push(...added);
|
|
33
|
+
const copy = await git(root, [
|
|
34
|
+
"rev-parse",
|
|
35
|
+
"--path-format=absolute",
|
|
36
|
+
"--git-path",
|
|
37
|
+
`cupola/e2e/${slug.replace("/", "--")}.md`,
|
|
38
|
+
]);
|
|
39
|
+
await mkdir(dirname(copy), { recursive: true });
|
|
40
|
+
await writeFile(copy, raw);
|
|
41
|
+
others.push(copy);
|
|
42
|
+
}
|
|
43
|
+
const profile = mine || environments.length > 0
|
|
44
|
+
? { front: mine?.front, environments, strays: mine?.strays ?? [] }
|
|
45
|
+
: undefined;
|
|
46
|
+
const component = (env) => environments.find((e) => e.id === env)?.component ?? env.split(".")[0] ?? "";
|
|
47
|
+
const rootOf = (env) => {
|
|
48
|
+
const slug = safeRepo(ws, component(env));
|
|
49
|
+
if (!slug || slug === ws.here.slug)
|
|
50
|
+
return root;
|
|
51
|
+
return ws.checkout(slug).main;
|
|
52
|
+
};
|
|
53
|
+
return { profile, rootOf, others };
|
|
54
|
+
}
|
|
55
|
+
function safeRepo(ws, component) {
|
|
56
|
+
try {
|
|
57
|
+
return ws.repoOf(component);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
}
|
package/dist/e2e.js
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { PRECISE, stripComments } from "./epic.js";
|
|
3
|
+
import { parseFrontMatter } from "./frontmatter.js";
|
|
4
|
+
import { git, gitWith } from "./git.js";
|
|
5
|
+
/**
|
|
6
|
+
* The e2e profile: `.kiro/steering/e2e.md`. The CLI
|
|
7
|
+
* reads only its front matter and each environment's `kind` and `pool`, for the
|
|
8
|
+
* locks of shared environments; agents read the rest.
|
|
9
|
+
*/
|
|
10
|
+
export const E2E_PROFILE = ".kiro/steering/e2e.md";
|
|
11
|
+
const KINDS = ["emulator", "isolated", "shared"];
|
|
12
|
+
const Front = z.object({
|
|
13
|
+
kind: z.literal("e2e"),
|
|
14
|
+
method: z.number().int(),
|
|
15
|
+
components: z.array(z.string().min(1)),
|
|
16
|
+
});
|
|
17
|
+
const ENV_ID = /^([a-z0-9]+(?:-[a-z0-9]+)*)\.([a-z0-9]+(?:-[a-z0-9]+)*)$/;
|
|
18
|
+
const FIELD = /^-\s+([a-z][a-z0-9-]*):\s*(.*)$/;
|
|
19
|
+
export function parseProfile(raw) {
|
|
20
|
+
const text = raw.replace(/^/, "").replace(/\r\n?/g, "\n");
|
|
21
|
+
const { data, body } = parseFrontMatter(text);
|
|
22
|
+
const offset = text.slice(0, text.length - body.length).split("\n").length - 1;
|
|
23
|
+
const environments = [];
|
|
24
|
+
const strays = [];
|
|
25
|
+
let precise = false;
|
|
26
|
+
let comment = false;
|
|
27
|
+
let current;
|
|
28
|
+
body.split("\n").forEach((original, i) => {
|
|
29
|
+
if (!comment && original.trim() === PRECISE) {
|
|
30
|
+
precise = true;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
let line;
|
|
34
|
+
[line, comment] = stripComments(original, comment);
|
|
35
|
+
if (!precise)
|
|
36
|
+
return;
|
|
37
|
+
const heading = /^(#{1,6})\s+(\S+)/.exec(line);
|
|
38
|
+
if (heading) {
|
|
39
|
+
const id = (heading[2] ?? "").replace(/^`(.*)`$/, "$1");
|
|
40
|
+
const m = ENV_ID.exec(id);
|
|
41
|
+
if (!m && heading[1] === "###")
|
|
42
|
+
strays.push({ text: id, line: offset + i + 1 });
|
|
43
|
+
current = m
|
|
44
|
+
? {
|
|
45
|
+
id,
|
|
46
|
+
component: m[1] ?? "",
|
|
47
|
+
kind: undefined,
|
|
48
|
+
pool: undefined,
|
|
49
|
+
covers: [],
|
|
50
|
+
line: offset + i + 1,
|
|
51
|
+
}
|
|
52
|
+
: undefined;
|
|
53
|
+
if (current)
|
|
54
|
+
environments.push(current);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const field = FIELD.exec(line);
|
|
58
|
+
if (!field || !current)
|
|
59
|
+
return;
|
|
60
|
+
const value = (field[2] ?? "").trim();
|
|
61
|
+
if (field[1] === "kind")
|
|
62
|
+
current.kind = KINDS.includes(value)
|
|
63
|
+
? value
|
|
64
|
+
: undefined;
|
|
65
|
+
if (field[1] === "pool" && value !== "")
|
|
66
|
+
current.pool = value;
|
|
67
|
+
if (field[1] === "covers")
|
|
68
|
+
current.covers = (value.split(/[((]|\s[—–-]\s/)[0] ?? "")
|
|
69
|
+
.replace(/[[\]`]/g, "")
|
|
70
|
+
.split(/[,、\s]+/)
|
|
71
|
+
.filter((c) => c !== "");
|
|
72
|
+
});
|
|
73
|
+
return { front: data, environments, strays };
|
|
74
|
+
}
|
|
75
|
+
/** What `cupola check` lints in the e2e profile. */
|
|
76
|
+
export function checkProfile(raw, components,
|
|
77
|
+
/** The components of this repository: each repository's profile lists its own. */
|
|
78
|
+
own = components) {
|
|
79
|
+
const problems = [];
|
|
80
|
+
const p = (message, line) => problems.push({ file: E2E_PROFILE, ...(line ? { line } : {}), message });
|
|
81
|
+
const profile = parseProfile(raw);
|
|
82
|
+
const front = Front.safeParse(profile.front);
|
|
83
|
+
if (!front.success) {
|
|
84
|
+
for (const issue of front.error.issues)
|
|
85
|
+
p(`front matter ${issue.path.join(".") || "(root)"}: ${issue.message}`);
|
|
86
|
+
}
|
|
87
|
+
for (const c of front.success ? front.data.components : []) {
|
|
88
|
+
if (!components.includes(c))
|
|
89
|
+
p(`components: ${c} is not a configured component`);
|
|
90
|
+
}
|
|
91
|
+
for (const s of profile.strays)
|
|
92
|
+
p(`${s.text}: an environment's heading is \`### <component>.<environment>\``, s.line);
|
|
93
|
+
const seen = new Set();
|
|
94
|
+
for (const e of profile.environments) {
|
|
95
|
+
if (seen.has(e.id))
|
|
96
|
+
p(`${e.id} is listed twice`, e.line);
|
|
97
|
+
seen.add(e.id);
|
|
98
|
+
if (!components.includes(e.component))
|
|
99
|
+
p(`${e.id}: ${e.component} is not a configured component`, e.line);
|
|
100
|
+
else if (!own.includes(e.component))
|
|
101
|
+
p(`${e.id}: ${e.component} lives in another repository, whose own profile lists it`, e.line);
|
|
102
|
+
if (!e.kind)
|
|
103
|
+
p(`${e.id}: kind must be one of ${KINDS.join(", ")}`, e.line);
|
|
104
|
+
if (e.pool && e.kind !== "shared")
|
|
105
|
+
p(`${e.id}: only a shared environment has a pool`, e.line);
|
|
106
|
+
for (const c of e.covers)
|
|
107
|
+
if (!components.includes(c))
|
|
108
|
+
p(`${e.id}: covers ${c}, which is not a configured component`, e.line);
|
|
109
|
+
}
|
|
110
|
+
return problems;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The shared environments a need may take: one environment by its id, or any
|
|
114
|
+
* shared environment of a pool (`pool:<name>`). Emulators and isolated
|
|
115
|
+
* environments need no lock, so a need for one takes nothing.
|
|
116
|
+
*/
|
|
117
|
+
export function candidatesOf(profile, need) {
|
|
118
|
+
if (need.startsWith("pool:")) {
|
|
119
|
+
const pool = need.slice(5);
|
|
120
|
+
const members = profile.environments.filter((e) => e.kind === "shared" && e.pool === pool);
|
|
121
|
+
return members.length > 0 ? members : undefined;
|
|
122
|
+
}
|
|
123
|
+
const env = profile.environments.find((e) => e.id === need);
|
|
124
|
+
if (!env)
|
|
125
|
+
return undefined;
|
|
126
|
+
return env.kind === "shared" ? [env] : [];
|
|
127
|
+
}
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
// Locks: a ref per shared environment, pushed to origin.
|
|
130
|
+
export const LOCK_REFS = "refs/cupola/locks/";
|
|
131
|
+
/** Where origin's locks are fetched to, so that reading them never touches FETCH_HEAD. */
|
|
132
|
+
const SEEN_REFS = "refs/cupola/origin-locks/";
|
|
133
|
+
/** The locks origin holds now, by environment. */
|
|
134
|
+
export async function readLocks(root) {
|
|
135
|
+
await git(root, ["fetch", "--quiet", "--prune", "origin", `+${LOCK_REFS}*:${SEEN_REFS}*`]);
|
|
136
|
+
const out = await git(root, [
|
|
137
|
+
"for-each-ref",
|
|
138
|
+
"--format=%(objectname)%00%(refname)%00%(contents)%00%00",
|
|
139
|
+
SEEN_REFS,
|
|
140
|
+
]);
|
|
141
|
+
const locks = new Map();
|
|
142
|
+
for (const record of out.split("\0\0")) {
|
|
143
|
+
const [sha, ref, contents] = record.replace(/^\n/, "").split("\0");
|
|
144
|
+
if (!sha || !ref)
|
|
145
|
+
continue;
|
|
146
|
+
const environment = ref.slice(SEEN_REFS.length);
|
|
147
|
+
try {
|
|
148
|
+
const lock = JSON.parse(contents ?? "");
|
|
149
|
+
locks.set(environment, { sha, lock });
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
// A lock nobody can read is still held: it names nobody to take it over from.
|
|
153
|
+
locks.set(environment, {
|
|
154
|
+
sha,
|
|
155
|
+
lock: {
|
|
156
|
+
kind: "lock",
|
|
157
|
+
method: 1,
|
|
158
|
+
id: sha.slice(0, 8),
|
|
159
|
+
environment,
|
|
160
|
+
work: "unknown",
|
|
161
|
+
phase: "unknown",
|
|
162
|
+
who: "unknown",
|
|
163
|
+
session: "unknown",
|
|
164
|
+
since: "unknown",
|
|
165
|
+
cli: "unknown",
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return locks;
|
|
171
|
+
}
|
|
172
|
+
/** Pushes a new lock: its commit, or undefined when origin already has one (the first push wins). */
|
|
173
|
+
export async function pushLock(root, lock) {
|
|
174
|
+
const tree = await gitWith(root, ["mktree"], { input: "" });
|
|
175
|
+
const sha = await gitWith(root, ["commit-tree", tree, "-m", JSON.stringify(lock)], {
|
|
176
|
+
env: {
|
|
177
|
+
GIT_AUTHOR_NAME: "Cupola",
|
|
178
|
+
GIT_AUTHOR_EMAIL: "cupola@localhost",
|
|
179
|
+
GIT_COMMITTER_NAME: "Cupola",
|
|
180
|
+
GIT_COMMITTER_EMAIL: "cupola@localhost",
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
try {
|
|
184
|
+
await git(root, ["push", "--quiet", "origin", `${sha}:${LOCK_REFS}${lock.environment}`]);
|
|
185
|
+
return sha;
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
const stderr = error.stderr ?? "";
|
|
189
|
+
if (/rejected|already exists|non-fast-forward|fetch first|stale info/.test(stderr))
|
|
190
|
+
return undefined;
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/** Removes the lock, only while origin still holds that exact one. */
|
|
195
|
+
export async function removeLock(root, environment, sha) {
|
|
196
|
+
const ref = `${LOCK_REFS}${environment}`;
|
|
197
|
+
try {
|
|
198
|
+
await git(root, ["push", "--quiet", `--force-with-lease=${ref}:${sha}`, "origin", `:${ref}`]);
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
const stderr = error.stderr ?? "";
|
|
202
|
+
// Gone already, or taken over by someone else: either way it is not ours to remove.
|
|
203
|
+
if (/stale info|does not exist|unable to delete|rejected/.test(stderr))
|
|
204
|
+
return;
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
}
|