@chernabog/sdk 0.10.0 → 0.11.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/package.json +1 -1
- package/src/address-review-blocks.ts +510 -0
- package/src/block.ts +466 -0
- package/src/blocks.ts +1051 -0
- package/src/compose.ts +2666 -0
- package/src/epic-blocks.ts +1217 -0
- package/src/human-initiated-blocks.ts +274 -0
- package/src/hybrid.ts +2 -1
- package/src/implement.ts +103 -16
- package/src/index.ts +3 -0
- package/src/plan-blocks.ts +312 -0
- package/src/post-merge-blocks.ts +826 -0
- package/src/pull-request.ts +17 -0
- package/src/re-review-blocks.ts +234 -0
- package/src/resolve-conflicts-blocks.ts +567 -0
- package/src/review.ts +3 -1
- package/src/settings.ts +22 -2
- package/src/suits.ts +156 -0
package/src/blocks.ts
ADDED
|
@@ -0,0 +1,1051 @@
|
|
|
1
|
+
// Blocks: the pieces a flow document is composed from, each a step of the SDK
|
|
2
|
+
// with its inputs, outputs and failures declared. What a block's inputs and
|
|
3
|
+
// outputs are is typed by what they mean, so a document that wires a branch
|
|
4
|
+
// where a worktree goes doesn't compile. Its failure table must name exactly
|
|
5
|
+
// the errors its effect can raise: an SDK change that adds one fails to compile
|
|
6
|
+
// here, not in a run.
|
|
7
|
+
//
|
|
8
|
+
// A value proves what happened to the world where a value alone can't show it:
|
|
9
|
+
// a pull request opens only from a push's `pushed`, and a push only from the
|
|
10
|
+
// agent's `committed`. These prove what must be true for a step to work, never
|
|
11
|
+
// what a team chooses: nothing here demands the local gate or CI.
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
activity,
|
|
15
|
+
Claims,
|
|
16
|
+
defineFlow,
|
|
17
|
+
type FlowError,
|
|
18
|
+
GitHub,
|
|
19
|
+
type ItemLookup,
|
|
20
|
+
NeedsHuman,
|
|
21
|
+
Repo,
|
|
22
|
+
reportAs,
|
|
23
|
+
type StillValid,
|
|
24
|
+
type WorkflowServices,
|
|
25
|
+
type WorkItem,
|
|
26
|
+
} from "@chernabog/core";
|
|
27
|
+
import * as Effect from "effect/Effect";
|
|
28
|
+
import * as Option from "effect/Option";
|
|
29
|
+
import * as Schema from "effect/Schema";
|
|
30
|
+
|
|
31
|
+
import {
|
|
32
|
+
addressReviewBlocks,
|
|
33
|
+
addressReviewSources,
|
|
34
|
+
changesAskedSubject,
|
|
35
|
+
} from "./address-review-blocks.ts";
|
|
36
|
+
import {
|
|
37
|
+
Approved,
|
|
38
|
+
BaseBranch,
|
|
39
|
+
BranchName,
|
|
40
|
+
Committed,
|
|
41
|
+
type Contribution,
|
|
42
|
+
type Days,
|
|
43
|
+
defineBlock,
|
|
44
|
+
defineSource,
|
|
45
|
+
type FailureContext,
|
|
46
|
+
gather,
|
|
47
|
+
inLane,
|
|
48
|
+
IssueNumber,
|
|
49
|
+
type LaneId,
|
|
50
|
+
laneSession,
|
|
51
|
+
onBase,
|
|
52
|
+
PullRequestNumber,
|
|
53
|
+
pullRequestRef,
|
|
54
|
+
Pushed,
|
|
55
|
+
runStage,
|
|
56
|
+
type Source,
|
|
57
|
+
ToPerson,
|
|
58
|
+
WorktreePath,
|
|
59
|
+
} from "./block.ts";
|
|
60
|
+
import {
|
|
61
|
+
epicBlocks,
|
|
62
|
+
epicSources,
|
|
63
|
+
epicSubject,
|
|
64
|
+
laneScopes,
|
|
65
|
+
loopStates as epicLoopStates,
|
|
66
|
+
} from "./epic-blocks.ts";
|
|
67
|
+
import { humanInitiatedBlocks } from "./human-initiated-blocks.ts";
|
|
68
|
+
import * as Hybrid from "./hybrid.ts";
|
|
69
|
+
import * as Implement from "./implement.ts";
|
|
70
|
+
import { planBlocks, planLoopStates, planSources, specSubject } from "./plan-blocks.ts";
|
|
71
|
+
import { mergedSubject, postMergeBlocks, postMergeSources } from "./post-merge-blocks.ts";
|
|
72
|
+
import * as PullRequest from "./pull-request.ts";
|
|
73
|
+
import { reReviewBlocks, reReviewSources } from "./re-review-blocks.ts";
|
|
74
|
+
import {
|
|
75
|
+
conflictSubject,
|
|
76
|
+
resolveConflictsBlocks,
|
|
77
|
+
resolveConflictsSources,
|
|
78
|
+
} from "./resolve-conflicts-blocks.ts";
|
|
79
|
+
import * as Review from "./review.ts";
|
|
80
|
+
import * as Routing from "./routing.ts";
|
|
81
|
+
import * as Settings from "./settings.ts";
|
|
82
|
+
import { suited } from "./suits.ts";
|
|
83
|
+
|
|
84
|
+
const summaryOf = (pullRequest: PullRequestNumber, round: number) =>
|
|
85
|
+
`#${pullRequest}: review ${round} requested changes`;
|
|
86
|
+
|
|
87
|
+
const MergeState = Schema.Struct({ mergeable_state: Schema.String });
|
|
88
|
+
|
|
89
|
+
// Whether GitHub says the pull request conflicts with its base. Recorded, so
|
|
90
|
+
// a run that resumes takes the same path.
|
|
91
|
+
const conflicts = (pullRequest: number) =>
|
|
92
|
+
activity(
|
|
93
|
+
`conflicts-pr-${pullRequest}`,
|
|
94
|
+
Effect.gen(function* () {
|
|
95
|
+
const repo = yield* Repo;
|
|
96
|
+
|
|
97
|
+
const pull = yield* (yield* GitHub).request(
|
|
98
|
+
"GET",
|
|
99
|
+
`/repos/${repo.slug}/pulls/${pullRequest}`,
|
|
100
|
+
Option.none(),
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
return Schema.decodeUnknownOption(MergeState)(pull).pipe(
|
|
104
|
+
Option.exists((state) => state.mergeable_state === "dirty"),
|
|
105
|
+
);
|
|
106
|
+
}),
|
|
107
|
+
Schema.Boolean,
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// Step names more than one block uses.
|
|
111
|
+
const CI_STEPS = "ci";
|
|
112
|
+
const CLAIM_STEPS = "claim";
|
|
113
|
+
const HAND_OFF_STEPS = "hand-off";
|
|
114
|
+
const LOCAL_GATE_STEPS = "local-gate";
|
|
115
|
+
const OPEN_STEPS = "open-pull-request";
|
|
116
|
+
const PUSH_STEPS = "push";
|
|
117
|
+
const WORKTREE_STEPS = "prepare-worktree";
|
|
118
|
+
|
|
119
|
+
// The blocks of the files beside this one.
|
|
120
|
+
const elsewhere = gather("block", "epic-blocks.ts", epicBlocks)
|
|
121
|
+
.and("re-review-blocks.ts", reReviewBlocks)
|
|
122
|
+
.and("address-review-blocks.ts", addressReviewBlocks)
|
|
123
|
+
.and("resolve-conflicts-blocks.ts", resolveConflictsBlocks)
|
|
124
|
+
.and("post-merge-blocks.ts", postMergeBlocks)
|
|
125
|
+
.and("human-initiated-blocks.ts", humanInitiatedBlocks)
|
|
126
|
+
.and("plan-blocks.ts", planBlocks);
|
|
127
|
+
|
|
128
|
+
// Everything a document can use, by the name it uses it by.
|
|
129
|
+
export const catalogue = elsewhere.and("blocks.ts", {
|
|
130
|
+
"issue.claim": defineBlock({
|
|
131
|
+
name: "issue.claim",
|
|
132
|
+
// Only these let its claim go: an issue's and a spec's scopes, and the
|
|
133
|
+
// engine for an epic's tasks.
|
|
134
|
+
suits: ["epic", "issue", "spec"],
|
|
135
|
+
fixed: ["adopt"],
|
|
136
|
+
about:
|
|
137
|
+
"Claims the issue, so only this run works on it, and settles its branch and base. With adopt, takes on an open pull request that already closes the issue instead.",
|
|
138
|
+
inputKinds: {
|
|
139
|
+
issue: "IssueNumber",
|
|
140
|
+
queued: "how the issue was queued, from the trigger",
|
|
141
|
+
adopt: "boolean: adopt a pull request that closes the issue",
|
|
142
|
+
base: "Option<BaseBranch>: the base to build on",
|
|
143
|
+
lane: "LaneId",
|
|
144
|
+
},
|
|
145
|
+
outputKinds: {
|
|
146
|
+
branch: "BranchName",
|
|
147
|
+
base: "Option<BaseBranch>",
|
|
148
|
+
adopted: "the adopted pull request, if any",
|
|
149
|
+
},
|
|
150
|
+
outcomeKinds: {
|
|
151
|
+
adopted:
|
|
152
|
+
"an object: with as: adopted, adopted.number (PullRequestNumber), adopted.url, adopted.draft",
|
|
153
|
+
},
|
|
154
|
+
opensOn: { adopted: ["hand-off"] },
|
|
155
|
+
stepNames: [CLAIM_STEPS],
|
|
156
|
+
inputs: {
|
|
157
|
+
issue: "required",
|
|
158
|
+
queued: "optional",
|
|
159
|
+
adopt: "optional",
|
|
160
|
+
base: "optional",
|
|
161
|
+
lane: "optional",
|
|
162
|
+
},
|
|
163
|
+
run: (input: {
|
|
164
|
+
readonly issue: IssueNumber;
|
|
165
|
+
readonly queued?: Implement.QueueOptions;
|
|
166
|
+
readonly adopt?: boolean;
|
|
167
|
+
readonly base?: Option.Option<BaseBranch>;
|
|
168
|
+
readonly lane?: LaneId;
|
|
169
|
+
}) =>
|
|
170
|
+
Implement.claim(input.issue, {
|
|
171
|
+
...input.queued,
|
|
172
|
+
...onBase(input.base ?? Option.none()),
|
|
173
|
+
...inLane(input.lane),
|
|
174
|
+
adopt: input.adopt === true,
|
|
175
|
+
}).pipe(
|
|
176
|
+
Effect.map((claim) => ({
|
|
177
|
+
branch: BranchName(claim.branch),
|
|
178
|
+
base: Option.map(Option.fromUndefinedOr(claim.base), BaseBranch),
|
|
179
|
+
adopted: claim.adopted,
|
|
180
|
+
})),
|
|
181
|
+
),
|
|
182
|
+
failures: { NeedsHuman: "told", Skip: "skip", StepFailed: "person" },
|
|
183
|
+
outcomes: {
|
|
184
|
+
adopted: (claim: { readonly adopted?: Implement.Adopted }) =>
|
|
185
|
+
Option.map(Option.fromUndefinedOr(claim.adopted), pullRequestRef),
|
|
186
|
+
},
|
|
187
|
+
contributes: { settings: Settings.Base.fields },
|
|
188
|
+
mustRoute: [],
|
|
189
|
+
}),
|
|
190
|
+
|
|
191
|
+
"pr.awaitReady": defineBlock({
|
|
192
|
+
name: "pr.awaitReady",
|
|
193
|
+
fixed: ["within"],
|
|
194
|
+
about:
|
|
195
|
+
"Waits, parked, for the author of an adopted draft to mark it ready, up to `within`; goes to a person if they don't. Doesn't wait for one that isn't a draft.",
|
|
196
|
+
inputKinds: {
|
|
197
|
+
issue: "IssueNumber",
|
|
198
|
+
adopted: "the adopted pull request, from issue.claim's adopted",
|
|
199
|
+
within: 'how long, as "14 days"',
|
|
200
|
+
},
|
|
201
|
+
outputKinds: "nothing",
|
|
202
|
+
outcomeKinds: {},
|
|
203
|
+
before: ["ci.waitPreparing", "issue.handOffAdopted"],
|
|
204
|
+
inputs: { issue: "required", adopted: "required", within: "required" },
|
|
205
|
+
run: (input: {
|
|
206
|
+
readonly issue: IssueNumber;
|
|
207
|
+
readonly adopted: Implement.Adopted;
|
|
208
|
+
readonly within: Days;
|
|
209
|
+
}) =>
|
|
210
|
+
Effect.gen(function* () {
|
|
211
|
+
// A draft is its author's until they mark it ready.
|
|
212
|
+
if (
|
|
213
|
+
input.adopted.draft &&
|
|
214
|
+
(yield* Implement.awaitReady(input.issue, input.adopted.number, input.within)) ===
|
|
215
|
+
"timeout"
|
|
216
|
+
) {
|
|
217
|
+
return yield* new NeedsHuman({
|
|
218
|
+
reason: `#${input.adopted.number} is still a draft after ${input.within}`,
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}),
|
|
222
|
+
failures: { NeedsHuman: "person", Skip: "skip", StepFailed: "person" },
|
|
223
|
+
contributes: {
|
|
224
|
+
labels: Implement.adoptionLabels,
|
|
225
|
+
wakes: Implement.readied.pipe(reportAs("trigger")),
|
|
226
|
+
},
|
|
227
|
+
outcomes: {},
|
|
228
|
+
mustRoute: [],
|
|
229
|
+
}),
|
|
230
|
+
|
|
231
|
+
"ci.waitPreparing": defineBlock({
|
|
232
|
+
name: "ci.waitPreparing",
|
|
233
|
+
about:
|
|
234
|
+
"Waits for CI on a pull request with no worktree yet, making one only if CI needs a fix.",
|
|
235
|
+
inputKinds: { pr: "PullRequestNumber", branch: "BranchName", base: "Option<BaseBranch>" },
|
|
236
|
+
outputKinds: "nothing",
|
|
237
|
+
outcomeKinds: {},
|
|
238
|
+
before: ["issue.handOffAdopted"],
|
|
239
|
+
stepNames: [CI_STEPS, WORKTREE_STEPS],
|
|
240
|
+
inputs: { pr: "required", branch: "required", base: "required" },
|
|
241
|
+
run: (input: {
|
|
242
|
+
readonly pr: PullRequestNumber;
|
|
243
|
+
readonly branch: BranchName;
|
|
244
|
+
readonly base: Option.Option<BaseBranch>;
|
|
245
|
+
}) =>
|
|
246
|
+
Implement.waitForCiMaking(
|
|
247
|
+
input.pr,
|
|
248
|
+
// The worktree is made only for a fix.
|
|
249
|
+
Implement.prepareWorktree(input.branch, onBase(input.base)),
|
|
250
|
+
{ ...Implement.thisRunsSession, branch: input.branch, ...onBase(input.base) },
|
|
251
|
+
),
|
|
252
|
+
failures: {
|
|
253
|
+
CiTimedOut: "person",
|
|
254
|
+
NeedsHuman: "person",
|
|
255
|
+
NothingCommitted: "person",
|
|
256
|
+
PushRejected: "person",
|
|
257
|
+
Skip: "skip",
|
|
258
|
+
StepFailed: "person",
|
|
259
|
+
},
|
|
260
|
+
contributes: { settings: Settings.Ci.fields },
|
|
261
|
+
outcomes: {},
|
|
262
|
+
mustRoute: [],
|
|
263
|
+
}),
|
|
264
|
+
|
|
265
|
+
"issue.handOffAdopted": defineBlock({
|
|
266
|
+
name: "issue.handOffAdopted",
|
|
267
|
+
about:
|
|
268
|
+
"Hands an adopted pull request to review, marks it the persona's to answer reviews on, and tells the issue.",
|
|
269
|
+
inputKinds: { issue: "IssueNumber", adopted: "the adopted pull request", branch: "BranchName" },
|
|
270
|
+
outputKinds: "a summary of the hand-off, for `end`'s summary",
|
|
271
|
+
outcomeKinds: {},
|
|
272
|
+
closes: ["hand-off"],
|
|
273
|
+
stepNames: [HAND_OFF_STEPS],
|
|
274
|
+
inputs: { issue: "required", adopted: "required", branch: "required" },
|
|
275
|
+
run: (input: {
|
|
276
|
+
readonly issue: IssueNumber;
|
|
277
|
+
readonly adopted: Implement.Adopted;
|
|
278
|
+
readonly branch: BranchName;
|
|
279
|
+
}) =>
|
|
280
|
+
Implement.handOff(input.issue, input.adopted, Implement.adoptedOutcome(input.adopted), {
|
|
281
|
+
needsReview: true,
|
|
282
|
+
adoptedBranch: input.branch,
|
|
283
|
+
}),
|
|
284
|
+
failures: {
|
|
285
|
+
NeedsHuman: "person",
|
|
286
|
+
NotFound: "skip",
|
|
287
|
+
PullRequestClosed: "person",
|
|
288
|
+
Skip: "skip",
|
|
289
|
+
StepFailed: "person",
|
|
290
|
+
},
|
|
291
|
+
contributes: { settings: Settings.Reviewers.fields, doctor: [Review.poolCheck] },
|
|
292
|
+
outcomes: {},
|
|
293
|
+
mustRoute: [],
|
|
294
|
+
}),
|
|
295
|
+
|
|
296
|
+
"worktree.prepare": defineBlock({
|
|
297
|
+
name: "worktree.prepare",
|
|
298
|
+
about: "Makes a worktree on the branch.",
|
|
299
|
+
inputKinds: { branch: "BranchName", base: "Option<BaseBranch>", lane: "LaneId" },
|
|
300
|
+
outputKinds: "WorktreePath",
|
|
301
|
+
outcomeKinds: {},
|
|
302
|
+
stepNames: [WORKTREE_STEPS],
|
|
303
|
+
inputs: { branch: "required", base: "required", lane: "optional" },
|
|
304
|
+
run: (input: {
|
|
305
|
+
readonly branch: BranchName;
|
|
306
|
+
readonly base: Option.Option<BaseBranch>;
|
|
307
|
+
readonly lane?: LaneId;
|
|
308
|
+
}) =>
|
|
309
|
+
Implement.prepareWorktree(input.branch, {
|
|
310
|
+
...onBase(input.base),
|
|
311
|
+
...inLane(input.lane),
|
|
312
|
+
}).pipe(Effect.map(WorktreePath)),
|
|
313
|
+
failures: { NeedsHuman: "told", Skip: "skip", StepFailed: "person" },
|
|
314
|
+
outcomes: {},
|
|
315
|
+
mustRoute: [],
|
|
316
|
+
contributes: {},
|
|
317
|
+
}),
|
|
318
|
+
|
|
319
|
+
"agent.implement": defineBlock({
|
|
320
|
+
name: "agent.implement",
|
|
321
|
+
about:
|
|
322
|
+
"Has the agent implement the issue in the worktree and commit. It finishes done, or with questions for a person.",
|
|
323
|
+
inputKinds: {
|
|
324
|
+
issue: "IssueNumber",
|
|
325
|
+
worktree: "WorktreePath",
|
|
326
|
+
base: "Option<BaseBranch>",
|
|
327
|
+
lane: "LaneId",
|
|
328
|
+
},
|
|
329
|
+
outputKinds: {
|
|
330
|
+
outcome: "what the agent said: status, summary, title, body, questions",
|
|
331
|
+
committed: "Committed: proof for a push",
|
|
332
|
+
},
|
|
333
|
+
outcomeKinds: { "needs-clarification": "what the agent said, with its questions" },
|
|
334
|
+
before: ["gate.local"],
|
|
335
|
+
opensOn: { "needs-clarification": ["ask-questions"] },
|
|
336
|
+
inputs: { issue: "required", worktree: "required", base: "required", lane: "optional" },
|
|
337
|
+
run: (input: {
|
|
338
|
+
readonly issue: IssueNumber;
|
|
339
|
+
readonly worktree: WorktreePath;
|
|
340
|
+
readonly base: Option.Option<BaseBranch>;
|
|
341
|
+
readonly lane?: LaneId;
|
|
342
|
+
}) =>
|
|
343
|
+
Implement.implementWithAgent(input.issue, input.worktree, {
|
|
344
|
+
...onBase(input.base),
|
|
345
|
+
...inLane(input.lane),
|
|
346
|
+
}).pipe(Effect.map((outcome) => ({ outcome, committed: new Committed({}) }))),
|
|
347
|
+
failures: { NeedsHuman: "told", NotFound: "skip", Skip: "skip", StepFailed: "person" },
|
|
348
|
+
outcomes: {
|
|
349
|
+
"needs-clarification": (done: { readonly outcome: Implement.ImplementOutcome }) =>
|
|
350
|
+
Option.liftPredicate(done.outcome, (outcome) => outcome.status === "needs-clarification"),
|
|
351
|
+
},
|
|
352
|
+
mustRoute: ["needs-clarification"],
|
|
353
|
+
contributes: { settings: Settings.Rules.fields },
|
|
354
|
+
}),
|
|
355
|
+
|
|
356
|
+
"issue.askClarification": defineBlock({
|
|
357
|
+
name: "issue.askClarification",
|
|
358
|
+
about:
|
|
359
|
+
"Posts the agent's questions as a comment on the issue, where its author sees them. Its output is the reason to end with. On a run's main path, end: needs-human and the run lets the claim go; in a lane, which goes on, follow it with issue.releaseClaim.",
|
|
360
|
+
inputKinds: {
|
|
361
|
+
issue: "IssueNumber",
|
|
362
|
+
outcome: "what the agent said, from agent.implement's needs-clarification",
|
|
363
|
+
queued: "how the issue was queued",
|
|
364
|
+
lane: "LaneId",
|
|
365
|
+
},
|
|
366
|
+
outputKinds: "the reason, for `end: needs-human`",
|
|
367
|
+
outcomeKinds: {},
|
|
368
|
+
opens: ["release-claim"],
|
|
369
|
+
closes: ["ask-questions"],
|
|
370
|
+
inputs: { issue: "required", outcome: "required", queued: "optional", lane: "optional" },
|
|
371
|
+
run: (input: {
|
|
372
|
+
readonly issue: IssueNumber;
|
|
373
|
+
readonly outcome: Implement.ImplementOutcome;
|
|
374
|
+
readonly queued?: Implement.QueueOptions;
|
|
375
|
+
readonly lane?: LaneId;
|
|
376
|
+
}) =>
|
|
377
|
+
Implement.askForClarification(input.issue, input.outcome, {
|
|
378
|
+
...input.queued,
|
|
379
|
+
...inLane(input.lane),
|
|
380
|
+
}),
|
|
381
|
+
failures: { NeedsHuman: "told", Skip: "skip", StepFailed: "person" },
|
|
382
|
+
outcomes: {},
|
|
383
|
+
mustRoute: [],
|
|
384
|
+
contributes: {},
|
|
385
|
+
}),
|
|
386
|
+
|
|
387
|
+
"gate.local": defineBlock({
|
|
388
|
+
name: "gate.local",
|
|
389
|
+
about: "Runs the repo's local gate in the worktree, resuming the agent to fix a failure.",
|
|
390
|
+
inputKinds: { worktree: "WorktreePath", lane: "LaneId" },
|
|
391
|
+
outputKinds: "nothing",
|
|
392
|
+
outcomeKinds: {},
|
|
393
|
+
before: ["branch.push", "pr.open", "stack.publish"],
|
|
394
|
+
stepNames: [LOCAL_GATE_STEPS],
|
|
395
|
+
inputs: { worktree: "required", lane: "optional" },
|
|
396
|
+
run: (input: { readonly worktree: WorktreePath; readonly lane?: LaneId }) =>
|
|
397
|
+
Effect.gen(function* () {
|
|
398
|
+
const session = yield* laneSession(input.lane);
|
|
399
|
+
|
|
400
|
+
return yield* Implement.localGate(input.worktree, { ...session, ...inLane(input.lane) });
|
|
401
|
+
}),
|
|
402
|
+
failures: { NeedsHuman: "person", Skip: "skip", StepFailed: "person" },
|
|
403
|
+
contributes: { settings: Settings.LocalGate.fields },
|
|
404
|
+
outcomes: {},
|
|
405
|
+
mustRoute: [],
|
|
406
|
+
}),
|
|
407
|
+
|
|
408
|
+
"branch.push": defineBlock({
|
|
409
|
+
name: "branch.push",
|
|
410
|
+
about: "Pushes the branch.",
|
|
411
|
+
inputKinds: {
|
|
412
|
+
worktree: "WorktreePath",
|
|
413
|
+
branch: "BranchName",
|
|
414
|
+
base: "Option<BaseBranch>",
|
|
415
|
+
committed: "Committed: from agent.implement",
|
|
416
|
+
},
|
|
417
|
+
outputKinds: { pushed: "Pushed: proof for pr.open" },
|
|
418
|
+
outcomeKinds: {},
|
|
419
|
+
stepNames: [PUSH_STEPS],
|
|
420
|
+
inputs: { worktree: "required", branch: "required", base: "required", committed: "required" },
|
|
421
|
+
run: (input: {
|
|
422
|
+
readonly worktree: WorktreePath;
|
|
423
|
+
readonly branch: BranchName;
|
|
424
|
+
readonly base: Option.Option<BaseBranch>;
|
|
425
|
+
readonly committed: Committed;
|
|
426
|
+
}) =>
|
|
427
|
+
Implement.push("push", input.worktree, input.branch, onBase(input.base)).pipe(
|
|
428
|
+
Effect.as({ pushed: new Pushed({}) }),
|
|
429
|
+
),
|
|
430
|
+
failures: {
|
|
431
|
+
NeedsHuman: "person",
|
|
432
|
+
NothingCommitted: "person",
|
|
433
|
+
PushRejected: "person",
|
|
434
|
+
Skip: "skip",
|
|
435
|
+
StepFailed: "person",
|
|
436
|
+
},
|
|
437
|
+
outcomes: {},
|
|
438
|
+
mustRoute: [],
|
|
439
|
+
contributes: {},
|
|
440
|
+
}),
|
|
441
|
+
|
|
442
|
+
"pr.open": defineBlock({
|
|
443
|
+
name: "pr.open",
|
|
444
|
+
fixed: ["draft"],
|
|
445
|
+
about:
|
|
446
|
+
"Opens the pull request for the pushed branch, ready for review, or with draft: true as a draft. It leaves the hand-off to do: issue.handOff, which asks nobody to review a draft and tells the issue a person marks it ready; pr.readyForPerson, which gives it to a person as the run's success; or pr.handToPerson and end: needs-human.",
|
|
447
|
+
inputKinds: {
|
|
448
|
+
issue: "IssueNumber",
|
|
449
|
+
outcome: "what the agent said",
|
|
450
|
+
queued: "how the issue was queued",
|
|
451
|
+
base: "Option<BaseBranch>",
|
|
452
|
+
pushed: "Pushed: from branch.push",
|
|
453
|
+
draft: "boolean: open it as a draft; false unless given",
|
|
454
|
+
},
|
|
455
|
+
outputKinds: {
|
|
456
|
+
number: "PullRequestNumber",
|
|
457
|
+
url: "its URL",
|
|
458
|
+
draft: "boolean: whether it opened as a draft, for issue.handOff",
|
|
459
|
+
},
|
|
460
|
+
outcomeKinds: {},
|
|
461
|
+
opens: ["hand-off"],
|
|
462
|
+
stepNames: [OPEN_STEPS],
|
|
463
|
+
inputs: {
|
|
464
|
+
issue: "required",
|
|
465
|
+
outcome: "required",
|
|
466
|
+
queued: "required",
|
|
467
|
+
base: "required",
|
|
468
|
+
pushed: "required",
|
|
469
|
+
draft: "optional",
|
|
470
|
+
},
|
|
471
|
+
run: (input: {
|
|
472
|
+
readonly issue: IssueNumber;
|
|
473
|
+
readonly outcome: Implement.ImplementOutcome;
|
|
474
|
+
readonly queued: Implement.QueueOptions;
|
|
475
|
+
readonly base: Option.Option<BaseBranch>;
|
|
476
|
+
readonly pushed: Pushed;
|
|
477
|
+
readonly draft?: boolean;
|
|
478
|
+
}) =>
|
|
479
|
+
Implement.openPullRequest(input.issue, input.outcome, {
|
|
480
|
+
...input.queued,
|
|
481
|
+
...onBase(input.base),
|
|
482
|
+
draft: input.draft === true,
|
|
483
|
+
}).pipe(Effect.map((opened) => ({ ...pullRequestRef(opened), draft: input.draft === true }))),
|
|
484
|
+
failures: { NeedsHuman: "person", Skip: "skip", StepFailed: "person" },
|
|
485
|
+
outcomes: {},
|
|
486
|
+
mustRoute: [],
|
|
487
|
+
contributes: {},
|
|
488
|
+
}),
|
|
489
|
+
|
|
490
|
+
"ci.waitAndFix": defineBlock({
|
|
491
|
+
name: "ci.waitAndFix",
|
|
492
|
+
about:
|
|
493
|
+
"Waits for CI, having the agent fix red in the worktree, up to the repo's limit of rounds.",
|
|
494
|
+
inputKinds: {
|
|
495
|
+
pr: "PullRequestNumber",
|
|
496
|
+
worktree: "WorktreePath",
|
|
497
|
+
branch: "BranchName",
|
|
498
|
+
base: "Option<BaseBranch>",
|
|
499
|
+
lane: "LaneId",
|
|
500
|
+
},
|
|
501
|
+
outputKinds: "nothing",
|
|
502
|
+
outcomeKinds: {},
|
|
503
|
+
before: ["issue.handOff"],
|
|
504
|
+
stepNames: [CI_STEPS],
|
|
505
|
+
inputs: {
|
|
506
|
+
pr: "required",
|
|
507
|
+
worktree: "required",
|
|
508
|
+
branch: "required",
|
|
509
|
+
base: "required",
|
|
510
|
+
lane: "optional",
|
|
511
|
+
},
|
|
512
|
+
run: (input: {
|
|
513
|
+
readonly pr: PullRequestNumber;
|
|
514
|
+
readonly worktree: WorktreePath;
|
|
515
|
+
readonly branch: BranchName;
|
|
516
|
+
readonly base: Option.Option<BaseBranch>;
|
|
517
|
+
readonly lane?: LaneId;
|
|
518
|
+
}) =>
|
|
519
|
+
Effect.gen(function* () {
|
|
520
|
+
const session = yield* laneSession(input.lane);
|
|
521
|
+
|
|
522
|
+
return yield* Implement.waitForCi(
|
|
523
|
+
input.pr,
|
|
524
|
+
input.worktree,
|
|
525
|
+
{ ...session, branch: input.branch, ...onBase(input.base) },
|
|
526
|
+
inLane(input.lane),
|
|
527
|
+
);
|
|
528
|
+
}),
|
|
529
|
+
failures: {
|
|
530
|
+
CiTimedOut: "person",
|
|
531
|
+
NeedsHuman: "person",
|
|
532
|
+
NothingCommitted: "person",
|
|
533
|
+
PushRejected: "person",
|
|
534
|
+
Skip: "skip",
|
|
535
|
+
StepFailed: "person",
|
|
536
|
+
},
|
|
537
|
+
contributes: { settings: Settings.Ci.fields },
|
|
538
|
+
outcomes: {},
|
|
539
|
+
mustRoute: [],
|
|
540
|
+
}),
|
|
541
|
+
|
|
542
|
+
"issue.handOff": defineBlock({
|
|
543
|
+
name: "issue.handOff",
|
|
544
|
+
about:
|
|
545
|
+
"Asks a reviewer from the pool for a review, and tells the issue. A draft from pr.open asks nobody: the issue is told a person marks it ready for review.",
|
|
546
|
+
inputKinds: {
|
|
547
|
+
issue: "IssueNumber",
|
|
548
|
+
pr: "a pull request, from pr.open or stack.publish",
|
|
549
|
+
outcome: "what the agent said",
|
|
550
|
+
lane: "LaneId",
|
|
551
|
+
},
|
|
552
|
+
outputKinds: "a summary of the hand-off, for `end`'s summary",
|
|
553
|
+
outcomeKinds: {},
|
|
554
|
+
closes: ["hand-off"],
|
|
555
|
+
stepNames: [HAND_OFF_STEPS],
|
|
556
|
+
inputs: { issue: "required", pr: "required", outcome: "required", lane: "optional" },
|
|
557
|
+
run: (input: {
|
|
558
|
+
readonly issue: IssueNumber;
|
|
559
|
+
readonly pr: {
|
|
560
|
+
readonly number: PullRequestNumber;
|
|
561
|
+
readonly url: string;
|
|
562
|
+
readonly draft?: boolean;
|
|
563
|
+
};
|
|
564
|
+
readonly outcome: Implement.ImplementOutcome;
|
|
565
|
+
readonly lane?: LaneId;
|
|
566
|
+
}) =>
|
|
567
|
+
Implement.handOff(
|
|
568
|
+
input.issue,
|
|
569
|
+
{ number: input.pr.number, url: input.pr.url },
|
|
570
|
+
input.outcome,
|
|
571
|
+
{
|
|
572
|
+
// A draft is a person's to mark ready, and to ask for its review.
|
|
573
|
+
needsReview: input.pr.draft !== true,
|
|
574
|
+
draft: input.pr.draft === true,
|
|
575
|
+
...inLane(input.lane),
|
|
576
|
+
},
|
|
577
|
+
),
|
|
578
|
+
failures: {
|
|
579
|
+
NeedsHuman: "person",
|
|
580
|
+
NotFound: "skip",
|
|
581
|
+
PullRequestClosed: "person",
|
|
582
|
+
Skip: "skip",
|
|
583
|
+
StepFailed: "person",
|
|
584
|
+
},
|
|
585
|
+
contributes: { settings: Settings.Reviewers.fields, doctor: [Review.poolCheck] },
|
|
586
|
+
outcomes: {},
|
|
587
|
+
mustRoute: [],
|
|
588
|
+
}),
|
|
589
|
+
|
|
590
|
+
"review.round": defineBlock({
|
|
591
|
+
name: "review.round",
|
|
592
|
+
fixed: ["role"],
|
|
593
|
+
// A single review gives its round; a loop's rounds take theirs from the loop.
|
|
594
|
+
either: ["round"],
|
|
595
|
+
about: "Has a fresh agent session review the pull request and post a real GitHub review.",
|
|
596
|
+
inputKinds: {
|
|
597
|
+
pr: "PullRequestNumber",
|
|
598
|
+
round:
|
|
599
|
+
"it names the review's steps: 1 for a single review, and in a repeat, round from the loop, so each round's review is a new one",
|
|
600
|
+
role: "any name, such as reviewer. Optional in config: flows.<flow>.agents.<role> picks the agent for it, and without that it's the flow's agent",
|
|
601
|
+
},
|
|
602
|
+
outputKinds: {
|
|
603
|
+
verdict: "approve or request-changes",
|
|
604
|
+
changesAsked: "a summary, for `end`'s summary",
|
|
605
|
+
},
|
|
606
|
+
outcomeKinds: {
|
|
607
|
+
approve: "an object: with as: verdict, verdict.approved is the Approved proof for pr.land",
|
|
608
|
+
changes: "nothing",
|
|
609
|
+
},
|
|
610
|
+
inputs: { pr: "required", round: "required", role: "required" },
|
|
611
|
+
run: (input: {
|
|
612
|
+
readonly pr: PullRequestNumber;
|
|
613
|
+
readonly round: number;
|
|
614
|
+
readonly role: string;
|
|
615
|
+
}) =>
|
|
616
|
+
Effect.gen(function* () {
|
|
617
|
+
const review = yield* Review.round(input.pr, input.round, { role: input.role });
|
|
618
|
+
yield* Review.doneReviewing(input.pr);
|
|
619
|
+
|
|
620
|
+
return {
|
|
621
|
+
verdict: review.verdict,
|
|
622
|
+
changesAsked: summaryOf(input.pr, input.round),
|
|
623
|
+
};
|
|
624
|
+
}),
|
|
625
|
+
failures: {
|
|
626
|
+
NeedsHuman: "told",
|
|
627
|
+
NotFound: "skip",
|
|
628
|
+
PullRequestClosed: "skip",
|
|
629
|
+
Skip: "skip",
|
|
630
|
+
StepFailed: "fail",
|
|
631
|
+
},
|
|
632
|
+
outcomes: {
|
|
633
|
+
approve: (review: { readonly verdict: string }) =>
|
|
634
|
+
Option.liftPredicate({ approved: new Approved({}) }, () => review.verdict === "approve"),
|
|
635
|
+
changes: (review: { readonly verdict: string }) =>
|
|
636
|
+
Option.liftPredicate({}, () => review.verdict !== "approve"),
|
|
637
|
+
},
|
|
638
|
+
mustRoute: ["approve", "changes"],
|
|
639
|
+
exhaustive: true,
|
|
640
|
+
contributes: { settings: { ...Settings.Reviewers.fields, ...Settings.Rules.fields } },
|
|
641
|
+
}),
|
|
642
|
+
|
|
643
|
+
// Chernabog merges only pull requests on the automated path (auto:ship), and
|
|
644
|
+
// only when that's safe; a person merges any other. A layer of a stack waits,
|
|
645
|
+
// approved, for the layer below. One refused because it conflicts with its
|
|
646
|
+
// base is left to resolve-conflicts.
|
|
647
|
+
"pr.land": defineBlock({
|
|
648
|
+
name: "pr.land",
|
|
649
|
+
fixed: ["method"],
|
|
650
|
+
about:
|
|
651
|
+
"Merges an approved pull request only if it is labelled auto:ship (the repo's ship label), and when that's safe. Without the label it merges nothing: a person does. One that conflicts is left to resolve-conflicts.",
|
|
652
|
+
inputKinds: {
|
|
653
|
+
pr: "PullRequestNumber",
|
|
654
|
+
approved: "Approved: from review.round",
|
|
655
|
+
method: "squash, merge or rebase",
|
|
656
|
+
},
|
|
657
|
+
outputKinds: "a summary, for `end`'s summary",
|
|
658
|
+
outcomeKinds: {},
|
|
659
|
+
inputs: { pr: "required", approved: "required", method: "required" },
|
|
660
|
+
run: (input: {
|
|
661
|
+
readonly pr: PullRequestNumber;
|
|
662
|
+
readonly approved: Approved;
|
|
663
|
+
readonly method: "squash" | "merge" | "rebase";
|
|
664
|
+
}) =>
|
|
665
|
+
Effect.gen(function* () {
|
|
666
|
+
if (!(yield* PullRequest.shipping(input.pr))) {
|
|
667
|
+
return `#${input.pr} approved; a person merges`;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
const waiting = yield* PullRequest.waitingOnLayer(input.pr);
|
|
671
|
+
|
|
672
|
+
if (Option.isSome(waiting)) {
|
|
673
|
+
return `#${input.pr} approved; ${waiting.value}`;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
// While CI on the base branch is red, the merge holds rather than going to a person.
|
|
677
|
+
return yield* PullRequest.mergeHolding(input.pr, { method: input.method }).pipe(
|
|
678
|
+
Effect.catchTag("MergeRefused", (refused) =>
|
|
679
|
+
Effect.gen(function* () {
|
|
680
|
+
if (yield* conflicts(input.pr)) {
|
|
681
|
+
return `#${input.pr} approved, but it conflicts with its base; resolve-conflicts takes it`;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
return yield* PullRequest.handToPerson(input.pr, refused.message);
|
|
685
|
+
}),
|
|
686
|
+
),
|
|
687
|
+
);
|
|
688
|
+
}),
|
|
689
|
+
failures: {
|
|
690
|
+
NeedsHuman: "told",
|
|
691
|
+
NotFound: "skip",
|
|
692
|
+
PullRequestClosed: "skip",
|
|
693
|
+
Skip: "skip",
|
|
694
|
+
StepFailed: "fail",
|
|
695
|
+
},
|
|
696
|
+
contributes: {
|
|
697
|
+
settings: { ...Settings.Base.fields, ...Settings.MergeHold.fields },
|
|
698
|
+
wakes: PullRequest.baseTurnedGreen.pipe(reportAs("trigger")),
|
|
699
|
+
},
|
|
700
|
+
outcomes: {},
|
|
701
|
+
mustRoute: [],
|
|
702
|
+
}),
|
|
703
|
+
}).entries;
|
|
704
|
+
|
|
705
|
+
export type BlockName = keyof typeof catalogue;
|
|
706
|
+
|
|
707
|
+
// The sources of the files beside this one.
|
|
708
|
+
const sourcesElsewhere = gather("source", "epic-blocks.ts", epicSources)
|
|
709
|
+
.and("re-review-blocks.ts", reReviewSources)
|
|
710
|
+
.and("address-review-blocks.ts", addressReviewSources)
|
|
711
|
+
.and("resolve-conflicts-blocks.ts", resolveConflictsSources)
|
|
712
|
+
.and("post-merge-blocks.ts", postMergeSources)
|
|
713
|
+
.and("plan-blocks.ts", planSources);
|
|
714
|
+
|
|
715
|
+
// The labels issues.queued queues by, by what each means.
|
|
716
|
+
const QUEUE_LABELS = ["eligible", "ship"] as const;
|
|
717
|
+
|
|
718
|
+
type QueueLabel = (typeof QUEUE_LABELS)[number];
|
|
719
|
+
|
|
720
|
+
// Where a flow's work comes from, by the name a document uses it by.
|
|
721
|
+
export const sources = sourcesElsewhere.and("blocks.ts", {
|
|
722
|
+
// Issues carrying each of `labels` of the repo's labels, oldest first but a
|
|
723
|
+
// post-merge follow-up before the rest, the first label's before the next's.
|
|
724
|
+
// One carrying several comes under the first. Any label but eligible queues
|
|
725
|
+
// its issue by that label, which its pull request carries too.
|
|
726
|
+
"issues.queued": defineSource(
|
|
727
|
+
"issues.queued",
|
|
728
|
+
"issue",
|
|
729
|
+
(input: { readonly labels: ReadonlyArray<QueueLabel> }) => ({
|
|
730
|
+
trigger: Effect.gen(function* () {
|
|
731
|
+
const github = yield* GitHub;
|
|
732
|
+
const names = yield* Settings.labels;
|
|
733
|
+
const found = new Set<number>();
|
|
734
|
+
const items: Array<WorkItem> = [];
|
|
735
|
+
|
|
736
|
+
for (const key of input.labels) {
|
|
737
|
+
const issues = (yield* github.queuedIssuesLabelled(names[key])).filter(
|
|
738
|
+
(issue) => !found.has(issue.number),
|
|
739
|
+
);
|
|
740
|
+
|
|
741
|
+
for (const issue of issues) {
|
|
742
|
+
found.add(issue.number);
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
const queuedAs = Option.liftPredicate(names[key], () => key !== "eligible");
|
|
746
|
+
|
|
747
|
+
for (const item of yield* Implement.queued(Implement.followUpsFirst(issues), queuedAs)) {
|
|
748
|
+
items.push(item);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
return items;
|
|
753
|
+
}).pipe(reportAs("trigger")),
|
|
754
|
+
contributes: {
|
|
755
|
+
settings: { ...Routing.Settings.fields, ...Settings.Board.fields },
|
|
756
|
+
},
|
|
757
|
+
}),
|
|
758
|
+
),
|
|
759
|
+
|
|
760
|
+
// Pull requests that ask this persona for a review it hasn't given yet,
|
|
761
|
+
// routed by their assignees and reviewers.
|
|
762
|
+
"review.requested": defineSource("review.requested", "pull-request", () => ({
|
|
763
|
+
trigger: Effect.gen(function* () {
|
|
764
|
+
const found = yield* Review.reviewRequests;
|
|
765
|
+
|
|
766
|
+
return yield* Effect.forEach(
|
|
767
|
+
found.filter(({ reviewed }) => reviewed === 0),
|
|
768
|
+
({ pull }) => Review.routed(pull, Review.reviewItem(pull, `pr-${pull.number}`)),
|
|
769
|
+
);
|
|
770
|
+
}).pipe(reportAs("trigger")),
|
|
771
|
+
contributes: {
|
|
772
|
+
settings: Routing.Settings.fields,
|
|
773
|
+
doctor: [Review.reviewerCheck],
|
|
774
|
+
ownWork: true,
|
|
775
|
+
},
|
|
776
|
+
})),
|
|
777
|
+
|
|
778
|
+
// Those waiting on a reviewer no persona on this machine acts as, which
|
|
779
|
+
// status lists.
|
|
780
|
+
"review.waitingElsewhere": defineSource("review.waitingElsewhere", "pull-request", () => ({
|
|
781
|
+
trigger: Review.waitingElsewhere.pipe(reportAs("trigger")),
|
|
782
|
+
contributes: {},
|
|
783
|
+
})),
|
|
784
|
+
} satisfies { readonly [name: string]: { readonly subject: SubjectName } }).entries;
|
|
785
|
+
|
|
786
|
+
// What a loop's state can start from, by the name a document uses it by.
|
|
787
|
+
export const loopStates = { ...epicLoopStates, ...planLoopStates };
|
|
788
|
+
|
|
789
|
+
export { Approved, Committed, defineBlock, gather, laneScopes, Pushed, runStage, ToPerson };
|
|
790
|
+
|
|
791
|
+
export type { Disposition, Failures, StageFailures, StageKey } from "./block.ts";
|
|
792
|
+
|
|
793
|
+
// What each source does and takes, for someone choosing one.
|
|
794
|
+
export const sourceDescriptions = {
|
|
795
|
+
"epic.queued": {
|
|
796
|
+
about: "Open issues labelled auto:epic with an open task, and wakes a run parked on one.",
|
|
797
|
+
with: {},
|
|
798
|
+
},
|
|
799
|
+
"issues.queued": {
|
|
800
|
+
about:
|
|
801
|
+
"Open issues carrying each of the labels, oldest first but a post-merge follow-up before the rest.",
|
|
802
|
+
with: {
|
|
803
|
+
labels:
|
|
804
|
+
"a list of eligible and ship, which name the repo's labels (auto:eligible and auto:ship by default). The first is the normal queue; an issue queued by a later one carries that label to its pull request. To queue a flow's issues by a label of its own, such as docs, set flows.<flow>.settings.labels.eligible to it in .chernabog/config.yaml.",
|
|
805
|
+
},
|
|
806
|
+
choices: { labels: QUEUE_LABELS },
|
|
807
|
+
},
|
|
808
|
+
"pr.conflicting": {
|
|
809
|
+
about:
|
|
810
|
+
"Pull requests this persona wrote that GitHub says conflict with their base (mergeable_state dirty), except those labelled auto:needs-human or owned by another run. One GitHub hasn't checked yet is left for the next tick. Each run's key carries the head: pr-7-abc1234, so a conflict that comes back after a later push is new work.",
|
|
811
|
+
with: {},
|
|
812
|
+
},
|
|
813
|
+
"plan.queued": {
|
|
814
|
+
about:
|
|
815
|
+
"Open issues labelled auto:plan, oldest first, each a spec to plan; with a board, only those whose Status is its readyStatus. Each is routed by its assignees, and its run's key is the issue's: issue-7.",
|
|
816
|
+
with: {},
|
|
817
|
+
},
|
|
818
|
+
"pr.mergedRecently": {
|
|
819
|
+
about:
|
|
820
|
+
"Pull requests this persona wrote that merged into the flow's base branch (its base setting, or GitHub's default branch) in the last day, among the 30 closed most lately, and that no run has watched yet: merged.markWatched leaves a marker that this looks for. Each run's key is the pull request's: merged-7.",
|
|
821
|
+
with: {},
|
|
822
|
+
},
|
|
823
|
+
"review.requested": {
|
|
824
|
+
about: "Pull requests that ask this persona for a review it hasn't given yet.",
|
|
825
|
+
with: {},
|
|
826
|
+
},
|
|
827
|
+
"review.changesRequested": {
|
|
828
|
+
about:
|
|
829
|
+
"Pull requests this persona wrote whose latest approval or request for changes asks for changes on the current head, so nothing was pushed since, except those labelled auto:needs-human or owned by another run. Each run's key carries the review: pr-7-changes-2.",
|
|
830
|
+
with: {},
|
|
831
|
+
},
|
|
832
|
+
"review.requestedAgain": {
|
|
833
|
+
about:
|
|
834
|
+
"Pull requests that ask this persona to review again after it reviewed them, one run per request, except those labelled auto:needs-human. Each run's key carries the review it asks for: pr-7-review-2.",
|
|
835
|
+
with: {},
|
|
836
|
+
},
|
|
837
|
+
"review.waitingElsewhere": {
|
|
838
|
+
about: "Pull requests waiting on a reviewer no persona on this machine acts as, for status.",
|
|
839
|
+
with: {},
|
|
840
|
+
},
|
|
841
|
+
} satisfies {
|
|
842
|
+
readonly [Name in SourceName]: {
|
|
843
|
+
readonly about: string;
|
|
844
|
+
readonly with: { readonly [input: string]: string };
|
|
845
|
+
// The values a list under with may hold, where they are a closed set.
|
|
846
|
+
readonly choices?: { readonly [input: string]: ReadonlyArray<string> };
|
|
847
|
+
};
|
|
848
|
+
};
|
|
849
|
+
|
|
850
|
+
export type SourceName = keyof typeof sources;
|
|
851
|
+
|
|
852
|
+
type Scoped<A, E, R> = (context: FailureContext) => Effect.Effect<A, E, R>;
|
|
853
|
+
|
|
854
|
+
// What a flow works on: what a work item says of it, what a failure going to
|
|
855
|
+
// a person does with it, and how the engine tells whether it is still there.
|
|
856
|
+
export const subjects = {
|
|
857
|
+
"changes-asked": changesAskedSubject,
|
|
858
|
+
|
|
859
|
+
conflict: conflictSubject,
|
|
860
|
+
|
|
861
|
+
epic: epicSubject,
|
|
862
|
+
|
|
863
|
+
issue: {
|
|
864
|
+
fields: ["issue", "queued"],
|
|
865
|
+
kinds: { issue: "IssueNumber", queued: "how the issue was queued" },
|
|
866
|
+
failureContext: ["pr"],
|
|
867
|
+
read: (item: WorkItem) =>
|
|
868
|
+
Effect.gen(function* () {
|
|
869
|
+
return {
|
|
870
|
+
issue: IssueNumber(yield* Implement.issueOf(item)),
|
|
871
|
+
queued: yield* Implement.queuedBy(item),
|
|
872
|
+
};
|
|
873
|
+
}),
|
|
874
|
+
// However the run ends, a claim on the issue goes; the branch and any pull
|
|
875
|
+
// request stay.
|
|
876
|
+
scope: <A, E, R>(
|
|
877
|
+
trigger: { readonly issue: IssueNumber },
|
|
878
|
+
options: { readonly claims: boolean },
|
|
879
|
+
body: Scoped<A, E | ToPerson, R>,
|
|
880
|
+
) => {
|
|
881
|
+
const context: FailureContext = { pr: Option.none() };
|
|
882
|
+
|
|
883
|
+
const handled = Effect.suspend(() => body(context)).pipe(
|
|
884
|
+
Effect.catchIf(
|
|
885
|
+
(failure): failure is ToPerson => failure instanceof ToPerson,
|
|
886
|
+
(failure) =>
|
|
887
|
+
Implement.handToHuman(trigger.issue, failure.reason, context.pr).pipe(
|
|
888
|
+
Effect.flatMap(Implement.endForPerson),
|
|
889
|
+
),
|
|
890
|
+
),
|
|
891
|
+
);
|
|
892
|
+
|
|
893
|
+
if (options.claims) {
|
|
894
|
+
return handled.pipe(Claims.releasing(Claims.issue(trigger.issue)));
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
return handled;
|
|
898
|
+
},
|
|
899
|
+
stillValid: Option.some(Implement.stillValid),
|
|
900
|
+
},
|
|
901
|
+
|
|
902
|
+
merged: mergedSubject,
|
|
903
|
+
|
|
904
|
+
spec: specSubject,
|
|
905
|
+
|
|
906
|
+
"pull-request": {
|
|
907
|
+
fields: ["pr"],
|
|
908
|
+
kinds: { pr: "PullRequestNumber" },
|
|
909
|
+
failureContext: [],
|
|
910
|
+
read: (item: WorkItem) =>
|
|
911
|
+
Review.pullRequestOf(item).pipe(
|
|
912
|
+
Effect.map(({ pullRequest }) => ({ pr: PullRequestNumber(pullRequest) })),
|
|
913
|
+
),
|
|
914
|
+
scope: <A, E, R>(
|
|
915
|
+
trigger: { readonly pr: PullRequestNumber },
|
|
916
|
+
_options: { readonly claims: boolean },
|
|
917
|
+
body: Scoped<A, E | ToPerson, R>,
|
|
918
|
+
) =>
|
|
919
|
+
Effect.suspend(() => body({ pr: Option.some(trigger.pr) })).pipe(
|
|
920
|
+
Effect.catchIf(
|
|
921
|
+
(failure): failure is ToPerson => failure instanceof ToPerson,
|
|
922
|
+
(failure) => PullRequest.handToPerson(trigger.pr, failure.reason),
|
|
923
|
+
),
|
|
924
|
+
),
|
|
925
|
+
stillValid: Option.some(Review.stillValid),
|
|
926
|
+
},
|
|
927
|
+
};
|
|
928
|
+
|
|
929
|
+
export type SubjectName = keyof typeof subjects;
|
|
930
|
+
|
|
931
|
+
// The subjects each block suits, by what it needs: see suits.ts.
|
|
932
|
+
export const blockSubjects = suited(
|
|
933
|
+
Object.values(catalogue),
|
|
934
|
+
Object.fromEntries(Object.entries(subjects).map(([name, subject]) => [name, subject.kinds])),
|
|
935
|
+
);
|
|
936
|
+
|
|
937
|
+
// How a person names the work of a flow they start, rather than a trigger
|
|
938
|
+
// finding it, by the subject it's on: `chernabog start <issue>` names an issue,
|
|
939
|
+
// whatever its labels. A subject without one can't be started; so far only an
|
|
940
|
+
// issue can.
|
|
941
|
+
export const startOf = (subject: string): Option.Option<ItemLookup> =>
|
|
942
|
+
Option.liftPredicate(Hybrid.itemFor, () => subject === "issue");
|
|
943
|
+
|
|
944
|
+
const unique = <A>(all: ReadonlyArray<A>) => [...new Set(all)];
|
|
945
|
+
|
|
946
|
+
// How the engine tells whether a subject's run is still wanted, when it can.
|
|
947
|
+
const stillValidOf = (subject: SubjectName): Option.Option<StillValid> =>
|
|
948
|
+
subjects[subject].stillValid;
|
|
949
|
+
|
|
950
|
+
// A flow from what a document says, with what its sources and blocks add:
|
|
951
|
+
// settings, labels, doctor checks, and the wake-ups of runs parked in them. A
|
|
952
|
+
// flow a person starts has no sources and no trigger: it runs only when they
|
|
953
|
+
// name its work, which its subject's item lookup finds, and each run is
|
|
954
|
+
// theirs, so it finds only its own persona's work.
|
|
955
|
+
export const assemble = (flow: {
|
|
956
|
+
readonly subject: SubjectName;
|
|
957
|
+
readonly gate?: "usage";
|
|
958
|
+
readonly concurrency?: number;
|
|
959
|
+
readonly start?: boolean;
|
|
960
|
+
readonly sources?: ReadonlyArray<Source>;
|
|
961
|
+
readonly blocks: ReadonlyArray<BlockName>;
|
|
962
|
+
// The document's tree as data (Compose.plan), which a run is drawn on. The
|
|
963
|
+
// flow carries it, so the loader finds it on a flow compiled from a document.
|
|
964
|
+
readonly plan?: Schema.Json;
|
|
965
|
+
readonly workflow: (item: WorkItem) => Effect.Effect<string | void, FlowError, WorkflowServices>;
|
|
966
|
+
}) => {
|
|
967
|
+
const flowSources = flow.sources ?? [];
|
|
968
|
+
|
|
969
|
+
const contributions: ReadonlyArray<Contribution> = [
|
|
970
|
+
...flowSources.map((source) => source.contributes),
|
|
971
|
+
...flow.blocks.map((block) => catalogue[block].contributes),
|
|
972
|
+
];
|
|
973
|
+
|
|
974
|
+
const labels = unique(contributions.flatMap((each) => each.labels ?? []));
|
|
975
|
+
const namedLabels = unique(contributions.flatMap((each) => each.namedLabels ?? []));
|
|
976
|
+
|
|
977
|
+
const fields = {
|
|
978
|
+
...Settings.Labels.fields,
|
|
979
|
+
...Object.fromEntries(contributions.flatMap((each) => Object.entries(each.settings ?? {}))),
|
|
980
|
+
};
|
|
981
|
+
|
|
982
|
+
const triggers = [
|
|
983
|
+
...flowSources.map((source) => source.trigger),
|
|
984
|
+
...unique(contributions.flatMap((each) => Option.toArray(Option.fromUndefinedOr(each.wakes)))),
|
|
985
|
+
];
|
|
986
|
+
|
|
987
|
+
const doctor = unique(contributions.flatMap((each) => each.doctor ?? []));
|
|
988
|
+
|
|
989
|
+
const common = {
|
|
990
|
+
// None, as a recipe without one declares it, when the document gives none; the engine's default is 1.
|
|
991
|
+
...Option.match(Option.fromUndefinedOr(flow.concurrency), {
|
|
992
|
+
onNone: () => ({}),
|
|
993
|
+
onSome: (concurrency) => ({ concurrency }),
|
|
994
|
+
}),
|
|
995
|
+
labels: Effect.map(
|
|
996
|
+
Effect.all([Settings.declaredLabels, Effect.all(namedLabels)]),
|
|
997
|
+
([set, named]) => [...set, ...labels, ...named],
|
|
998
|
+
),
|
|
999
|
+
settings: Schema.Struct(fields),
|
|
1000
|
+
// None, as a recipe with no checks declares it, when nothing contributes one.
|
|
1001
|
+
...Option.match(
|
|
1002
|
+
Option.liftPredicate(doctor, (checks) => checks.length > 0),
|
|
1003
|
+
{
|
|
1004
|
+
onNone: () => ({}),
|
|
1005
|
+
onSome: (checks) => ({ doctor: checks }),
|
|
1006
|
+
},
|
|
1007
|
+
),
|
|
1008
|
+
// None, as a recipe that finds every persona's work declares it, when the flow does.
|
|
1009
|
+
...Option.match(
|
|
1010
|
+
Option.liftPredicate(
|
|
1011
|
+
true,
|
|
1012
|
+
() => flow.start === true || contributions.some((each) => each.ownWork === true),
|
|
1013
|
+
),
|
|
1014
|
+
{
|
|
1015
|
+
onNone: () => ({}),
|
|
1016
|
+
onSome: (ownWork) => ({ ownWork }),
|
|
1017
|
+
},
|
|
1018
|
+
),
|
|
1019
|
+
// None, as a recipe without one declares it, when the subject has none.
|
|
1020
|
+
...Option.match(stillValidOf(flow.subject), {
|
|
1021
|
+
onNone: () => ({}),
|
|
1022
|
+
onSome: (stillValid) => ({ stillValid }),
|
|
1023
|
+
}),
|
|
1024
|
+
workflow: flow.workflow,
|
|
1025
|
+
...Option.match(
|
|
1026
|
+
Option.liftPredicate(Implement.gate, () => flow.gate === "usage"),
|
|
1027
|
+
{
|
|
1028
|
+
onNone: () => ({}),
|
|
1029
|
+
onSome: (gate) => ({ gate }),
|
|
1030
|
+
},
|
|
1031
|
+
),
|
|
1032
|
+
};
|
|
1033
|
+
|
|
1034
|
+
const started = Option.flatMap(
|
|
1035
|
+
Option.liftPredicate(flow.subject, () => flow.start === true),
|
|
1036
|
+
startOf,
|
|
1037
|
+
);
|
|
1038
|
+
|
|
1039
|
+
const defined = Option.match(started, {
|
|
1040
|
+
onNone: () =>
|
|
1041
|
+
defineFlow({ ...common, trigger: Effect.map(Effect.all(triggers), (found) => found.flat()) }),
|
|
1042
|
+
onSome: (item) => defineFlow({ ...common, item, start: true }),
|
|
1043
|
+
});
|
|
1044
|
+
|
|
1045
|
+
// Not enumerable, so the flow's keys stay those of a recipe written in
|
|
1046
|
+
// TypeScript, which declares none.
|
|
1047
|
+
return Option.match(Option.fromUndefinedOr(flow.plan), {
|
|
1048
|
+
onNone: () => defined,
|
|
1049
|
+
onSome: (plan) => Object.defineProperty({ ...defined }, "plan", { value: plan }),
|
|
1050
|
+
});
|
|
1051
|
+
};
|