@chernabog/sdk 0.4.0 → 0.7.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 +39 -29
- package/package.json +1 -1
- package/src/claims.ts +169 -0
- package/src/comment.ts +6 -6
- package/src/default-prompts.ts +52 -0
- package/src/errors.ts +209 -20
- package/src/flow.ts +227 -62
- package/src/hybrid.ts +308 -0
- package/src/implement.ts +477 -191
- package/src/index.ts +17 -14
- package/src/issue.ts +2 -2
- package/src/json.ts +7 -4
- package/src/markers.ts +73 -16
- package/src/paths.ts +18 -10
- package/src/plan.ts +762 -0
- package/src/prompt.ts +76 -24
- package/src/pull-request.ts +254 -55
- package/src/review.ts +406 -301
- package/src/roles.ts +15 -8
- package/src/services.ts +239 -51
- package/src/stage.ts +4 -4
package/src/implement.ts
CHANGED
|
@@ -1,39 +1,63 @@
|
|
|
1
|
-
// The building blocks of the
|
|
1
|
+
// The building blocks of the `implement` recipe: find a queued issue,
|
|
2
2
|
// claim it, prepare a worktree, let the agent implement it, run the local gate,
|
|
3
3
|
// open the pull request, wait for CI and hand off for review. Each block is an
|
|
4
4
|
// activity, so a flow can reorder them, leave one out, or put its own step
|
|
5
5
|
// from .chernabog/stages/ in between.
|
|
6
6
|
|
|
7
|
-
import { createHash } from
|
|
8
|
-
|
|
9
|
-
import * as DateTime from
|
|
10
|
-
import * as Duration from
|
|
11
|
-
import * as Effect from
|
|
12
|
-
import * as Option from
|
|
13
|
-
import * as ChildProcess from
|
|
14
|
-
import * as ChildProcessSpawner from
|
|
15
|
-
import * as Schema from
|
|
16
|
-
import * as Stream from
|
|
17
|
-
|
|
18
|
-
import
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import
|
|
22
|
-
import
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
25
|
-
import
|
|
26
|
-
import {
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
|
|
9
|
+
import * as DateTime from "effect/DateTime";
|
|
10
|
+
import * as Duration from "effect/Duration";
|
|
11
|
+
import * as Effect from "effect/Effect";
|
|
12
|
+
import * as Option from "effect/Option";
|
|
13
|
+
import * as ChildProcess from "effect/process/ChildProcess";
|
|
14
|
+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
|
|
15
|
+
import * as Schema from "effect/Schema";
|
|
16
|
+
import * as Stream from "effect/Stream";
|
|
17
|
+
|
|
18
|
+
import * as Claims from "./claims.ts";
|
|
19
|
+
import { ADDRESS, CONSIDER } from "./default-prompts.ts";
|
|
20
|
+
import { CiTimedOut, NeedsHuman, NothingCommitted, Skip, StepFailed } from "./errors.ts";
|
|
21
|
+
import { activity, reportAs, sleep } from "./flow.ts";
|
|
22
|
+
import type { Json } from "./json.ts";
|
|
23
|
+
import { type ClaimMarker, claimMarker, latestMarker, sessionMarker } from "./markers.ts";
|
|
24
|
+
import { pathsMatching } from "./paths.ts";
|
|
25
|
+
import { renderPrompt, renderPromptOr } from "./prompt.ts";
|
|
26
|
+
import { requestReview } from "./pull-request.ts";
|
|
27
|
+
import type { ReviewResult } from "./review.ts";
|
|
28
|
+
import {
|
|
29
|
+
Agent,
|
|
30
|
+
type AgentResult,
|
|
31
|
+
Git,
|
|
32
|
+
GitHub,
|
|
33
|
+
type Issue,
|
|
34
|
+
Repo,
|
|
35
|
+
Run,
|
|
36
|
+
RunLog,
|
|
37
|
+
Usage,
|
|
38
|
+
type WorkItem,
|
|
39
|
+
Worktree,
|
|
40
|
+
} from "./services.ts";
|
|
27
41
|
|
|
28
42
|
export const branchFor = (issue: number) => `auto/issue-${issue}`;
|
|
29
43
|
|
|
30
|
-
|
|
44
|
+
// An issue to implement, and the label that queued it, when it's not eligible's.
|
|
45
|
+
export const IssueItem = Schema.Struct({
|
|
46
|
+
issue: Schema.Finite,
|
|
47
|
+
label: Schema.optionalKey(Schema.String),
|
|
48
|
+
});
|
|
31
49
|
|
|
32
|
-
export const issueItem = (
|
|
50
|
+
export const issueItem = (
|
|
51
|
+
issue: Issue,
|
|
52
|
+
label: Option.Option<string> = Option.none(),
|
|
53
|
+
): WorkItem => ({
|
|
33
54
|
key: `issue-${issue.number}`,
|
|
34
55
|
title: issue.title,
|
|
35
56
|
url: issue.url,
|
|
36
|
-
data: {
|
|
57
|
+
data: {
|
|
58
|
+
issue: issue.number,
|
|
59
|
+
...Option.match(label, { onNone: () => ({}), onSome: (queued) => ({ label: queued }) }),
|
|
60
|
+
},
|
|
37
61
|
assignees: issue.assignees,
|
|
38
62
|
});
|
|
39
63
|
|
|
@@ -41,29 +65,74 @@ export const issueItem = (issue: Issue): WorkItem => ({
|
|
|
41
65
|
export const issueOf = (item: WorkItem) =>
|
|
42
66
|
Schema.decodeUnknownEffect(IssueItem)(item.data).pipe(
|
|
43
67
|
Effect.map((data) => data.issue),
|
|
44
|
-
Effect.mapError(
|
|
68
|
+
Effect.mapError(
|
|
69
|
+
(issue) =>
|
|
70
|
+
new StepFailed({
|
|
71
|
+
step: "work item",
|
|
72
|
+
message: `${item.key} is not an issue: ${issue.message}`,
|
|
73
|
+
}),
|
|
74
|
+
),
|
|
45
75
|
);
|
|
46
76
|
|
|
47
77
|
// A stable session id per run and purpose, so a rerun after a crash resumes
|
|
48
78
|
// the agent's session instead of starting over.
|
|
49
79
|
export const sessionIdFor = (runId: string, purpose: string) => {
|
|
50
|
-
const hex = createHash(
|
|
80
|
+
const hex = createHash("sha256").update(`${runId}/${purpose}`).digest("hex");
|
|
51
81
|
const variant = ((Number.parseInt(hex[16], 16) & 0x3) | 0x8).toString(16);
|
|
52
82
|
|
|
53
83
|
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-4${hex.slice(13, 16)}-${variant}${hex.slice(17, 20)}-${hex.slice(20, 32)}`;
|
|
54
84
|
};
|
|
55
85
|
|
|
56
86
|
// Issues with a queue label, oldest first, each with its assignees. A flow
|
|
57
|
-
// keyed by its own label
|
|
58
|
-
export const triggerFor = (label: string) =>
|
|
87
|
+
// keyed by its own label pairs it with `claim(issue, queuedBy(item))`.
|
|
88
|
+
export const triggerFor = (label: string) =>
|
|
89
|
+
GitHub.use((github) => github.queuedIssuesLabelled(label)).pipe(
|
|
90
|
+
Effect.map((issues) => issues.map((issue) => issueItem(issue, Option.some(label)))),
|
|
91
|
+
reportAs("trigger"),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// The issues with no open blocker, in the order given. One that an open issue
|
|
95
|
+
// still blocks waits, so the tasks of an epic start in the order its plan gave.
|
|
96
|
+
export const unblocked = (issues: ReadonlyArray<Issue>) =>
|
|
97
|
+
Effect.gen(function* () {
|
|
98
|
+
const github = yield* GitHub;
|
|
99
|
+
const ready: Array<Issue> = [];
|
|
59
100
|
|
|
60
|
-
|
|
61
|
-
|
|
101
|
+
for (const issue of issues) {
|
|
102
|
+
const blockers = yield* github.blockedBy(issue.number);
|
|
103
|
+
|
|
104
|
+
if (!blockers.some((blocker) => blocker.state === "open")) {
|
|
105
|
+
ready.push(issue);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return ready;
|
|
110
|
+
});
|
|
62
111
|
|
|
63
112
|
// Which label queued the issue: eligible unless the flow is keyed by its own.
|
|
64
113
|
export type QueueOptions = { readonly label?: string };
|
|
65
114
|
|
|
66
|
-
|
|
115
|
+
// The label that queued a work item from `trigger`, for claim,
|
|
116
|
+
// askForClarification and openPullRequest.
|
|
117
|
+
export const queuedBy = (item: WorkItem) =>
|
|
118
|
+
Schema.decodeUnknownEffect(IssueItem)(item.data).pipe(
|
|
119
|
+
Effect.map((data): QueueOptions =>
|
|
120
|
+
Option.match(Option.fromUndefinedOr(data.label), {
|
|
121
|
+
onNone: () => ({}),
|
|
122
|
+
onSome: (label) => ({ label }),
|
|
123
|
+
}),
|
|
124
|
+
),
|
|
125
|
+
Effect.mapError(
|
|
126
|
+
(issue) =>
|
|
127
|
+
new StepFailed({
|
|
128
|
+
step: "work item",
|
|
129
|
+
message: `${item.key} is not an issue: ${issue.message}`,
|
|
130
|
+
}),
|
|
131
|
+
),
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const queueLabel = (options: QueueOptions) =>
|
|
135
|
+
Repo.use((repo) => Effect.succeed(options.label ?? repo.labels.eligible));
|
|
67
136
|
|
|
68
137
|
// The account is the one machine config expects, and it has room for a task.
|
|
69
138
|
export const gate = Effect.gen(function* () {
|
|
@@ -71,69 +140,100 @@ export const gate = Effect.gen(function* () {
|
|
|
71
140
|
yield* usage.checkAccount;
|
|
72
141
|
const decision = yield* usage.gate;
|
|
73
142
|
|
|
74
|
-
if (!decision.launch)
|
|
75
|
-
});
|
|
143
|
+
if (!decision.launch) {
|
|
144
|
+
return yield* new Skip({ reason: `usage gate: ${decision.reason}` });
|
|
145
|
+
}
|
|
146
|
+
}).pipe(reportAs("gate"));
|
|
76
147
|
|
|
77
148
|
// Issues are often public, so the comment says nothing about the machine: no
|
|
78
149
|
// hostname, and no worktree path with the user's home in it.
|
|
79
150
|
export const claimComment = (claim: ClaimMarker) =>
|
|
80
151
|
[
|
|
81
152
|
`Claimed by Chernabog at ${claim.claimedAt}, on branch \`${claim.branch}\`.`,
|
|
82
|
-
|
|
153
|
+
"",
|
|
83
154
|
`- Run: \`${claim.runId}\``,
|
|
84
|
-
|
|
155
|
+
"",
|
|
85
156
|
claimMarker.write(claim),
|
|
86
|
-
].join(
|
|
87
|
-
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
if (Option.isSome(latest)) return { ours: latest.value.runId === runId, commented: true };
|
|
101
|
-
|
|
102
|
-
return { ours: (yield* github.issue(issue)).labels.includes(label), commented: false };
|
|
103
|
-
});
|
|
157
|
+
].join("\n");
|
|
158
|
+
|
|
159
|
+
// The latest claim marker on the issue, from its comments.
|
|
160
|
+
export const latestClaim = (issue: number) =>
|
|
161
|
+
GitHub.use((github) => github.comments(issue)).pipe(
|
|
162
|
+
Effect.map((comments) =>
|
|
163
|
+
latestMarker(
|
|
164
|
+
claimMarker.read,
|
|
165
|
+
comments.map((comment) => comment.body),
|
|
166
|
+
),
|
|
167
|
+
),
|
|
168
|
+
);
|
|
104
169
|
|
|
105
|
-
//
|
|
106
|
-
// and
|
|
107
|
-
//
|
|
170
|
+
// Migration: a run of an earlier version holds an issue by its branch alone,
|
|
171
|
+
// and its claim marker names no claim ref. While such runs may be in flight,
|
|
172
|
+
// an existing branch whose latest marker is one of theirs is still theirs.
|
|
173
|
+
// Drop this a release after claims became refs.
|
|
174
|
+
export const legacyClaim = (latest: Option.Option<ClaimMarker>, runId: string) =>
|
|
175
|
+
Option.filter(latest, (marker) => marker.ref !== true && marker.runId !== runId);
|
|
176
|
+
|
|
177
|
+
// Claims the issue with its claim ref, which only one run can create, then
|
|
178
|
+
// creates the issue's branch, or adopts one with no claim on it, and labels,
|
|
179
|
+
// assigns and comments as a person picking it up would. Safe to repeat: a run
|
|
180
|
+
// that already holds the claim keeps it. Removes the label that queued the
|
|
181
|
+
// issue. Wrap the run in `Claims.releasing(Claims.issue(issue))` to let the
|
|
182
|
+
// claim go when it ends.
|
|
108
183
|
export const claim = (issue: number, options: QueueOptions = {}) =>
|
|
109
184
|
activity(
|
|
110
|
-
|
|
185
|
+
"claim",
|
|
111
186
|
Effect.gen(function* () {
|
|
112
187
|
const github = yield* GitHub;
|
|
113
188
|
const repo = yield* Repo;
|
|
114
189
|
const run = yield* Run;
|
|
115
190
|
const label = yield* queueLabel(options);
|
|
116
191
|
const branch = branchFor(issue);
|
|
192
|
+
const item = Claims.issue(issue);
|
|
117
193
|
const baseSha = yield* github.branchSha(repo.baseBranch);
|
|
118
194
|
|
|
119
|
-
if (Option.isNone(baseSha))
|
|
195
|
+
if (Option.isNone(baseSha)) {
|
|
196
|
+
return yield* new StepFailed({
|
|
197
|
+
step: "claim",
|
|
198
|
+
message: `base branch ${repo.baseBranch} not found`,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const claimed = yield* Claims.acquire(item);
|
|
203
|
+
|
|
204
|
+
if (!claimed.acquired) {
|
|
205
|
+
return yield* new Skip({
|
|
206
|
+
reason: `#${issue} is already claimed (${Claims.describeHolder(item, claimed.holder)})`,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if ((yield* github.createBranch(branch, baseSha.value)) === "already-exists") {
|
|
211
|
+
const latest = yield* latestClaim(issue);
|
|
120
212
|
|
|
121
|
-
|
|
122
|
-
|
|
213
|
+
if (Option.exists(latest, (marker) => marker.runId === run.runId)) {
|
|
214
|
+
return { branch };
|
|
215
|
+
}
|
|
123
216
|
|
|
124
|
-
if (
|
|
217
|
+
if (Option.isSome(legacyClaim(latest, run.runId))) {
|
|
218
|
+
yield* Claims.release(item);
|
|
125
219
|
|
|
126
|
-
|
|
220
|
+
return yield* new Skip({ reason: `#${issue} is already claimed (${branch} exists)` });
|
|
221
|
+
}
|
|
127
222
|
}
|
|
128
223
|
|
|
129
224
|
yield* github.addLabel(issue, repo.labels.inProgress);
|
|
130
225
|
yield* github.removeLabel(issue, label);
|
|
131
226
|
|
|
132
|
-
if (Option.isSome(repo.board))
|
|
227
|
+
if (Option.isSome(repo.board)) {
|
|
228
|
+
yield* github.moveToStatus(issue, repo.board.value.inProgressStatus);
|
|
229
|
+
}
|
|
133
230
|
|
|
134
231
|
yield* github.assignSelf(issue);
|
|
135
232
|
const claimedAt = DateTime.formatIso(yield* DateTime.now);
|
|
136
|
-
yield* github.comment(
|
|
233
|
+
yield* github.comment(
|
|
234
|
+
issue,
|
|
235
|
+
claimComment({ runId: run.runId, claimedAt, branch, ref: true }),
|
|
236
|
+
);
|
|
137
237
|
|
|
138
238
|
return { branch };
|
|
139
239
|
}),
|
|
@@ -145,8 +245,20 @@ export const claim = (issue: number, options: QueueOptions = {}) =>
|
|
|
145
245
|
export const shell = (command: string, cwd: string, env: Readonly<Record<string, string>>) =>
|
|
146
246
|
Effect.gen(function* () {
|
|
147
247
|
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
|
148
|
-
|
|
149
|
-
const
|
|
248
|
+
|
|
249
|
+
const handle = yield* spawner.spawn(
|
|
250
|
+
ChildProcess.make("sh", ["-c", command], {
|
|
251
|
+
cwd,
|
|
252
|
+
env: { ...env, CI: "1" },
|
|
253
|
+
extendEnv: true,
|
|
254
|
+
forceKillAfter: "5 seconds",
|
|
255
|
+
}),
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
const [output, exitCode] = yield* Effect.all(
|
|
259
|
+
[Stream.mkString(Stream.decodeText(handle.all)), handle.exitCode],
|
|
260
|
+
{ concurrency: "unbounded" },
|
|
261
|
+
);
|
|
150
262
|
|
|
151
263
|
return { exitCode: Number(exitCode), output };
|
|
152
264
|
}).pipe(
|
|
@@ -154,12 +266,12 @@ export const shell = (command: string, cwd: string, env: Readonly<Record<string,
|
|
|
154
266
|
Effect.mapError((error) => new StepFailed({ step: command, message: error.message })),
|
|
155
267
|
);
|
|
156
268
|
|
|
157
|
-
const tail = (text: string, lines = 80) => text.trimEnd().split(
|
|
269
|
+
const tail = (text: string, lines = 80) => text.trimEnd().split("\n").slice(-lines).join("\n");
|
|
158
270
|
|
|
159
271
|
// The worktree for the claim branch, with dependencies installed.
|
|
160
272
|
export const prepareWorktree = (branch: string) =>
|
|
161
273
|
activity(
|
|
162
|
-
|
|
274
|
+
"prepare-worktree",
|
|
163
275
|
Effect.gen(function* () {
|
|
164
276
|
const repo = yield* Repo;
|
|
165
277
|
const path = yield* (yield* Worktree).ensure(branch);
|
|
@@ -167,7 +279,9 @@ export const prepareWorktree = (branch: string) =>
|
|
|
167
279
|
if (Option.isSome(repo.install)) {
|
|
168
280
|
const installed = yield* shell(repo.install.value, path, repo.env);
|
|
169
281
|
|
|
170
|
-
if (installed.exitCode !== 0)
|
|
282
|
+
if (installed.exitCode !== 0) {
|
|
283
|
+
return yield* new StepFailed({ step: "install", message: tail(installed.output, 30) });
|
|
284
|
+
}
|
|
171
285
|
}
|
|
172
286
|
|
|
173
287
|
return path;
|
|
@@ -176,7 +290,7 @@ export const prepareWorktree = (branch: string) =>
|
|
|
176
290
|
);
|
|
177
291
|
|
|
178
292
|
export const ImplementOutcome = Schema.Struct({
|
|
179
|
-
status: Schema.Literals([
|
|
293
|
+
status: Schema.Literals(["done", "needs-clarification"]),
|
|
180
294
|
summary: Schema.String,
|
|
181
295
|
title: Schema.String,
|
|
182
296
|
body: Schema.String,
|
|
@@ -186,29 +300,46 @@ export const ImplementOutcome = Schema.Struct({
|
|
|
186
300
|
export type ImplementOutcome = typeof ImplementOutcome.Type;
|
|
187
301
|
|
|
188
302
|
export const implementOutcomeJsonSchema = {
|
|
189
|
-
type:
|
|
303
|
+
type: "object",
|
|
190
304
|
properties: {
|
|
191
|
-
status: { type:
|
|
192
|
-
summary: {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
305
|
+
status: { type: "string", enum: ["done", "needs-clarification"] },
|
|
306
|
+
summary: {
|
|
307
|
+
type: "string",
|
|
308
|
+
description: "What changed and how it was tested, for the issue comment",
|
|
309
|
+
},
|
|
310
|
+
title: {
|
|
311
|
+
type: "string",
|
|
312
|
+
description: "The pull request title, following the repo commit convention",
|
|
313
|
+
},
|
|
314
|
+
body: {
|
|
315
|
+
type: "string",
|
|
316
|
+
description: "The pull request body in Markdown, without the Closes line",
|
|
317
|
+
},
|
|
318
|
+
questions: {
|
|
319
|
+
type: "string",
|
|
320
|
+
description: "For needs-clarification: numbered questions, each led by a recommendation",
|
|
321
|
+
},
|
|
196
322
|
},
|
|
197
|
-
required: [
|
|
323
|
+
required: ["status", "summary", "title", "body"],
|
|
198
324
|
additionalProperties: false,
|
|
199
325
|
} satisfies Json;
|
|
200
326
|
|
|
201
|
-
const RESUME_PROMPT =
|
|
327
|
+
const RESUME_PROMPT =
|
|
328
|
+
"You were interrupted. Continue the task from where you left off, then give your final answer in the required format.";
|
|
202
329
|
|
|
203
330
|
const decodeOutcome = (result: AgentResult) =>
|
|
204
|
-
Schema.decodeUnknownEffect(ImplementOutcome)(result.output).pipe(
|
|
331
|
+
Schema.decodeUnknownEffect(ImplementOutcome)(result.output).pipe(
|
|
332
|
+
Effect.mapError(
|
|
333
|
+
(issue) => new StepFailed({ step: "agent", message: `agent answer: ${issue.message}` }),
|
|
334
|
+
),
|
|
335
|
+
);
|
|
205
336
|
|
|
206
337
|
// The agent implements the issue in the worktree and commits, from
|
|
207
338
|
// .chernabog/prompts/implement.md. It answers done, with the pull request's
|
|
208
339
|
// title and body, or needs-clarification, with its questions.
|
|
209
340
|
export const implementWithAgent = (issue: number, worktree: string) =>
|
|
210
341
|
activity(
|
|
211
|
-
|
|
342
|
+
"implement",
|
|
212
343
|
Effect.gen(function* () {
|
|
213
344
|
const github = yield* GitHub;
|
|
214
345
|
const repo = yield* Repo;
|
|
@@ -216,7 +347,7 @@ export const implementWithAgent = (issue: number, worktree: string) =>
|
|
|
216
347
|
const details = yield* github.issue(issue);
|
|
217
348
|
const comments = yield* github.trustedComments(issue);
|
|
218
349
|
|
|
219
|
-
const prompt = yield* renderPrompt(
|
|
350
|
+
const prompt = yield* renderPrompt("implement", {
|
|
220
351
|
repo: repo.slug,
|
|
221
352
|
issue,
|
|
222
353
|
title: details.title,
|
|
@@ -232,7 +363,7 @@ export const implementWithAgent = (issue: number, worktree: string) =>
|
|
|
232
363
|
const result = yield* (yield* Agent).run({
|
|
233
364
|
cwd: worktree,
|
|
234
365
|
prompt,
|
|
235
|
-
sessionId: sessionIdFor(run.runId,
|
|
366
|
+
sessionId: sessionIdFor(run.runId, "implement"),
|
|
236
367
|
resumePrompt: Option.some(RESUME_PROMPT),
|
|
237
368
|
outputSchema: Option.some(implementOutcomeJsonSchema),
|
|
238
369
|
adapter: Option.none(),
|
|
@@ -243,13 +374,23 @@ export const implementWithAgent = (issue: number, worktree: string) =>
|
|
|
243
374
|
ImplementOutcome,
|
|
244
375
|
);
|
|
245
376
|
|
|
246
|
-
// Which session a fix goes to: this run's implement session when none is
|
|
247
|
-
|
|
377
|
+
// Which session a fix goes to: this run's implement session when none is
|
|
378
|
+
// given. withPerson: a person works in it with the agent, on the hybrid path.
|
|
379
|
+
export type AuthorSession = {
|
|
380
|
+
readonly sessionId: Option.Option<string>;
|
|
381
|
+
readonly adapter: Option.Option<string>;
|
|
382
|
+
readonly withPerson?: boolean;
|
|
383
|
+
};
|
|
248
384
|
|
|
249
385
|
export const thisRunsSession: AuthorSession = { sessionId: Option.none(), adapter: Option.none() };
|
|
250
386
|
|
|
251
387
|
// Sends the agent back into its session with a failure to fix.
|
|
252
|
-
export const fixWithAgent = (
|
|
388
|
+
export const fixWithAgent = (
|
|
389
|
+
name: string,
|
|
390
|
+
worktree: string,
|
|
391
|
+
prompt: string,
|
|
392
|
+
session: AuthorSession,
|
|
393
|
+
) =>
|
|
253
394
|
activity(
|
|
254
395
|
name,
|
|
255
396
|
Effect.gen(function* () {
|
|
@@ -258,10 +399,11 @@ export const fixWithAgent = (name: string, worktree: string, prompt: string, ses
|
|
|
258
399
|
const result = yield* (yield* Agent).run({
|
|
259
400
|
cwd: worktree,
|
|
260
401
|
prompt,
|
|
261
|
-
sessionId: Option.getOrElse(session.sessionId, () => sessionIdFor(run.runId,
|
|
402
|
+
sessionId: Option.getOrElse(session.sessionId, () => sessionIdFor(run.runId, "implement")),
|
|
262
403
|
resumePrompt: Option.some(prompt),
|
|
263
404
|
outputSchema: Option.some(implementOutcomeJsonSchema),
|
|
264
405
|
adapter: session.adapter,
|
|
406
|
+
withPerson: session.withPerson === true,
|
|
265
407
|
});
|
|
266
408
|
|
|
267
409
|
return yield* decodeOutcome(result);
|
|
@@ -271,19 +413,27 @@ export const fixWithAgent = (name: string, worktree: string, prompt: string, ses
|
|
|
271
413
|
|
|
272
414
|
// The review's findings for a prompt: its summary, then each line comment.
|
|
273
415
|
const describeReview = (review: ReviewResult) =>
|
|
274
|
-
[
|
|
275
|
-
|
|
276
|
-
|
|
416
|
+
[
|
|
417
|
+
review.summary ?? `The review's verdict was ${review.verdict}.`,
|
|
418
|
+
...(review.comments ?? []).map(
|
|
419
|
+
(comment) => `- ${comment.path}:${comment.line}: ${comment.body}`,
|
|
420
|
+
),
|
|
421
|
+
].join("\n\n");
|
|
422
|
+
|
|
423
|
+
export const Consideration = Schema.Struct({
|
|
424
|
+
status: Schema.Literals(["address", "decline"]),
|
|
425
|
+
reasons: Schema.String,
|
|
426
|
+
});
|
|
277
427
|
|
|
278
428
|
export type Consideration = typeof Consideration.Type;
|
|
279
429
|
|
|
280
430
|
export const considerationJsonSchema = {
|
|
281
|
-
type:
|
|
431
|
+
type: "object",
|
|
282
432
|
properties: {
|
|
283
|
-
status: { type:
|
|
284
|
-
reasons: { type:
|
|
433
|
+
status: { type: "string", enum: ["address", "decline"] },
|
|
434
|
+
reasons: { type: "string", description: "Why, point by point, in Markdown" },
|
|
285
435
|
},
|
|
286
|
-
required: [
|
|
436
|
+
required: ["status", "reasons"],
|
|
287
437
|
additionalProperties: false,
|
|
288
438
|
} satisfies Json;
|
|
289
439
|
|
|
@@ -297,7 +447,11 @@ const authorWorktree = (pullRequest: number) =>
|
|
|
297
447
|
// Resumes the author's session with a review, and asks whether to address it
|
|
298
448
|
// or decline it, with reasons. Changes nothing. Its prompt is
|
|
299
449
|
// .chernabog/prompts/consider.md, or the SDK's own.
|
|
300
|
-
export const considerFindings = (
|
|
450
|
+
export const considerFindings = (
|
|
451
|
+
pullRequest: number,
|
|
452
|
+
review: ReviewResult,
|
|
453
|
+
session: AuthorSession,
|
|
454
|
+
) => {
|
|
301
455
|
const round = review.round ?? 1;
|
|
302
456
|
|
|
303
457
|
return activity(
|
|
@@ -305,18 +459,32 @@ export const considerFindings = (pullRequest: number, review: ReviewResult, sess
|
|
|
305
459
|
Effect.gen(function* () {
|
|
306
460
|
const run = yield* Run;
|
|
307
461
|
const worktree = yield* authorWorktree(pullRequest);
|
|
308
|
-
|
|
462
|
+
|
|
463
|
+
const prompt = yield* renderPromptOr("consider", CONSIDER, {
|
|
464
|
+
pullRequest,
|
|
465
|
+
round,
|
|
466
|
+
review: describeReview(review),
|
|
467
|
+
});
|
|
309
468
|
|
|
310
469
|
const result = yield* (yield* Agent).run({
|
|
311
470
|
cwd: worktree,
|
|
312
471
|
prompt,
|
|
313
|
-
sessionId: Option.getOrElse(session.sessionId, () => sessionIdFor(run.runId,
|
|
472
|
+
sessionId: Option.getOrElse(session.sessionId, () => sessionIdFor(run.runId, "implement")),
|
|
314
473
|
resumePrompt: Option.some(prompt),
|
|
315
474
|
outputSchema: Option.some(considerationJsonSchema),
|
|
316
475
|
adapter: session.adapter,
|
|
476
|
+
withPerson: session.withPerson === true,
|
|
317
477
|
});
|
|
318
478
|
|
|
319
|
-
return yield* Schema.decodeUnknownEffect(Consideration)(result.output).pipe(
|
|
479
|
+
return yield* Schema.decodeUnknownEffect(Consideration)(result.output).pipe(
|
|
480
|
+
Effect.mapError(
|
|
481
|
+
(issue) =>
|
|
482
|
+
new StepFailed({
|
|
483
|
+
step: `consider-review-${round}`,
|
|
484
|
+
message: `agent answer: ${issue.message}`,
|
|
485
|
+
}),
|
|
486
|
+
),
|
|
487
|
+
);
|
|
320
488
|
}),
|
|
321
489
|
Consideration,
|
|
322
490
|
);
|
|
@@ -325,7 +493,11 @@ export const considerFindings = (pullRequest: number, review: ReviewResult, sess
|
|
|
325
493
|
// Resumes the author's session to make the changes a review asks for, and
|
|
326
494
|
// commit them, as fixWithAgent does for a failing check. Its prompt is
|
|
327
495
|
// .chernabog/prompts/address.md, or the SDK's own.
|
|
328
|
-
export const addressFindings = (
|
|
496
|
+
export const addressFindings = (
|
|
497
|
+
pullRequest: number,
|
|
498
|
+
review: ReviewResult,
|
|
499
|
+
session: AuthorSession,
|
|
500
|
+
) =>
|
|
329
501
|
Effect.gen(function* () {
|
|
330
502
|
const round = review.round ?? 1;
|
|
331
503
|
|
|
@@ -333,30 +505,54 @@ export const addressFindings = (pullRequest: number, review: ReviewResult, sessi
|
|
|
333
505
|
`prepare-address-${round}`,
|
|
334
506
|
Effect.gen(function* () {
|
|
335
507
|
const worktree = yield* authorWorktree(pullRequest);
|
|
336
|
-
|
|
508
|
+
|
|
509
|
+
const prompt = yield* renderPromptOr("address", ADDRESS, {
|
|
510
|
+
pullRequest,
|
|
511
|
+
round,
|
|
512
|
+
review: describeReview(review),
|
|
513
|
+
});
|
|
337
514
|
|
|
338
515
|
return { worktree, prompt };
|
|
339
516
|
}),
|
|
340
517
|
Schema.Struct({ worktree: Schema.String, prompt: Schema.String }),
|
|
341
518
|
);
|
|
342
519
|
|
|
343
|
-
return yield* fixWithAgent(
|
|
520
|
+
return yield* fixWithAgent(
|
|
521
|
+
`address-review-${round}`,
|
|
522
|
+
prepared.worktree,
|
|
523
|
+
prepared.prompt,
|
|
524
|
+
session,
|
|
525
|
+
);
|
|
344
526
|
});
|
|
345
527
|
|
|
346
528
|
const requeueHint = (board: Option.Option<{ readonly readyStatus: string }>) =>
|
|
347
|
-
Option.match(board, {
|
|
529
|
+
Option.match(board, {
|
|
530
|
+
onNone: () => "apply",
|
|
531
|
+
onSome: (found) => `set Status back to ${found.readyStatus} and apply`,
|
|
532
|
+
});
|
|
348
533
|
|
|
349
534
|
// Posts the agent's questions, releases the claim and stops, as a person would
|
|
350
535
|
// who needs an answer before going on. The requeue hint names the label that queued it.
|
|
351
|
-
export const askForClarification = (
|
|
536
|
+
export const askForClarification = (
|
|
537
|
+
issue: number,
|
|
538
|
+
outcome: ImplementOutcome,
|
|
539
|
+
options: QueueOptions = {},
|
|
540
|
+
) =>
|
|
352
541
|
activity(
|
|
353
|
-
|
|
542
|
+
"needs-clarification",
|
|
354
543
|
Effect.gen(function* () {
|
|
355
544
|
const github = yield* GitHub;
|
|
356
545
|
const repo = yield* Repo;
|
|
357
546
|
const label = yield* queueLabel(options);
|
|
358
547
|
const questions = outcome.questions ?? outcome.summary;
|
|
359
|
-
yield* github.comment(
|
|
548
|
+
yield* github.comment(
|
|
549
|
+
issue,
|
|
550
|
+
[
|
|
551
|
+
questions,
|
|
552
|
+
"",
|
|
553
|
+
`To requeue: answer the questions above, then ${requeueHint(repo.board)} \`${label}\` again.`,
|
|
554
|
+
].join("\n"),
|
|
555
|
+
);
|
|
360
556
|
yield* github.addLabel(issue, repo.labels.needsClarification);
|
|
361
557
|
yield* github.removeLabel(issue, repo.labels.inProgress);
|
|
362
558
|
yield* github.deleteBranch(branchFor(issue));
|
|
@@ -374,7 +570,14 @@ const LocalGateResult = Schema.Struct({ ok: Schema.Boolean, failure: Schema.Stri
|
|
|
374
570
|
// activities are named like `local-gate-1#fix-1`, as `step` does with its id.
|
|
375
571
|
export type RepeatOptions = { readonly id?: string };
|
|
376
572
|
|
|
377
|
-
|
|
573
|
+
// The session the local gate sends the agent back to: this run's implement session unless given.
|
|
574
|
+
export type LocalGateOptions = RepeatOptions & { readonly session?: AuthorSession };
|
|
575
|
+
|
|
576
|
+
const named = (options: RepeatOptions, name: string) =>
|
|
577
|
+
Option.match(Option.fromUndefinedOr(options.id), {
|
|
578
|
+
onNone: () => name,
|
|
579
|
+
onSome: (id) => `${name}#${id}`,
|
|
580
|
+
});
|
|
378
581
|
|
|
379
582
|
const runLocalGate = (worktree: string, round: number, options: RepeatOptions) =>
|
|
380
583
|
activity(
|
|
@@ -387,35 +590,56 @@ const runLocalGate = (worktree: string, round: number, options: RepeatOptions) =
|
|
|
387
590
|
yield* log.log(`local gate: ${command}`);
|
|
388
591
|
const result = yield* shell(command, worktree, repo.env);
|
|
389
592
|
|
|
390
|
-
if (result.exitCode !== 0)
|
|
593
|
+
if (result.exitCode !== 0) {
|
|
594
|
+
return {
|
|
595
|
+
ok: false,
|
|
596
|
+
failure: `\`${command}\` exited ${result.exitCode}:\n\n${tail(result.output)}`,
|
|
597
|
+
};
|
|
598
|
+
}
|
|
391
599
|
}
|
|
392
600
|
|
|
393
|
-
return { ok: true, failure:
|
|
601
|
+
return { ok: true, failure: "" };
|
|
394
602
|
}),
|
|
395
603
|
LocalGateResult,
|
|
396
604
|
);
|
|
397
605
|
|
|
398
606
|
// Runs the repo's local gate, and sends the agent back to fix what fails, up to
|
|
399
607
|
// limits.localGateRounds times.
|
|
400
|
-
export const localGate = (worktree: string, options:
|
|
608
|
+
export const localGate = (worktree: string, options: LocalGateOptions = {}) =>
|
|
401
609
|
Effect.gen(function* () {
|
|
402
610
|
const repo = yield* Repo;
|
|
403
611
|
|
|
404
612
|
for (let round = 1; ; round++) {
|
|
405
613
|
const result = yield* runLocalGate(worktree, round, options);
|
|
406
614
|
|
|
407
|
-
if (result.ok)
|
|
615
|
+
if (result.ok) {
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
408
618
|
|
|
409
619
|
if (round > repo.limits.localGateRounds) {
|
|
410
|
-
return yield* new NeedsHuman({
|
|
620
|
+
return yield* new NeedsHuman({
|
|
621
|
+
reason: `the local gate still fails after ${repo.limits.localGateRounds} fix attempts:\n\n${result.failure}`,
|
|
622
|
+
});
|
|
411
623
|
}
|
|
412
624
|
|
|
413
|
-
yield* fixWithAgent(
|
|
625
|
+
yield* fixWithAgent(
|
|
626
|
+
named(options, `fix-local-gate-${round}`),
|
|
627
|
+
worktree,
|
|
628
|
+
`The local gate failed. Fix the cause, commit, and answer again.\n\n${result.failure}`,
|
|
629
|
+
options.session ?? thisRunsSession,
|
|
630
|
+
);
|
|
414
631
|
}
|
|
415
632
|
});
|
|
416
633
|
|
|
417
|
-
//
|
|
418
|
-
|
|
634
|
+
// withPerson: a person is in the session that wrote the change, so it may
|
|
635
|
+
// touch humanOnlyPaths; a person still merges it (PullRequest.merge checks them again).
|
|
636
|
+
export type PushOptions = { readonly withPerson?: boolean };
|
|
637
|
+
|
|
638
|
+
// Checks the branch has commits and, unless a person is in the session,
|
|
639
|
+
// touches no human-only paths, and pushes it. Fails with NothingCommitted when
|
|
640
|
+
// there's nothing to push, and PushRejected when the remote refuses it, such
|
|
641
|
+
// as for a token without the workflow scope.
|
|
642
|
+
export const push = (name: string, worktree: string, branch: string, options: PushOptions = {}) =>
|
|
419
643
|
activity(
|
|
420
644
|
name,
|
|
421
645
|
Effect.gen(function* () {
|
|
@@ -423,13 +647,20 @@ export const push = (name: string, worktree: string, branch: string) =>
|
|
|
423
647
|
const repo = yield* Repo;
|
|
424
648
|
|
|
425
649
|
if ((yield* git.commitsAhead(worktree, repo.baseBranch)) === 0) {
|
|
426
|
-
return yield* new
|
|
650
|
+
return yield* new NothingCommitted({ branch });
|
|
427
651
|
}
|
|
428
652
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
653
|
+
if (options.withPerson !== true) {
|
|
654
|
+
const touched = pathsMatching(
|
|
655
|
+
yield* git.changedFiles(worktree, repo.baseBranch),
|
|
656
|
+
repo.humanOnlyPaths,
|
|
657
|
+
);
|
|
658
|
+
|
|
659
|
+
if (touched.length > 0) {
|
|
660
|
+
return yield* new NeedsHuman({
|
|
661
|
+
reason: `the change touches paths a person must handle: ${touched.map((path) => `\`${path}\``).join(", ")}`,
|
|
662
|
+
});
|
|
663
|
+
}
|
|
433
664
|
}
|
|
434
665
|
|
|
435
666
|
yield* git.push(worktree, branch);
|
|
@@ -440,25 +671,50 @@ export const PullRequestRef = Schema.Struct({ number: Schema.Finite, url: Schema
|
|
|
440
671
|
|
|
441
672
|
export type PullRequestRef = typeof PullRequestRef.Type;
|
|
442
673
|
|
|
443
|
-
|
|
444
|
-
|
|
674
|
+
const footer = (shipping: boolean) => {
|
|
675
|
+
if (shipping) {
|
|
676
|
+
return "Opened by Chernabog for an unattended run, on the automated path: it merges once its review approves.";
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
return "Opened by Chernabog for an unattended run. A person merges.";
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
// Opens the pull request for the branch, or finds the one already open. An
|
|
683
|
+
// issue queued by ship (auto:ship) gives its pull request the label too, the
|
|
684
|
+
// automated path, which merges once approved.
|
|
685
|
+
export const openPullRequest = (
|
|
686
|
+
issue: number,
|
|
687
|
+
outcome: ImplementOutcome,
|
|
688
|
+
options: QueueOptions = {},
|
|
689
|
+
) =>
|
|
445
690
|
activity(
|
|
446
|
-
|
|
691
|
+
"open-pull-request",
|
|
447
692
|
Effect.gen(function* () {
|
|
448
693
|
const github = yield* GitHub;
|
|
449
694
|
const repo = yield* Repo;
|
|
450
695
|
const branch = branchFor(issue);
|
|
451
|
-
const
|
|
696
|
+
const shipping = options.label === repo.labels.ship;
|
|
452
697
|
|
|
453
|
-
|
|
698
|
+
const existing = Option.filter(
|
|
699
|
+
yield* github.pullRequestForBranch(branch),
|
|
700
|
+
(pull) => pull.state === "open",
|
|
701
|
+
);
|
|
454
702
|
|
|
455
|
-
const opened = yield*
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
703
|
+
const opened = yield* Option.match(existing, {
|
|
704
|
+
onSome: Effect.succeed,
|
|
705
|
+
onNone: () =>
|
|
706
|
+
github.openPullRequest({
|
|
707
|
+
head: branch,
|
|
708
|
+
base: repo.baseBranch,
|
|
709
|
+
title: outcome.title,
|
|
710
|
+
body: `Closes #${issue}\n\n${outcome.body}\n\n---\n${footer(shipping)}`,
|
|
711
|
+
}),
|
|
460
712
|
});
|
|
461
713
|
|
|
714
|
+
if (shipping) {
|
|
715
|
+
yield* github.addLabel(opened.number, repo.labels.ship);
|
|
716
|
+
}
|
|
717
|
+
|
|
462
718
|
return { number: opened.number, url: opened.url };
|
|
463
719
|
}),
|
|
464
720
|
PullRequestRef,
|
|
@@ -467,27 +723,42 @@ export const openPullRequest = (issue: number, outcome: ImplementOutcome) =>
|
|
|
467
723
|
const CI_POLL = Duration.minutes(1);
|
|
468
724
|
|
|
469
725
|
const CiReading = Schema.Union([
|
|
470
|
-
Schema.Struct({ state: Schema.Literal(
|
|
471
|
-
Schema.Struct({ state: Schema.Literal(
|
|
472
|
-
Schema.Struct({ state: Schema.Literal(
|
|
726
|
+
Schema.Struct({ state: Schema.Literal("pending"), waitingOn: Schema.Array(Schema.String) }),
|
|
727
|
+
Schema.Struct({ state: Schema.Literal("green") }),
|
|
728
|
+
Schema.Struct({ state: Schema.Literal("red"), failedChecks: Schema.Array(Schema.String) }),
|
|
473
729
|
]);
|
|
474
730
|
|
|
475
731
|
type CiReading = typeof CiReading.Type;
|
|
476
732
|
|
|
477
|
-
// Polls the pull request's checks durably until they stop pending, or
|
|
733
|
+
// Polls the pull request's checks durably until they stop pending, or limits.ciTimeout passes.
|
|
478
734
|
const awaitChecks = (pullRequest: number, round: number, options: RepeatOptions) =>
|
|
479
735
|
Effect.gen(function* () {
|
|
480
736
|
const repo = yield* Repo;
|
|
481
|
-
const maxPolls = Math.max(1, Math.ceil(Duration.toMillis(repo.limits.ciTimeout) / Duration.toMillis(CI_POLL)));
|
|
482
|
-
let reading: CiReading = { state: 'pending', waitingOn: [] };
|
|
483
737
|
|
|
484
|
-
|
|
738
|
+
const maxPolls = Math.max(
|
|
739
|
+
1,
|
|
740
|
+
Math.ceil(Duration.toMillis(repo.limits.ciTimeout) / Duration.toMillis(CI_POLL)),
|
|
741
|
+
);
|
|
742
|
+
|
|
743
|
+
let reading: CiReading = { state: "pending", waitingOn: [] };
|
|
744
|
+
|
|
745
|
+
for (let poll = 1; reading.state === "pending"; poll++) {
|
|
485
746
|
if (poll > maxPolls) {
|
|
486
|
-
return yield* new
|
|
747
|
+
return yield* new CiTimedOut({
|
|
748
|
+
pullRequest,
|
|
749
|
+
after: Duration.format(Duration.fromInputUnsafe(repo.limits.ciTimeout)),
|
|
750
|
+
});
|
|
487
751
|
}
|
|
488
752
|
|
|
489
|
-
if (poll > 1)
|
|
490
|
-
|
|
753
|
+
if (poll > 1) {
|
|
754
|
+
yield* sleep(named(options, `ci-${round}-wait-${poll}`), CI_POLL);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
reading = yield* activity(
|
|
758
|
+
named(options, `ci-${round}-${poll}`),
|
|
759
|
+
GitHub.use((github) => github.ciState(pullRequest)),
|
|
760
|
+
CiReading,
|
|
761
|
+
);
|
|
491
762
|
}
|
|
492
763
|
|
|
493
764
|
return reading;
|
|
@@ -497,19 +768,31 @@ export type CiBranch = AuthorSession & { readonly branch: string };
|
|
|
497
768
|
|
|
498
769
|
// Waits durably for the pull request's checks. On red, the agent reads the
|
|
499
770
|
// failure, fixes, commits, and the branch is pushed again, up to
|
|
500
|
-
// limits.ciRounds times.
|
|
501
|
-
|
|
771
|
+
// limits.ciRounds times. Fails with CiTimedOut when the checks stay pending
|
|
772
|
+
// past limits.ciTimeout, and as push does for a fix it can't push.
|
|
773
|
+
export const waitForCi = (
|
|
774
|
+
pullRequest: number,
|
|
775
|
+
worktree: string,
|
|
776
|
+
target: CiBranch,
|
|
777
|
+
options: RepeatOptions = {},
|
|
778
|
+
) =>
|
|
502
779
|
Effect.gen(function* () {
|
|
503
780
|
const repo = yield* Repo;
|
|
504
781
|
|
|
505
782
|
for (let round = 1; ; round++) {
|
|
506
783
|
const reading = yield* awaitChecks(pullRequest, round, options);
|
|
507
784
|
|
|
508
|
-
if (reading.state !==
|
|
785
|
+
if (reading.state !== "red") {
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
509
788
|
|
|
510
|
-
const failed = reading.failedChecks.join(
|
|
789
|
+
const failed = reading.failedChecks.join(", ");
|
|
511
790
|
|
|
512
|
-
if (round > repo.limits.ciRounds)
|
|
791
|
+
if (round > repo.limits.ciRounds) {
|
|
792
|
+
return yield* new NeedsHuman({
|
|
793
|
+
reason: `CI is still red after ${repo.limits.ciRounds} fix attempts: ${failed}`,
|
|
794
|
+
});
|
|
795
|
+
}
|
|
513
796
|
|
|
514
797
|
yield* fixWithAgent(
|
|
515
798
|
named(options, `fix-ci-${round}`),
|
|
@@ -517,58 +800,91 @@ export const waitForCi = (pullRequest: number, worktree: string, target: CiBranc
|
|
|
517
800
|
[
|
|
518
801
|
`CI failed on pull request #${pullRequest}: ${failed}.`,
|
|
519
802
|
`Read why with \`gh run list --branch ${target.branch} --limit 5\` and \`gh run view <run id> --log-failed\`.`,
|
|
520
|
-
|
|
521
|
-
].join(
|
|
803
|
+
"Fix the cause and commit. A failure unrelated to your change may be re-run once with `gh run rerun <run id> --failed`. Then answer again.",
|
|
804
|
+
].join("\n"),
|
|
522
805
|
target,
|
|
523
806
|
);
|
|
524
|
-
yield* push(named(options, `push-ci-${round}`), worktree, target.branch
|
|
807
|
+
yield* push(named(options, `push-ci-${round}`), worktree, target.branch, {
|
|
808
|
+
withPerson: target.withPerson === true,
|
|
809
|
+
});
|
|
525
810
|
}
|
|
526
811
|
});
|
|
527
812
|
|
|
528
|
-
|
|
529
|
-
|
|
813
|
+
const askForReview = (pullRequest: number, options: HandOffOptions) => {
|
|
814
|
+
if (options.needsReview) {
|
|
815
|
+
return requestReview(pullRequest).pipe(Effect.asVoid);
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
return Effect.void;
|
|
819
|
+
};
|
|
820
|
+
|
|
821
|
+
// Whether hand-off asks the team's reviewer pool for a review. A flow that
|
|
822
|
+
// reviews the pull request itself says false.
|
|
530
823
|
export type HandOffOptions = { readonly needsReview: boolean };
|
|
531
824
|
|
|
532
825
|
// Hands the green pull request to review: the review trigger if the repo has
|
|
533
|
-
// one,
|
|
534
|
-
//
|
|
535
|
-
|
|
826
|
+
// one, a review requested of one reviewer from the pool (needs-review for a
|
|
827
|
+
// person without one) unless the options leave it off, and a closing comment
|
|
828
|
+
// on the issue.
|
|
829
|
+
export const handOff = (
|
|
830
|
+
issue: number,
|
|
831
|
+
pullRequest: PullRequestRef,
|
|
832
|
+
outcome: ImplementOutcome,
|
|
833
|
+
options: HandOffOptions = { needsReview: true },
|
|
834
|
+
) =>
|
|
536
835
|
activity(
|
|
537
|
-
|
|
836
|
+
"hand-off",
|
|
538
837
|
Effect.gen(function* () {
|
|
539
838
|
const github = yield* GitHub;
|
|
540
839
|
const repo = yield* Repo;
|
|
541
840
|
const run = yield* Run;
|
|
542
841
|
|
|
543
|
-
if (Option.isSome(repo.reviewTrigger))
|
|
842
|
+
if (Option.isSome(repo.reviewTrigger)) {
|
|
843
|
+
yield* github.comment(pullRequest.number, repo.reviewTrigger.value);
|
|
844
|
+
}
|
|
544
845
|
|
|
545
846
|
// The fix flow resumes this session to answer a review.
|
|
546
|
-
const session = {
|
|
547
|
-
|
|
847
|
+
const session = {
|
|
848
|
+
adapter: repo.agent,
|
|
849
|
+
sessionId: sessionIdFor(run.runId, "implement"),
|
|
850
|
+
issue,
|
|
851
|
+
branch: branchFor(issue),
|
|
852
|
+
};
|
|
548
853
|
|
|
549
|
-
|
|
854
|
+
yield* github.comment(
|
|
855
|
+
pullRequest.number,
|
|
856
|
+
`Implemented by ${repo.agent} for #${issue}. ${sessionMarker.write(session)}`,
|
|
857
|
+
);
|
|
550
858
|
|
|
551
859
|
yield* github.removeLabel(issue, repo.labels.inProgress);
|
|
552
|
-
yield* github.comment(
|
|
860
|
+
yield* github.comment(
|
|
861
|
+
issue,
|
|
862
|
+
`Pull request ready for review: ${pullRequest.url}\n\n${outcome.summary}`,
|
|
863
|
+
);
|
|
553
864
|
|
|
554
865
|
return `#${issue}: ${pullRequest.url} is ready for review`;
|
|
555
866
|
}),
|
|
556
867
|
Schema.String,
|
|
557
|
-
);
|
|
868
|
+
).pipe(Effect.tap(() => askForReview(pullRequest.number, options)));
|
|
558
869
|
|
|
559
870
|
// Labels the work item for a person and says why. No flow picks it up again.
|
|
560
871
|
export const handToHuman = (issue: number, reason: string, pullRequest: Option.Option<number>) =>
|
|
561
872
|
activity(
|
|
562
|
-
|
|
873
|
+
"needs-human",
|
|
563
874
|
Effect.gen(function* () {
|
|
564
875
|
const github = yield* GitHub;
|
|
565
876
|
const repo = yield* Repo;
|
|
566
877
|
yield* github.addLabel(issue, repo.labels.needsHuman);
|
|
567
878
|
yield* github.removeLabel(issue, repo.labels.inProgress);
|
|
568
879
|
|
|
569
|
-
if (Option.isSome(pullRequest))
|
|
880
|
+
if (Option.isSome(pullRequest)) {
|
|
881
|
+
yield* github.addLabel(pullRequest.value, repo.labels.needsHuman);
|
|
882
|
+
}
|
|
570
883
|
|
|
571
|
-
yield* github.comment(
|
|
884
|
+
yield* github.comment(
|
|
885
|
+
issue,
|
|
886
|
+
`Chernabog stopped and needs a person: ${reason}\n\nThe branch \`${branchFor(issue)}\` and its worktree are left as they are.`,
|
|
887
|
+
);
|
|
572
888
|
|
|
573
889
|
return `#${issue} needs a person: ${reason}`;
|
|
574
890
|
}),
|
|
@@ -577,33 +893,3 @@ export const handToHuman = (issue: number, reason: string, pullRequest: Option.O
|
|
|
577
893
|
|
|
578
894
|
// Ends the run as waiting on a person, once the issue says why.
|
|
579
895
|
export const endForPerson = (summary: string) => Effect.fail(new NeedsHuman({ reason: summary }));
|
|
580
|
-
|
|
581
|
-
// The whole default flow, for a flow file that wants it as it is.
|
|
582
|
-
export const workflow = (item: WorkItem) =>
|
|
583
|
-
Effect.gen(function* () {
|
|
584
|
-
const issue = yield* issueOf(item);
|
|
585
|
-
const { branch } = yield* claim(issue);
|
|
586
|
-
let pullRequest = Option.none<number>();
|
|
587
|
-
const toPerson = (reason: string) => handToHuman(issue, reason, pullRequest).pipe(Effect.flatMap(endForPerson));
|
|
588
|
-
|
|
589
|
-
return yield* Effect.gen(function* () {
|
|
590
|
-
const worktree = yield* prepareWorktree(branch);
|
|
591
|
-
const outcome = yield* implementWithAgent(issue, worktree);
|
|
592
|
-
|
|
593
|
-
if (outcome.status === 'needs-clarification') return yield* askForClarification(issue, outcome).pipe(Effect.flatMap(endForPerson));
|
|
594
|
-
|
|
595
|
-
return yield* Effect.gen(function* () {
|
|
596
|
-
yield* localGate(worktree);
|
|
597
|
-
yield* push('push', worktree, branch);
|
|
598
|
-
const opened = yield* openPullRequest(issue, outcome);
|
|
599
|
-
pullRequest = Option.some(opened.number);
|
|
600
|
-
yield* waitForCi(opened.number, worktree, { ...thisRunsSession, branch });
|
|
601
|
-
|
|
602
|
-
return yield* handOff(issue, opened, outcome);
|
|
603
|
-
}).pipe(Effect.catchTag('NeedsHuman', (needsHuman) => toPerson(needsHuman.reason)));
|
|
604
|
-
}).pipe(
|
|
605
|
-
// Any other failure once the issue is claimed hands it to a person, rather
|
|
606
|
-
// than leaving it claimed with nobody working on it.
|
|
607
|
-
Effect.catchTag('StepFailed', (failed) => toPerson(`${failed.step}: ${failed.message}`)),
|
|
608
|
-
);
|
|
609
|
-
});
|