@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/src/hybrid.ts CHANGED
@@ -9,29 +9,48 @@
9
9
  // reopens their session, and only a person merges, since the pull request
10
10
  // isn't on the automated path (auto:ship).
11
11
 
12
+ import {
13
+ activity,
14
+ Agent,
15
+ claimMarker,
16
+ Claims,
17
+ GitHub,
18
+ type Json,
19
+ Markers,
20
+ Repo,
21
+ reportAs,
22
+ Run,
23
+ sessionMarker,
24
+ Skip,
25
+ StepFailed,
26
+ Subjects,
27
+ type WorkItem,
28
+ } from "@chernabog/core";
12
29
  import * as DateTime from "effect/DateTime";
13
30
  import * as Effect from "effect/Effect";
14
31
  import * as Option from "effect/Option";
15
32
  import * as Schema from "effect/Schema";
16
33
 
17
- import * as Claims from "./claims.ts";
18
34
  import { HUMAN_INITIATED } from "./default-prompts.ts";
19
- import { Skip, StepFailed } from "./errors.ts";
20
- import { activity, reportAs } from "./flow.ts";
21
35
  import {
22
36
  type AuthorSession,
37
+ baseHead,
38
+ baseOf,
39
+ type BaseOptions,
23
40
  branchFor,
41
+ Claimed,
42
+ claimedBase,
43
+ dropInProgress,
24
44
  issueItem,
25
45
  latestClaim,
26
46
  legacyClaim,
47
+ markClaimed,
27
48
  PullRequestRef,
28
49
  sessionIdFor,
29
50
  } from "./implement.ts";
30
- import type { Json } from "./json.ts";
31
- import { claimMarker, sessionMarker } from "./markers.ts";
32
51
  import { renderPromptOr } from "./prompt.ts";
33
52
  import { requestReview } from "./pull-request.ts";
34
- import { Agent, GitHub, Repo, Run, type WorkItem } from "./services.ts";
53
+ import * as Settings from "./settings.ts";
35
54
 
36
55
  // Finds nothing, on whatever schedule it is given.
37
56
  /** @deprecated Leave `trigger` out instead: a flow with `item` and no trigger is manual, and runs only when a person names its work. */
@@ -67,13 +86,13 @@ export const personSession = (issue: number) =>
67
86
  } satisfies AuthorSession;
68
87
  });
69
88
 
70
- const claimComment = (claimedAt: string, branch: string, runId: string) =>
89
+ const claimComment = (claimedAt: string, branch: string, base: string, runId: string) =>
71
90
  [
72
91
  `Started by a person with Chernabog at ${claimedAt}, on branch \`${branch}\`.`,
73
92
  "",
74
93
  `- Run: \`${runId}\``,
75
94
  "",
76
- claimMarker.write({ runId, claimedAt, branch, withPerson: true, ref: true }),
95
+ claimMarker.write({ runId, claimedAt, branch, base, withPerson: true, ref: true }),
77
96
  ].join("\n");
78
97
 
79
98
  // How to let go of the claim another run holds: cancelling it, which for a run
@@ -86,27 +105,21 @@ const cancelHint = (holder: Option.Option<Claims.Holder>) =>
86
105
 
87
106
  // Claims the issue with its claim ref, then creates the issue's branch, or
88
107
  // takes up one an earlier start or an ended run left, and labels, assigns and
