@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/src/index.ts CHANGED
@@ -1,27 +1,29 @@
1
+ import type * as Core from "@chernabog/core";
2
+
1
3
  import * as IssueSteps from "./issue.ts";
2
4
  import * as PullRequestSteps from "./pull-request.ts";
3
- import type * as Services from "./services.ts";
4
5
 
5
- export * as Claims from "./claims.ts";
6
- export * from "./errors.ts";
7
- export * from "./flow.ts";
6
+ // @chernabog/sdk: the blocks a recipe is written from, over core's ports and
7
+ // nothing else, and core itself, so a flow imports from one package. A repo
8
+ // pins its own version; the binary only provides core.
9
+ export * as Ci from "./ci.ts";
10
+ export * as Epic from "./epic.ts";
8
11
  export * as Hybrid from "./hybrid.ts";
9
12
  export * as Implement from "./implement.ts";
10
- export * from "./json.ts";
11
- export * from "./markers.ts";
12
- export * from "./paths.ts";
13
13
  export * as Plan from "./plan.ts";
14
14
  export * from "./prompt.ts";
15
15
  export * as Review from "./review.ts";
16
- export * from "./roles.ts";
17
- export * from "./services.ts";
18
- export * from "./stage.ts";
16
+ export * as Routing from "./routing.ts";
17
+ export * as Settings from "./settings.ts";
18
+ export * as Stack from "./stack.ts";
19
+ export * from "@chernabog/core";
19
20
 
20
- // PullRequest and Issue are each both a type and the steps on one.
21
+ // PullRequest and Issue are each both a type, core's, and the steps on one;
22
+ // these win over core's types of the same name.
21
23
  export const PullRequest = PullRequestSteps;
22
24
 
23
- export type PullRequest = Services.PullRequest;
25
+ export type PullRequest = Core.PullRequest;
24
26
 
25
27
  export const Issue = IssueSteps;
26
28
 
27
- export type Issue = Services.Issue;
29
+ export type Issue = Core.Issue;
@@ -0,0 +1,9 @@
1
+ // The labels of a plan's life on its spec: queued, proposed and waiting on a
2
+ // person, approved, and made into an epic. Plan.LABELS; here, apart, so
3
+ // implement's trigger can find epics without importing the plan recipe.
4
+ export const LABELS = {
5
+ plan: "auto:plan",
6
+ proposed: "auto:plan-proposed",
7
+ approved: "auto:plan-approved",
8
+ epic: "auto:epic",
9
+ } as const;
package/src/plan.ts CHANGED
@@ -5,42 +5,50 @@
5
5
  // blocks it. The agent answers with data only: each task's labels decide which
6
6
  // of the repo's flows picks it up.
7
7
 
8
+ import {
9
+ activity,
10
+ Agent,
11
+ awaitSignal,
12
+ type Comment,
13
+ type FlowLabel,
14
+ GitHub,
15
+ hasMarker,
16
+ type Issue,
17
+ type Json,
18
+ NeedsHuman,
19
+ planMarker,
20
+ planNodeMarker,
21
+ Repo,
22
+ Run,
23
+ StepFailed,
24
+ type WorkItem,
25
+ Worktree,
26
+ } from "@chernabog/core";
8
27
  import type * as Duration from "effect/Duration";
9
28
  import * as Effect from "effect/Effect";
10
29
  import * as Option from "effect/Option";
11
30
  import * as Schema from "effect/Schema";
12
31
 
13
- import { NeedsHuman, StepFailed } from "./errors.ts";
14
- import { activity, awaitSignal, type FlowLabel } from "./flow.ts";
15
32
  import {
16
33
  askForClarification as askImplementClarification,
17
34
  branchFor,
18
35
  issueItem,
36
+ issueOf,
19
37
  sessionIdFor,
20
38
  } from "./implement.ts";
