@chernabog/sdk 0.9.0 → 0.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chernabog/sdk",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "license": "MIT",
5
5
  "description": "The blocks Chernabog flows are written from (Implement, Review, Plan, Hybrid, PullRequest, prompts), over @chernabog/core, which it re-exports",
6
6
  "repository": {
package/src/epic.ts CHANGED
@@ -39,8 +39,13 @@ export type Ready = {
39
39
  readonly stackedOn: Option.Option<number>;
40
40
  };
41
41
 
42
- // A task that waits, and why, as a WorkItem's `held` says it.
43
- export type Held = { readonly state: "held"; readonly reason: string };
42
+ // A task that waits, and why, as a WorkItem's `held` says it, and the tasks it
43
+ // waits on.
44
+ export type Held = {
45
+ readonly state: "held";
46
+ readonly reason: string;
47
+ readonly waitsOn: ReadonlyArray<number>;
48
+ };
44
49
 
45
50
  export type Task = { readonly number: number; readonly title: string; readonly kind: Kind } & (
46
51
  Ready | Held
@@ -79,7 +84,14 @@ const ready = (base: string, stackedOn: Option.Option<number>): Ready => ({
79
84
  stackedOn,
80
85
  });
81
86
 
82
- const held = (reason: string): Held => ({ state: "held", reason });
87
+ const held = (reason: string, blockers: ReadonlyArray<number>): Held => ({
88
+ state: "held",
89
+ reason,
90
+ waitsOn: blockers,
91
+ });
92
+
93
+ // A task held until `blockers` close.
94
+ const heldOn = (blockers: ReadonlyArray<number>) => held(waitsOn(blockers), blockers);
83
95
 
84
96
  // The tasks an epic run started, whose claim took their queue label off, so
85
97
  // they no longer read as auto: a task stacks on one as on an auto task.
@@ -171,23 +183,23 @@ export const tasks = (epic: number, options: TasksOptions = {}) =>
171
183
  }
172
184
 
173
185
  if (blockers.length > 1) {
174
- return held(waitsOn(blockers.map((blocker) => blocker.number)));
186
+ return heldOn(blockers.map((blocker) => blocker.number));
175
187
  }
176
188
 
177
189
  const [blocker] = blockers;
178
190
 
179
191
  if (!auto.has(blocker.number)) {
180
- return held(waitsOn([blocker.number]));
192
+ return heldOn([blocker.number]);
181
193
  }
182
194
 
183
195
  const pull = yield* pullOf(blocker.number);
184
196
 
185
197
  if (Option.isNone(pull)) {
186
- return held(waitsOn([blocker.number]));
198
+ return heldOn([blocker.number]);
187
199
  }
188
200
 
189
201
  if ((yield* depthOf(pull.value)) + 1 > stackDepth) {
190
- return held(`waits for #${blocker.number}'s stack to merge`);
202
+ return held(`waits for #${blocker.number}'s stack to merge`, [blocker.number]);
191
203
  }
192
204
 
193
205
  return ready(Claims.branchFor(blocker.number), Option.some(blocker.number));
@@ -213,7 +225,7 @@ export const tasks = (epic: number, options: TasksOptions = {}) =>
213
225
  return taskOf(issue, ready(base, Option.none()));
214
226
  }
215
227
 
216
- return taskOf(issue, held(waitsOn(others.map((other) => other.number))));
228
+ return taskOf(issue, heldOn(others.map((other) => other.number)));
217
229
  };
218
230
 
219
231
  return {
@@ -256,6 +268,9 @@ export const Layer = Schema.Struct({
256
268
  stackedOn: Schema.optionalKey(Schema.Finite),
257
269
  reason: Schema.optionalKey(Schema.String),
258
270
  pullRequest: Schema.optionalKey(Schema.Finite),
271
+ // A held layer's: the sub-issues it waits on. None in a progress read before
272
+ // layers kept it.
273
+ waitsOn: Schema.optionalKey(Schema.Array(Schema.Finite)),
259
274
  });
260
275
 
261
276
  export type Layer = typeof Layer.Type;
@@ -382,7 +397,7 @@ export const progressOf = (epic: number) =>
382
397
  }
383
398
 
384
399
  if (task.state === "held") {
385
- return { ...head, state: "held", reason: task.reason };
400
+ return { ...head, state: "held", reason: task.reason, waitsOn: task.waitsOn };
386
401
  }
387
402
 
