@chernabog/recipes 0.6.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.
@@ -7,6 +7,8 @@
7
7
  // An issue an open pull request on another branch already closes is not
8
8
  // implemented again: claim adopts that pull request (auto:adopted), and the
9
9
  // run waits for CI, fixing red in a worktree on its branch, and hands it off.
10
+ // Its wait for CI parks on the issue's record too, on a phase of its own
11
+ // (awaiting-adopted-ci), which keeps the adopted pull request.
10
12
  // A draft waits for its author to mark it ready first. Several, or one on a
11
13
  // fork, go to a person.
12
14
  //
@@ -21,6 +23,7 @@
21
23
  // in flight resumes by, so renaming one restarts that step.
22
24
 
23
25
  import {
26
+ Blocks,
24
27
  type CiTimedOut,
25
28
  Claims,
26
29
  defineFlow,
@@ -36,6 +39,7 @@ import {
36
39
  Routing,
37
40
  Settings,
38
41
  Skip,
42
+ StatusLabels,
39
43
  } from "@chernabog/sdk";
40
44
  import * as Effect from "effect/Effect";
41
45
  import * as Option from "effect/Option";
@@ -49,8 +53,9 @@ import * as Schema from "effect/Schema";
49
53
  // with a board, only those whose Status is its readyStatus. An issue another
50
54
  // open issue blocks waits for it, as the tasks of a plan's epic do, and a task
51
55
  // of an epic that an epic run holds is that run's to start; both are held,
52
- // and `chernabog status` says why. And the issues whose adopted draft its
53
- // author marked ready, which wake their run.
56
+ // and `chernabog status` says why. And the issues waiting on an adopted
57
+ // draft whose author marked it ready, or whose wait's time is up: they wake a
58
+ // run still parked on it, or a CI job's ended one is taken up by a new run.
54
59
  const trigger = Effect.gen(function* () {
55
60
  const github = yield* GitHub;
56
61
  const labelNames = yield* Settings.labels;
@@ -68,7 +73,11 @@ const trigger = Effect.gen(function* () {
68
73
  ...(yield* Implement.queued(Implement.followUpsFirst(shipping), Option.some(labelNames.ship))),
69
74
  ...(yield* Implement.readied),
70
75
  ];
71
- }).pipe(reportAs("trigger"));
76
+ }).pipe(
77
+ // Every record it reads is checked first, as its document's are (ADR 21, decision 5).
78
+ Blocks.checked("issues.queued", "ci.waitAndFix", "ci.waitPreparing", "pr.awaitReady"),
79
+ reportAs("trigger"),
80
+ );
72
81
 
73
82
  // How long an adopted draft waits for its author to mark it ready.
74
83
  const READY_WITHIN = "14 days";
