@chernabog/sdk 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -12
- package/index.ts +5 -0
- package/package.json +8 -3
- package/src/ci.ts +82 -0
- package/src/comment.ts +1 -3
- package/src/default-prompts.ts +24 -1
- package/src/epic.ts +568 -0
- package/src/hybrid.ts +67 -53
- package/src/implement.ts +731 -149
- package/src/index.ts +15 -13
- package/src/plan-labels.ts +9 -0
- package/src/plan.ts +48 -39
- package/src/prompt.ts +1 -3
- package/src/pull-request.ts +234 -39
- package/src/review.ts +236 -56
- package/src/routing.ts +113 -0
- package/src/settings.ts +361 -0
- package/src/stack.ts +534 -0
- package/src/still-valid.ts +142 -0
- package/src/claims.ts +0 -169
- package/src/errors.ts +0 -222
- package/src/flow.ts +0 -325
- package/src/json.ts +0 -21
- package/src/markers.ts +0 -103
- package/src/paths.ts +0 -46
- package/src/roles.ts +0 -30
- package/src/services.ts +0 -505
- package/src/stage.ts +0 -25
package/src/stack.ts
ADDED
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
// The blocks of a stack of pull requests, each layer built on the one below:
|
|
2
|
+
// bringing a layer up to date with its base, by merging only, and adjusting
|
|
3
|
+
// it to what moved. Nothing is rebased or force-pushed, so a layer's history
|
|
4
|
+
// is never rewritten.
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
activity,
|
|
8
|
+
Git,
|
|
9
|
+
GitHub,
|
|
10
|
+
type Json,
|
|
11
|
+
NeedsHuman,
|
|
12
|
+
type PullRequest,
|
|
13
|
+
Repo,
|
|
14
|
+
SessionMarker,
|
|
15
|
+
StepFailed,
|
|
16
|
+
} from "@chernabog/core";
|
|
17
|
+
import * as Arr from "effect/Array";
|
|
18
|
+
import * as Effect from "effect/Effect";
|
|
19
|
+
import * as Option from "effect/Option";
|
|
20
|
+
import * as Schema from "effect/Schema";
|
|
21
|
+
|
|
22
|
+
import { REFRESH } from "./default-prompts.ts";
|
|
23
|
+
import {
|
|
24
|
+
type AuthorSession,
|
|
25
|
+
baseOf,
|
|
26
|
+
fixWithAgent,
|
|
27
|
+
localGate,
|
|
28
|
+
push,
|
|
29
|
+
waitForCi,
|
|
30
|
+
} from "./implement.ts";
|
|
31
|
+
import { renderPromptOr } from "./prompt.ts";
|
|
32
|
+
import { onlyMergedBase } from "./pull-request.ts";
|
|
33
|
+
import { decisiveReviews, findAuthorSession, requestAgain } from "./review.ts";
|
|
34
|
+
|
|
35
|
+
// base: the branch the layer is built on now, the repo's base branch unless
|
|
36
|
+
// given. lower: the pull request of the layer below, whose head branch is
|
|
37
|
+
// `base` until it merges. id: tells apart two updates in one run, as
|
|
38
|
+
// Implement's RepeatOptions do.
|
|
39
|
+
export type UpdateOptions = {
|
|
40
|
+
readonly base?: string;
|
|
41
|
+
readonly lower?: number;
|
|
42
|
+
readonly id?: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// What updating did. clean: the merge is committed; otherwise it is left in
|
|
46
|
+
// progress with `conflicts` unresolved, for the agent to resolve and commit as
|
|
47
|
+
// resolve-conflicts does, and updating again (with another id) finishes it.
|
|
48
|
+
// base: the branch the layer is now built on, the lower layer's base once it
|
|
49
|
+
// merged. oldBase and newBase: what the layer's own changes were against
|
|
50
|
+
// before, and the base's head merged in now; oldBase is the lower head
|
|
51
|
+
// whenever the lower layer's squash or rebase is being reconciled, finished
|
|
52
|
+
// or not. reconciled: that squash or rebase was recorded as already
|
|
53
|
+
// contained. It is false while a conflict in the base before the lower
|
|
54
|
+
// layer's commits stops the reconcile short, so a flow reads `reconciled`,
|
|
55
|
+
// not `oldBase`, to tell whether the reconcile got that far.
|
|
56
|
+
export const Updated = Schema.Struct({
|
|
57
|
+
clean: Schema.Boolean,
|
|
58
|
+
conflicts: Schema.Array(Schema.String),
|
|
59
|
+
base: Schema.String,
|
|
60
|
+
oldBase: Schema.String,
|
|
61
|
+
newBase: Schema.String,
|
|
62
|
+
reconciled: Schema.Boolean,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
export type Updated = typeof Updated.Type;
|
|
66
|
+
|
|
67
|
+
const MergeCommit = Schema.Struct({ merge_commit_sha: Schema.OptionFromNullOr(Schema.String) });
|
|
68
|
+
|
|
69
|
+
const step = "update-from-base";
|
|
70
|
+
|
|
71
|
+
// The commit GitHub made merging the pull request.
|
|
72
|
+
const mergeCommitOf = (pullRequest: number) =>
|
|
73
|
+
Effect.gen(function* () {
|
|
74
|
+
const repo = yield* Repo;
|
|
75
|
+
|
|
76
|
+
const answer = yield* (yield* GitHub)
|
|
77
|
+
.request("GET", `/repos/${repo.slug}/pulls/${pullRequest}`, Option.none())
|
|
78
|
+
.pipe(
|
|
79
|
+
Effect.flatMap((json: Json) => Schema.decodeUnknownEffect(MergeCommit)(json)),
|
|
80
|
+
Effect.mapError((error) => new StepFailed({ step, message: error.message })),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
return yield* Effect.fromOption(answer.merge_commit_sha).pipe(
|
|
84
|
+
Effect.mapError(
|
|
85
|
+
() => new StepFailed({ step, message: `#${pullRequest} has no merge commit` }),
|
|
86
|
+
),
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const output = (said: string) => said.trim();
|
|
91
|
+
|
|
92
|
+
const lines = (said: string) =>
|
|
93
|
+
said
|
|
94
|
+
.split("\n")
|
|
95
|
+
.map((line) => line.trim())
|
|
96
|
+
.filter((line) => line.length > 0);
|
|
97
|
+
|
|
98
|
+
// Whether `ancestor` is in HEAD's history. git exits 1 for no, and fails
|
|
99
|
+
// alike for a commit it doesn't have, which HEAD's history can't hold either.
|
|
100
|
+
const contains = (worktree: string, ancestor: string) =>
|
|
101
|
+
Git.use((git) => git.run(["merge-base", "--is-ancestor", ancestor, "HEAD"], worktree)).pipe(
|
|
102
|
+
Effect.as(true),
|
|
103
|
+
Effect.catchTag("GitError", () => Effect.succeed(false)),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
// Merges `ref` into HEAD. One that conflicts is left in progress, and its
|
|
107
|
+
// conflicted paths returned; any other failure stays a failure.
|
|
108
|
+
const merge = (worktree: string, args: ReadonlyArray<string>) =>
|
|
109
|
+
Effect.gen(function* () {
|
|
110
|
+
const git = yield* Git;
|
|
111
|
+
|
|
112
|
+
return yield* git.run(["merge", "--no-edit", ...args], worktree).pipe(
|
|
113
|
+
Effect.as<ReadonlyArray<string>>([]),
|
|
114
|
+
Effect.catchTag("GitError", (error) =>
|
|
115
|
+
git.run(["diff", "--name-only", "--diff-filter=U"], worktree).pipe(
|
|
116
|
+
Effect.map(lines),
|
|
117
|
+
Effect.filterOrFail(
|
|
118
|
+
(conflicts) => conflicts.length > 0,
|
|
119
|
+
() => error,
|
|
120
|
+
),
|
|
121
|
+
),
|
|
122
|
+
),
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// The commits a squash or rebase put on the base for the lower layer: those
|
|
127
|
+
// at the tip of the base's first-parent line, ending at `merged`, whose change
|
|
128
|
+
// the lower layer's head has (git cherry's "-"). A squash's one commit
|
|
129
|
+
// matches none of the layer's commits, and is one commit too. A commit on the
|
|
130
|
+
// base just before them whose change matches one of the layer's would be
|
|
131
|
+
// counted too, and recorded with them rather than merged; that is still sound,
|
|
132
|
+
// since its change is one this layer already holds.
|
|
133
|
+
const mergedCount = (worktree: string, lowerHead: string, merged: string) =>
|
|
134
|
+
Effect.gen(function* () {
|
|
135
|
+
const git = yield* Git;
|
|
136
|
+
const fork = output(yield* git.run(["merge-base", lowerHead, merged], worktree));
|
|
137
|
+
const marks = lines(yield* git.run(["cherry", lowerHead, merged, fork], worktree));
|
|
138
|
+
const count = Arr.takeWhile(Arr.reverse(marks), (mark) => mark.startsWith("-")).length;
|
|
139
|
+
|
|
140
|
+
return Math.max(count, 1);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// Merges the base the lower layer squash- or rebase-merged into, in three
|
|
144
|
+
// steps. The base as it was before the lower layer's commits is merged
|
|
145
|
+
// normally, so its conflicts are real ones. Those commits are recorded as
|
|
146
|
+
// merged with `-s ours`, sound because they are exactly the lower layer's
|
|
147
|
+
// change, which this layer already holds: it is built on the head that
|
|
148
|
+
// merged. The rest of the base is then merged normally. A plain merge would
|
|
149
|
+
// instead see this layer's edits of lines the lower layer added as a conflict.
|
|
150
|
+
const reconcile = (worktree: string, base: string, lowerHead: string, merged: string) =>
|
|
151
|
+
Effect.gen(function* () {
|
|
152
|
+
const count = yield* mergedCount(worktree, lowerHead, merged);
|
|
153
|
+
const before = yield* merge(worktree, [`${merged}~${count}`]);
|
|
154
|
+
|
|
155
|
+
if (before.length > 0) {
|
|
156
|
+
return { conflicts: before, reconciled: false };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const message = `Merge ${merged.slice(0, 7)}, the layer below as ${base} merged it, already here`;
|
|
160
|
+
yield* merge(worktree, ["-s", "ours", "-m", message, merged]);
|
|
161
|
+
|
|
162
|
+
return { conflicts: yield* merge(worktree, [`origin/${base}`]), reconciled: true };
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// The layer below's pull request, when there is one.
|
|
166
|
+
const lowerPull = (options: UpdateOptions) =>
|
|
167
|
+
Option.match(Option.fromUndefinedOr(options.lower), {
|
|
168
|
+
onNone: () => Effect.succeed(Option.none<PullRequest>()),
|
|
169
|
+
onSome: (number) => GitHub.use((github) => github.pullRequest(number)).pipe(Effect.asSome),
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// Brings the layer checked out in `worktree` up to date with its base, by
|
|
173
|
+
// merging: the base's new commits when it moved, such as the layer below
|
|
174
|
+
// getting more; or, once the layer below merged, the branch it merged into.
|
|
175
|
+
// A merge commit is merged plainly. A squash or rebase is reconciled when this
|
|
176
|
+
// layer is built on the head that merged, and merged plainly otherwise.
|
|
177
|
+
export const updateFromBase = (worktree: string, options: UpdateOptions = {}) =>
|
|
178
|
+
Effect.gen(function* () {
|
|
179
|
+
const git = yield* Git;
|
|
180
|
+
const given = yield* baseOf(options);
|
|
181
|
+
const below = Option.filter(yield* lowerPull(options), (pull) => pull.state === "merged");
|
|
182
|
+
|
|
183
|
+
const base = Option.match(below, { onNone: () => given, onSome: (pull) => pull.baseRef });
|
|
184
|
+
yield* git.run(["fetch", "--quiet", "origin", base], worktree);
|
|
185
|
+
|
|
186
|
+
const newBase = output(yield* git.run(["rev-parse", `origin/${base}`], worktree));
|
|
187
|
+
|
|
188
|
+
const forkPoint = git
|
|
189
|
+
.run(["merge-base", "HEAD", `origin/${base}`], worktree)
|
|
190
|
+
.pipe(Effect.map(output));
|
|
191
|
+
|
|
192
|
+
const done = (
|
|
193
|
+
oldBase: string,
|
|
194
|
+
merged: { conflicts: ReadonlyArray<string>; reconciled: boolean },
|
|
195
|
+
) => ({
|
|
196
|
+
clean: merged.conflicts.length === 0,
|
|
197
|
+
conflicts: [...merged.conflicts],
|
|
198
|
+
base,
|
|
199
|
+
oldBase,
|
|
200
|
+
newBase,
|
|
201
|
+
reconciled: merged.reconciled,
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
const plainly = Effect.gen(function* () {
|
|
205
|
+
const oldBase = yield* forkPoint;
|
|
206
|
+
|
|
207
|
+
return done(oldBase, {
|
|
208
|
+
conflicts: yield* merge(worktree, [`origin/${base}`]),
|
|
209
|
+
reconciled: false,
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
if (Option.isNone(below)) {
|
|
214
|
+
return yield* plainly;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const lowerHead = below.value.headSha;
|
|
218
|
+
const merged = yield* mergeCommitOf(below.value.number);
|
|
219
|
+
const parents = yield* git.run(["rev-list", "--parents", "-n", "1", merged], worktree);
|
|
220
|
+
// One parent: a squash or rebase. A merge commit has two.
|
|
221
|
+
const squashed = output(parents).split(" ").length === 2;
|
|
222
|
+
|
|
223
|
+
if (
|
|
224
|
+
!squashed ||
|
|
225
|
+
!(yield* contains(worktree, lowerHead)) ||
|
|
226
|
+
(yield* contains(worktree, merged))
|
|
227
|
+
) {
|
|
228
|
+
return yield* plainly;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return done(lowerHead, yield* reconcile(worktree, base, lowerHead, merged));
|
|
232
|
+
}).pipe(
|
|
233
|
+
Effect.catchTag("GitError", (error) =>
|
|
234
|
+
Effect.fail(new StepFailed({ step, message: error.message })),
|
|
235
|
+
),
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
// updateFromBase as a durable step: the merge is made once, and a replay
|
|
239
|
+
// returns what it did.
|
|
240
|
+
export const syncBase = (worktree: string, options: UpdateOptions = {}) =>
|
|
241
|
+
activity(
|
|
242
|
+
Option.match(Option.fromUndefinedOr(options.id), {
|
|
243
|
+
onNone: () => step,
|
|
244
|
+
onSome: (id) => `${step}#${id}`,
|
|
245
|
+
}),
|
|
246
|
+
updateFromBase(worktree, options),
|
|
247
|
+
Updated,
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
// The layer a refresh brings up to date: its pull request and branch, the
|
|
251
|
+
// base it is built on and the layer below's pull request, as updateFromBase
|
|
252
|
+
// takes them, and baseSha, the head of the base the caller saw move, which
|
|
253
|
+
// keys every step: a loop refreshes a layer once per base move. attempt: the
|
|
254
|
+
// caller's count of refreshes at that baseSha, which keys the steps too after
|
|
255
|
+
// the first, so a refresh handed to a person and back runs again rather than
|
|
256
|
+
// replaying the outcome it saved. session: the author's session to resume, the
|
|
257
|
+
// one the implementing run's marker on the pull request names unless given.
|
|
258
|
+
export type LayerRefresh = {
|
|
259
|
+
readonly pullRequest: number;
|
|
260
|
+
readonly branch: string;
|
|
261
|
+
readonly base?: string;
|
|
262
|
+
readonly lower?: number;
|
|
263
|
+
readonly baseSha: string;
|
|
264
|
+
readonly attempt?: number;
|
|
265
|
+
readonly session?: AuthorSession;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// What a refresh did. upToDate: the layer already held its base, so nothing
|
|
269
|
+
// was pushed. adjusted: the author agent was asked to resolve conflicts or
|
|
270
|
+
// adjust the layer. askedAgain: the reviewer asked to review again, none when
|
|
271
|
+
// the new head only merged the base into the head they reviewed, or nobody
|
|
272
|
+
// has reviewed it.
|
|
273
|
+
export type Refreshed = {
|
|
274
|
+
readonly upToDate: boolean;
|
|
275
|
+
readonly adjusted: boolean;
|
|
276
|
+
readonly askedAgain: Option.Option<string>;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
// The most lines of the base's diff a prompt carries; the agent reads the rest itself.
|
|
280
|
+
const DIFF_LINES = 400;
|
|
281
|
+
|
|
282
|
+
const BaseChange = Schema.Struct({
|
|
283
|
+
upToDate: Schema.Boolean,
|
|
284
|
+
touched: Schema.Array(Schema.String),
|
|
285
|
+
diff: Schema.String,
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// What the merge brought in, against what the layer changes itself: whether
|
|
289
|
+
// it changed anything, the files the base changed that the layer changes too,
|
|
290
|
+
// and the base's diff of them.
|
|
291
|
+
const baseChange = (worktree: string, before: string, synced: Updated) =>
|
|
292
|
+
Effect.gen(function* () {
|
|
293
|
+
const git = yield* Git;
|
|
294
|
+
|
|
295
|
+
if (synced.clean && output(yield* git.headSha(worktree)) === before) {
|
|
296
|
+
return { upToDate: true, touched: [], diff: "" };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const own = new Set(
|
|
300
|
+
lines(yield* git.run(["diff", "--name-only", synced.oldBase, before], worktree)),
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
const touched = lines(
|
|
304
|
+
yield* git.run(["diff", "--name-only", synced.oldBase, synced.newBase], worktree),
|
|
305
|
+
).filter((path) => own.has(path));
|
|
306
|
+
|
|
307
|
+
if (touched.length === 0) {
|
|
308
|
+
return { upToDate: false, touched, diff: "" };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const diff = (yield* git.run(
|
|
312
|
+
["diff", synced.oldBase, synced.newBase, "--", ...touched],
|
|
313
|
+
worktree,
|
|
314
|
+
)).split("\n");
|
|
315
|
+
|
|
316
|
+
const shown = diff.slice(0, DIFF_LINES);
|
|
317
|
+
|
|
318
|
+
if (diff.length > DIFF_LINES) {
|
|
319
|
+
shown.push(`… ${diff.length - DIFF_LINES} more lines`);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return { upToDate: false, touched, diff: shown.join("\n").trimEnd() };
|
|
323
|
+
}).pipe(
|
|
324
|
+
Effect.catchTag("GitError", (error) =>
|
|
325
|
+
Effect.fail(new StepFailed({ step: "refresh", message: error.message })),
|
|
326
|
+
),
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
const describeMerge = (synced: Updated) => {
|
|
330
|
+
if (synced.clean) {
|
|
331
|
+
return "The merge is committed; nothing conflicted.";
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return [
|
|
335
|
+
"The merge is left in progress, with these files in conflict:",
|
|
336
|
+
"",
|
|
337
|
+
...synced.conflicts.map((path) => `- ${path}`),
|
|
338
|
+
].join("\n");
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
// Fails with NeedsHuman when the agent left the merge uncommitted, or edits it
|
|
342
|
+
// didn't commit, which the push would leave behind.
|
|
343
|
+
const mergeCommitted = (name: string, worktree: string) =>
|
|
344
|
+
activity(
|
|
345
|
+
name,
|
|
346
|
+
Effect.gen(function* () {
|
|
347
|
+
const git = yield* Git;
|
|
348
|
+
|
|
349
|
+
const inProgress = yield* git
|
|
350
|
+
.run(["rev-parse", "--quiet", "--verify", "MERGE_HEAD"], worktree)
|
|
351
|
+
.pipe(
|
|
352
|
+
Effect.as(true),
|
|
353
|
+
Effect.catchTag("GitError", () => Effect.succeed(false)),
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
if (!inProgress) {
|
|
357
|
+
const uncommitted = lines(
|
|
358
|
+
yield* git
|
|
359
|
+
.run(["status", "--porcelain"], worktree)
|
|
360
|
+
.pipe(Effect.catchTag("GitError", () => Effect.succeed(""))),
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
if (uncommitted.length === 0) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return yield* new NeedsHuman({
|
|
368
|
+
reason: `the author agent left changes it didn't commit: ${uncommitted.join(", ")}`,
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const conflicts = lines(
|
|
373
|
+
yield* git
|
|
374
|
+
.run(["diff", "--name-only", "--diff-filter=U"], worktree)
|
|
375
|
+
.pipe(Effect.catchTag("GitError", () => Effect.succeed(""))),
|
|
376
|
+
);
|
|
377
|
+
|
|
378
|
+
return yield* new NeedsHuman({
|
|
379
|
+
reason: `the author agent left the merge of the moved base uncommitted, with conflicts in ${conflicts.join(", ")}`,
|
|
380
|
+
});
|
|
381
|
+
}),
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
// After the push: the latest approval or request for changes stands when the
|
|
385
|
+
// new head only merged the base into the head it saw (PullRequest.onlyMergedBase),
|
|
386
|
+
// and otherwise its reviewer is asked again. The reviewer, or none. Checked
|
|
387
|
+
// against `base`, the branch the refresh merged in, not the base GitHub still
|
|
388
|
+
// reports, which is the layer below's until the pull request is retargeted.
|
|
389
|
+
const reviewAfterRefresh = (name: string, pullRequest: number, base: string, said: string) =>
|
|
390
|
+
activity(
|
|
391
|
+
name,
|
|
392
|
+
Effect.gen(function* () {
|
|
393
|
+
const latest = Arr.last(yield* decisiveReviews(pullRequest));
|
|
394
|
+
|
|
395
|
+
if (Option.isNone(latest)) {
|
|
396
|
+
return Option.none<string>();
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const pull = yield* GitHub.use((github) => github.pullRequest(pullRequest));
|
|
400
|
+
const since = yield* onlyMergedBase({ ...pull, baseRef: base }, latest.value.commitSha);
|
|
401
|
+
|
|
402
|
+
if (since.only) {
|
|
403
|
+
return Option.none<string>();
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
yield* requestAgain(pullRequest, latest.value.author, said);
|
|
407
|
+
|
|
408
|
+
return Option.some(latest.value.author);
|
|
409
|
+
}).pipe(
|
|
410
|
+
Effect.catchTag("GitError", (error) =>
|
|
411
|
+
Effect.fail(new StepFailed({ step: name, message: error.message })),
|
|
412
|
+
),
|
|
413
|
+
),
|
|
414
|
+
Schema.Option(Schema.String),
|
|
415
|
+
);
|
|
416
|
+
|
|
417
|
+
// Brings a layer whose base moved up to date, outside the review rounds: it
|
|
418
|
+
// merges the base in (syncBase); when that conflicts, or the base changed
|
|
419
|
+
// files the layer changes too, it resumes the author's session with the
|
|
420
|
+
// base's diff, to resolve the conflicts and adjust the layer's own code, test
|
|
421
|
+
// first where behaviour changes (its prompt is .chernabog/prompts/refresh.md,
|
|
422
|
+
// or the SDK's own). Then it runs the local gate, pushes against the base,
|
|
423
|
+
// and waits for CI. A head that only merged the base keeps its review;
|
|
424
|
+
// otherwise the last reviewer is asked again. It writes no review marker, so
|
|
425
|
+
// it never counts against limits.reviewRounds. Every step is keyed by the
|
|
426
|
+
// pull request and baseSha, and by the attempt after the first. Fails as push and waitForCi do, and with
|
|
427
|
+
// NeedsHuman when the agent needs a person, leaves the merge unfinished, or
|
|
428
|
+
// leaves edits uncommitted.
|
|
429
|
+
export const refreshLayer = (worktree: string, layer: LayerRefresh) =>
|
|
430
|
+
Effect.gen(function* () {
|
|
431
|
+
const again = Option.match(
|
|
432
|
+
Option.filter(Option.fromUndefinedOr(layer.attempt), (attempt) => attempt > 1),
|
|
433
|
+
{ onNone: () => "", onSome: (attempt) => `-${attempt}` },
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
const key = `${layer.pullRequest}-${layer.baseSha.slice(0, 12)}${again}`;
|
|
437
|
+
|
|
438
|
+
const before = yield* activity(
|
|
439
|
+
`refresh-head-${key}`,
|
|
440
|
+
Git.use((git) => git.headSha(worktree)).pipe(
|
|
441
|
+
Effect.map(output),
|
|
442
|
+
Effect.catchTag("GitError", (error) =>
|
|
443
|
+
Effect.fail(new StepFailed({ step: "refresh", message: error.message })),
|
|
444
|
+
),
|
|
445
|
+
),
|
|
446
|
+
Schema.String,
|
|
447
|
+
);
|
|
448
|
+
|
|
449
|
+
const synced = yield* syncBase(worktree, {
|
|
450
|
+
...Option.match(Option.fromUndefinedOr(layer.base), {
|
|
451
|
+
onNone: () => ({}),
|
|
452
|
+
onSome: (base) => ({ base }),
|
|
453
|
+
}),
|
|
454
|
+
...Option.match(Option.fromUndefinedOr(layer.lower), {
|
|
455
|
+
onNone: () => ({}),
|
|
456
|
+
onSome: (lower) => ({ lower }),
|
|
457
|
+
}),
|
|
458
|
+
id: `refresh-${key}`,
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
const change = yield* activity(
|
|
462
|
+
`refresh-diff-${key}`,
|
|
463
|
+
baseChange(worktree, before, synced),
|
|
464
|
+
BaseChange,
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
if (change.upToDate) {
|
|
468
|
+
return { upToDate: true, adjusted: false, askedAgain: Option.none() } satisfies Refreshed;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const session = yield* Option.match(Option.fromUndefinedOr(layer.session), {
|
|
472
|
+
onSome: Effect.succeed,
|
|
473
|
+
onNone: () =>
|
|
474
|
+
activity(
|
|
475
|
+
`refresh-author-${key}`,
|
|
476
|
+
findAuthorSession(layer.pullRequest, `refresh-${key}`),
|
|
477
|
+
SessionMarker,
|
|
478
|
+
).pipe(
|
|
479
|
+
Effect.map((author): AuthorSession => ({
|
|
480
|
+
sessionId: Option.some(author.sessionId),
|
|
481
|
+
adapter: Option.some(author.adapter),
|
|
482
|
+
withPerson: author.withPerson === true,
|
|
483
|
+
})),
|
|
484
|
+
),
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
const adjusted = !synced.clean || change.touched.length > 0;
|
|
488
|
+
let said = `Merged \`${synced.base}\` at \`${synced.newBase.slice(0, 7)}\` into this branch.`;
|
|
489
|
+
|
|
490
|
+
if (adjusted) {
|
|
491
|
+
const prompt = yield* renderPromptOr("refresh", REFRESH, {
|
|
492
|
+
pullRequest: layer.pullRequest,
|
|
493
|
+
branch: layer.branch,
|
|
494
|
+
base: synced.base,
|
|
495
|
+
oldBase: synced.oldBase,
|
|
496
|
+
newBase: synced.newBase,
|
|
497
|
+
merge: describeMerge(synced),
|
|
498
|
+
diff: change.diff,
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
const outcome = yield* fixWithAgent(`adjust-${key}`, worktree, prompt, session);
|
|
502
|
+
|
|
503
|
+
if (outcome.status === "needs-clarification") {
|
|
504
|
+
return yield* new NeedsHuman({
|
|
505
|
+
reason: `the author agent needs a person to bring #${layer.pullRequest} up to date with \`${synced.base}\`:\n\n${outcome.questions ?? outcome.summary}`,
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
yield* mergeCommitted(`refresh-committed-${key}`, worktree);
|
|
510
|
+
said = `${said} ${outcome.summary}`;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
yield* localGate(worktree, { id: `refresh-${key}`, session });
|
|
514
|
+
// Checked against the base it is now on, so only the layer's own commits count.
|
|
515
|
+
yield* push(`push-refresh-${key}`, worktree, layer.branch, {
|
|
516
|
+
base: synced.base,
|
|
517
|
+
withPerson: session.withPerson === true,
|
|
518
|
+
});
|
|
519
|
+
yield* waitForCi(
|
|
520
|
+
layer.pullRequest,
|
|
521
|
+
worktree,
|
|
522
|
+
{ ...session, branch: layer.branch, base: synced.base },
|
|
523
|
+
{ id: `refresh-${key}` },
|
|
524
|
+
);
|
|
525
|
+
|
|
526
|
+
const askedAgain = yield* reviewAfterRefresh(
|
|
527
|
+
`refresh-review-${key}`,
|
|
528
|
+
layer.pullRequest,
|
|
529
|
+
synced.base,
|
|
530
|
+
said,
|
|
531
|
+
);
|
|
532
|
+
|
|
533
|
+
return { upToDate: false, adjusted, askedAgain } satisfies Refreshed;
|
|
534
|
+
});
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// What the SDK's blocks count as a run's work item still being there, for a
|
|
2
|
+
// flow's stillValid: the issue or pull request is open, no other run holds
|
|
3
|
+
// its claim, and each pull request the run named as a subject is open. Each reads GitHub as
|
|
4
|
+
// the run's persona; a read that fails fails the check, which is not Gone.
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
Claims,
|
|
8
|
+
GitHub,
|
|
9
|
+
type GitHubError,
|
|
10
|
+
gone,
|
|
11
|
+
reportAs,
|
|
12
|
+
Run,
|
|
13
|
+
type RunInfo,
|
|
14
|
+
valid,
|
|
15
|
+
type Validity,
|
|
16
|
+
} from "@chernabog/core";
|
|
17
|
+
import * as Effect from "effect/Effect";
|
|
18
|
+
import * as Option from "effect/Option";
|
|
19
|
+
|
|
20
|
+
// Gone when another run holds the claim on `item`; this run holding it, or
|
|
21
|
+
// nobody, is fine: a run may not have claimed it yet, or has let it go. A
|
|
22
|
+
// claim whose lease expired is nobody's: the run may be taking it over.
|
|
23
|
+
const claimValidity = (item: string, what: string) =>
|
|
24
|
+
Effect.gen(function* () {
|
|
25
|
+
const run = yield* Run;
|
|
26
|
+
|
|
27
|
+
const other = Option.filter(
|
|
28
|
+
yield* Claims.liveHolder(item),
|
|
29
|
+
(found) => found.runId !== run.runId,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
return Option.match(other, {
|
|
33
|
+
onNone: (): Validity => valid,
|
|
34
|
+
onSome: (found) => gone(`run ${found.runId} of ${found.flow} holds the claim on ${what}`),
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Gone when the pull request is closed, merged or no longer there.
|
|
39
|
+
const pullRequestOpen = (number: number) =>
|
|
40
|
+
GitHub.use((github) => github.pullRequest(number)).pipe(
|
|
41
|
+
Effect.map((pull): Validity => {
|
|
42
|
+
if (pull.state === "open") {
|
|
43
|
+
return valid;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return gone(`#${number} was ${pull.state}`);
|
|
47
|
+
}),
|
|
48
|
+
Effect.catchTag("NotFound", () => Effect.succeed(gone(`#${number} no longer exists`))),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
// Gone when the issue is closed or no longer there.
|
|
52
|
+
const issueOpen = (number: number) =>
|
|
53
|
+
GitHub.use((github) => github.issue(number)).pipe(
|
|
54
|
+
Effect.map((issue): Validity => {
|
|
55
|
+
if (issue.state === "closed") {
|
|
56
|
+
return gone(`#${number} was closed`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return valid;
|
|
60
|
+
}),
|
|
61
|
+
Effect.catchTag("NotFound", () => Effect.succeed(gone(`#${number} no longer exists`))),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
export type IssueChecks = {
|
|
65
|
+
// Whether each pull request the run named as a subject must still be open.
|
|
66
|
+
readonly pullRequest: boolean;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const PULL_REQUEST_SUBJECT = /^pr-(\d+)$/;
|
|
70
|
+
|
|
71
|
+
// The pull requests the run named as subjects, or, from a binary from before
|
|
72
|
+
// subjects, the one it linked.
|
|
73
|
+
const pullRequestsOf = (run: RunInfo) =>
|
|
74
|
+
Option.match(Option.fromUndefinedOr(run.subjects), {
|
|
75
|
+
onNone: () => Option.toArray(Option.fromUndefinedOr(run.pullRequest)),
|
|
76
|
+
onSome: (subjects) =>
|
|
77
|
+
subjects.flatMap((subject) =>
|
|
78
|
+
Option.toArray(
|
|
79
|
+
Option.map(Option.fromNullOr(PULL_REQUEST_SUBJECT.exec(subject)), (match) =>
|
|
80
|
+
Number(match[1]),
|
|
81
|
+
),
|
|
82
|
+
),
|
|
83
|
+
),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// The first of `checks` that says gone, or valid.
|
|
87
|
+
const allValid = <R>(checks: ReadonlyArray<Effect.Effect<Validity, GitHubError, R>>) =>
|
|
88
|
+
Effect.gen(function* () {
|
|
89
|
+
for (const check of checks) {
|
|
90
|
+
const validity = yield* check;
|
|
91
|
+
|
|
92
|
+
if (!validity.valid) {
|
|
93
|
+
return validity;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return valid;
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// An issue's run: the issue is open, its claim is this run's or nobody's,
|
|
101
|
+
// and, when asked, each pull request it named as a subject is open.
|
|
102
|
+
export const issueValidity = (number: number, checks: IssueChecks) =>
|
|
103
|
+
Effect.gen(function* () {
|
|
104
|
+
const run = yield* Run;
|
|
105
|
+
const open = yield* issueOpen(number);
|
|
106
|
+
|
|
107
|
+
if (!open.valid) {
|
|
108
|
+
return open;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const claim = yield* claimValidity(Claims.issue(number), `#${number}`);
|
|
112
|
+
|
|
113
|
+
if (!claim.valid || !checks.pullRequest) {
|
|
114
|
+
return claim;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return yield* allValid(pullRequestsOf(run).map(pullRequestOpen));
|
|
118
|
+
}).pipe(reportAs("still-valid"));
|
|
119
|
+
|
|
120
|
+
// A pull request's run: the pull request is open, and its claim is this run's or nobody's.
|
|
121
|
+
export const pullRequestValidity = (number: number) =>
|
|
122
|
+
Effect.gen(function* () {
|
|
123
|
+
const open = yield* pullRequestOpen(number);
|
|
124
|
+
|
|
125
|
+
if (!open.valid) {
|
|
126
|
+
return open;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return yield* claimValidity(Claims.pullRequest(number), `#${number}`);
|
|
130
|
+
}).pipe(reportAs("still-valid"));
|
|
131
|
+
|
|
132
|
+
// An epic's run: the epic is open, and its claim is this run's or nobody's.
|
|
133
|
+
export const epicValidity = (number: number) =>
|
|
134
|
+
Effect.gen(function* () {
|
|
135
|
+
const open = yield* issueOpen(number);
|
|
136
|
+
|
|
137
|
+
if (!open.valid) {
|
|
138
|
+
return open;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return yield* claimValidity(Claims.epic(number), `#${number}`);
|
|
142
|
+
}).pipe(reportAs("still-valid"));
|