@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/claims.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
// Claims on work items, each a ref of its own, apart from the work branch:
|
|
2
|
-
// `refs/chernabog/claims/<item>` (`issue-12`, `pr-73`), pointing at a commit
|
|
3
|
-
// with an empty tree whose message says whose run holds it. Creating a ref is
|
|
4
|
-
// the one write GitHub refuses to a second writer, so whoever creates it
|
|
5
|
-
// holds the item. Releasing it deletes only the ref; the work branch and any
|
|
6
|
-
// pull request stay. Labels, assignees and the claim comment are how a person
|
|
7
|
-
// sees a claim, written after it is won.
|
|
8
|
-
|
|
9
|
-
import * as Cause from "effect/Cause";
|
|
10
|
-
import * as DateTime from "effect/DateTime";
|
|
11
|
-
import * as Effect from "effect/Effect";
|
|
12
|
-
import * as Exit from "effect/Exit";
|
|
13
|
-
import * as Option from "effect/Option";
|
|
14
|
-
|
|
15
|
-
import { activity } from "./flow.ts";
|
|
16
|
-
import { GitHub, Run } from "./services.ts";
|
|
17
|
-
|
|
18
|
-
// Git's empty tree, which GitHub has in every repo.
|
|
19
|
-
const EMPTY_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
|
|
20
|
-
|
|
21
|
-
const TITLE = "chernabog claim";
|
|
22
|
-
|
|
23
|
-
// The claim on an issue, and on a pull request.
|
|
24
|
-
export const issue = (number: number) => `issue-${number}`;
|
|
25
|
-
export const pullRequest = (number: number) => `pr-${number}`;
|
|
26
|
-
|
|
27
|
-
export const refFor = (item: string) => `refs/chernabog/claims/${item}`;
|
|
28
|
-
|
|
29
|
-
// Whose run holds a claim. The commit says nothing about the machine: it is
|
|
30
|
-
// as public as the repo.
|
|
31
|
-
export type Holder = {
|
|
32
|
-
readonly runId: string;
|
|
33
|
-
readonly persona: string;
|
|
34
|
-
readonly flow: string;
|
|
35
|
-
readonly claimedAt: string;
|
|
36
|
-
readonly withPerson: boolean;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export const messageOf = (holder: Holder) =>
|
|
40
|
-
[
|
|
41
|
-
TITLE,
|
|
42
|
-
"",
|
|
43
|
-
`Run: ${holder.runId}`,
|
|
44
|
-
`Persona: ${holder.persona}`,
|
|
45
|
-
`Flow: ${holder.flow}`,
|
|
46
|
-
`Claimed-At: ${holder.claimedAt}`,
|
|
47
|
-
...Option.toArray(Option.liftPredicate("With-Person: true", () => holder.withPerson)),
|
|
48
|
-
].join("\n");
|
|
49
|
-
|
|
50
|
-
const TRAILER = /^([A-Za-z-]+): (.*)$/;
|
|
51
|
-
|
|
52
|
-
// The holder a claim commit's message names; none for a message that isn't a claim's.
|
|
53
|
-
export const holderOf = (message: string): Option.Option<Holder> => {
|
|
54
|
-
const lines = message.split("\n");
|
|
55
|
-
|
|
56
|
-
if (lines[0]?.trim() !== TITLE) {
|
|
57
|
-
return Option.none();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const trailers = new Map(
|
|
61
|
-
lines.flatMap((line) =>
|
|
62
|
-
Option.toArray(
|
|
63
|
-
Option.map(
|
|
64
|
-
Option.fromNullishOr(TRAILER.exec(line.trim())),
|
|
65
|
-
(match) => [match[1], match[2]] as const,
|
|
66
|
-
),
|
|
67
|
-
),
|
|
68
|
-
),
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
return Option.map(Option.fromUndefinedOr(trailers.get("Run")), (runId) => ({
|
|
72
|
-
runId,
|
|
73
|
-
persona: trailers.get("Persona") ?? "",
|
|
74
|
-
flow: trailers.get("Flow") ?? "",
|
|
75
|
-
claimedAt: trailers.get("Claimed-At") ?? "",
|
|
76
|
-
withPerson: trailers.get("With-Person") === "true",
|
|
77
|
-
}));
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
// Who holds the claim on `item`: none when nobody does.
|
|
81
|
-
export const holder = (item: string) =>
|
|
82
|
-
GitHub.use((github) => github.ref(refFor(item))).pipe(
|
|
83
|
-
Effect.map(Option.flatMap((commit) => holderOf(commit.message))),
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
export type AcquireOptions = {
|
|
87
|
-
// A person works on the item in the run, on the hybrid path.
|
|
88
|
-
readonly withPerson?: boolean;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
export type Acquired =
|
|
92
|
-
| { readonly acquired: true }
|
|
93
|
-
| { readonly acquired: false; readonly holder: Option.Option<Holder> };
|
|
94
|
-
|
|
95
|
-
// Claims `item` for this run, or says who holds it. Safe to repeat: a run that
|
|
96
|
-
// already holds the claim keeps it.
|
|
97
|
-
export const acquire = (item: string, options: AcquireOptions = {}) =>
|
|
98
|
-
Effect.gen(function* () {
|
|
99
|
-
const github = yield* GitHub;
|
|
100
|
-
const run = yield* Run;
|
|
101
|
-
|
|
102
|
-
const sha = yield* github.createCommit({
|
|
103
|
-
message: messageOf({
|
|
104
|
-
runId: run.runId,
|
|
105
|
-
persona: yield* github.login,
|
|
106
|
-
flow: run.flow,
|
|
107
|
-
claimedAt: DateTime.formatIso(yield* DateTime.now),
|
|
108
|
-
withPerson: options.withPerson === true,
|
|
109
|
-
}),
|
|
110
|
-
tree: EMPTY_TREE,
|
|
111
|
-
parents: [],
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
if ((yield* github.createRef(refFor(item), sha)) === "created") {
|
|
115
|
-
return { acquired: true } satisfies Acquired;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const held = yield* holder(item);
|
|
119
|
-
|
|
120
|
-
if (Option.exists(held, (found) => found.runId === run.runId)) {
|
|
121
|
-
return { acquired: true } satisfies Acquired;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return { acquired: false, holder: held } satisfies Acquired;
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
// Lets go of the claim on `item` if `runId` holds it, and says whether it did.
|
|
128
|
-
// A claim another run holds is left to it.
|
|
129
|
-
export const releaseFor = (item: string, runId: string) =>
|
|
130
|
-
Effect.gen(function* () {
|
|
131
|
-
const held = yield* holder(item);
|
|
132
|
-
|
|
133
|
-
if (!Option.exists(held, (found) => found.runId === runId)) {
|
|
134
|
-
return false;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
yield* (yield* GitHub).deleteRef(refFor(item));
|
|
138
|
-
|
|
139
|
-
return true;
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
// Lets go of this run's claim on `item`.
|
|
143
|
-
export const release = (item: string) => Run.use((run) => releaseFor(item, run.runId));
|
|
144
|
-
|
|
145
|
-
// Holds the claim on `item` for as long as `effect` runs, and releases it
|
|
146
|
-
// however that ends, as the step `release-claim-<item>`. A run waiting on a
|
|
147
|
-
// signal keeps it.
|
|
148
|
-
export const releasing =
|
|
149
|
-
(item: string) =>
|
|
150
|
-
<A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
151
|
-
effect.pipe(
|
|
152
|
-
// A wait suspends the run by interrupting it; that is not an ending.
|
|
153
|
-
Effect.onExit((exit) => {
|
|
154
|
-
if (Exit.isFailure(exit) && Cause.hasInterrupts(exit.cause)) {
|
|
155
|
-
return Effect.void;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return activity(`release-claim-${item}`, release(item).pipe(Effect.asVoid)).pipe(
|
|
159
|
-
Effect.ignore,
|
|
160
|
-
);
|
|
161
|
-
}),
|
|
162
|
-
);
|
|
163
|
-
|
|
164
|
-
// A note for a skip: who holds the claim, as far as its ref says.
|
|
165
|
-
export const describeHolder = (item: string, held: Option.Option<Holder>) =>
|
|
166
|
-
Option.match(held, {
|
|
167
|
-
onNone: () => `${refFor(item)} exists`,
|
|
168
|
-
onSome: (found) => `run ${found.runId} of ${found.flow} holds ${refFor(item)}`,
|
|
169
|
-
});
|
package/src/errors.ts
DELETED
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
// What can go wrong, split by who acts on it. A flow ends in one of its own
|
|
2
|
-
// outcomes (Skip, NeedsHuman, StepFailed). The work's errors are typed at the
|
|
3
|
-
// call that raises them, and a flow must handle each one before it compiles.
|
|
4
|
-
// The errors a flow can only report (GitHubError and the like) become
|
|
5
|
-
// StepFailed. Setup errors are raised while a persona's services are built,
|
|
6
|
-
// before any flow runs, and the engine reports them; no flow sees one.
|
|
7
|
-
|
|
8
|
-
import * as Option from "effect/Option";
|
|
9
|
-
import * as Schema from "effect/Schema";
|
|
10
|
-
|
|
11
|
-
// Ends a run cleanly: nothing to do, or a gate said not now. Never a failure.
|
|
12
|
-
export class Skip extends Schema.TaggedError<Skip, { readonly brand: "Skip" }>()("Skip", {
|
|
13
|
-
reason: Schema.String,
|
|
14
|
-
}) {}
|
|
15
|
-
|
|
16
|
-
// Hands a work item to a person: the flow labels it and no flow picks it up again.
|
|
17
|
-
export class NeedsHuman extends Schema.TaggedError<NeedsHuman, { readonly brand: "NeedsHuman" }>()(
|
|
18
|
-
"NeedsHuman",
|
|
19
|
-
{
|
|
20
|
-
reason: Schema.String,
|
|
21
|
-
},
|
|
22
|
-
) {}
|
|
23
|
-
|
|
24
|
-
// A step or activity that failed for any other reason, named so the run log says which.
|
|
25
|
-
export class StepFailed extends Schema.TaggedError<StepFailed, { readonly brand: "StepFailed" }>()(
|
|
26
|
-
"StepFailed",
|
|
27
|
-
{
|
|
28
|
-
step: Schema.String,
|
|
29
|
-
message: Schema.String,
|
|
30
|
-
},
|
|
31
|
-
) {}
|
|
32
|
-
|
|
33
|
-
// The errors a flow can only report, each with a message that says what failed.
|
|
34
|
-
// An activity turns them into StepFailed.
|
|
35
|
-
|
|
36
|
-
export class GitHubError extends Schema.TaggedError<
|
|
37
|
-
GitHubError,
|
|
38
|
-
{ readonly brand: "GitHubError" }
|
|
39
|
-
>()("GitHubError", {
|
|
40
|
-
message: Schema.String,
|
|
41
|
-
}) {}
|
|
42
|
-
|
|
43
|
-
export class GitError extends Schema.TaggedError<GitError, { readonly brand: "GitError" }>()(
|
|
44
|
-
"GitError",
|
|
45
|
-
{
|
|
46
|
-
message: Schema.String,
|
|
47
|
-
},
|
|
48
|
-
) {}
|
|
49
|
-
|
|
50
|
-
export class AgentError extends Schema.TaggedError<AgentError, { readonly brand: "AgentError" }>()(
|
|
51
|
-
"AgentError",
|
|
52
|
-
{
|
|
53
|
-
message: Schema.String,
|
|
54
|
-
},
|
|
55
|
-
) {}
|
|
56
|
-
|
|
57
|
-
export class UsageError extends Schema.TaggedError<UsageError, { readonly brand: "UsageError" }>()(
|
|
58
|
-
"UsageError",
|
|
59
|
-
{
|
|
60
|
-
message: Schema.String,
|
|
61
|
-
},
|
|
62
|
-
) {}
|
|
63
|
-
|
|
64
|
-
export type ReportedError = GitHubError | GitError | AgentError | UsageError;
|
|
65
|
-
|
|
66
|
-
// The work's errors: each one a flow can decide on.
|
|
67
|
-
|
|
68
|
-
// An issue or pull request GitHub doesn't have, such as one deleted or transferred.
|
|
69
|
-
export class NotFound extends Schema.TaggedError<NotFound, { readonly brand: "NotFound" }>()(
|
|
70
|
-
"NotFound",
|
|
71
|
-
{
|
|
72
|
-
what: Schema.String,
|
|
73
|
-
},
|
|
74
|
-
) {
|
|
75
|
-
override get message() {
|
|
76
|
-
return `${this.what} not found`;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// A pull request closed or merged before the call that needed it open.
|
|
81
|
-
export class PullRequestClosed extends Schema.TaggedError<
|
|
82
|
-
PullRequestClosed,
|
|
83
|
-
{ readonly brand: "PullRequestClosed" }
|
|
84
|
-
>()("PullRequestClosed", {
|
|
85
|
-
pullRequest: Schema.Finite,
|
|
86
|
-
state: Schema.Literals(["closed", "merged"]),
|
|
87
|
-
}) {
|
|
88
|
-
override get message() {
|
|
89
|
-
return `#${this.pullRequest} is ${this.state}`;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// A merge that isn't safe, or that GitHub refused, with why.
|
|
94
|
-
export class MergeRefused extends Schema.TaggedError<
|
|
95
|
-
MergeRefused,
|
|
96
|
-
{ readonly brand: "MergeRefused" }
|
|
97
|
-
>()("MergeRefused", {
|
|
98
|
-
pullRequest: Schema.Finite,
|
|
99
|
-
reason: Schema.String,
|
|
100
|
-
}) {
|
|
101
|
-
override get message() {
|
|
102
|
-
return `#${this.pullRequest} not merged: ${this.reason}`;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// A push the remote refused: a branch that moved since, or a token without a
|
|
107
|
-
// scope the change needs, such as `workflow` for a change to .github/workflows/.
|
|
108
|
-
export class PushRejected extends Schema.TaggedError<
|
|
109
|
-
PushRejected,
|
|
110
|
-
{ readonly brand: "PushRejected" }
|
|
111
|
-
>()("PushRejected", {
|
|
112
|
-
branch: Schema.String,
|
|
113
|
-
reason: Schema.String,
|
|
114
|
-
missingScope: Schema.optionalKey(Schema.String),
|
|
115
|
-
}) {
|
|
116
|
-
override get message() {
|
|
117
|
-
const rejected = `pushing ${this.branch} was rejected: ${this.reason}`;
|
|
118
|
-
|
|
119
|
-
return Option.match(Option.fromUndefinedOr(this.missingScope), {
|
|
120
|
-
onNone: () => rejected,
|
|
121
|
-
onSome: (scope) =>
|
|
122
|
-
`${rejected}. Fix: give the token it pushes with the \`${scope}\` scope (\`gh auth refresh --hostname github.com --scopes ${scope}\`, logged in as that account), then push ${this.branch} again`,
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// The branch has nothing to push: the agent finished without committing.
|
|
128
|
-
export class NothingCommitted extends Schema.TaggedError<
|
|
129
|
-
NothingCommitted,
|
|
130
|
-
{ readonly brand: "NothingCommitted" }
|
|
131
|
-
>()("NothingCommitted", {
|
|
132
|
-
branch: Schema.String,
|
|
133
|
-
}) {
|
|
134
|
-
override get message() {
|
|
135
|
-
return `nothing was committed on ${this.branch}`;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// CI stayed pending for longer than limits.ciTimeout.
|
|
140
|
-
export class CiTimedOut extends Schema.TaggedError<CiTimedOut, { readonly brand: "CiTimedOut" }>()(
|
|
141
|
-
"CiTimedOut",
|
|
142
|
-
{
|
|
143
|
-
pullRequest: Schema.Finite,
|
|
144
|
-
after: Schema.String,
|
|
145
|
-
},
|
|
146
|
-
) {
|
|
147
|
-
override get message() {
|
|
148
|
-
return `CI on #${this.pullRequest} is still pending after ${this.after}`;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export const WorkError = Schema.Union([
|
|
153
|
-
NotFound,
|
|
154
|
-
PullRequestClosed,
|
|
155
|
-
MergeRefused,
|
|
156
|
-
PushRejected,
|
|
157
|
-
NothingCommitted,
|
|
158
|
-
CiTimedOut,
|
|
159
|
-
]);
|
|
160
|
-
|
|
161
|
-
export type WorkError = typeof WorkError.Type;
|
|
162
|
-
|
|
163
|
-
// Setup errors: what a person fixes in their configuration or logins. Each
|
|
164
|
-
// names its fix.
|
|
165
|
-
|
|
166
|
-
// A persona under github: whose token gh can't give: not logged in, or logged out since.
|
|
167
|
-
export class PersonaUnavailable extends Schema.TaggedError<
|
|
168
|
-
PersonaUnavailable,
|
|
169
|
-
{ readonly brand: "PersonaUnavailable" }
|
|
170
|
-
>()("PersonaUnavailable", {
|
|
171
|
-
login: Schema.String,
|
|
172
|
-
reason: Schema.String,
|
|
173
|
-
}) {
|
|
174
|
-
override get message() {
|
|
175
|
-
return `${this.login} is unavailable: ${this.reason}. Fix: run \`gh auth login --hostname github.com\` as ${this.login}, or take it out of github: in machine config`;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// No token for the implicit account: no GH_TOKEN or GITHUB_TOKEN, and gh has no login.
|
|
180
|
-
export class NotAuthenticated extends Schema.TaggedError<
|
|
181
|
-
NotAuthenticated,
|
|
182
|
-
{ readonly brand: "NotAuthenticated" }
|
|
183
|
-
>()("NotAuthenticated", {
|
|
184
|
-
reason: Schema.String,
|
|
185
|
-
}) {
|
|
186
|
-
override get message() {
|
|
187
|
-
return `not authenticated to GitHub: ${this.reason}. Fix: set GH_TOKEN, run \`gh auth login\`, or name a persona under github: in machine config`;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// A flow that reviews runs as a persona that has no seat in the reviewer
|
|
192
|
-
// pool: the implicit account, or a login the repo's reviewers leave out.
|
|
193
|
-
export class NotInReviewerPool extends Schema.TaggedError<
|
|
194
|
-
NotInReviewerPool,
|
|
195
|
-
{ readonly brand: "NotInReviewerPool" }
|
|
196
|
-
>()("NotInReviewerPool", {
|
|
197
|
-
login: Schema.optionalKey(Schema.String),
|
|
198
|
-
reviewers: Schema.Array(Schema.String),
|
|
199
|
-
}) {
|
|
200
|
-
override get message() {
|
|
201
|
-
const pool = Option.match(
|
|
202
|
-
Option.liftPredicate(this.reviewers, (logins) => logins.length > 0),
|
|
203
|
-
{
|
|
204
|
-
onNone: () => "the repo names no reviewers",
|
|
205
|
-
onSome: (logins) => `reviewers: ${logins.join(", ")}`,
|
|
206
|
-
},
|
|
207
|
-
);
|
|
208
|
-
|
|
209
|
-
return Option.match(Option.fromUndefinedOr(this.login), {
|
|
210
|
-
onNone: () =>
|
|
211
|
-
`a flow that reviews runs as the implicit account, which has no seat in the reviewer pool (${pool}). Fix: run it as a persona under github: that the repo's reviewers list`,
|
|
212
|
-
onSome: (login) =>
|
|
213
|
-
`${login} has no seat in the reviewer pool (${pool}). Fix: add ${login} to reviewers in .chernabog/config.yaml, or turn the flow off for ${login}`,
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
export const SetupError = Schema.Union([PersonaUnavailable, NotAuthenticated, NotInReviewerPool]);
|
|
219
|
-
|
|
220
|
-
export type SetupError = typeof SetupError.Type;
|
|
221
|
-
|
|
222
|
-
export const isSetupError = Schema.is(SetupError);
|