@chernabog/recipes 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,36 @@
1
- # Temporary Holding Version
1
+ # @chernabog/recipes
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Chernabog's recipes: complete flows, written from [`@chernabog/sdk`](https://www.npmjs.com/package/@chernabog/sdk)'s blocks, that a repo copies into its `.chernabog/` or imports from this package. Each recipe is documented at <https://dataquail.github.io/chernabog/recipes/>.
4
+
5
+ Its recipes are `implement`, `review`, `re-review`, `address-review` and `resolve-conflicts`, the review loop; `plan`, which splits a spec into an epic of sub-issues; `epic`, which works through an epic's tasks as pull requests, each based on the branch of the task it builds on; and `human-initiated`, the hybrid path.
6
+
7
+ `chernabog init` adds this package to a repo's `.chernabog/package.json`. You don't need to install it by hand.
8
+
9
+ ## The recipe package format
10
+
11
+ Any npm package can hold recipes. This one is the first, and a company's own package is read in exactly the same way. Its `package.json` names them under `chernabog.recipes`:
12
+
13
+ ```json
14
+ {
15
+ "name": "@acme/chernabog-recipes",
16
+ "version": "1.0.0",
17
+ "peerDependencies": { "@chernabog/sdk": "^0.6.0" },
18
+ "chernabog": {
19
+ "recipes": {
20
+ "deploy": {
21
+ "description": "Deploys a merged pull request",
22
+ "flow": "flows/deploy.ts",
23
+ "prompts": ["prompts/deploy.md"],
24
+ "stages": ["stages/smoke-test.ts"],
25
+ "docs": "https://acme.example/chernabog/deploy"
26
+ }
27
+ }
28
+ }
29
+ }
30
+ ```
31
+
32
+ - `flow` is the recipe's flow, under `flows/`. `prompts` are the prompts it renders, under `prompts/`, and `stages` are the steps it runs, under `stages/`. The files keep the layout of a repo's `.chernabog/`, so a copy lands at the same path.
33
+ - The `@chernabog/sdk` peer range is the SDK versions its flows are written against.
34
+ - **Step names are public API.** A run in flight resumes by its step names, and a new version of the package reaches every repo that imports from it at once. Renaming a step is a breaking change for the package.
35
+
36
+ [Writing a recipe package](https://dataquail.github.io/chernabog/recipes/packages/) covers the format, the SDK range, type-checking, rehearsing and publishing.
@@ -0,0 +1,166 @@
1
+ // Answers a review that asks for changes on a pull request this persona
2
+ // wrote, in the session that wrote the code. It considers the findings first:
3
+ // when it disagrees, it replies with its reasons and hands the pull request to
4
+ // a person, without pushing. Otherwise it makes the changes, runs the local
5
+ // gate, pushes, waits for CI and asks the reviewer again, whose re-review
6
+ // takes it from there.
7
+ //
8
+ // This file is chernabog's address-review recipe, and this repo's to change:
9
+ // `chernabog recipes diff` shows how it differs from the recipe's current
10
+ // version. The names of its steps ("consider-review-1", "push-review-1") are
11
+ // the keys a run in flight resumes by, so renaming one restarts that step.
12
+
13
+ import {
14
+ defineFlow,
15
+ Implement,
16
+ NeedsHuman,
17
+ PullRequest,
18
+ reportAs,
19
+ Review,
20
+ Routing,
21
+ Settings,
22
+ Skip,
23
+ type WorkItem,
24
+ } from "@chernabog/sdk";
25
+ import * as Arr from "effect/Array";
26
+ import * as Effect from "effect/Effect";
27
+ import * as Option from "effect/Option";
28
+ import * as Schema from "effect/Schema";
29
+
30
+ // Pull requests this persona wrote whose latest approval or request for
31
+ // changes asks for changes on the current head, so nothing was pushed since.
32
+ // A person has one labelled auto:needs-human, and a run of another flow one it
33
+ // owns; both are left alone. Each is routed by its assignees and reviewers.
34
+ const trigger = Effect.gen(function* () {
35
+ const labelNames = yield* Settings.labels;
36
+ const items: Array<WorkItem> = [];
37
+
38
+ for (const pull of yield* Review.authoredByMe) {
39
+ if (pull.labels.includes(labelNames.needsHuman)) {
40
+ continue;
41
+ }
42
+
43
+ const decisive = yield* Review.decisiveReviews(pull.number);
44
+ const latest = Arr.last(decisive);
45
+
46
+ if (
47
+ !Option.exists(
48
+ latest,
49
+ (review) => review.state === "CHANGES_REQUESTED" && review.commitSha === pull.headSha,
50
+ )
51
+ ) {
52
+ continue;
53
+ }
54
+
55
+ if (yield* Review.ownedByARun(pull)) {
56
+ continue;
57
+ }
58
+
59
+ for (const review of Option.toArray(latest)) {
60
+ items.push(yield* Review.routed(pull, Review.addressItem(pull, review, decisive.length)));
61
+ }
62
+ }
63
+
64
+ return items;
65
+ }).pipe(reportAs("trigger"));
66
+
67
+ const answerReview = (address: Review.AddressItem) =>
68
+ Effect.gen(function* () {
69
+ const review = yield* Review.changesAsked(address);
70
+ const round = review.round ?? 1;
71
+ const author = yield* Review.authorSession(address.pullRequest);
72
+
73
+ // A pull request a person worked on with the agent reopens their session in front of them.
74
+ const session = {
75
+ sessionId: Option.some(author.sessionId),
76
+ adapter: Option.some(author.adapter),
77
+ withPerson: author.withPerson === true,
78
+ };
79
+
80
+ const worktree = yield* Review.prepareAuthorWorktree(address.headRef);
81
+ const decision = yield* Implement.considerFindings(address.pullRequest, review, session);
82
+
83
+ // A dispute is a person's to settle: it never pushes, nor asks the reviewer again.
84
+ if (decision.status === "decline") {
85
+ yield* PullRequest.comment(
86
+ address.pullRequest,
87
+ `Not addressing review ${round}:\n\n${decision.reasons}`,
88
+ );
89
+
90
+ return yield* new NeedsHuman({
91
+ reason: `the author disputes review ${round}; see its reasons above`,
92
+ });
93
+ }
94
+
95
+ const outcome = yield* Implement.addressFindings(address.pullRequest, review, session);
96
+
97
+ if (outcome.status === "needs-clarification") {
98
+ return yield* new NeedsHuman({
99
+ reason: `the author agent needs a person to answer review ${round}:\n\n${outcome.questions ?? outcome.summary}`,
100
+ });
101
+ }
102
+
103
+ yield* Review.checkChanged(address.pullRequest, round, address.reviewedSha, worktree);
104
+ yield* Implement.localGate(worktree, { id: `review-${round}`, session });
105
+ // Only a fix made in front of a person may touch humanOnlyPaths. A layer
106
+ // of a stack is checked against the layer below, so only its own commits count.
107
+ yield* Implement.push(`push-review-${round}`, worktree, address.headRef, {
108
+ base: address.baseRef,
109
+ withPerson: session.withPerson,
110
+ });
111
+ yield* Implement.waitForCi(
112
+ address.pullRequest,
113
+ worktree,
114
+ { ...session, branch: address.headRef, base: address.baseRef },
115
+ { id: `review-${round}` },
116
+ );
117
+ yield* Review.askAgain(address.pullRequest, round, address.reviewer, outcome.summary);
118
+
119
+ return `#${address.pullRequest}: addressed review ${round}; asked ${address.reviewer} to review again`;
120
+ });
121
+
122
+ const workflow = (item: WorkItem) =>
123
+ Effect.gen(function* () {
124
+ const address = yield* Review.addressItemOf(item);
125
+
126
+ const toPerson = (reason: string) => PullRequest.handToPerson(address.pullRequest, reason);
127
+
128
+ // Anything that stops it hands the pull request to a person, unless it closed meanwhile.
129
+ return yield* answerReview(address).pipe(
130
+ Effect.catchTags({
131
+ NeedsHuman: (needsHuman) => toPerson(needsHuman.reason),
132
+ StepFailed: (failed) => toPerson(`${failed.step}: ${failed.message}`),
133
+ NothingCommitted: (nothing) =>
134
+ toPerson(`the author agent committed nothing on ${nothing.branch}`),
135
+ // Its message says how to fix the token, such as one without the workflow scope.
136
+ PushRejected: (rejected) => toPerson(rejected.message),
137
+ CiTimedOut: (timedOut) => toPerson(timedOut.message),
138
+ PullRequestClosed: (closed) => Effect.fail(new Skip({ reason: closed.message })),
139
+ NotFound: (gone) => Effect.fail(new Skip({ reason: gone.message })),
140
+ }),
141
+ );
142
+ });
143
+
144
+ // What config.yaml may set for this flow, under settings: or flows.<name>.settings.
145
+ const settings = Schema.Struct({
146
+ ...Routing.Settings.fields,
147
+ ...Settings.Labels.fields,
148
+ ...Settings.Base.fields,
149
+ ...Settings.LocalGate.fields,
150
+ ...Settings.Rules.fields,
151
+ ...Settings.Ci.fields,
152
+ ...Settings.Reviewers.fields,
153
+ });
154
+
155
+ export default defineFlow({
156
+ labels: Settings.declaredLabels,
157
+ settings,
158
+ trigger,
159
+ gate: Implement.gate,
160
+ concurrency: 1,
161
+ // Its trigger finds only pull requests this persona wrote.
162
+ ownWork: true,
163
+ // A pull request closed or merged ends the run as superseded.
164
+ stillValid: Review.stillValid,
165
+ workflow,
166
+ });