@kitman-sh/software-engineering 0.12.0-rc.2 → 0.12.0-rc.4

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/src/shepherd.ts CHANGED
@@ -225,6 +225,9 @@ const repliesPrompt = (threads: ReviewThreads['threads']) =>
225
225
  /** A thread's key changes when someone adds to it, so a new comment on an answered thread is answered again. */
226
226
  const threadKey = (thread: ReviewThreads['threads'][number]) => `${thread.id}:${thread.comments.length}`
227
227
 
228
+ /** A passing review of a head with tests verify never ran: Shepherd's own `review` Step, or another Run's it `reuses`. */
229
+ type UnranReview = { readonly notRun: readonly string[] } & ({ readonly review: string } | { readonly reuses: string })
230
+
228
231
  /**
229
232
  * Takes an open ChangeRequest to merge: it waits on its Events with no model time between them, fixes a pipeline the
230
233
  * branch broke (after ruling out a flake and a target branch that fails the same check, which it waits out), answers
@@ -233,7 +236,8 @@ const threadKey = (thread: ReviewThreads['threads'][number]) => `${thread.id}:${
233
236
  * pipeline, merges its target in before any pipeline is run or fixed; one that is only behind catches up when its merge
234
237
  * finds it behind, on its turn. One whose merge Shepherd cannot settle goes back to Build. It merges the
235
238
  * target in and merges only while it holds the target's turn, so ChangeRequests for one target land one at a time, in
236
- * order. A head nobody approved gets a review of what changed since the last approved head, and the head a catch-up
239
+ * order. A head with a passed Review of the whole change that any Run recorded, such as the Build that shipped it, is
240
+ * approved with that Review instead of being reviewed again. A head nobody approved gets a review of what changed since the last approved head, and the head a catch-up
237
241
  * pushed gets its pipeline started and its review as soon as the forge shows it, not once its pipeline passes. It runs one ci-fix and two
238
242
  * head-fix rounds, each judged by the pipeline at the head the last fix pushed, then hands the ChangeRequest back to
239
243
  * Build, and stops as needs you, with why, instead of spinning. Left for a person to merge, it watches the target for up to
@@ -244,7 +248,7 @@ const threadKey = (thread: ReviewThreads['threads'][number]) => `${thread.id}:${
244
248
  */
