@chernabog/sdk 0.7.0 → 0.8.1
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/README.md +16 -12
- package/index.ts +5 -0
- package/package.json +8 -3
- package/src/ci.ts +82 -0
- package/src/comment.ts +1 -3
- package/src/default-prompts.ts +24 -1
- package/src/epic.ts +568 -0
- package/src/hybrid.ts +67 -53
- package/src/implement.ts +731 -149
- package/src/index.ts +15 -13
- package/src/plan-labels.ts +9 -0
- package/src/plan.ts +48 -39
- package/src/prompt.ts +1 -3
- package/src/pull-request.ts +234 -39
- package/src/review.ts +236 -56
- package/src/routing.ts +113 -0
- package/src/settings.ts +361 -0
- package/src/stack.ts +534 -0
- package/src/still-valid.ts +142 -0
- package/src/claims.ts +0 -169
- package/src/errors.ts +0 -222
- package/src/flow.ts +0 -325
- package/src/json.ts +0 -21
- package/src/markers.ts +0 -103
- package/src/paths.ts +0 -46
- package/src/roles.ts +0 -30
- package/src/services.ts +0 -505
- package/src/stage.ts +0 -25
package/src/epic.ts
ADDED
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
// The building blocks of the `epic` recipe: from an epic's sub-issues and the
|
|
2
|
+
// issues that block each, which open tasks may start, on the base branch or
|
|
3
|
+
// stacked on the work branch of the one task that blocks them, and why the
|
|
4
|
+
// rest wait. A closed blocker counts as done: the epic run closes a task only
|
|
5
|
+
// once its work reaches the base branch.
|
|
6
|
+
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
activity,
|
|
11
|
+
Claims,
|
|
12
|
+
GitHub,
|
|
13
|
+
type LinkedIssue,
|
|
14
|
+
planNodeMarker,
|
|
15
|
+
type PullRequest,
|
|
16
|
+
Repo,
|
|
17
|
+
Run,
|
|
18
|
+
RunLog,
|
|
19
|
+
Skip,
|
|
20
|
+
type WorkItem,
|
|
21
|
+
} from "@chernabog/core";
|
|
22
|
+
import * as Effect from "effect/Effect";
|
|
23
|
+
import * as Option from "effect/Option";
|
|
24
|
+
import * as Schema from "effect/Schema";
|
|
25
|
+
|
|
26
|
+
import { issueItem, issueOf, routed } from "./implement.ts";
|
|
27
|
+
import { type Kind, VERIFY } from "./plan.ts";
|
|
28
|
+
import { LABELS as PLAN_LABELS } from "./plan-labels.ts";
|
|
29
|
+
import * as Settings from "./settings.ts";
|
|
30
|
+
import { epicValidity } from "./still-valid.ts";
|
|
31
|
+
|
|
32
|
+
export type { Kind } from "./plan.ts";
|
|
33
|
+
|
|
34
|
+
// A task that may start now, on `base`: the flow's integration base
|
|
35
|
+
// (Settings.base), or the work branch of the task it stacks on.
|
|
36
|
+
export type Ready = {
|
|
37
|
+
readonly state: "ready";
|
|
38
|
+
readonly base: string;
|
|
39
|
+
readonly stackedOn: Option.Option<number>;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// A task that waits, and why, as a WorkItem's `held` says it.
|
|
43
|
+
export type Held = { readonly state: "held"; readonly reason: string };
|
|
44
|
+
|
|
45
|
+
export type Task = { readonly number: number; readonly title: string; readonly kind: Kind } & (
|
|
46
|
+
Ready | Held
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
// Each open task of the epic, in GitHub's order, and its verify task apart.
|
|
50
|
+
export type Tasks = { readonly tasks: ReadonlyArray<Task>; readonly verify: Option.Option<Task> };
|
|
51
|
+
|
|
52
|
+
// Who takes a task, by the labels the plan gave it: auto:needs-human for a
|
|
53
|
+
// person, auto:eligible for an agent, and neither for a person with an agent.
|
|
54
|
+
export const kindOf = (labels: ReadonlyArray<string>, names: Settings.LabelNames): Kind => {
|
|
55
|
+
if (labels.includes(names.needsHuman)) {
|
|
56
|
+
return "human";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (labels.includes(names.eligible)) {
|
|
60
|
+
return "auto";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return "hybrid";
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// Whether a sub-issue is the verify task the plan for this epic added.
|
|
67
|
+
export const isVerify = (epic: number, issue: LinkedIssue) =>
|
|
68
|
+
Option.exists(
|
|
69
|
+
planNodeMarker.read(issue.body),
|
|
70
|
+
(marker) => marker.spec === epic && marker.key === VERIFY,
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const waitsOn = (blockers: ReadonlyArray<number>) =>
|
|
74
|
+
`waits on ${blockers.map((blocker) => `#${blocker}`).join(", ")}`;
|
|
75
|
+
|
|
76
|
+
const ready = (base: string, stackedOn: Option.Option<number>): Ready => ({
|
|
77
|
+
state: "ready",
|
|
78
|
+
base,
|
|
79
|
+
stackedOn,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const held = (reason: string): Held => ({ state: "held", reason });
|
|
83
|
+
|
|
84
|
+
// The tasks an epic run started, whose claim took their queue label off, so
|
|
85
|
+
// they no longer read as auto: a task stacks on one as on an auto task.
|
|
86
|
+
export type TasksOptions = { readonly started?: ReadonlyArray<number> };
|
|
87
|
+
|
|
88
|
+
// The epic's open tasks, each ready or held: ready on the base branch with no
|
|
89
|
+
// open blocker; stacked on its blocker's work branch when that one open
|
|
90
|
+
// blocker is an auto task of this epic (or one `started`) with an open pull
|
|
91
|
+
// request, and the stack stays within limits.stackDepth; held otherwise. The
|
|
92
|
+
// verify task is ready only once every other sub-issue is closed.
|
|
93
|
+
export const tasks = (epic: number, options: TasksOptions = {}) =>
|
|
94
|
+
Effect.gen(function* () {
|
|
95
|
+
const github = yield* GitHub;
|
|
96
|
+
const labelNames = yield* Settings.labels;
|
|
97
|
+
const stackDepth = yield* Settings.stackDepth;
|
|
98
|
+
const base = yield* Settings.base;
|
|
99
|
+
const open = (yield* github.subIssues(epic)).filter((issue) => issue.state === "open");
|
|
100
|
+
const verify = open.find((issue) => isVerify(epic, issue));
|
|
101
|
+
const work = open.filter((issue) => !isVerify(epic, issue));
|
|
102
|
+
|
|
103
|
+
// The epic's open auto tasks, and those started, the only ones another task stacks on.
|
|
104
|
+
const auto = new Set(
|
|
105
|
+
work
|
|
106
|
+
.filter(
|
|
107
|
+
(issue) =>
|
|
108
|
+
kindOf(issue.labels, labelNames) === "auto" ||
|
|
109
|
+
(options.started ?? []).includes(issue.number),
|
|
110
|
+
)
|
|
111
|
+
.map((issue) => issue.number),
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
const byBranch = new Map(work.map((issue) => [Claims.branchFor(issue.number), issue.number]));
|
|
115
|
+
const pulls = new Map<number, Option.Option<PullRequest>>();
|
|
116
|
+
|
|
117
|
+
// The open pull request from a task's work branch, asked once a call.
|
|
118
|
+
const pullOf = (task: number) =>
|
|
119
|
+
Effect.gen(function* () {
|
|
120
|
+
const known = Option.fromUndefinedOr(pulls.get(task));
|
|
121
|
+
|
|
122
|
+
if (Option.isSome(known)) {
|
|
123
|
+
return known.value;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const found = Option.filter(
|
|
127
|
+
yield* github.pullRequestForBranch(Claims.branchFor(task)),
|
|
128
|
+
(pull) => pull.state === "open",
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
pulls.set(task, found);
|
|
132
|
+
|
|
133
|
+
return found;
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// The layers in the stack `pull` tops: itself, and each open task's pull
|
|
137
|
+
// request below it, by its base branch.
|
|
138
|
+
const depthOf = (pull: PullRequest) =>
|
|
139
|
+
Effect.gen(function* () {
|
|
140
|
+
let depth = 1;
|
|
141
|
+
let top = pull;
|
|
142
|
+
|
|
143
|
+
for (let hops = 0; hops < work.length; hops++) {
|
|
144
|
+
const below = Option.fromUndefinedOr(byBranch.get(top.baseRef));
|
|
145
|
+
|
|
146
|
+
if (Option.isNone(below)) {
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const next = yield* pullOf(below.value);
|
|
151
|
+
|
|
152
|
+
if (Option.isNone(next)) {
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
depth += 1;
|
|
157
|
+
top = next.value;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return depth;
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const stateOf = (task: LinkedIssue) =>
|
|
164
|
+
Effect.gen(function* () {
|
|
165
|
+
const blockers = (yield* github.blockedBy(task.number)).filter(
|
|
166
|
+
(blocker) => blocker.state === "open",
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
if (blockers.length === 0) {
|
|
170
|
+
return ready(base, Option.none());
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (blockers.length > 1) {
|
|
174
|
+
return held(waitsOn(blockers.map((blocker) => blocker.number)));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const [blocker] = blockers;
|
|
178
|
+
|
|
179
|
+
if (!auto.has(blocker.number)) {
|
|
180
|
+
return held(waitsOn([blocker.number]));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const pull = yield* pullOf(blocker.number);
|
|
184
|
+
|
|
185
|
+
if (Option.isNone(pull)) {
|
|
186
|
+
return held(waitsOn([blocker.number]));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if ((yield* depthOf(pull.value)) + 1 > stackDepth) {
|
|
190
|
+
return held(`waits for #${blocker.number}'s stack to merge`);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return ready(Claims.branchFor(blocker.number), Option.some(blocker.number));
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const taskOf = (issue: LinkedIssue, state: Ready | Held): Task => ({
|
|
197
|
+
number: issue.number,
|
|
198
|
+
title: issue.title,
|
|
199
|
+
kind: kindOf(issue.labels, labelNames),
|
|
200
|
+
...state,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const found: Array<Task> = [];
|
|
204
|
+
|
|
205
|
+
for (const issue of work) {
|
|
206
|
+
found.push(taskOf(issue, yield* stateOf(issue)));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const verifyState = (issue: LinkedIssue) => {
|
|
210
|
+
const others = open.filter((other) => other.number !== issue.number);
|
|
211
|
+
|
|
212
|
+
if (others.length === 0) {
|
|
213
|
+
return taskOf(issue, ready(base, Option.none()));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return taskOf(issue, held(waitsOn(others.map((other) => other.number))));
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
tasks: found,
|
|
221
|
+
verify: Option.map(Option.fromUndefinedOr(verify), verifyState),
|
|
222
|
+
} satisfies Tasks;
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// Claims the epic for this run with its claim ref (Claims.epic), or skips the
|
|
226
|
+
// run when another run holds it. Safe to repeat. A flow lets it go with
|
|
227
|
+
// `Claims.releasing(Claims.epic(epic))`.
|
|
228
|
+
export const claim = (epic: number) =>
|
|
229
|
+
activity(
|
|
230
|
+
"claim-epic",
|
|
231
|
+
Effect.gen(function* () {
|
|
232
|
+
const item = Claims.epic(epic);
|
|
233
|
+
const claimed = yield* Claims.acquire(item);
|
|
234
|
+
|
|
235
|
+
if (!claimed.acquired) {
|
|
236
|
+
return yield* new Skip({
|
|
237
|
+
reason: `#${epic} is already claimed (${Claims.describeHolder(item, claimed.holder)})`,
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}),
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
// Whether the epic is still this run's, for a flow's stillValid: it is open,
|
|
244
|
+
// and no other run holds its claim. A run whose epic is gone ends as superseded.
|
|
245
|
+
export const stillValid = (item: WorkItem) => issueOf(item).pipe(Effect.flatMap(epicValidity));
|
|
246
|
+
|
|
247
|
+
// Where one of the epic's sub-issues stands, as the epic's status comment
|
|
248
|
+
// lists it: ready to start (on `base`, `stackedOn` a task), held with its
|
|
249
|
+
// reason, in review as `pullRequest`, a person's (with the reason), or done:
|
|
250
|
+
// merged, or closed without a merge.
|
|
251
|
+
export const Layer = Schema.Struct({
|
|
252
|
+
number: Schema.Finite,
|
|
253
|
+
title: Schema.String,
|
|
254
|
+
state: Schema.Literals(["ready", "held", "in-review", "person", "merged", "closed"]),
|
|
255
|
+
base: Schema.optionalKey(Schema.String),
|
|
256
|
+
stackedOn: Schema.optionalKey(Schema.Finite),
|
|
257
|
+
reason: Schema.optionalKey(Schema.String),
|
|
258
|
+
pullRequest: Schema.optionalKey(Schema.Finite),
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
export type Layer = typeof Layer.Type;
|
|
262
|
+
|
|
263
|
+
// Every sub-issue of the epic as a layer, in GitHub's order, and the
|
|
264
|
+
// fingerprint of the epic it was read from.
|
|
265
|
+
export const Progress = Schema.Struct({
|
|
266
|
+
layers: Schema.Array(Layer),
|
|
267
|
+
fingerprint: Schema.String,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
export type Progress = typeof Progress.Type;
|
|
271
|
+
|
|
272
|
+
// Whether every sub-issue of the epic is closed.
|
|
273
|
+
export const finished = (progress: Progress) =>
|
|
274
|
+
progress.layers.every((layer) => layer.state === "merged" || layer.state === "closed");
|
|
275
|
+
|
|
276
|
+
// The layers the run may start now.
|
|
277
|
+
export const startable = (progress: Progress) =>
|
|
278
|
+
progress.layers.filter((layer) => layer.state === "ready");
|
|
279
|
+
|
|
280
|
+
// A layer's state in a few words: `ready`, `stacked on #12`, `held: waits on
|
|
281
|
+
// #12, #13`, `in review: #31`, `merged`.
|
|
282
|
+
export const describeLayer = (layer: Layer) => {
|
|
283
|
+
const on = Option.match(Option.fromUndefinedOr(layer.stackedOn), {
|
|
284
|
+
onNone: () => "",
|
|
285
|
+
onSome: (below) => `, stacked on #${below}`,
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
switch (layer.state) {
|
|
289
|
+
case "ready":
|
|
290
|
+
return Option.match(Option.fromUndefinedOr(layer.stackedOn), {
|
|
291
|
+
onNone: () => "ready",
|
|
292
|
+
onSome: (below) => `stacked on #${below}`,
|
|
293
|
+
});
|
|
294
|
+
case "held":
|
|
295
|
+
return `held: ${layer.reason ?? ""}`;
|
|
296
|
+
case "in-review":
|
|
297
|
+
return `in review: #${layer.pullRequest ?? 0}${on}`;
|
|
298
|
+
case "person":
|
|
299
|
+
return `a person's: ${layer.reason ?? ""}`;
|
|
300
|
+
default:
|
|
301
|
+
return layer.state;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
// What a sub-issue the run doesn't implement waits on: answers to its
|
|
306
|
+
// questions, a person, or a person with an agent.
|
|
307
|
+
const personReason = (labels: ReadonlyArray<string>, names: Settings.LabelNames) => {
|
|
308
|
+
if (labels.includes(names.needsClarification)) {
|
|
309
|
+
return "waits for answers to its questions";
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (labels.includes(names.needsHuman)) {
|
|
313
|
+
return "needs a person";
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return "for a person with an agent (`chernabog start`)";
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
// The pull request from a task's work branch, open or not.
|
|
320
|
+
const pullFor = (task: number) =>
|
|
321
|
+
GitHub.use((github) => github.pullRequestForBranch(Claims.branchFor(task)));
|
|
322
|
+
|
|
323
|
+
const digest = (text: string) => createHash("sha256").update(text).digest("hex").slice(0, 16);
|
|
324
|
+
|
|
325
|
+
// A digest of what the epic's run waits on: each sub-issue's state and kind,
|
|
326
|
+
// and each layer's pull request: its state, head and reviews. A push, a
|
|
327
|
+
// review, a merge or a close changes it, as does a task closed or relabelled.
|
|
328
|
+
export const fingerprint = (epic: number) =>
|
|
329
|
+
Effect.gen(function* () {
|
|
330
|
+
const github = yield* GitHub;
|
|
331
|
+
const labelNames = yield* Settings.labels;
|
|
332
|
+
const parts: Array<string> = [];
|
|
333
|
+
|
|
334
|
+
for (const issue of yield* github.subIssues(epic)) {
|
|
335
|
+
parts.push(`#${issue.number}:${issue.state}:${kindOf(issue.labels, labelNames)}`);
|
|
336
|
+
|
|
337
|
+
for (const pull of Option.toArray(yield* pullFor(issue.number))) {
|
|
338
|
+
const reviews = yield* github.reviews(pull.number);
|
|
339
|
+
parts.push(`pr${pull.number}:${pull.state}:${pull.headSha}:${reviews.length}`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return digest(parts.join("\n"));
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
// Where each of the epic's sub-issues stands, for this run: the tasks whose
|
|
347
|
+
// claim it holds are its own, in review once their pull request is open; an
|
|
348
|
+
// open task it hasn't started and that isn't an agent's is a person's; the
|
|
349
|
+
// rest are ready or held, as `tasks` says, stacking on the run's own too.
|
|
350
|
+
export const progressOf = (epic: number) =>
|
|
351
|
+
Effect.gen(function* () {
|
|
352
|
+
const github = yield* GitHub;
|
|
353
|
+
const labelNames = yield* Settings.labels;
|
|
354
|
+
const run = yield* Run;
|
|
355
|
+
const all = yield* github.subIssues(epic);
|
|
356
|
+
const started: Array<number> = [];
|
|
357
|
+
|
|
358
|
+
for (const issue of all.filter((sub) => sub.state === "open")) {
|
|
359
|
+
if (
|
|
360
|
+
Option.exists(
|
|
361
|
+
yield* Claims.holder(Claims.issue(issue.number)),
|
|
362
|
+
(holder) => holder.runId === run.runId,
|
|
363
|
+
)
|
|
364
|
+
) {
|
|
365
|
+
started.push(issue.number);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const found = yield* tasks(epic, { started });
|
|
370
|
+
|
|
371
|
+
const byNumber = new Map(
|
|
372
|
+
[...found.tasks, ...Option.toArray(found.verify)].map((task) => [task.number, task]),
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
const byBranch = new Map(all.map((issue) => [Claims.branchFor(issue.number), issue.number]));
|
|
376
|
+
|
|
377
|
+
// Where an open sub-issue without an open pull request stands.
|
|
378
|
+
const waiting = (issue: LinkedIssue, head: Pick<Layer, "number" | "title">) =>
|
|
379
|
+
Option.map(Option.fromUndefinedOr(byNumber.get(issue.number)), (task): Layer => {
|
|
380
|
+
if (task.kind !== "auto" && !started.includes(issue.number)) {
|
|
381
|
+
return { ...head, state: "person", reason: personReason(issue.labels, labelNames) };
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (task.state === "held") {
|
|
385
|
+
return { ...head, state: "held", reason: task.reason };
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return Option.match(task.stackedOn, {
|
|
389
|
+
onNone: (): Layer => ({ ...head, state: "ready", base: task.base }),
|
|
390
|
+
onSome: (below): Layer => ({
|
|
391
|
+
...head,
|
|
392
|
+
state: "ready",
|
|
393
|
+
base: task.base,
|
|
394
|
+
stackedOn: below,
|
|
395
|
+
}),
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
// Where a sub-issue stands, from its pull request if it has one.
|
|
400
|
+
const layerOf = (issue: LinkedIssue, pull: Option.Option<PullRequest>) => {
|
|
401
|
+
const head = { number: issue.number, title: issue.title };
|
|
402
|
+
|
|
403
|
+
if (issue.state === "closed") {
|
|
404
|
+
return Option.some(
|
|
405
|
+
Option.match(
|
|
406
|
+
Option.filter(pull, (closed) => closed.state === "merged"),
|
|
407
|
+
{
|
|
408
|
+
onNone: (): Layer => ({ ...head, state: "closed" }),
|
|
409
|
+
onSome: (merged): Layer => ({ ...head, state: "merged", pullRequest: merged.number }),
|
|
410
|
+
},
|
|
411
|
+
),
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return Option.match(
|
|
416
|
+
Option.filter(pull, (open) => open.state === "open"),
|
|
417
|
+
{
|
|
418
|
+
onNone: () => waiting(issue, head),
|
|
419
|
+
onSome: (open) =>
|
|
420
|
+
Option.some(
|
|
421
|
+
Option.match(Option.fromUndefinedOr(byBranch.get(open.baseRef)), {
|
|
422
|
+
onNone: (): Layer => ({ ...head, state: "in-review", pullRequest: open.number }),
|
|
423
|
+
onSome: (below): Layer => ({
|
|
424
|
+
...head,
|
|
425
|
+
state: "in-review",
|
|
426
|
+
pullRequest: open.number,
|
|
427
|
+
stackedOn: below,
|
|
428
|
+
}),
|
|
429
|
+
}),
|
|
430
|
+
),
|
|
431
|
+
},
|
|
432
|
+
);
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
const layers: Array<Layer> = [];
|
|
436
|
+
|
|
437
|
+
for (const issue of all) {
|
|
438
|
+
for (const layer of Option.toArray(layerOf(issue, yield* pullFor(issue.number)))) {
|
|
439
|
+
layers.push(layer);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
return { layers, fingerprint: yield* fingerprint(epic) } satisfies Progress;
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
// Reads where the epic stands, as the step `epic-tasks-<round>`, and writes
|
|
447
|
+
// each held task and its reason to the run log.
|
|
448
|
+
export const progress = (epic: number, round: number) =>
|
|
449
|
+
activity(
|
|
450
|
+
`epic-tasks-${round}`,
|
|
451
|
+
Effect.gen(function* () {
|
|
452
|
+
const log = yield* RunLog;
|
|
453
|
+
const read = yield* progressOf(epic);
|
|
454
|
+
|
|
455
|
+
for (const layer of read.layers.filter((each) => each.state === "held")) {
|
|
456
|
+
yield* log.log(`#${layer.number} is held: ${layer.reason ?? ""}`);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
return read;
|
|
460
|
+
}),
|
|
461
|
+
Progress,
|
|
462
|
+
);
|
|
463
|
+
|
|
464
|
+
// The status comment's marker: the fingerprint of the epic it was written
|
|
465
|
+
// from, so the trigger can tell when a layer has changed since.
|
|
466
|
+
export const StatusMarker = Schema.Struct({ fingerprint: Schema.String });
|
|
467
|
+
|
|
468
|
+
export type StatusMarker = typeof StatusMarker.Type;
|
|
469
|
+
|
|
470
|
+
const STATUS_PATTERN = /<!-- chernabog-epic-status (\{.*?\}) -->/;
|
|
471
|
+
const statusCodec = Schema.fromJsonString(StatusMarker);
|
|
472
|
+
|
|
473
|
+
export const statusMarker = {
|
|
474
|
+
write: (value: StatusMarker) =>
|
|
475
|
+
`<!-- chernabog-epic-status ${Schema.encodeSync(statusCodec)(value)} -->`,
|
|
476
|
+
read: (body: string): Option.Option<StatusMarker> =>
|
|
477
|
+
Option.fromNullishOr(body.match(STATUS_PATTERN)).pipe(
|
|
478
|
+
Option.flatMap((match) => Schema.decodeOption(statusCodec)(match[1])),
|
|
479
|
+
),
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
// The epic's status comment: each sub-issue and where it stands.
|
|
483
|
+
export const statusComment = (read: Progress) =>
|
|
484
|
+
[
|
|
485
|
+
"**Chernabog's epic run.** Where each task stands:",
|
|
486
|
+
"",
|
|
487
|
+
...read.layers.map((layer) => `- #${layer.number} ${layer.title}: ${describeLayer(layer)}`),
|
|
488
|
+
"",
|
|
489
|
+
statusMarker.write({ fingerprint: read.fingerprint }),
|
|
490
|
+
].join("\n");
|
|
491
|
+
|
|
492
|
+
// The persona's latest status comment on the epic, if it wrote one.
|
|
493
|
+
const latestStatus = (epic: number) =>
|
|
494
|
+
Effect.gen(function* () {
|
|
495
|
+
const github = yield* GitHub;
|
|
496
|
+
const login = yield* github.login;
|
|
497
|
+
|
|
498
|
+
return Option.fromUndefinedOr(
|
|
499
|
+
(yield* github.comments(epic)).findLast(
|
|
500
|
+
(comment) => comment.author === login && Option.isSome(statusMarker.read(comment.body)),
|
|
501
|
+
),
|
|
502
|
+
);
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
// Writes where the epic stands into its one status comment, editing the one
|
|
506
|
+
// there or posting it the first time, as the step `epic-status-<round>`.
|
|
507
|
+
export const postStatus = (epic: number, round: number, read: Progress) =>
|
|
508
|
+
activity(
|
|
509
|
+
`epic-status-${round}`,
|
|
510
|
+
Effect.gen(function* () {
|
|
511
|
+
const github = yield* GitHub;
|
|
512
|
+
const repo = yield* Repo;
|
|
513
|
+
const body = statusComment(read);
|
|
514
|
+
|
|
515
|
+
yield* Option.match(yield* latestStatus(epic), {
|
|
516
|
+
onNone: () => github.comment(epic, body),
|
|
517
|
+
onSome: (found) =>
|
|
518
|
+
github
|
|
519
|
+
.request(
|
|
520
|
+
"PATCH",
|
|
521
|
+
`/repos/${repo.slug}/issues/comments/${found.id}`,
|
|
522
|
+
Option.some({ body }),
|
|
523
|
+
)
|
|
524
|
+
.pipe(Effect.asVoid),
|
|
525
|
+
});
|
|
526
|
+
}),
|
|
527
|
+
);
|
|
528
|
+
|
|
529
|
+
// The signal the trigger gives an epic whose layers changed since its run
|
|
530
|
+
// last wrote its status: a push, a review, a merge or a close.
|
|
531
|
+
export const LAYER_CHANGED = "layer-changed";
|
|
532
|
+
|
|
533
|
+
// Open epics (auto:epic) with an open sub-issue, oldest first, as work items
|
|
534
|
+
// keyed by the epic's issue; each whose fingerprint differs from its status
|
|
535
|
+
// comment's carries LAYER_CHANGED, which wakes its parked run. An epic whose
|
|
536
|
+
// last task closed since is reported only to wake its run, to end it. Each is
|
|
537
|
+
// routed by its assignees.
|
|
538
|
+
export const queued = Effect.gen(function* () {
|
|
539
|
+
const github = yield* GitHub;
|
|
540
|
+
const items: Array<WorkItem> = [];
|
|
541
|
+
|
|
542
|
+
for (const epic of yield* github.issuesLabelled(PLAN_LABELS.epic)) {
|
|
543
|
+
if (epic.state === "closed") {
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
const open = (yield* github.subIssues(epic.number)).some((task) => task.state === "open");
|
|
548
|
+
|
|
549
|
+
const recorded = Option.flatMap(yield* latestStatus(epic.number), (found) =>
|
|
550
|
+
statusMarker.read(found.body),
|
|
551
|
+
);
|
|
552
|
+
|
|
553
|
+
const changed =
|
|
554
|
+
Option.isSome(recorded) && recorded.value.fingerprint !== (yield* fingerprint(epic.number));
|
|
555
|
+
|
|
556
|
+
const item = yield* routed(epic, issueItem(epic));
|
|
557
|
+
|
|
558
|
+
if (changed && open) {
|
|
559
|
+
items.push({ ...item, signals: [LAYER_CHANGED] });
|
|
560
|
+
} else if (changed) {
|
|
561
|
+
items.push({ ...item, signals: [LAYER_CHANGED], wakeOnly: true });
|
|
562
|
+
} else if (open) {
|
|
563
|
+
items.push(item);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return items;
|
|
568
|
+
});
|