21
- import type { Json } from "./json.ts";
22
- import { hasMarker, planMarker, planNodeMarker } from "./markers.ts";
39
+ import { LABELS } from "./plan-labels.ts";
23
40
  import { renderPrompt } from "./prompt.ts";
24
- import {
25
- Agent,
26
- type Comment,
27
- GitHub,
28
- type Issue,
29
- type Labels,
30
- Repo,
31
- Run,
32
- type WorkItem,
33
- Worktree,
34
- } from "./services.ts";
41
+ import * as Settings from "./settings.ts";
42
+ import { issueValidity } from "./still-valid.ts";
43
+
44
+ // Whether the spec is still this run's to plan, for a flow's stillValid: it
45
+ // is open, and no other run holds its claim.
46
+ export const stillValid = (item: WorkItem) =>
47
+ issueOf(item).pipe(Effect.flatMap((spec) => issueValidity(spec, { pullRequest: false })));
35
48
 
36
49
  // The labels of a plan's life on its spec: queued, proposed and waiting on a
37
50
  // person, approved, and made into an epic.
38
- export const LABELS = {
39
- plan: "auto:plan",
40
- proposed: "auto:plan-proposed",
41
- approved: "auto:plan-approved",
42
- epic: "auto:epic",
43
- } as const;
51
+ export { LABELS };
44
52
 
45
53
  // For a flow's `labels`, so `chernabog labels` creates them.
46
54
  export const flowLabels: ReadonlyArray<FlowLabel> = [
@@ -393,6 +401,8 @@ const specPrompt = (spec: number) =>
393
401
  Effect.gen(function* () {
394
402
  const github = yield* GitHub;
395
403
  const repo = yield* Repo;
404
+ const planLimits = yield* Settings.plans;
405
+ const rules = yield* Settings.rules;
396
406
  const details = yield* github.issue(spec);
397
407
  const comments = yield* github.trustedComments(spec);
398
408
 
@@ -404,10 +414,10 @@ const specPrompt = (spec: number) =>
404
414
  comments: comments
405
415
  .filter((comment) => !hasMarker(comment.body))
406
416
  .map((comment) => `${comment.author}: ${comment.body}`),
407
- base: repo.baseBranch,
408
- planNodes: repo.limits.planNodes,
409
- humanOnly: repo.humanOnly,
410
- notes: repo.notes,
417
+ base: yield* Settings.base,
418
+ planNodes: planLimits.nodes,
419
+ humanOnly: rules.humanOnly,
420
+ notes: rules.notes,
411
421
  });
412
422
  });
413
423
 