89
- // comments as claim does. It takes the issue out of the unattended queues. An
90
- // issue another run holds is left to it. Wrap the run in
91
- // `Claims.releasing(Claims.issue(issue))` to let the claim go when it ends.
92
- export const claim = (issue: number) =>
108
+ // comments as claim does, holding the same markers. It takes the issue out of
109
+ // the unattended queues. An issue another run holds is left to it. The branch
110
+ // starts from `base`, as Implement.claim's does, and a run that resumes keeps
111
+ // the base its claim marker records.
112
+ export const claim = (issue: number, options: BaseOptions = {}) =>
93
113
  activity(
94
114
  "claim",
95
115
  Effect.gen(function* () {
96
116
  const github = yield* GitHub;
97
- const repo = yield* Repo;
117
+ const labelNames = yield* Settings.labels;
98
118
  const run = yield* Run;
99
119
  const branch = branchFor(issue);
100
120
  const item = Claims.issue(issue);
101
- const baseSha = yield* github.branchSha(repo.baseBranch);
102
-
103
- if (Option.isNone(baseSha)) {
104
- return yield* new StepFailed({
105
- step: "claim",
106
- message: `base branch ${repo.baseBranch} not found`,
107
- });
108
- }
109
-
121
+ const base = yield* baseOf(options);
122
+ const baseSha = yield* baseHead(base);
110
123
  const claimed = yield* Claims.acquire(item, { withPerson: true });
111
124
 
112
125
  if (!claimed.acquired) {
@@ -115,11 +128,11 @@ export const claim = (issue: number) =>
115
128
  });
116
129
  }
117
130
 
118
- if ((yield* github.createBranch(branch, baseSha.value)) === "already-exists") {
131
+ if ((yield* github.createBranch(branch, baseSha)) === "already-exists") {
119
132
  const latest = yield* latestClaim(issue);
120
133
 
121
- if (Option.exists(latest, (marker) => marker.runId === run.runId)) {
122
- return { branch };
134
+ if (Option.isSome(latest) && latest.value.runId === run.runId) {
135
+ return { branch, base: yield* claimedBase(latest.value) };
123
136
  }
124
137
 
125
138
  // An earlier start that held it by its branch alone is taken over, as before claim refs.
@@ -130,27 +143,26 @@ export const claim = (issue: number) =>
130
143
  reason: `#${issue} is already claimed by an unattended run (${branch} exists)`,
131
144
  });
132
145
  }
146
+ } else {
147
+ yield* Markers.hold(Markers.claimBranch, { branch, base }, { keepOn: ["needs-human"] });
133
148
  }
134
149
 
135
- yield* github.addLabel(issue, repo.labels.inProgress);
150
+ // A person's session that fails, as one they close does, leaves the
151
+ // issue in progress for their next start.
152
+ yield* markClaimed(issue, { keepOn: ["failed"] });
136
153
 
137
- for (const queued of [repo.labels.eligible, repo.labels.ship]) {
154
+ for (const queued of [labelNames.eligible, labelNames.ship]) {
138
155
  yield* github.removeLabel(issue, queued);
139
156
  }
140
157
 
141
- if (Option.isSome(repo.board)) {
142
- yield* github.moveToStatus(issue, repo.board.value.inProgressStatus);
143
- }
144
-
145
- yield* github.assignSelf(issue);
146
158
  yield* github.comment(
147
159
  issue,
148
- claimComment(DateTime.formatIso(yield* DateTime.now), branch, run.runId),
160
+ claimComment(DateTime.formatIso(yield* DateTime.now), branch, base, run.runId),
149
161
  );
150
162
 
151
- return { branch };
163
+ return { branch, base };
152
164
  }),
153
- Schema.Struct({ branch: Schema.String }),
165
+ Claimed,
154
166
  );
155
167
 
156
168
  // What `chernabog open-pr` records when the person and the agent are done.
@@ -187,13 +199,16 @@ const RESUME_PROMPT =
187
199
 
188
200
  // The person and the agent work on the issue in a session in front of the
189
201
  // person, until the person's Yes to the agent's draft (or their own
