@kyuki3rain/cupola 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +184 -0
- package/LICENSE +202 -0
- package/README.md +41 -2
- package/dist/adr.js +106 -0
- package/dist/branch-store.js +73 -0
- package/dist/catalog.js +137 -0
- package/dist/check.js +192 -0
- package/dist/cli.js +8 -0
- package/dist/code.js +216 -0
- package/dist/commands/change.js +728 -0
- package/dist/commands/epic.js +311 -0
- package/dist/commands/execution.js +482 -0
- package/dist/commands/install.js +109 -0
- package/dist/commands/locks.js +243 -0
- package/dist/commands/reaction.js +185 -0
- package/dist/commands/revision.js +139 -0
- package/dist/commands/run.js +1021 -0
- package/dist/commands/setup.js +414 -0
- package/dist/commands/spec.js +80 -0
- package/dist/commands/status.js +1457 -0
- package/dist/commands/trace.js +474 -0
- package/dist/commands/wait.js +105 -0
- package/dist/commands/work.js +126 -0
- package/dist/commands/workspace.js +137 -0
- package/dist/commands/worktree.js +422 -0
- package/dist/config.js +353 -0
- package/dist/day.js +29 -0
- package/dist/e2e-project.js +62 -0
- package/dist/e2e.js +207 -0
- package/dist/engine/cc-sdd.js +164 -0
- package/dist/epic.js +379 -0
- package/dist/execution.js +272 -0
- package/dist/feedback.js +20 -0
- package/dist/frontmatter.js +20 -0
- package/dist/git.js +365 -0
- package/dist/host/git.js +224 -0
- package/dist/host/github.js +387 -0
- package/dist/host/host.js +3 -0
- package/dist/host/index.js +63 -0
- package/dist/install.js +182 -0
- package/dist/io.js +46 -0
- package/dist/main.js +866 -0
- package/dist/method/v1.js +56 -0
- package/dist/reaction.js +110 -0
- package/dist/record.js +507 -0
- package/dist/release.js +84 -0
- package/dist/repo.js +13 -0
- package/dist/runbook.js +132 -0
- package/dist/safe-write.js +80 -0
- package/dist/spec.js +171 -0
- package/dist/status.js +321 -0
- package/dist/templates.js +50 -0
- package/dist/trace.js +275 -0
- package/dist/tracker/files.js +99 -0
- package/dist/tracker/github-issues.js +217 -0
- package/dist/tracker/index.js +34 -0
- package/dist/tracker/tracker.js +69 -0
- package/dist/tree.js +59 -0
- package/dist/trust.js +13 -0
- package/dist/version.js +5 -0
- package/dist/work.js +170 -0
- package/dist/workspace.js +324 -0
- package/npm-shrinkwrap.json +2014 -0
- package/package.json +41 -4
- package/skills/cupola-abort/SKILL.md +51 -0
- package/skills/cupola-adr/SKILL.md +54 -0
- package/skills/cupola-ask/SKILL.md +42 -0
- package/skills/cupola-catalog/SKILL.md +41 -0
- package/skills/cupola-catalog-draft/SKILL.md +116 -0
- package/skills/cupola-complete/SKILL.md +107 -0
- package/skills/cupola-decide/SKILL.md +99 -0
- package/skills/cupola-doc/SKILL.md +52 -0
- package/skills/cupola-e2e-setup/SKILL.md +78 -0
- package/skills/cupola-e2e-test/SKILL.md +115 -0
- package/skills/cupola-epic-accept/SKILL.md +166 -0
- package/skills/cupola-epic-create/SKILL.md +160 -0
- package/skills/cupola-epic-revise/SKILL.md +92 -0
- package/skills/cupola-epic-run/SKILL.md +166 -0
- package/skills/cupola-fix/SKILL.md +117 -0
- package/skills/cupola-init/SKILL.md +143 -0
- package/skills/cupola-intake/SKILL.md +95 -0
- package/skills/cupola-operate/SKILL.md +81 -0
- package/skills/cupola-resolve/SKILL.md +93 -0
- package/skills/cupola-respond/SKILL.md +73 -0
- package/skills/cupola-review/SKILL.md +131 -0
- package/skills/cupola-spec-design/SKILL.md +169 -0
- package/skills/cupola-spec-impl/SKILL.md +137 -0
- package/skills/cupola-spec-run/SKILL.md +137 -0
- package/skills/cupola-steering/SKILL.md +53 -0
- package/skills/cupola-try/SKILL.md +38 -0
- package/templates/install/.kiro/steering/e2e.md +48 -0
- package/templates/install/.kiro/steering/operations.md +109 -0
- package/templates/install/.kiro/steering/principles.md +25 -0
- package/templates/install/CLAUDE.md +19 -0
- package/templates/install/adr/TEMPLATE.md +38 -0
- package/templates/install/cc-sdd/patches.json +31 -0
- package/templates/method-1/behavior-area.md +43 -0
- package/templates/method-1/behavior-index.md +16 -0
- package/templates/method-1/card.md +49 -0
- package/templates/method-1/case-draft.md +25 -0
- package/templates/method-1/case.md +38 -0
- package/templates/method-1/catalog-spec.md +33 -0
- package/templates/method-1/changes/adr.md +4 -0
- package/templates/method-1/changes/catalog.md +29 -0
- package/templates/method-1/changes/doc.md +5 -0
- package/templates/method-1/changes/e2e.md +7 -0
- package/templates/method-1/changes/epic.md +30 -0
- package/templates/method-1/changes/fix.md +21 -0
- package/templates/method-1/changes/release.md +23 -0
- package/templates/method-1/changes/spec.md +55 -0
- package/templates/method-1/changes/system.md +13 -0
- package/templates/method-1/cupola-issue-draft.md +17 -0
- package/templates/method-1/epic.md +147 -0
- package/templates/method-1/execution.md +55 -0
- package/templates/method-1/fix-spec.md +34 -0
- package/templates/method-1/fix.md +39 -0
- package/templates/method-1/lock.json +13 -0
- package/templates/method-1/no-go.md +28 -0
- package/templates/method-1/phase-outcome.json +29 -0
- package/templates/method-1/reaction.md +17 -0
- package/templates/method-1/record.json +63 -0
- package/templates/method-1/report.md +27 -0
- package/templates/method-1/runbook.md +45 -0
- package/templates/method-1/runner-result.json +6 -0
- package/templates/methods.json +13 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Method 1: the phase table of each mode. The order
|
|
3
|
+
* of phases is decided here, never by a runner's judgment.
|
|
4
|
+
*/
|
|
5
|
+
export const METHOD = 1;
|
|
6
|
+
const FULL = [
|
|
7
|
+
{ name: "acceptance", skill: "cupola-spec-design" },
|
|
8
|
+
{ name: "design", skill: "cupola-spec-design" },
|
|
9
|
+
{ name: "impl", skill: "cupola-spec-impl", resumesAfterCards: true },
|
|
10
|
+
{ name: "summary", skill: "cupola-spec-impl" },
|
|
11
|
+
{ name: "mutation", skill: "cupola-spec-impl" },
|
|
12
|
+
{ name: "doc", skill: "cupola-doc" },
|
|
13
|
+
{ name: "review", skill: "cupola-review" },
|
|
14
|
+
{ name: "e2e", skill: "cupola-e2e-test", locks: true },
|
|
15
|
+
{ name: "accept", skill: "cupola-epic-accept" },
|
|
16
|
+
];
|
|
17
|
+
const FIX = [
|
|
18
|
+
{ name: "reproduce", skill: "cupola-fix" },
|
|
19
|
+
{ name: "impl", skill: "cupola-fix" },
|
|
20
|
+
{ name: "summary", skill: "cupola-fix" },
|
|
21
|
+
{ name: "review", skill: "cupola-review" },
|
|
22
|
+
{ name: "doc", skill: "cupola-doc" },
|
|
23
|
+
{ name: "e2e", skill: "cupola-e2e-test", locks: true },
|
|
24
|
+
];
|
|
25
|
+
const CATALOG = [
|
|
26
|
+
{ name: "survey-draft", skill: "cupola-catalog-draft" },
|
|
27
|
+
{ name: "tag-pin", skill: "cupola-catalog-draft" },
|
|
28
|
+
{ name: "mutation", skill: "cupola-catalog-draft" },
|
|
29
|
+
{ name: "review", skill: "cupola-review" },
|
|
30
|
+
];
|
|
31
|
+
const EPIC = [
|
|
32
|
+
{ name: "accept", skill: "cupola-epic-accept", locks: true },
|
|
33
|
+
{ name: "open-release", skill: "cupola-epic-accept" },
|
|
34
|
+
];
|
|
35
|
+
export const PHASES = {
|
|
36
|
+
full: FULL,
|
|
37
|
+
fix: FIX,
|
|
38
|
+
catalog: CATALOG,
|
|
39
|
+
epic: EPIC,
|
|
40
|
+
};
|
|
41
|
+
export function modeOf(kind) {
|
|
42
|
+
switch (kind) {
|
|
43
|
+
case "spec":
|
|
44
|
+
return "full";
|
|
45
|
+
case "fix":
|
|
46
|
+
case "fix-spec":
|
|
47
|
+
return "fix";
|
|
48
|
+
case "catalog":
|
|
49
|
+
return "catalog";
|
|
50
|
+
case "epic":
|
|
51
|
+
return "epic";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export function phaseDef(mode, name) {
|
|
55
|
+
return PHASES[mode].find((p) => p.name === name);
|
|
56
|
+
}
|
package/dist/reaction.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { byReviewer, trusted } from "./feedback.js";
|
|
3
|
+
import { parseFrontMatter } from "./frontmatter.js";
|
|
4
|
+
import { git } from "./git.js";
|
|
5
|
+
import { runDir, WorkError } from "./work.js";
|
|
6
|
+
/**
|
|
7
|
+
* The reactions of a work at its gate (comment, try, No-Go), wherever they
|
|
8
|
+
* were given: on the change, through the host's adapter, or in a session,
|
|
9
|
+
* kept as files in `.cupola/runs/<work>/reactions/` until a gate processes
|
|
10
|
+
* them. They are processed only at a gate of the epic (start, acceptance, release); what comes outside one
|
|
11
|
+
* waits for the next. Card answers are not here: they are recorded in the
|
|
12
|
+
* card files.
|
|
13
|
+
*/
|
|
14
|
+
export const REACTION_KINDS = ["comment", "try", "no-go"];
|
|
15
|
+
/** Where a session's reactions to a work are kept until a gate processes them. */
|
|
16
|
+
export function reactionsDir(id) {
|
|
17
|
+
return join(runDir(id), "reactions");
|
|
18
|
+
}
|
|
19
|
+
/** How a session's reaction is named among the host's: `session:<n>`. */
|
|
20
|
+
export const SESSION = "session:";
|
|
21
|
+
/**
|
|
22
|
+
* The reactions given in a session, from their files. A file exists until a
|
|
23
|
+
* gate processes it, so none of them is answered.
|
|
24
|
+
*/
|
|
25
|
+
export async function sessionReactions(tree, id) {
|
|
26
|
+
const dir = reactionsDir(id);
|
|
27
|
+
const out = [];
|
|
28
|
+
for (const file of (await tree.list(dir)).filter((f) => f.endsWith(".md"))) {
|
|
29
|
+
const where = join(dir, file);
|
|
30
|
+
const { data, body } = parseFrontMatter((await tree.read(where)) ?? "", where);
|
|
31
|
+
const kind = String(data.type ?? "");
|
|
32
|
+
if (data.kind !== "reaction" || typeof data.reaction !== "number")
|
|
33
|
+
throw new WorkError(`${where}: not a reaction`);
|
|
34
|
+
if (!REACTION_KINDS.includes(kind))
|
|
35
|
+
throw new WorkError(`${where}: type is ${REACTION_KINDS.join(", ")}, not ${kind}`);
|
|
36
|
+
out.push({
|
|
37
|
+
id: `${SESSION}${data.reaction}`,
|
|
38
|
+
author: String(data.who ?? ""),
|
|
39
|
+
association: "SESSION",
|
|
40
|
+
bot: false,
|
|
41
|
+
at: data.at instanceof Date ? data.at.toISOString() : String(data.at ?? ""),
|
|
42
|
+
kind,
|
|
43
|
+
body: body.replace(/<!--[\s\S]*?-->/g, "").trim(),
|
|
44
|
+
place: null,
|
|
45
|
+
answered: false,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return out.sort((a, b) => a.at.localeCompare(b.at));
|
|
49
|
+
}
|
|
50
|
+
/** The text of a session's reaction file (`templates/method-1/reaction.md`). */
|
|
51
|
+
export function reactionFile(n, kind, who, at, body) {
|
|
52
|
+
return `---\nkind: reaction\nmethod: 1\nreaction: ${n}\ntype: ${kind}\nwho: ${JSON.stringify(who)}\nat: ${JSON.stringify(at)}\n---\n\n${body.trim()}\n`;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* People's reactions on a change: from someone the configuration trusts,
|
|
56
|
+
* neither a bot nor the configured reviewer (whose remarks are the review
|
|
57
|
+
* phase's). Anyone else's are never acted on.
|
|
58
|
+
*/
|
|
59
|
+
export async function hostReactions(host, change, config) {
|
|
60
|
+
const { reviewer } = config.review;
|
|
61
|
+
const ok = (a) => trusted(a, reviewer, config.trust);
|
|
62
|
+
try {
|
|
63
|
+
const all = await host.reactions(change.number, ok);
|
|
64
|
+
return { reactions: all.filter((r) => !r.bot && ok(r) && !byReviewer(r, reviewer)) };
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
return { reactions: [], unread: error.message };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export function pendingOf(reactions) {
|
|
71
|
+
return reactions
|
|
72
|
+
.filter((r) => !r.answered)
|
|
73
|
+
.map((r) => ({ id: r.id, kind: r.kind, who: r.author, at: r.at }));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The comments a No-Go takes in with its own words: those given since the
|
|
77
|
+
* last No-Go was processed (answered or not), for they are what this gate
|
|
78
|
+
* said. A No-Go with no words and none of these has no content.
|
|
79
|
+
*/
|
|
80
|
+
export function gateComments(reactions) {
|
|
81
|
+
const last = reactions
|
|
82
|
+
.filter((r) => r.kind === "no-go" && r.answered)
|
|
83
|
+
.map((r) => r.at)
|
|
84
|
+
.sort()
|
|
85
|
+
.at(-1);
|
|
86
|
+
return reactions.filter((r) => r.kind === "comment" && (last === undefined || r.at > last));
|
|
87
|
+
}
|
|
88
|
+
/** The branch a try of a work is built on: an internal convention of `cupola-try`. */
|
|
89
|
+
export function tryBranch(id, n) {
|
|
90
|
+
return `${TRY_PREFIX}${id}/${n}`;
|
|
91
|
+
}
|
|
92
|
+
const TRY_PREFIX = "cupola-try/";
|
|
93
|
+
/** The tries of a work on origin, by number, oldest first. */
|
|
94
|
+
export async function triesOf(root, id) {
|
|
95
|
+
let out;
|
|
96
|
+
try {
|
|
97
|
+
out = await git(root, ["ls-remote", "--heads", "origin", `refs/heads/${TRY_PREFIX}${id}/*`]);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
const prefix = `refs/heads/${TRY_PREFIX}${id}/`;
|
|
103
|
+
return out
|
|
104
|
+
.split("\n")
|
|
105
|
+
.map((l) => l.split("\t")[1] ?? "")
|
|
106
|
+
.filter((ref) => ref.startsWith(prefix))
|
|
107
|
+
.map((ref) => Number(ref.slice(prefix.length)))
|
|
108
|
+
.filter((n) => Number.isInteger(n) && n > 0)
|
|
109
|
+
.sort((a, b) => a - b);
|
|
110
|
+
}
|
package/dist/record.js
ADDED
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { METHOD, modeOf, PHASES, phaseDef } from "./method/v1.js";
|
|
4
|
+
/**
|
|
5
|
+
* The progress record: `.cupola/runs/<work>/record.json`
|
|
6
|
+
* on the work's branch. Only the CLI writes it; every write raises `serial`.
|
|
7
|
+
*/
|
|
8
|
+
const workKind = z.enum(["spec", "fix", "fix-spec", "catalog", "epic"]);
|
|
9
|
+
const mode = z.enum(["full", "fix", "catalog", "epic"]);
|
|
10
|
+
const phaseState = z.enum(["pending", "running", "done", "redo", "skipped"]);
|
|
11
|
+
const active = z.strictObject({
|
|
12
|
+
id: z.string().min(1),
|
|
13
|
+
who: z.string(),
|
|
14
|
+
session: z.string().min(1),
|
|
15
|
+
since: z.string(),
|
|
16
|
+
cli: z.string(),
|
|
17
|
+
phase: z.string(),
|
|
18
|
+
});
|
|
19
|
+
const outside = z.strictObject({
|
|
20
|
+
kind: z.string().min(1),
|
|
21
|
+
id: z.string().min(1),
|
|
22
|
+
where: z.string(),
|
|
23
|
+
credential: z.string(),
|
|
24
|
+
phase: z.string().optional(),
|
|
25
|
+
cleanup: z.string().regex(/^(done|pending|kept: .+)$/, "done | pending | kept: <reason>"),
|
|
26
|
+
});
|
|
27
|
+
const phase = z.strictObject({
|
|
28
|
+
name: z.string().min(1),
|
|
29
|
+
state: phaseState,
|
|
30
|
+
commit: z.string().optional(),
|
|
31
|
+
started: z.string().optional(),
|
|
32
|
+
finished: z.string().optional(),
|
|
33
|
+
note: z.string().optional(),
|
|
34
|
+
/** For a skipped phase: the phase whose outcome skipped it, so that a No-Go keeps the skip. */
|
|
35
|
+
skipped_by: z.string().optional(),
|
|
36
|
+
/** The shared e2e environments its plan needs (an id, or `pool:<name>`); `run next` locks them. */
|
|
37
|
+
needs: z.array(z.string()).optional(),
|
|
38
|
+
/** The shared environments this phase holds the locks of, until `run record` releases them. */
|
|
39
|
+
locks: z.array(z.string()).optional(),
|
|
40
|
+
});
|
|
41
|
+
const event = z.strictObject({
|
|
42
|
+
at: z.string(),
|
|
43
|
+
phase: z.string(),
|
|
44
|
+
what: z.string().min(1),
|
|
45
|
+
note: z.string().optional(),
|
|
46
|
+
});
|
|
47
|
+
export const recordSchema = z.strictObject({
|
|
48
|
+
kind: z.literal("record"),
|
|
49
|
+
method: z.literal(METHOD),
|
|
50
|
+
work: z.strictObject({
|
|
51
|
+
kind: workKind,
|
|
52
|
+
id: z.string().min(1),
|
|
53
|
+
epic: z.string().nullable(),
|
|
54
|
+
component: z.string().min(1),
|
|
55
|
+
mode,
|
|
56
|
+
}),
|
|
57
|
+
lineage: z.string().min(1),
|
|
58
|
+
serial: z.int().nonnegative(),
|
|
59
|
+
started_from: z.strictObject({ card: z.int().positive().nullable() }),
|
|
60
|
+
active: active.nullable(),
|
|
61
|
+
phases: z.array(phase).min(1),
|
|
62
|
+
outside: z.array(outside),
|
|
63
|
+
drafts: z.array(z.string()),
|
|
64
|
+
events: z.array(event),
|
|
65
|
+
review: z.strictObject({ rounds: z.int().nonnegative(), max: z.int().positive() }),
|
|
66
|
+
});
|
|
67
|
+
export const outcomeSchema = z
|
|
68
|
+
.strictObject({
|
|
69
|
+
phase: z.string().min(1),
|
|
70
|
+
result: z.enum([
|
|
71
|
+
"done",
|
|
72
|
+
"redo",
|
|
73
|
+
"cards",
|
|
74
|
+
"blocked",
|
|
75
|
+
"waiting",
|
|
76
|
+
"not-a-fix",
|
|
77
|
+
"needs-environment",
|
|
78
|
+
]),
|
|
79
|
+
redo: z.string().nullable().optional(),
|
|
80
|
+
cards: z.array(z.string()).optional(),
|
|
81
|
+
blocker: z.string().nullable().optional(),
|
|
82
|
+
outside: z.array(outside.omit({ phase: true })).optional(),
|
|
83
|
+
drafts: z.array(z.string()).optional(),
|
|
84
|
+
/** Later phases found not to apply to this work, e.g. doc and e2e of a fix. */
|
|
85
|
+
skip: z.array(z.string()).optional(),
|
|
86
|
+
/** For `needs-environment`: the shared environments the plan needs. */
|
|
87
|
+
environments: z.array(z.string().min(1)).optional(),
|
|
88
|
+
note: z.string(),
|
|
89
|
+
})
|
|
90
|
+
.superRefine((o, ctx) => {
|
|
91
|
+
if (o.result === "redo" && !o.redo) {
|
|
92
|
+
ctx.addIssue({ code: "custom", path: ["redo"], message: "required when result is redo" });
|
|
93
|
+
}
|
|
94
|
+
if (o.result === "cards" && !o.cards?.length) {
|
|
95
|
+
ctx.addIssue({ code: "custom", path: ["cards"], message: "required when result is cards" });
|
|
96
|
+
}
|
|
97
|
+
if (o.result === "needs-environment" && !o.environments?.length) {
|
|
98
|
+
ctx.addIssue({
|
|
99
|
+
code: "custom",
|
|
100
|
+
path: ["environments"],
|
|
101
|
+
message: "required when result is needs-environment",
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (o.result === "blocked" && !o.blocker) {
|
|
105
|
+
ctx.addIssue({
|
|
106
|
+
code: "custom",
|
|
107
|
+
path: ["blocker"],
|
|
108
|
+
message: "required when result is blocked",
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
export class RecordError extends Error {
|
|
113
|
+
constructor(message) {
|
|
114
|
+
super(message);
|
|
115
|
+
this.name = "RecordError";
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function issues(error) {
|
|
119
|
+
return error.issues.map((i) => `${i.path.join(".") || "(root)"}: ${i.message}`).join("; ");
|
|
120
|
+
}
|
|
121
|
+
export function parseRecord(raw) {
|
|
122
|
+
const result = recordSchema.safeParse(JSON.parse(raw));
|
|
123
|
+
if (!result.success)
|
|
124
|
+
throw new RecordError(`invalid progress record: ${issues(result.error)}`);
|
|
125
|
+
return result.data;
|
|
126
|
+
}
|
|
127
|
+
export function parseOutcome(raw) {
|
|
128
|
+
let json;
|
|
129
|
+
try {
|
|
130
|
+
json = JSON.parse(raw);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
throw new RecordError(`phase outcome is not valid JSON: ${error.message}`);
|
|
134
|
+
}
|
|
135
|
+
const result = outcomeSchema.safeParse(json);
|
|
136
|
+
if (!result.success)
|
|
137
|
+
throw new RecordError(`invalid phase outcome: ${issues(result.error)}`);
|
|
138
|
+
return result.data;
|
|
139
|
+
}
|
|
140
|
+
export function serializeRecord(record) {
|
|
141
|
+
return `${JSON.stringify(record, null, 2)}\n`;
|
|
142
|
+
}
|
|
143
|
+
export function createRecord(work, maxRounds) {
|
|
144
|
+
const m = modeOf(work.kind);
|
|
145
|
+
return {
|
|
146
|
+
kind: "record",
|
|
147
|
+
method: METHOD,
|
|
148
|
+
work: { kind: work.kind, id: work.id, epic: work.epic, component: work.component, mode: m },
|
|
149
|
+
lineage: randomUUID(),
|
|
150
|
+
serial: 0,
|
|
151
|
+
started_from: { card: null },
|
|
152
|
+
active: null,
|
|
153
|
+
phases: PHASES[m].map((p) => ({
|
|
154
|
+
name: p.name,
|
|
155
|
+
state: m === "full" && p.name === "accept" && !work.onlySpecOfEpic ? "skipped" : "pending",
|
|
156
|
+
})),
|
|
157
|
+
outside: [],
|
|
158
|
+
drafts: [],
|
|
159
|
+
events: [],
|
|
160
|
+
review: { rounds: 0, max: maxRounds },
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function clone(record) {
|
|
164
|
+
return structuredClone(record);
|
|
165
|
+
}
|
|
166
|
+
function bump(record) {
|
|
167
|
+
record.serial += 1;
|
|
168
|
+
return record;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Whether a `--take-over` names this marker: by its id, or by the session holding it — the
|
|
172
|
+
* session itself, or one of its runners (`<session>-<component>`), as `cupola-epic-run <epic>
|
|
173
|
+
* --take-over <session>` names every marker of an epic's run at once.
|
|
174
|
+
*/
|
|
175
|
+
export function takeOverNames(takeOver, held) {
|
|
176
|
+
if (!takeOver)
|
|
177
|
+
return false;
|
|
178
|
+
return (takeOver === held.id || takeOver === held.session || held.session.startsWith(`${takeOver}-`));
|
|
179
|
+
}
|
|
180
|
+
/** Takes over another session's marker when it is named; returns whether the record changed. */
|
|
181
|
+
function claim(record, runner) {
|
|
182
|
+
const held = record.active;
|
|
183
|
+
if (held && held.session !== runner.session) {
|
|
184
|
+
if (!takeOverNames(runner.takeOver, held)) {
|
|
185
|
+
throw new RecordError(`${record.work.id} is being run by ${held.who} (session ${held.session}, since ${held.since}). ` +
|
|
186
|
+
`If that session is gone, run again with --take-over ${held.id}`);
|
|
187
|
+
}
|
|
188
|
+
record.events.push({
|
|
189
|
+
at: runner.now,
|
|
190
|
+
phase: held.phase,
|
|
191
|
+
what: "taken-over",
|
|
192
|
+
note: `marker ${held.id} of ${held.who} (session ${held.session})`,
|
|
193
|
+
});
|
|
194
|
+
record.active = null;
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
/** Effects of an answer that end the run; `cupola-decide` carries them out. */
|
|
200
|
+
const ENDING_EFFECTS = new Set(["revise-epic", "abort", "to-case"]);
|
|
201
|
+
/**
|
|
202
|
+
* No-Go cards of a work's acceptance, answered (they name the phase to redo) and
|
|
203
|
+
* written after the record started, oldest first.
|
|
204
|
+
*/
|
|
205
|
+
export function pendingNoGos(record, cards) {
|
|
206
|
+
const since = record?.started_from.card ?? 0;
|
|
207
|
+
return cards.filter((c) => c.type === "no-go" && c.answered && c.effect === "continue" && c.card > since);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* An answered card of this record whose effect ends the run (abort, revise
|
|
211
|
+
* the epic, turn into a case). Cards from before the record started are
|
|
212
|
+
* history: their effects were carried out.
|
|
213
|
+
*/
|
|
214
|
+
export function endingCard(record, cards) {
|
|
215
|
+
const since = record?.started_from.card ?? 0;
|
|
216
|
+
return cards.find((c) => c.card > since && c.effect !== null && ENDING_EFFECTS.has(c.effect));
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Sends a full spec's run back to its acceptance tests for an agreed revision
|
|
220
|
+
* that redesigns it: the tests were written from the examples the revision may
|
|
221
|
+
* change. Changes `record` in place.
|
|
222
|
+
*/
|
|
223
|
+
export function redoFromAcceptance(record) {
|
|
224
|
+
const order = record.phases.map((p) => p.name);
|
|
225
|
+
const from = order.indexOf("acceptance");
|
|
226
|
+
if (from < 0)
|
|
227
|
+
return;
|
|
228
|
+
for (const p of record.phases.slice(from)) {
|
|
229
|
+
if (p.state === "done")
|
|
230
|
+
p.state = "redo";
|
|
231
|
+
else if (p.state === "running")
|
|
232
|
+
p.state = "pending";
|
|
233
|
+
else if (p.state === "skipped" && p.skipped_by && order.indexOf(p.skipped_by) >= from) {
|
|
234
|
+
p.state = "pending";
|
|
235
|
+
delete p.skipped_by;
|
|
236
|
+
delete p.note;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Starts a new record from No-Go cards: the phases they touch are
|
|
242
|
+
* to be redone from the earliest one named, and the phases before it are done.
|
|
243
|
+
*/
|
|
244
|
+
export function createFromNoGos(work, maxRounds, noGos, now, previous,
|
|
245
|
+
/** Whether `previous` is the record a finish removed: its drafts are filed already. */
|
|
246
|
+
finished = false) {
|
|
247
|
+
const record = createRecord(work, maxRounds);
|
|
248
|
+
// What the earlier run left outside the repository, or did not file yet, is still owed.
|
|
249
|
+
if (previous) {
|
|
250
|
+
record.outside = previous.outside.filter((o) => o.cleanup !== "done");
|
|
251
|
+
if (!finished)
|
|
252
|
+
record.drafts = [...previous.drafts];
|
|
253
|
+
}
|
|
254
|
+
const order = record.phases.map((p) => p.name);
|
|
255
|
+
for (const c of noGos) {
|
|
256
|
+
if (!order.includes(c.redo ?? "")) {
|
|
257
|
+
throw new RecordError(`No-Go card ${c.card}: redo must name a phase of this work, not ${String(c.redo)}`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
const from = Math.min(...noGos.map((c) => order.indexOf(c.redo)));
|
|
261
|
+
record.phases.forEach((p, i) => {
|
|
262
|
+
if (i < from && p.state !== "skipped") {
|
|
263
|
+
p.state = "done";
|
|
264
|
+
p.note = "done before the No-Go";
|
|
265
|
+
}
|
|
266
|
+
// A later phase skipped by a phase that does not run again stays skipped.
|
|
267
|
+
const before = previous?.phases.find((q) => q.name === p.name);
|
|
268
|
+
if (i >= from && before?.state === "skipped" && before.skipped_by) {
|
|
269
|
+
const by = order.indexOf(before.skipped_by);
|
|
270
|
+
if (by >= 0 && by < from)
|
|
271
|
+
Object.assign(p, { state: "skipped", note: before.note, skipped_by: before.skipped_by });
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
const numbers = noGos.map((c) => c.card).sort((a, b) => a - b);
|
|
275
|
+
record.started_from.card = numbers.at(-1) || null;
|
|
276
|
+
record.events.push({
|
|
277
|
+
at: now,
|
|
278
|
+
phase: order[from],
|
|
279
|
+
what: "no-go",
|
|
280
|
+
note: noGos.map((c) => `card ${c.card}`).join(", ") +
|
|
281
|
+
(previous ? ` (after lineage ${previous.lineage})` : ""),
|
|
282
|
+
});
|
|
283
|
+
return record;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Decides what comes next for a work item: the phase to run, or where the run
|
|
287
|
+
* stops. `unansweredCards` are the card numbers still waiting for a human.
|
|
288
|
+
*/
|
|
289
|
+
export function next(current, cards, runner) {
|
|
290
|
+
const record = clone(current);
|
|
291
|
+
const tookOver = claim(record, runner);
|
|
292
|
+
const stop = (stopped, note) => {
|
|
293
|
+
const changed = tookOver || record.active !== null;
|
|
294
|
+
record.active = null;
|
|
295
|
+
return {
|
|
296
|
+
record: changed ? bump(record) : record,
|
|
297
|
+
action: { kind: "stop", stopped, note },
|
|
298
|
+
};
|
|
299
|
+
};
|
|
300
|
+
const unanswered = cards.filter((c) => !c.answered).map((c) => c.card);
|
|
301
|
+
if (unanswered.length > 0) {
|
|
302
|
+
return stop("decision", `cards waiting for a decision: ${unanswered.join(", ")}`);
|
|
303
|
+
}
|
|
304
|
+
const ending = endingCard(record, cards);
|
|
305
|
+
if (ending) {
|
|
306
|
+
return stop("blocked", `card ${ending.card} chose ${ending.effect}; \`cupola-decide\` carries that out, not the run`);
|
|
307
|
+
}
|
|
308
|
+
const running = record.phases.find((p) => p.state === "running");
|
|
309
|
+
const target = running ?? record.phases.find((p) => p.state === "pending" || p.state === "redo");
|
|
310
|
+
if (!target) {
|
|
311
|
+
const spent = record.events.filter((e) => e.what === "review-budget-spent").at(-1);
|
|
312
|
+
return stop(
|
|
313
|
+
// An epic's own run ends at the release gate.
|
|
314
|
+
record.work.mode === "epic" ? "release" : "acceptance", spent
|
|
315
|
+
? `every phase is done; review budget spent: ${spent.note ?? ""}`
|
|
316
|
+
: "every phase is done; look back over the record, then finish with `cupola run finish`");
|
|
317
|
+
}
|
|
318
|
+
const def = phaseDef(record.work.mode, target.name);
|
|
319
|
+
if (!def)
|
|
320
|
+
throw new RecordError(`phase ${target.name} is not in the ${record.work.mode} table`);
|
|
321
|
+
const restarted = running !== undefined;
|
|
322
|
+
if (restarted) {
|
|
323
|
+
record.events.push({ at: runner.now, phase: target.name, what: "restarted" });
|
|
324
|
+
}
|
|
325
|
+
target.state = "running";
|
|
326
|
+
target.started = runner.now;
|
|
327
|
+
delete target.finished;
|
|
328
|
+
record.active = {
|
|
329
|
+
id: record.active?.id ?? randomUUID().slice(0, 8),
|
|
330
|
+
who: runner.who,
|
|
331
|
+
session: runner.session,
|
|
332
|
+
since: record.active?.since ?? runner.now,
|
|
333
|
+
cli: runner.cli,
|
|
334
|
+
phase: target.name,
|
|
335
|
+
};
|
|
336
|
+
return {
|
|
337
|
+
record: bump(record),
|
|
338
|
+
action: { kind: "phase", phase: target.name, skill: def.skill, restarted },
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
/** Records a phase agent's outcome on the running phase. */
|
|
342
|
+
export function recordOutcome(current, outcome, runner, cards = []) {
|
|
343
|
+
const record = clone(current);
|
|
344
|
+
const held = record.active;
|
|
345
|
+
if (!held || held.session !== runner.session) {
|
|
346
|
+
throw new RecordError(`${record.work.id}: this session holds no active run; ask \`cupola run next\` first`);
|
|
347
|
+
}
|
|
348
|
+
const running = record.phases.find((p) => p.state === "running");
|
|
349
|
+
if (!running || running.name !== outcome.phase) {
|
|
350
|
+
throw new RecordError(`${record.work.id}: the outcome is for ${outcome.phase}, but the running phase is ${running?.name ?? "none"}`);
|
|
351
|
+
}
|
|
352
|
+
for (const o of outcome.outside ?? []) {
|
|
353
|
+
const i = record.outside.findIndex((x) => x.kind === o.kind && x.id === o.id);
|
|
354
|
+
const entry = { ...o, phase: outcome.phase };
|
|
355
|
+
if (i >= 0)
|
|
356
|
+
record.outside[i] = entry;
|
|
357
|
+
else
|
|
358
|
+
record.outside.push(entry);
|
|
359
|
+
}
|
|
360
|
+
for (const d of outcome.drafts ?? [])
|
|
361
|
+
if (!record.drafts.includes(d))
|
|
362
|
+
record.drafts.push(d);
|
|
363
|
+
const order = record.phases.map((p) => p.name);
|
|
364
|
+
const here = order.indexOf(running.name);
|
|
365
|
+
for (const name of outcome.skip ?? []) {
|
|
366
|
+
const i = order.indexOf(name);
|
|
367
|
+
const p = record.phases[i];
|
|
368
|
+
// A phase that runs again (after a blocked stop, say) may repeat its own skip.
|
|
369
|
+
if (p?.state === "skipped" && p.skipped_by === running.name && i > here)
|
|
370
|
+
continue;
|
|
371
|
+
if (!p || i <= here || p.state !== "pending") {
|
|
372
|
+
throw new RecordError(`cannot skip ${name}: only a later, pending phase of this work can be skipped`);
|
|
373
|
+
}
|
|
374
|
+
p.state = "skipped";
|
|
375
|
+
p.note = outcome.note;
|
|
376
|
+
p.skipped_by = running.name;
|
|
377
|
+
}
|
|
378
|
+
if (outcome.result === "cards" || outcome.result === "not-a-fix") {
|
|
379
|
+
const named = outcome.cards ?? [];
|
|
380
|
+
for (const file of named) {
|
|
381
|
+
const card = cards.find((c) => c.file === file);
|
|
382
|
+
if (!card)
|
|
383
|
+
throw new RecordError(`card ${file} is not in the run's cards directory`);
|
|
384
|
+
if (card.answered)
|
|
385
|
+
throw new RecordError(`card ${file} is already answered`);
|
|
386
|
+
}
|
|
387
|
+
const stopFix = cards.find((c) => named.includes(c.file) && c.type === "stop-fix");
|
|
388
|
+
if (outcome.result === "not-a-fix" && !stopFix) {
|
|
389
|
+
throw new RecordError("not-a-fix needs a stop-fix card among `cards`");
|
|
390
|
+
}
|
|
391
|
+
if (stopFix?.offers?.includes("to-case") &&
|
|
392
|
+
!(stopFix.case && record.drafts.includes(stopFix.case))) {
|
|
393
|
+
throw new RecordError(`card ${stopFix.file} offers to-case: name its case draft in \`case:\` and list it in \`drafts\``);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (running.name === "review" && (outcome.result === "done" || outcome.result === "redo")) {
|
|
397
|
+
record.review.rounds += 1;
|
|
398
|
+
}
|
|
399
|
+
const finish = (state) => {
|
|
400
|
+
running.state = state;
|
|
401
|
+
running.note = outcome.note;
|
|
402
|
+
// Whatever the result, the phase's locks are released by `run record`.
|
|
403
|
+
delete running.needs;
|
|
404
|
+
delete running.locks;
|
|
405
|
+
if (state === "done") {
|
|
406
|
+
running.commit = runner.commit;
|
|
407
|
+
running.finished = runner.now;
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
delete running.started;
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
const stop = (stopped, note) => {
|
|
414
|
+
record.active = null;
|
|
415
|
+
return { record: bump(record), result: { continue: false, stopped, note } };
|
|
416
|
+
};
|
|
417
|
+
switch (outcome.result) {
|
|
418
|
+
case "needs-environment": {
|
|
419
|
+
if (!phaseDef(record.work.mode, running.name)?.locks) {
|
|
420
|
+
throw new RecordError(`${running.name} takes no shared environment`);
|
|
421
|
+
}
|
|
422
|
+
if (running.locks?.length) {
|
|
423
|
+
throw new RecordError(`${running.name} already holds ${running.locks.join(", ")}: run the checks, then record their outcome`);
|
|
424
|
+
}
|
|
425
|
+
const needs = outcome.environments ?? [];
|
|
426
|
+
finish("pending");
|
|
427
|
+
running.needs = needs;
|
|
428
|
+
record.events.push({
|
|
429
|
+
at: runner.now,
|
|
430
|
+
phase: running.name,
|
|
431
|
+
what: "needs-environment",
|
|
432
|
+
note: needs.join(", "),
|
|
433
|
+
});
|
|
434
|
+
return { record: bump(record), result: { continue: true } };
|
|
435
|
+
}
|
|
436
|
+
case "done":
|
|
437
|
+
finish("done");
|
|
438
|
+
return { record: bump(record), result: { continue: true } };
|
|
439
|
+
case "redo": {
|
|
440
|
+
const target = outcome.redo;
|
|
441
|
+
const from = order.indexOf(target);
|
|
442
|
+
if (from < 0)
|
|
443
|
+
throw new RecordError(`cannot redo ${target}: not a phase of this work`);
|
|
444
|
+
if (from > here) {
|
|
445
|
+
throw new RecordError(`cannot redo ${target}: it comes after ${running.name}`);
|
|
446
|
+
}
|
|
447
|
+
finish("done");
|
|
448
|
+
if (running.name === "review" && record.review.rounds >= record.review.max) {
|
|
449
|
+
// The round budget is spent: the loop stops and reports.
|
|
450
|
+
record.events.push({
|
|
451
|
+
at: runner.now,
|
|
452
|
+
phase: "review",
|
|
453
|
+
what: "review-budget-spent",
|
|
454
|
+
note: `${record.review.rounds} of ${record.review.max} rounds; not redoing ${target}: ${outcome.note}`,
|
|
455
|
+
});
|
|
456
|
+
return { record: bump(record), result: { continue: true } };
|
|
457
|
+
}
|
|
458
|
+
for (const p of record.phases.slice(from)) {
|
|
459
|
+
if (p.state === "done")
|
|
460
|
+
p.state = "redo";
|
|
461
|
+
// A skip decided by a phase that is redone is decided again.
|
|
462
|
+
else if (p.state === "skipped" && p.skipped_by && order.indexOf(p.skipped_by) >= from) {
|
|
463
|
+
p.state = "pending";
|
|
464
|
+
delete p.skipped_by;
|
|
465
|
+
delete p.note;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
record.events.push({
|
|
469
|
+
at: runner.now,
|
|
470
|
+
phase: running.name,
|
|
471
|
+
what: "redo",
|
|
472
|
+
note: `${target}: ${outcome.note}`,
|
|
473
|
+
});
|
|
474
|
+
return { record: bump(record), result: { continue: true } };
|
|
475
|
+
}
|
|
476
|
+
case "cards":
|
|
477
|
+
case "not-a-fix":
|
|
478
|
+
finish(outcome.result === "cards" && phaseDef(record.work.mode, running.name)?.resumesAfterCards
|
|
479
|
+
? "pending"
|
|
480
|
+
: "done");
|
|
481
|
+
record.events.push({
|
|
482
|
+
at: runner.now,
|
|
483
|
+
phase: running.name,
|
|
484
|
+
what: outcome.result === "cards" ? "card" : "not-a-fix",
|
|
485
|
+
note: (outcome.cards ?? []).join(", ") || outcome.note,
|
|
486
|
+
});
|
|
487
|
+
return stop(outcome.result === "cards" ? "decision" : "not-a-fix", outcome.note);
|
|
488
|
+
case "blocked":
|
|
489
|
+
finish("pending");
|
|
490
|
+
record.events.push({
|
|
491
|
+
at: runner.now,
|
|
492
|
+
phase: running.name,
|
|
493
|
+
what: "blocked",
|
|
494
|
+
note: outcome.blocker ?? "",
|
|
495
|
+
});
|
|
496
|
+
return stop("blocked", outcome.blocker ?? outcome.note);
|
|
497
|
+
case "waiting":
|
|
498
|
+
finish("pending");
|
|
499
|
+
record.events.push({
|
|
500
|
+
at: runner.now,
|
|
501
|
+
phase: running.name,
|
|
502
|
+
what: "waiting",
|
|
503
|
+
note: outcome.note,
|
|
504
|
+
});
|
|
505
|
+
return stop("waiting", outcome.note);
|
|
506
|
+
}
|
|
507
|
+
}
|