@@ -431,13 +441,12 @@ const askForPlan = <E, R>(name: string, spec: number, asking: Effect.Effect<Aski
431
441
  activity(
432
442
  name,
433
443
  Effect.gen(function* () {
434
- const repo = yield* Repo;
435
444
  const run = yield* Run;
436
445
  const { prompt, resume } = yield* asking;
437
446
 
438
447
  const worktree = yield* (yield* Worktree).checkout(
439
448
  planCheckout(spec),
440
- `origin/${repo.baseBranch}`,
449
+ `origin/${yield* Settings.base}`,
441
450
  );
442
451
 
443
452
  const result = yield* (yield* Agent).run({
@@ -490,19 +499,19 @@ const fix = (spec: number, round: number, attempt: number, problems: ReadonlyArr
490
499
  // to limits.planRounds times. A plan that still fails needs a person.
491
500
  export const checkedDraft = (spec: number, round: number, changes: Option.Option<string>) =>
492
501
  Effect.gen(function* () {
493
- const repo = yield* Repo;
502
+ const planLimits = yield* Settings.plans;
494
503
  let answer = yield* draft(spec, round, changes);
495
504
 
496
505
  for (let attempt = 1; answer.status === "plan"; attempt++) {
497
- const problems = checkPlan(answer.nodes, repo.limits.planNodes);
506
+ const problems = checkPlan(answer.nodes, planLimits.nodes);
498
507
 
499
508
  if (problems.length === 0) {
500
509
  return answer;
501
510
  }
502
511
 
503
- if (attempt > repo.limits.planRounds) {
512
+ if (attempt > planLimits.rounds) {
504
513
  return yield* new NeedsHuman({
505
- reason: `the plan still fails its checks after ${repo.limits.planRounds} attempts to fix it:\n\n${problems.map((problem) => `- ${problem}`).join("\n")}`,
514
+ reason: `the plan still fails its checks after ${planLimits.rounds} attempts to fix it:\n\n${problems.map((problem) => `- ${problem}`).join("\n")}`,
506
515
  });
507
516
  }
508
517
 
@@ -564,7 +573,7 @@ export const changesAsked = (spec: number, round: number) =>
564
573
  Schema.String,
565
574
  );
566
575
 
567
- const labelsFor = (kind: string, labels: Labels): ReadonlyArray<string> => {
576
+ const labelsFor = (kind: string, labels: Settings.LabelNames): ReadonlyArray<string> => {
568
577
  if (kind === "auto") {
569
578
  return [labels.eligible];
570
579
  }
@@ -651,7 +660,7 @@ export const create = (spec: number, plan: Plan) =>
651
660
  "create-issues",
652
661
  Effect.gen(function* () {
653
662
  const github = yield* GitHub;
654
- const repo = yield* Repo;
663
+ const labelNames = yield* Settings.labels;
655
664
  const made = yield* madeFor(spec);
656
665
 
657
666
  for (const node of plan) {
@@ -687,7 +696,7 @@ export const create = (spec: number, plan: Plan) =>
687
696
  }
688
697
 
689
698
  for (const node of plan) {
690
- for (const label of labelsFor(node.kind, repo.labels)) {
699
+ for (const label of labelsFor(node.kind, labelNames)) {
691
700
  yield* github.addLabel(yield* numberOf(node.key), label);
692
701
  }
693
702
  }
@@ -704,10 +713,10 @@ export const becomeEpic = (spec: number, plan: Plan, created: Created) =>
704
713
  "epic",
705
714
  Effect.gen(function* () {
706
715
  const github = yield* GitHub;
707
- const repo = yield* Repo;
716
+ const labelNames = yield* Settings.labels;
708
717
  yield* github.addLabel(spec, LABELS.epic);
709
718
 
710
- for (const label of [LABELS.approved, LABELS.proposed, repo.labels.inProgress]) {
719
+ for (const label of [LABELS.approved, LABELS.proposed, labelNames.inProgress]) {
711
720
  yield* github.removeLabel(spec, label);
712
721
  }
713
722
 
@@ -743,10 +752,10 @@ export const handToHuman = (spec: number, reason: string) =>
743
752
  "needs-human",
744
753
  Effect.gen(function* () {
745
754
  const github = yield* GitHub;
746
- const repo = yield* Repo;
747
- yield* github.addLabel(spec, repo.labels.needsHuman);
755
+ const labelNames = yield* Settings.labels;
756
+ yield* github.addLabel(spec, labelNames.needsHuman);
748
757
 
749
- for (const label of [LABELS.proposed, repo.labels.inProgress]) {
758
+ for (const label of [LABELS.proposed, labelNames.inProgress]) {
750
759
  yield* github.removeLabel(spec, label);
751
760
  }
752
761
 
package/src/prompt.ts CHANGED
@@ -3,14 +3,12 @@
3
3
  // flow's prompts are looked up in layers: the repo's prompts/<name>.md first,
4
4
  // then its package's, then the default the SDK carries, if any.
5
5
 
6
+ import { FlowPackage, Repo, StepFailed } from "@chernabog/core";
6
7
  import * as Effect from "effect/Effect";
7
8
  import * as FileSystem from "effect/FileSystem";
8
9
  import * as Option from "effect/Option";
9
10
  import * as Predicate from "effect/Predicate";
10
11
 
11
- import { StepFailed } from "./errors.ts";
12
- import { FlowPackage, Repo } from "./services.ts";
13
-
14
12
  export type PromptValue = string | number | ReadonlyArray<string> | Option.Option<string>;
15
13
 
16
14
  const render = (value: PromptValue): string => {
@@ -3,28 +3,37 @@
3
3
  // and handing it to a person. A flow that works on a pull request itself can
4
4
  // own it while it does, so the standalone flows leave it alone.
5
5
 
6
+ import {
7
+ activity,
8
+ Claims,
9
+ Git,
10
+ GitHub,
11
+ latestMarker,
12
+ type MergeMethod,
13
+ MergeRefused,
14
+ NeedsHuman,
15
+ type OwnerMarker,
16
+ ownerMarker,
17
+ pathsMatching,
18
+ type PullRequest,
19
+ PullRequestClosed,
20
+ reviewMarker,
21
+ Run,
22
+ Runs,
23
+ Skip,
24
+ Worktree,
25
+ } from "@chernabog/core";
6
26
  import * as Cause from "effect/Cause";
27
+ import * as DateTime from "effect/DateTime";
7
28
  import * as Effect from "effect/Effect";
8
29
  import * as Exit from "effect/Exit";
9
30
  import * as Option from "effect/Option";
10
31
  import * as Random from "effect/Random";
11
32
  import * as Schema from "effect/Schema";
12
33
 
13
- import * as Claims from "./claims.ts";
34
+ import * as Ci from "./ci.ts";
14
35
  import { commentStep } from "./comment.ts";
15
- import { MergeRefused, NeedsHuman, PullRequestClosed, Skip } from "./errors.ts";
16
- import { activity } from "./flow.ts";
17
- import { latestMarker, type OwnerMarker, ownerMarker, reviewMarker } from "./markers.ts";
18
- import { pathsMatching } from "./paths.ts";
19
- import {
20
- GitHub,
21
- type Labels,
22
- type MergeMethod,
23
- type PullRequest,
24
- Repo,
25
- Run,
26
- Runs,
27
- } from "./services.ts";
36
+ import * as Settings from "./settings.ts";
28
37
 
29
38
  // Marks the pull request as this run's: its claim ref, and a comment for people to read.
30
39
  export const own = (pullRequest: number) =>
@@ -50,7 +59,10 @@ export const own = (pullRequest: number) =>
50
59
 
51
60
  yield* (yield* GitHub).comment(
52
61
  pullRequest,
53
- `The \`${run.flow}\` run owns this pull request until it ends. ${marker}`,
62
+ [
63
+ ...Option.toArray(Option.map(claimed.takenOver, Claims.describeTakeover)),
64
+ `The \`${run.flow}\` run owns this pull request until it ends. ${marker}`,
65
+ ].join(" "),
54
66
  );
55
67
  }),
56
68
  );
@@ -138,13 +150,19 @@ export const ownerOf = (bodies: ReadonlyArray<string>) =>
138
150
  export type Owner = { readonly flow: string; readonly runId: string };
139
151
 
140
152
  // The run that owns the pull request: the holder of its claim ref, none when
141
- // this machine knows that run has ended. Migration: while runs that owned a
153
+ // this machine knows that run has ended, or its lease expired, so another run
154
+ // may take it over. Migration: while runs that owned a
142
155
  // pull request by comment alone may be in flight, one with no claim ref falls
143
156
  // back to the latest owner marker that names no ref (ownerOf). Drop the
144
157
  // fallback a release after owners became claim refs.
145
158
  export const owner = (pullRequest: number) =>
146
159
  Effect.gen(function* () {
147
160
  const held = yield* Claims.holder(Claims.pullRequest(pullRequest));
161
+ const now = yield* DateTime.now;
162
+
163
+ if (Option.exists(held, (found) => Claims.expired(found, now))) {
164
+ return Option.none<Owner>();
165
+ }
148
166
 
149
167
  if (Option.isSome(held)) {
150
168
  if (yield* knownEnded(held.value.runId)) {
@@ -163,21 +181,163 @@ export const owner = (pullRequest: number) =>
163
181
  );
164
182
  });
165
183
 
166
- export type MergeOptions = { readonly method: MergeMethod };
184
+ // id: tells apart two tries at merging one pull request in a run, as an epic
185
+ // run's rounds do (`merge-pr-12#land-3`); a refused try is saved like any step.
186
+ export type MergeOptions = { readonly method: MergeMethod; readonly id?: string };
167
187
 
168
188
  const refuse = (pullRequest: number, reason: string) => new MergeRefused({ pullRequest, reason });
169
189
 
170
- // Merges the pull request, only when that is safe: it is open, its required
171
- // checks are green on the current head, the latest review approved that very
172
- // head, it touches no humanOnlyPaths, and GitHub's rules for the repo allow it.
190
+ const short = (sha: string) => sha.slice(0, 7);
191
+
192
+ // Whether the head only brought the pull request up to date with its base
193
+ // since `reviewedSha`, and if not, which commit did more.
194
+ export type BaseMerges =
195
+ { readonly only: true } | { readonly only: false; readonly reason: string };
196
+
197
+ const notOnly = (reason: string): BaseMerges => ({ only: false, reason });
198
+
199
+ // Whether every commit between `reviewedSha` and the pull request's head, along
200
+ // its first parents, is a merge of the base branch (baseRef) that git would
201
+ // have made by itself: two parents, the second on the base, and the tree a
202
+ // clean automatic merge of the two gives. A merge where someone resolved a
203
+ // conflict or added edits of their own is not, so the change still needs a
204
+ // review. Reads the history in a detached checkout of the head.
205
+ export const onlyMergedBase = (pull: PullRequest, reviewedSha: string) =>
206
+ Effect.gen(function* () {
207
+ const git = yield* Git;
208
+ const base = pull.baseRef;
209
+
210
+ const worktree = yield* (yield* Worktree).checkout(
211
+ `merge-pr-${pull.number}`,
212
+ `origin/${pull.headRef}`,
213
+ );
214
+
215
+ // The head by its sha, not origin/<headRef>, so the walk checks the very
216
+ // commit merge asks GitHub to merge, even if the branch moved since.
217
+ yield* git.run(
218
+ [
219
+ "fetch",
220
+ "--quiet",
221
+ "origin",
222
+ `+refs/heads/${base}:refs/remotes/origin/${base}`,
223
+ pull.headSha,
224
+ ],
225
+ worktree,
226
+ );
227
+
228
+ const reviewedKnown = yield* git
229
+ .run(["rev-parse", "--verify", "--quiet", `${reviewedSha}^{commit}`], worktree)
230
+ .pipe(
231
+ Effect.as(true),
232
+ Effect.catchTag("GitError", () => Effect.succeed(false)),
233
+ );
234
+
235
+ if (!reviewedKnown) {
236
+ return notOnly(`the reviewed commit \`${short(reviewedSha)}\` is no longer in its history`);
237
+ }
238
+
239
+ // Newest first: each commit and its parents.
240
+ const commits = (yield* git.run(
241
+ ["rev-list", "--first-parent", "--parents", pull.headSha, `^${reviewedSha}`],
242
+ worktree,
243
+ ))
244
+ .split("\n")
245
+ .filter((line) => line.trim().length > 0)
246
+ .map((line) => line.trim().split(/\s+/));
247
+
248
+ for (const [commit = "", ...parents] of commits) {
249
+ const [first = "", second = ""] = parents;
250
+
251
+ if (parents.length !== 2) {
252
+ return notOnly(`\`${short(commit)}\` is not a merge of \`${base}\``);
253
+ }
254
+
255
+ const onBase = yield* git.run(
256
+ [
257
+ "for-each-ref",
258
+ "--format=%(refname)",
259
+ "--contains",
260
+ second,
261
+ `refs/remotes/origin/${base}`,
262
+ ],
263
+ worktree,
264
+ );
265
+
266
+ if (onBase.trim().length === 0) {
267
+ return notOnly(`\`${short(commit)}\` is not a merge of \`${base}\``);
268
+ }
269
+
270
+ // merge-tree fails on a conflict, which only a person could have resolved.
271
+ const clean = yield* git.run(["merge-tree", "--write-tree", first, second], worktree).pipe(
272
+ Effect.map((said) => Option.fromUndefinedOr(said.trim().split("\n")[0])),
273
+ Effect.catchTag("GitError", () => Effect.succeedNone),
274
+ );
275
+
276
+ const tree = (yield* git.run(["rev-parse", `${commit}^{tree}`], worktree)).trim();
277
+
278
+ if (!Option.contains(clean, tree)) {
279
+ return notOnly(`\`${short(commit)}\` is not a clean merge of \`${base}\``);
280
+ }
281
+
282
+ // The walk reached the reviewed commit through first parents alone.
283
+ if (first === reviewedSha) {
284
+ return { only: true } satisfies BaseMerges;
285
+ }
286
+ }
287
+
288
+ return notOnly(
289
+ `the reviewed commit \`${short(reviewedSha)}\` is not on the head's first-parent history`,
290
+ );
291
+ });
292
+
293
+ // The pull request whose head branch `pull` is based on, which makes `pull`
294
+ // a layer stacked on it: one still open, or one that merged while its branch
295
+ // stayed, which nothing retargets `pull` off, so a merge would land in that
296
+ // branch and never reach the base below it. None when `pull`'s base is the
297
+ // flow's integration base, or no open or merged pull request comes from it.
298
+ const layerBelow = (pull: PullRequest) =>
299
+ Effect.gen(function* () {
300
+ if (pull.baseRef === (yield* Settings.base)) {
301
+ return Option.none<PullRequest>();
302
+ }
303
+
304
+ return Option.filter(
305
+ yield* (yield* GitHub).pullRequestForBranch(pull.baseRef),
306
+ (below) => below.state === "open" || below.state === "merged",
307
+ );
308
+ });
309
+
310
+ // Why a layer stacked on `below` waits.
311
+ const waitsOn = (pull: PullRequest, below: PullRequest) => {
312
+ if (below.state === "merged") {
313
+ return `stacked on ${pull.baseRef}, and the layer below (#${below.number}) merged; it waits to be retargeted onto ${below.baseRef}`;
314
+ }
315
+
316
+ return `stacked on ${pull.baseRef} (#${below.number}); it merges after the layer below`;
317
+ };
318
+
319
+ // Merges the pull request, only when that is safe: it is open and not a
320
+ // layer of a stack, its checks are green on the current head by the flow's
321
+ // CI policy (Ci.verdict: by default, its required checks passed and no check
322
+ // failed), the latest review approved that very head or one the head has only merged
323
+ // its base into since (onlyMergedBase), it touches no humanOnlyPaths, and
324
+ // GitHub's rules for the repo allow it. A pull request into any branch may
325
+ // merge, such as develop in git flow. One whose base is the head branch of
326
+ // another pull request is a layer stacked on it (layerBelow): it waits while
327
+ // the layer below is open, and after it merges, until it is retargeted onto
328
+ // the base below. A pull request based on the flow's integration base
329
+ // (Settings.base) is never a layer, whatever came from that branch.
173
330
  // Otherwise it fails with MergeRefused, saying why, or PullRequestClosed for
174
331
  // one closed without merging. Nothing merges unless a flow calls this.
175
332
  export const merge = (pullRequest: number, options: MergeOptions) =>
176
333
  activity(
177
- `merge-pr-${pullRequest}`,
334
+ Option.match(Option.fromUndefinedOr(options.id), {
335
+ onNone: () => `merge-pr-${pullRequest}`,
336
+ onSome: (id) => `merge-pr-${pullRequest}#${id}`,
337
+ }),
178
338
  Effect.gen(function* () {
179
339
  const github = yield* GitHub;
180
- const repo = yield* Repo;
340
+ const rules = yield* Settings.rules;
181
341
  const pull = yield* github.pullRequest(pullRequest);
182
342
 
183
343
  // A rerun after a crash finds its own merge done.
@@ -189,7 +349,13 @@ export const merge = (pullRequest: number, options: MergeOptions) =>
189
349
  return yield* new PullRequestClosed({ pullRequest, state: "closed" });
190
350
  }
191
351
 
192
- const ci = yield* github.ciState(pullRequest);
352
+ const below = yield* layerBelow(pull);
353
+
354
+ if (Option.isSome(below)) {
355
+ return yield* refuse(pullRequest, waitsOn(pull, below.value));
356
+ }
357
+
358
+ const ci = yield* Ci.pullRequestState(pullRequest);
193
359
 
194
360
  if (ci.state === "red") {
195
361
  return yield* refuse(pullRequest, `checks failed: ${ci.failedChecks.join(", ")}`);
@@ -209,10 +375,14 @@ export const merge = (pullRequest: number, options: MergeOptions) =>
209
375
  }
210
376
 
211
377
  if (reviewed.value.sha !== pull.headSha) {
212
- return yield* refuse(
213
- pullRequest,
214
- `the head moved to \`${pull.headSha.slice(0, 7)}\` since review ${reviewed.value.round} saw \`${reviewed.value.sha.slice(0, 7)}\``,
215
- );
378
+ const since = yield* onlyMergedBase(pull, reviewed.value.sha);
379
+
380
+ if (!since.only) {
381
+ return yield* refuse(
382
+ pullRequest,
383
+ `the head moved to \`${short(pull.headSha)}\` since review ${reviewed.value.round} saw \`${short(reviewed.value.sha)}\`, and ${since.reason}`,
384
+ );
385
+ }
216
386
  }
217
387
 
218
388
  if (reviewed.value.verdict !== "approve") {
@@ -221,7 +391,7 @@ export const merge = (pullRequest: number, options: MergeOptions) =>
221
391
 
222
392
  const touched = pathsMatching(
223
393
  yield* github.pullRequestFiles(pullRequest),
224
- repo.humanOnlyPaths,
394
+ rules.humanOnlyPaths,
225
395
  );
226
396
 
227
397
  if (touched.length > 0) {
@@ -252,14 +422,15 @@ export const requestReview = (pullRequest: number) =>
252
422
  `request-review-${pullRequest}`,
253
423
  Effect.gen(function* () {
254
424
  const github = yield* GitHub;
255
- const repo = yield* Repo;
425
+ const reviewerPool = (yield* Settings.reviewers).pool;
426
+ const labelNames = yield* Settings.labels;
256
427
  const pull = yield* github.pullRequest(pullRequest);
257
- const pool = (repo.reviewers ?? []).filter((login) => !sameLogin(login, pull.author));
428
+ const pool = reviewerPool.filter((login) => !sameLogin(login, pull.author));
258
429
 
259
430
  if (pool.length === 0) {
260
- yield* github.addLabel(pullRequest, repo.labels.needsReview);
431
+ yield* github.addLabel(pullRequest, labelNames.needsReview);
261
432
 
262
- return `#${pullRequest}: no reviewer in the pool but its author; labelled ${repo.labels.needsReview} for a person`;
433
+ return `#${pullRequest}: no reviewer in the pool but its author; labelled ${labelNames.needsReview} for a person`;
263
434
  }
264
435
 
265
436
  const reviewer = pool[yield* Random.nextIntBetween(0, pool.length, { halfOpen: true })];
@@ -273,7 +444,8 @@ export const requestReview = (pullRequest: number) =>
273
444
  // Whether chernabog may merge the pull request: only on the automated path,
274
445
  // one labelled ship (auto:ship). Anything else, a missing or misspelt label
275
446
  // included, is left for a person to merge. The one place that decides it.
276
- export const ships = (pull: PullRequest, labels: Labels) => pull.labels.includes(labels.ship);
447
+ export const ships = (pull: PullRequest, labels: Settings.LabelNames) =>
448
+ pull.labels.includes(labels.ship);
277
449
 
278
450
  // Labels the pull request needs-human and says why; no flow picks it up again
279
451
  // until a person removes the label.
@@ -282,8 +454,8 @@ export const handToPerson = (pullRequest: number, reason: string) =>
282
454
  `needs-human-pr-${pullRequest}`,
283
455
  Effect.gen(function* () {
284
456
  const github = yield* GitHub;
285
- const repo = yield* Repo;
286
- yield* github.addLabel(pullRequest, repo.labels.needsHuman);
457
+ const labelNames = yield* Settings.labels;
458
+ yield* github.addLabel(pullRequest, labelNames.needsHuman);
287
459
  yield* github.comment(pullRequest, `Chernabog stopped and needs a person: ${reason}`);
288
460
  }),
289
461
  ).pipe(Effect.andThen(Effect.fail(new NeedsHuman({ reason: `#${pullRequest}: ${reason}` }))));
@@ -294,22 +466,45 @@ export const shipping = (pullRequest: number) =>
294
466
  activity(
295
467
  `ships-pr-${pullRequest}`,
296
468
  Effect.gen(function* () {
297
- const repo = yield* Repo;
469
+ const labelNames = yield* Settings.labels;
298
470
 
299
- return ships(yield* (yield* GitHub).pullRequest(pullRequest), repo.labels);
471
+ return ships(yield* (yield* GitHub).pullRequest(pullRequest), labelNames);
300
472
  }),
301
473
  Schema.Boolean,
302
474
  );
303
475
 
476
+ // Why the pull request, a layer of a stack, waits for the layer below rather
477
+ // than merging (layerBelow), read once in an activity so a replay decides the
478
+ // same; none for one on the flow's integration base. An approved layer that
479
+ // waits is left approved, not handed to a person: the epic run that stacked
480
+ // it merges it once the layer below has merged and it is on the base.
481
+ export const waitingOnLayer = (pullRequest: number) =>
482
+ activity(
483
+ `layer-below-pr-${pullRequest}`,
484
+ Effect.gen(function* () {
485
+ const pull = yield* (yield* GitHub).pullRequest(pullRequest);
486
+
487
+ return Option.map(yield* layerBelow(pull), (below) => waitsOn(pull, below));
488
+ }),
489
+ Schema.Option(Schema.String),
490
+ );
491
+
304
492
  // After an approval: on the automated path, merges the pull request when that
305
- // is safe, and otherwise hands it to a person; any other pull request is left
306
- // for a person to merge, and one closed meanwhile is left alone.
493
+ // is safe, and otherwise hands it to a person; a layer waiting for the one
494
+ // below is left approved, any other pull request is left for a person to
495
+ // merge, and one closed meanwhile is left alone.
307
496
  export const mergeOrLeave = (pullRequest: number) =>
308
497
  Effect.gen(function* () {
309
498
  if (!(yield* shipping(pullRequest))) {
310
499
  return `#${pullRequest} approved; a person merges`;
311
500
  }
312
501
 
502
+ const waiting = yield* waitingOnLayer(pullRequest);
503
+
504
+ if (Option.isSome(waiting)) {
505
+ return `#${pullRequest} approved; ${waiting.value}`;
506
+ }
507
+
313
508
  return yield* merge(pullRequest, { method: "squash" }).pipe(
314
509
  Effect.catchTags({
315
510
  MergeRefused: (refused) => handToPerson(pullRequest, refused.message),