245
249
  export default defineLoop({
246
250
  id: 'shepherd',
247
- version: '1.8.0',
251
+ version: '1.9.0',
248
252
  title: 'Shepherd',
249
253
  input: Input,
250
254
  output: Output,
@@ -283,8 +287,10 @@ export default defineLoop({
283
287
  const leftToPerson = new Set<string>()
284
288
  /** The last review of an unapproved head's open findings, for the review of the head that fixes them. */
285
289
  let headFindings: readonly MergedFinding[] = []
286
- /** Each head Shepherd's review passed but left unapproved, as verify never ran these of its tests. */
287
- const unranAt = new Map<string, { readonly notRun: readonly string[]; readonly review: string }>()
290
+ /** The head that review read. */
291
+ let headFindingsAt: string | undefined
292
+ /** Each head a review passed but left unapproved, as verify never ran these of its tests. */
293
+ const unranAt = new Map<string, UnranReview>()
288
294
  const rounds: Record<RoundKind, number> = { 'ci-fix': 0, 'head-fix': 0 }
289
295
  /** Rounds past the limit a person allowed. */
290
296
  const allowed: Record<RoundKind, number> = { 'ci-fix': 0, 'head-fix': 0 }
@@ -300,6 +306,8 @@ export default defineLoop({
300
306
  /** The base sync whose merged-from head's approval covers the rest of the change. */
301
307
  readonly sync: string
302
308
  readonly earlier: readonly MergedFinding[]
309
+ /** The head the review that found `earlier` read. */
310
+ readonly reviewed?: string
303
311
  }
304
312
  | undefined
305
313
 
@@ -307,8 +315,12 @@ export default defineLoop({
307
315
  let reviewOnceShown: string | undefined
308
316
  /** The head the last catch-up pushed, whose pipeline runs before Shepherd catches up again. */
309
317
  let pipelineFirst: string | undefined
310
- /** Heads Shepherd's own review passed, approved or left to a person for tests verify never ran. */
318
+ /** Heads Shepherd's own review passed, approved or left to a person for tests verify never ran, or it reused a Review of. */
311
319
  const reviewedHeads = new Set<string>()
320
+ /** Each passed Review another Run recorded that stood for a review of a head here. */
321
+ const reused: { readonly review: string; readonly head: string }[] = []
322
+ /** Heads it tried to approve with a Review another Run recorded, so a refusal is not asked again. */
323
+ const reuseTried = new Set<string>()
312
324
  /** The id of the approval Shepherd's own review recorded, by the head it approved. */
313
325
  const approvalOf = new Map<string, string>()
314
326
 
@@ -361,6 +373,7 @@ export default defineLoop({
361
373
  reason,
362
374
  fixRounds: fixRounds(),
363
375
  replies,
376
+ ...(reused.length === 0 ? undefined : { reused }),
364
377
  ...(input.releaseWhen === undefined ? undefined : { releaseWhen: input.releaseWhen }),
365
378
  ...(input.deliversTo === undefined ? undefined : { deliversTo: input.deliversTo }),
366
379
  ...(status === 'needs_you' || status === 'handed_back' ? { unsettled: [reason] } : undefined)
@@ -407,14 +420,19 @@ export default defineLoop({
407
420
  }
408
421
  })
409
422
 
410
- const handBack = (reason: string, open: string): Output => ({
423
+ const handBack = (
424
+ reason: string,
425
+ open: string,
426
+ review?: { readonly reviewed: string; readonly earlier: readonly MergedFinding[] }
427
+ ): Output => ({
411
428
  ...done('handed_back', reason),
412
429
  handBack: {
413
430
  changeRequestId,
414
431
  words: `Shepherd handed ChangeRequest ${changeRequestId} back: ${reason}. Fix it forward on its branch:\n${open}`,
415
432
  ...(input.taskId === undefined ? undefined : { taskId: input.taskId }),
416
433
  // Build's target, over the offer's default of this Run's branch, which is the ChangeRequest's own.
417
- ...(target === 'HEAD' ? undefined : { branch: target })
434
+ ...(target === 'HEAD' ? undefined : { branch: target }),
435
+ ...review
418
436
  }
419
437
  })
420
438
 
@@ -797,7 +815,8 @@ export default defineLoop({
797
815
  head,
798
816
  files: files.files,
799
817
  ...(head === files.synced ? undefined : { since: files.synced }),
800
- ...(files.earlier.length === 0 ? undefined : { earlier: files.earlier })
818
+ ...(files.earlier.length === 0 ? undefined : { earlier: files.earlier }),
819
+ ...(files.reviewed === undefined ? undefined : { reviewed: files.reviewed })
801
820
  })
802
821
 
803
822
  if (reviewed.blocking) {
@@ -807,7 +826,8 @@ export default defineLoop({
807
826
  return stop(
808
827
  handBack(
809
828
  `The review of the catch-up merge's files still finds ${blocking.map(finding => finding.id).join(', ')} after ${rounds['head-fix']} head-fix rounds`,
810
- findingLines(blocking)
829
+ findingLines(blocking),
830
+ { reviewed: head, earlier: reviewed.findings }
811
831
  )
812
832
  )
813
833
  rounds['head-fix']++
@@ -819,7 +839,7 @@ export default defineLoop({
819
839
  timeout: stepBudget
820
840
  })
821
841
 
822
- unreviewed = { ...files, earlier: reviewed.findings }
842
+ unreviewed = { ...files, earlier: reviewed.findings, reviewed: head }
823
843
  fixedFrom = { head, changed: fix.changed, looks: 0 }
824
844
 
825
845
  return { kind: 'fixing' } as const
@@ -899,7 +919,8 @@ export default defineLoop({
899
919
  head,
900
920
  ...(notRun.length === 0 ? undefined : { notRun }),
901
921
  ...(since === undefined ? undefined : { approved: since }),
902
- ...(headFindings.length === 0 ? undefined : { earlier: headFindings })
922
+ ...(headFindings.length === 0 ? undefined : { earlier: headFindings }),
923
+ ...(headFindingsAt === undefined ? undefined : { reviewed: headFindingsAt })
903
924
  })
904
925
 
905
926
  if (reviewed.blocking) {
@@ -909,7 +930,8 @@ export default defineLoop({
909
930
  return stop(
910
931
  handBack(
911
932
  `The review of ${head} still finds ${blocking.map(finding => finding.id).join(', ')} after ${rounds['head-fix']} head-fix rounds`,
912
- findingLines(blocking)
933
+ findingLines(blocking),
934
+ { reviewed: head, earlier: reviewed.findings }
913
935
  )
914
936
  )
915
937
  rounds['head-fix']++
@@ -925,12 +947,14 @@ export default defineLoop({
925
947
  })
926
948
 
927
949
  headFindings = reviewed.findings
950
+ headFindingsAt = head
928
951
  fixedFrom = { head, changed: fix.changed, looks: 0 }
929
952
 
930
953
  return { kind: 'fixing' } as const
931
954
  }
932
955
 
933
956
  headFindings = []
957
+ headFindingsAt = undefined
934
958
 
935
959
  if (notRun.length > 0) unranAt.set(head, { notRun, review: `head-review:${cycle}` })
936
960
  else {
@@ -957,6 +981,45 @@ export default defineLoop({
957
981
  return { kind: 'reviewed' } as const
958
982
  })
959
983
 
984
+ /**
985
+ * Approves the head of `pipeline` with the passed Review of the whole change at it that another Run recorded, such
986
+ * as the Build that shipped it, rather than reviewing it again; later heads are reviewed only since it. Like its own
987
+ * review, it leaves a head with tests verify never ran unapproved until a person chooses to merge it.
988
+ */
989
+ const reuseReview = (cycle: number, pipeline: PipelineStatus, reviewed: NonNullable<PipelineStatus['reviewed']>) =>
990
+ Effect.gen(function* () {
991
+ const { head } = pipeline
992
+
993
+ reuseTried.add(head)
994
+
995
+ const notRun = [...new Set([...(input.notRun ?? []), ...(reviewed.notRun ?? [])])]
996
+
997
+ if (notRun.length > 0) {
998
+ unranAt.set(head, { notRun, reuses: reviewed.review })
999
+ reviewedHeads.add(head)
1000
+ reused.push({ review: reviewed.review, head })
1001
+
1002
+ return true
1003
+ }
1004
+
1005
+ const recorded = yield* approveChangeRequest(`reuse:${cycle}`, {
1006
+ changeRequestId,
1007
+ head,
1008
+ summary: `Review ${reviewed.review} passed the whole change at ${head}, so Shepherd approves it without reviewing it again.`,
1009
+ reuses: reviewed.review
1010
+ }).pipe(
1011
+ Effect.catchTag('StepFailed', error => Effect.succeed({ approved: false as const, reason: error.reason }))
1012
+ )
1013
+
1014
+ if (!recorded.approved) return false
1015
+
1016
+ approvalOf.set(head, recorded.reviewId)
1017
+ reviewedHeads.add(head)
1018
+ reused.push({ review: reviewed.review, head })
1019
+
1020
+ return true
1021
+ })
1022
+
960
1023
  /** Reviews the head the last catch-up pushed, if `pipeline` shows it and it still needs a review. */
961
1024
  const reviewCaughtUp = (cycle: number, pipeline: PipelineStatus) =>
962
1025
  Effect.gen(function* () {
@@ -1035,6 +1098,14 @@ export default defineLoop({
1035
1098
  else return found
1036
1099
  }
1037
1100
 
1101
+ const reviewedElsewhere =
1102
+ pipeline.approved === pipeline.head || reviewedHeads.has(pipeline.head) || reuseTried.has(pipeline.head)
1103
+ ? undefined
1104
+ : pipeline.reviewed
1105
+
1106
+ // Read again, so this head's approval is what the gates below see.
1107
+ if (reviewedElsewhere !== undefined && (yield* reuseReview(cycle, pipeline, reviewedElsewhere))) continue
1108
+
1038
1109
  // Behind alone waits for the merge, which catches up while holding the turn: catching up any sooner is undone
1039
1110
  // by every merge that lands before this one's turn. A failing pipeline catches up first, so a ci-fix never
1040
1111
  // chases what the target already fixed.
@@ -1432,7 +1503,7 @@ export default defineLoop({
1432
1503
  changeRequestId,
1433
1504
  head: pipeline.head,
1434
1505
  summary: `A review of ${pipeline.head} passed with nothing blocking; a person chose to merge it though verify never ran some of its tests.`,
1435
- review: unapproved.review
1506
+ ...('reuses' in unapproved ? { reuses: unapproved.reuses } : { review: unapproved.review })
1436
1507
  }).pipe(
1437
1508
  Effect.catchTag('StepFailed', error => Effect.succeed({ approved: false as const, reason: error.reason }))
1438
1509
  )