190
- // `chernabog open-pr`) records the pull request's title, body and summary. Its prompt is .chernabog/prompts/human-initiated.md, or the SDK's own.
191
- export const workWithPerson = (issue: number, worktree: string) =>
202
+ // `chernabog open-pr`) records the pull request's title, body and summary. Its prompt is .chernabog/prompts/human-initiated.md, or the SDK's own,
203
+ // whose `base` is the given one, the repo's base branch unless given.
204
+ export const workWithPerson = (issue: number, worktree: string, options: BaseOptions = {}) =>
192
205
  activity(
193
206
  "work-with-person",
194
207
  Effect.gen(function* () {
195
208
  const github = yield* GitHub;
196
209
  const repo = yield* Repo;
210
+ const rules = yield* Settings.rules;
211
+ const localGate = yield* Settings.localGate;
197
212
  const details = yield* github.issue(issue);
198
213
  const comments = yield* github.trustedComments(issue);
199
214
 
@@ -204,10 +219,10 @@ export const workWithPerson = (issue: number, worktree: string) =>
204
219
  body: details.body,
205
220
  comments: comments.map((comment) => `${comment.author}: ${comment.body}`),
206
221
  branch: branchFor(issue),
207
- base: repo.baseBranch,
208
- localGate: repo.localGate.map((command) => `\`${command}\``),
209
- humanOnly: repo.humanOnly,
210
- notes: repo.notes,
222
+ base: yield* baseOf(options),
223
+ localGate: localGate.commands.map((command) => `\`${command}\``),
224
+ humanOnly: rules.humanOnly,
225
+ notes: rules.notes,
211
226
  });
212
227
 
213
228
  const result = yield* (yield* Agent).run({
@@ -235,14 +250,17 @@ export const workWithPerson = (issue: number, worktree: string) =>
235
250
 
236
251
  // Opens the pull request for the branch, or finds the one already open, and
237
252
  // labels it hybrid for people reading GitHub. It never carries ship, so a
238
- // person merges it.
239
- export const openPullRequest = (issue: number, finished: Finished) =>
253
+ // person merges it. It targets `base`, the repo's base branch unless given.
254
+ // The run names the pull request as a subject.
255
+ export const openPullRequest = (issue: number, finished: Finished, options: BaseOptions = {}) =>
240
256
  activity(
241
257
  "open-pull-request",
242
258
  Effect.gen(function* () {
243
259
  const github = yield* GitHub;
244
260
  const repo = yield* Repo;
261
+ const labelNames = yield* Settings.labels;
245
262
  const branch = branchFor(issue);
263
+ const base = yield* baseOf(options);
246
264
 
247
265
  const existing = Option.filter(
248
266
  yield* github.pullRequestForBranch(branch),
@@ -256,13 +274,14 @@ export const openPullRequest = (issue: number, finished: Finished) =>
256
274
  onNone: () =>
257
275
  github.openPullRequest({
258
276
  head: branch,
259
- base: repo.baseBranch,
277
+ base,
260
278
  title: finished.title,
261
279
  body: `Closes #${issue}\n\n${finished.body}\n\n---\n${footer}`,
262
280
  }),
263
281
  });
264
282
 
265
- yield* github.addLabel(opened.number, repo.labels.hybrid);
283
+ yield* Subjects.name(Claims.pullRequest(opened.number));
284
+ yield* github.addLabel(opened.number, labelNames.hybrid);
266
285
 
267
286
  return { number: opened.number, url: opened.url };
268
287
  }),
@@ -270,9 +289,8 @@ export const openPullRequest = (issue: number, finished: Finished) =>
270
289
  );
271
290
 
272
291
  // Hands the green pull request to review, as Implement.handOff does: the
273
- // session marker, so answering a review reopens the person's session, the
274
- // review trigger if the repo has one, a comment on the issue, and a review
275
- // asked of one reviewer from the pool.
292
+ // session marker, so answering a review reopens the person's session, a
293
+ // comment on the issue, and a review asked of one reviewer from the pool.
276
294
  export const handOff = (issue: number, pullRequest: PullRequestRef, finished: Finished) =>
277
295
  activity(
278
296
  "hand-off",
@@ -280,10 +298,6 @@ export const handOff = (issue: number, pullRequest: PullRequestRef, finished: Fi
280
298
  const github = yield* GitHub;
281
299
  const repo = yield* Repo;
282
300
 
283
- if (Option.isSome(repo.reviewTrigger)) {
284
- yield* github.comment(pullRequest.number, repo.reviewTrigger.value);
285
- }
286
-
287
301
  const session = {
288
302
  adapter: repo.agent,
289
303
  sessionId: yield* sessionIdOf(issue),
@@ -296,7 +310,7 @@ export const handOff = (issue: number, pullRequest: PullRequestRef, finished: Fi
296
310
  pullRequest.number,
297
311
  `Worked on by a person with ${repo.agent} for #${issue}. ${sessionMarker.write(session)}`,
298
312
  );
299
- yield* github.removeLabel(issue, repo.labels.inProgress);
313
+ yield* dropInProgress(issue);
300
314
  yield* github.comment(
301
315
  issue,
302
316
  `Pull request ready for review: ${pullRequest.url}\n\n${finished.summary}`,