388
403
  return Option.match(task.stackedOn, {
package/src/stack.ts CHANGED
@@ -103,6 +103,13 @@ const contains = (worktree: string, ancestor: string) =>
103
103
  Effect.catchTag("GitError", () => Effect.succeed(false)),
104
104
  );
105
105
 
106
+ // Whether `descendant` holds `ancestor` in its history.
107
+ const holds = (worktree: string, descendant: string, ancestor: string) =>
108
+ Git.use((git) => git.run(["merge-base", "--is-ancestor", ancestor, descendant], worktree)).pipe(
109
+ Effect.as(true),
110
+ Effect.catchTag("GitError", () => Effect.succeed(false)),
111
+ );
112
+
106
113
  // Merges `ref` into HEAD. One that conflicts is left in progress, and its
107
114
  // conflicted paths returned; any other failure stays a failure.
108
115
  const merge = (worktree: string, args: ReadonlyArray<string>) =>
@@ -235,6 +242,84 @@ export const updateFromBase = (worktree: string, options: UpdateOptions = {}) =>
235
242
  ),
236
243
  );
237
244
 
245
+ const ClosedPull = Schema.Struct({
246
+ number: Schema.Finite,
247
+ merged_at: Schema.OptionFromNullOr(Schema.String),
248
+ merge_commit_sha: Schema.OptionFromNullOr(Schema.String),
249
+ head: Schema.Struct({ sha: Schema.String }),
250
+ });
251
+
252
+ // The pull requests closed lately into `base`, newest first, one page.
253
+ const closedInto = (base: string) =>
254
+ Effect.gen(function* () {
255
+ const repo = yield* Repo;
256
+ const query = `state=closed&base=${encodeURIComponent(base)}&sort=updated&direction=desc&per_page=30`;
257
+
258
+ return yield* (yield* GitHub)
259
+ .request("GET", `/repos/${repo.slug}/pulls?${query}`, Option.none())
260
+ .pipe(
261
+ Effect.flatMap((json: Json) => Schema.decodeUnknownEffect(Schema.Array(ClosedPull))(json)),
262
+ Effect.mapError(
263
+ (error) => new StepFailed({ step: "merged-below", message: error.message }),
264
+ ),
265
+ );
266
+ });
267
+
268
+ // The layer below the one checked out in `worktree` that squash- or
269
+ // rebase-merged into `base` without this layer recording it: a pull request
270
+ // merged lately into `base` whose head is in this layer's history and whose
271
+ // merge commit isn't. Its number, for updateFromBase's `lower`; none when
272
+ // there is none. It is for a layer whose pull request already moved onto
273
+ // `base`, as GitHub moves one when the branch below it is deleted, so its
274
+ // layer below is no longer its base: merging `base` plainly would see this
275
+ // layer's edits of lines the layer below added as conflicts, and bring back
276
+ // files `base` has since moved or deleted (#311). With two such layers below,
277
+ // it is the higher one, whose head holds the other's.
278
+ export const mergedBelow = (worktree: string, base: string) =>
279
+ Effect.gen(function* () {
280
+ const git = yield* Git;
281
+ yield* git.run(["fetch", "--quiet", "origin", base], worktree);
282
+
283
+ const found: Array<{ number: number; head: string }> = [];
284
+
285
+ for (const pull of yield* closedInto(base)) {
286
+ if (Option.isNone(pull.merged_at) || Option.isNone(pull.merge_commit_sha)) {
287
+ continue;
288
+ }
289
+
290
+ if (
291
+ (yield* contains(worktree, pull.head.sha)) &&
292
+ !(yield* contains(worktree, pull.merge_commit_sha.value))
293
+ ) {
294
+ found.push({ number: pull.number, head: pull.head.sha });
295
+ }
296
+ }
297
+
298
+ // The one no other found head is built on.
299
+ for (const candidate of found) {
300
+ let highest = true;
301
+
302
+ for (const other of found) {
303
+ if (
304
+ other.number !== candidate.number &&
305
+ (yield* holds(worktree, other.head, candidate.head))
306
+ ) {
307
+ highest = false;
308
+ }
309
+ }
310
+
311
+ if (highest) {
312
+ return Option.some(candidate.number);
313
+ }
314
+ }
315
+
316
+ return Option.none<number>();
317
+ }).pipe(
318
+ Effect.catchTag("GitError", (error) =>
319
+ Effect.fail(new StepFailed({ step: "merged-below", message: error.message })),
320
+ ),
321
+ );
322
+
238
323
  // updateFromBase as a durable step: the merge is made once, and a replay
239
324
  // returns what it did.
240
325
  export const syncBase = (worktree: string, options: UpdateOptions = {}) =>