@@ -126,26 +135,42 @@ const flow = defineFlow({
126
135
  const adoption = Option.fromUndefinedOr(claimed.adopted);
127
136
  const resuming = Implement.resumingCi(item);
128
137
 
129
- // A CI job ended while this issue's pull request's CI ran, and it has
138
+ // A run ended while this issue's pull request's CI ran, and it has
130
139
  // finished: wait on from the round the record names (a fix of red CI
131
- // starts a session of its own), then hand off.
140
+ // starts a session of its own), then hand off. The record's phase
141
+ // says which wait it was: awaiting-adopted-ci hands the pull request
142
+ // off as adopted, since no agent implemented it.
132
143
  if (Option.isSome(resuming)) {
133
144
  const waited = resuming.value;
145
+ const resumedAdoption = Implement.resumedAdoption(waited);
134
146
  pullRequest = Option.some(waited.pullRequest);
135
147
 
148
+ const parking = Option.match(resumedAdoption, {
149
+ onNone: () => Implement.parkOnIssue(issue),
150
+ onSome: (adopted) => Implement.parkOnAdopted(issue, adopted.number),
151
+ });
152
+
136
153
  return yield* Effect.gen(function* () {
137
154
  yield* Implement.waitForCiMaking(
138
155
  waited.pullRequest,
139
156
  Implement.prepareWorktree(branch, { base }),
140
157
  { ...Implement.thisRunsSession, branch, base },
141
- { ...Implement.parkOnIssue(issue), firstRound: waited.round },
158
+ { ...parking, firstRound: waited.round },
142
159
  );
143
160
 
144
- return yield* Implement.handOff(
145
- issue,
146
- { number: waited.pullRequest, url: waited.url },
147
- Implement.resumedOutcome,
148
- );
161
+ return yield* Option.match(resumedAdoption, {
162
+ onNone: () =>
163
+ Implement.handOff(
164
+ issue,
165
+ { number: waited.pullRequest, url: waited.url },
166
+ Implement.resumedOutcome,
167
+ ),
168
+ onSome: (adopted) =>
169
+ Implement.handOff(issue, adopted, Implement.adoptedOutcome(adopted), {
170
+ needsReview: true,
171
+ adoptedBranch: branch,
172
+ }),
173
+ });
149
174
  }).pipe(Effect.catchTags(forPerson));
150
175
  }
151
176
 
@@ -164,7 +189,9 @@ const flow = defineFlow({
164
189
  });
165
190
  }
166
191
 
167
- // The worktree is made only for a fix.
192
+ // The worktree is made only for a fix. Still-pending CI parks on
193
+ // the issue's record, which keeps the pull request, and the item
194
+ // comes back through Implement.awaitingCi once CI finishes.
168
195
  yield* Implement.waitForCiMaking(
169
196
  adopted.number,
170
197
  Implement.prepareWorktree(branch, { base }),
@@ -173,6 +200,7 @@ const flow = defineFlow({
173
200
  branch,
174
201
  base,
175
202
  },
203
+ Implement.parkOnAdopted(issue, adopted.number),
176
204
  );
177
205
 
178
206
  return yield* Implement.handOff(issue, adopted, Implement.adoptedOutcome(adopted), {
@@ -213,7 +241,7 @@ const flow = defineFlow({
213
241
  Effect.catchTag("StepFailed", (failed) => toPerson(`${failed.step}: ${failed.message}`)),
214
242
  // However the run ends, its claim on the issue goes; the branch and
215
243
  // any pull request stay.
216
- Claims.releasing(Claims.issue(issue)),
244
+ StatusLabels.releasing(Claims.issue(issue)),
217
245
  );
218
246
  }).pipe(
219
247
  // An issue deleted or transferred under the run leaves nothing to do.
package/flows/plan.ts CHANGED
@@ -13,6 +13,7 @@
13
13
  // run in flight resumes by, so renaming one restarts that step.
14
14
 
15
15
  import {
16
+ Blocks,
16
17
  Claims,
17
18
  defineFlow,
18
19
  Events,
@@ -21,9 +22,11 @@ import {
21
22
  NeedsHuman,
22
23
  Plan,
23
24
  reportAs,
25
+ Resume,
24
26
  Routing,
25
27
  Settings,
26
28
  Skip,
29
+ StatusLabels,
27
30
  type WorkItem,
28
31
  } from "@chernabog/sdk";
29
32
  import * as Effect from "effect/Effect";
@@ -36,27 +39,70 @@ const ANSWER_WITHIN = "14 days";
36
39
 
37
40
  // Specs labelled auto:plan, oldest first, as work to start; with a board,
38
41
  // only those whose Status is its readyStatus, routed by their assignees. Then
39
- // specs whose posted plan a person has answered, which wake the run waiting on it.
42
+ // specs whose posted plan a person has answered, or nobody answered within
43
+ // ANSWER_WITHIN: they wake the run still waiting on it, or a CI job's ended
44
+ // one is taken up by a new run, from the spec's record.
40
45
  const trigger = Effect.gen(function* () {
41
46
  const github = yield* GitHub;
42
47
  const queued = yield* github.queuedIssuesLabelled(Plan.LABELS.plan);
43
- const answered = yield* Plan.answers;
48
+ const answered = yield* Plan.answered(ANSWER_WITHIN);
44
49
 
45
50
  const specs = yield* Effect.forEach(queued, (issue) =>
46
51
  Implement.routed(issue, Implement.issueItem(issue, Option.some(Plan.LABELS.plan))),
47
52
  );
48
53
 
49
54
  return [...specs, ...answered];
50
- }).pipe(reportAs("trigger"));
55
+ }).pipe(
56
+ // Every record it reads is checked first, as its document's are (ADR 21, decision 5).
57
+ Blocks.checked("plan.queued", "plan.awaitAnswer"),
58
+ reportAs("trigger"),
59
+ );
60
+
61
+ // Nobody answered the plan in time: a person's to pick up.
62
+ const unanswered = new NeedsHuman({
63
+ reason: `nobody answered the plan within ${ANSWER_WITHIN}`,
64
+ });
51
65
 
52
66
  // Drafts, checks and proposes the plan, and waits for a person's answer,
53
67
  // round after round until they approve it. Returns the run's summary, or,
54
68
  // for a spec that needs answers first, the agent's draft with its questions.
55
- const planUntilApproved = (spec: number) =>
69
+ // A run that takes up an answer to a plan an earlier run posted (`resumed`)
70
+ // acts on it first: makes the approved plan, or drafts the next round.
71
+ const planUntilApproved = (spec: number, resumed: Option.Option<Plan.Resumed>) =>
56
72
  Effect.gen(function* () {
57
73
  let changes = Option.none<string>();
74
+ let first = 1;
75
+
76
+ if (Option.isSome(resumed)) {
77
+ const { round } = resumed.value;
78
+
79
+ // The answer as the spec's record has it now; none yet, as for a run
80
+ // started before a person answered, which waits again.
81
+ const answer = yield* Plan.answerNow(spec, round, ANSWER_WITHIN).pipe(
82
+ Effect.flatMap(
83
+ Option.match({
84
+ onSome: Effect.succeed,
85
+ onNone: () => Plan.awaitAnswer(spec, round, ANSWER_WITHIN),
86
+ }),
87
+ ),
88
+ );
89
+
90
+ if (answer === "timeout") {
91
+ return yield* unanswered;
92
+ }
93
+
94
+ if (answer === "approved") {
95
+ const plan = yield* Plan.posted(spec, round);
96
+ const created = yield* Plan.create(spec, plan);
58
97
 
59
- for (let round = 1; ; round++) {
98
+ return yield* Plan.becomeEpic(spec, plan, created);
99
+ }
100
+
101
+ changes = Option.some(yield* Plan.changesAsked(spec, round));
102
+ first = round + 1;
103
+ }
104
+
105
+ for (let round = first; ; round++) {
60
106
  const draft = yield* Plan.checkedDraft(spec, round, changes);
61
107
 
62
108
  if (draft.status === "needs-clarification") {
@@ -64,7 +110,7 @@ const planUntilApproved = (spec: number) =>
64
110
  }
65
111
 
66
112
  const plan = yield* Plan.propose(spec, round, draft);
67
- const answer = yield* Plan.awaitAnswer(round, ANSWER_WITHIN);
113
+ const answer = yield* Plan.awaitAnswer(spec, round, ANSWER_WITHIN);
68
114
 
69
115
  if (answer === "approved") {
70
116
  const created = yield* Plan.create(spec, plan);
@@ -73,9 +119,7 @@ const planUntilApproved = (spec: number) =>
73
119
  }
74
120
 
75
121
  if (answer === "timeout") {
76
- return yield* new NeedsHuman({
77
- reason: `nobody answered the plan within ${ANSWER_WITHIN}`,
78
- });
122
+ return yield* unanswered;
79
123
  }
80
124
 
81
125
  changes = Option.some(yield* Plan.changesAsked(spec, round));
@@ -84,7 +128,15 @@ const planUntilApproved = (spec: number) =>
84
128
 
85
129
  const work = (spec: number, item: WorkItem) =>
86
130
  Effect.gen(function* () {
87
- yield* Implement.claim(spec, yield* Implement.queuedBy(item));
131
+ const resumed = Plan.resuming(item);
132
+
133
+ // A run that takes up a plan an earlier run posted claims the spec again
134
+ // as that run held it, saying nothing new on it; a new spec is claimed.
135
+ if (Option.isSome(resumed)) {
136
+ yield* Resume.claimAgainOn(Claims.issue(spec));
137
+ } else {
138
+ yield* Implement.claim(spec, yield* Implement.queuedBy(item));
139
+ }
88
140
 
89
141
  // Each ending removes the plan's checkout first.
90
142
  const toPerson = (reason: string) =>
@@ -93,7 +145,7 @@ const work = (spec: number, item: WorkItem) =>
93
145
  Effect.flatMap(Implement.endForPerson),
94
146
  );
95
147
 
96
- const outcome = yield* planUntilApproved(spec).pipe(
148
+ const outcome = yield* planUntilApproved(spec, resumed).pipe(
97
149
  Effect.catchTags({
98
150
  NeedsHuman: (needsHuman) => toPerson(needsHuman.reason),
99
151
  // Any other failure once the spec is claimed hands it to a person,
@@ -119,7 +171,7 @@ const workflow = (item: WorkItem) =>
119
171
 
120
172
  // However the run ends, its claim on the spec goes; a run waiting on an
121
173
  // answer to its plan keeps it.
122
- return yield* work(spec, item).pipe(Claims.releasing(Claims.issue(spec)));
174
+ return yield* work(spec, item).pipe(StatusLabels.releasing(Claims.issue(spec)));
123
175
  }).pipe(
124
176
  // An issue deleted or transferred under the run leaves nothing to do.
125
177
  Effect.catchTag("NotFound", (gone) => Effect.fail(new Skip({ reason: gone.message }))),