@ygrishajev/jig 1.0.5 → 1.2.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/README.md +16 -0
- package/dist/adapters/git/remote-ref.js +125 -0
- package/dist/adapters/git/run-code.js +66 -0
- package/dist/adapters/git/run-history.js +79 -0
- package/dist/adapters/local-store/run-artefacts.js +2 -1
- package/dist/adapters/run-store/composite-run-store.js +68 -14
- package/dist/adapters/run-store/git-remote-run-store.js +34 -129
- package/dist/app/run-jig.js +28 -7
- package/dist/app/run-stores.js +4 -3
- package/dist/cli/commands/run.js +20 -7
- package/dist/cli/commands/status.js +3 -0
- package/dist/cli/main.js +3 -15
- package/dist/domain/apply-outcome.js +30 -13
- package/dist/domain/diagnostics.js +104 -0
- package/dist/domain/errors.js +8 -0
- package/dist/domain/final-check.js +21 -1
- package/dist/domain/run-id.js +4 -1
- package/dist/domain/run-state.js +51 -2
- package/dist/domain/slice.js +8 -2
- package/dist/flow/engine.js +4 -0
- package/dist/flow/run-flow.js +6 -2
- package/dist/integrations/github/cli.js +20 -0
- package/dist/integrations/github/command.js +68 -0
- package/dist/integrations/github/errors.js +19 -0
- package/dist/integrations/github/findings-comment.js +53 -0
- package/dist/integrations/github/github-port.js +1 -0
- package/dist/integrations/github/implement-issue.js +281 -0
- package/dist/integrations/github/octokit-github.js +101 -0
- package/dist/integrations/github/orchestration-state.js +132 -0
- package/dist/integrations/github/orchestration-store.js +109 -0
- package/dist/integrations/github/repository-identity.js +27 -0
- package/dist/integrations/github/request-snapshot.js +3 -0
- package/dist/integrations/github/run-branch-publication.js +102 -0
- package/dist/ports/run-code.js +1 -0
- package/dist/prompts/design-review.js +2 -0
- package/dist/prompts/planning.js +5 -1
- package/dist/prompts/pull-request.js +4 -1
- package/dist/prompts/slice.js +11 -0
- package/dist/steps/pull-request-description.js +1 -1
- package/dist/steps/slice.js +1 -0
- package/package.json +5 -4
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { panic, Result } from 'better-result';
|
|
2
|
+
import { getCurrentBranch } from '../../adapters/git/branch.js';
|
|
3
|
+
import { exactHead } from '../../adapters/git/run-history.js';
|
|
4
|
+
import { runJig } from '../../app/run-jig.js';
|
|
5
|
+
import { openRunStore } from '../../app/run-stores.js';
|
|
6
|
+
import { runIdFor } from '../../domain/run-id.js';
|
|
7
|
+
import { runBranchFor } from '../../domain/run-state.js';
|
|
8
|
+
import { outcomeIsComplete } from '../../flow/engine.js';
|
|
9
|
+
import { BaseBranchMismatch, GithubRefused, } from './errors.js';
|
|
10
|
+
import { findingsCommentBody, stuckReason } from './findings-comment.js';
|
|
11
|
+
import { acceptedRequest, acceptRequest, commentMarker, commentReceipt, findingsCommentReceipt, findingsMarker, pullRequestReceipt, refusalCommentReceipt, refusalMarker, refusalOf, withComment, withFindingsComment, withPullRequest, withRefusal, withRefusalComment, } from './orchestration-state.js';
|
|
12
|
+
import { OrchestrationStore } from './orchestration-store.js';
|
|
13
|
+
import { requestText } from './request-snapshot.js';
|
|
14
|
+
import { hasWorkBeyondStart, inspectRunBranch, publishRunBranch, } from './run-branch-publication.js';
|
|
15
|
+
export function implementIssue(app, github, repository, issueNumber) {
|
|
16
|
+
return Result.gen(async function* () {
|
|
17
|
+
app.cradle.logger.info({ event: 'githubIssuePreparationStarted', issueNumber, repository }, `Checking GitHub issue #${issueNumber} in ${repository.owner}/${repository.repo}`);
|
|
18
|
+
const base = yield* Result.await(github.getDefaultBranch(repository));
|
|
19
|
+
const current = yield* Result.await(getCurrentBranch(app.cradle.repoRoot));
|
|
20
|
+
if (current !== base)
|
|
21
|
+
return Result.err(new BaseBranchMismatch({
|
|
22
|
+
message: `Check out the default branch "${base}" before implementing an issue (currently "${current}").`,
|
|
23
|
+
}));
|
|
24
|
+
const store = new OrchestrationStore(app.cradle.repoRoot);
|
|
25
|
+
const key = `${repository.owner}/${repository.repo}#${issueNumber}`;
|
|
26
|
+
app.cradle.logger.info({ event: 'githubIssueRequestStarted', issueNumber }, `Loading accepted request or fetching GitHub issue #${issueNumber}`);
|
|
27
|
+
const accepted = yield* Result.await(acceptedRequestFor(store, github, repository, key, issueNumber));
|
|
28
|
+
app.cradle.logger.info({ event: 'githubIssueRequestAccepted', issueNumber, runId: accepted.runId }, `GitHub issue #${issueNumber} is ready as run ${accepted.runId}`);
|
|
29
|
+
const report = yield* Result.await(runJig(app, {
|
|
30
|
+
start: { text: accepted.text, merge: false, codePublication: 'deferred' },
|
|
31
|
+
runId: accepted.runId,
|
|
32
|
+
requiredStores: REQUIRED_STORES,
|
|
33
|
+
}));
|
|
34
|
+
app.cradle.logger.info({
|
|
35
|
+
event: 'githubIssueRunFinished',
|
|
36
|
+
issueNumber,
|
|
37
|
+
runId: accepted.runId,
|
|
38
|
+
status: report.status,
|
|
39
|
+
}, `Run ${accepted.runId} finished ${report.status}; publishing the GitHub outcome`);
|
|
40
|
+
const target = { github, repository, issueNumber, key, base, title: accepted.title };
|
|
41
|
+
return store.withPublicationOwnership(key, app.cradle.logger, (whileOwned) => publishOutcome(app, store, { ...target, whileOwned }, report));
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function acceptedRequestFor(store, github, repository, key, issueNumber) {
|
|
45
|
+
return Result.gen(async function* () {
|
|
46
|
+
const existing = yield* Result.await(store.read());
|
|
47
|
+
const accepted = acceptedRequest(existing, key);
|
|
48
|
+
if (accepted)
|
|
49
|
+
return Result.ok(accepted);
|
|
50
|
+
const issue = yield* Result.await(github.getIssue(repository, issueNumber));
|
|
51
|
+
const state = yield* Result.await(store.update((state) => acceptRequest(state, key, {
|
|
52
|
+
runId: runIdFor({ name: issue.title, key }),
|
|
53
|
+
title: issue.title,
|
|
54
|
+
text: requestText(issue),
|
|
55
|
+
})));
|
|
56
|
+
const request = acceptedRequest(state, key);
|
|
57
|
+
if (!request)
|
|
58
|
+
panic('Accepting a request must preserve its snapshot');
|
|
59
|
+
return Result.ok(request);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/** Issue runs always keep their state on the remote, so a retried job on another clone resumes it. */
|
|
63
|
+
const REQUIRED_STORES = ['git-remote'];
|
|
64
|
+
/**
|
|
65
|
+
* Publishes what the run left: a ready PR for a completed run with approved changes, a draft PR
|
|
66
|
+
* with a findings comment for a stuck run that committed work, a refusal for one that changes a
|
|
67
|
+
* workflow, and a status comment otherwise. Every run with code is inspected before any receipt is
|
|
68
|
+
* reused, and its code reaches the remote only through `publishRunBranch`.
|
|
69
|
+
*/
|
|
70
|
+
function publishOutcome(app, store, target, report) {
|
|
71
|
+
return Result.gen(async function* () {
|
|
72
|
+
const { repoRoot } = app.cradle;
|
|
73
|
+
const checkpoint = yield* Result.await(codeCheckpointOf(app, report));
|
|
74
|
+
const publication = yield* Result.await(publicationFor(repoRoot, report, checkpoint));
|
|
75
|
+
if (publication.kind === 'comment')
|
|
76
|
+
return publishStatus(repoRoot, store, target, { report, revision: publication.revision });
|
|
77
|
+
const { revision } = publication;
|
|
78
|
+
const pushed = yield* Result.await(target.whileOwned(() => publishRunBranch(repoRoot, {
|
|
79
|
+
runId: report.state.runId,
|
|
80
|
+
revision,
|
|
81
|
+
defaultBranch: target.base,
|
|
82
|
+
pushUnchanged: publication.kind === 'draft',
|
|
83
|
+
})));
|
|
84
|
+
if (pushed.kind === 'refused')
|
|
85
|
+
return refuse(store, target, { runId: report.state.runId, revision, paths: pushed.paths });
|
|
86
|
+
if (pushed.kind === 'unchanged')
|
|
87
|
+
return publishComment(store, target, { report, revision });
|
|
88
|
+
const pullRequest = pullRequestFor(report, target, publication.kind === 'draft');
|
|
89
|
+
if (publication.kind === 'ready')
|
|
90
|
+
return publishReady(store, target, pullRequest);
|
|
91
|
+
return publishDraft(store, target, {
|
|
92
|
+
report,
|
|
93
|
+
revision,
|
|
94
|
+
pullRequest,
|
|
95
|
+
changesFiles: pushed.changesFiles,
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The code checkpoint the run's saved state records, which the run branch must still hold. A run
|
|
101
|
+
* saved before checkpoints were recorded has none, so its branch here is what gets inspected, and
|
|
102
|
+
* it has no starting commit to tell its work by.
|
|
103
|
+
*/
|
|
104
|
+
function codeCheckpointOf(app, report) {
|
|
105
|
+
return Result.gen(async function* () {
|
|
106
|
+
const { runId } = report.state;
|
|
107
|
+
const store = yield* Result.await(openRunStore(app.cradle, REQUIRED_STORES));
|
|
108
|
+
const saved = yield* Result.await(store.peek(runId));
|
|
109
|
+
if (saved?.codeRevision)
|
|
110
|
+
return Result.ok({ startingCommit: saved.startingCommit, codeRevision: saved.codeRevision });
|
|
111
|
+
const head = yield* Result.await(exactHead(app.cradle.repoRoot, `refs/heads/${runBranchFor(runId)}`));
|
|
112
|
+
return Result.ok({ codeRevision: head });
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* A completed run with code is offered as a ready PR and a stuck run with commits past where it
|
|
117
|
+
* started as a draft; every other run, and one with no code, is reported in an issue comment.
|
|
118
|
+
*/
|
|
119
|
+
function publicationFor(repoRoot, report, checkpoint) {
|
|
120
|
+
return Result.gen(async function* () {
|
|
121
|
+
const revision = checkpoint.codeRevision;
|
|
122
|
+
if (!revision)
|
|
123
|
+
return Result.ok({ kind: 'comment' });
|
|
124
|
+
if (outcomeIsComplete(report.status))
|
|
125
|
+
return Result.ok({ kind: 'ready', revision });
|
|
126
|
+
if (report.status !== 'stuck')
|
|
127
|
+
return Result.ok({ kind: 'comment', revision });
|
|
128
|
+
const committed = yield* Result.await(hasWorkBeyondStart(repoRoot, checkpoint));
|
|
129
|
+
return Result.ok({ kind: committed ? 'draft' : 'comment', revision });
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
function publishStatus(repoRoot, store, target, { report, revision }) {
|
|
133
|
+
return Result.gen(async function* () {
|
|
134
|
+
if (!revision)
|
|
135
|
+
return publishComment(store, target, { report });
|
|
136
|
+
const { runId } = report.state;
|
|
137
|
+
const inspection = yield* Result.await(inspectRunBranch(repoRoot, { runId, revision, defaultBranch: target.base }));
|
|
138
|
+
if (inspection.kind === 'refused')
|
|
139
|
+
return refuse(store, target, { runId, revision, paths: inspection.paths });
|
|
140
|
+
return publishComment(store, target, { report, revision });
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
function publishComment(store, target, { report, revision }) {
|
|
144
|
+
return Result.gen(async function* () {
|
|
145
|
+
const identity = {
|
|
146
|
+
runId: report.state.runId,
|
|
147
|
+
outcome: report.status,
|
|
148
|
+
revision: revision ?? NO_CODE_REVISION,
|
|
149
|
+
};
|
|
150
|
+
const state = yield* Result.await(store.read());
|
|
151
|
+
const url = yield* Result.await(commentOnce(target, target.issueNumber, {
|
|
152
|
+
receipt: commentReceipt(state, identity),
|
|
153
|
+
marker: commentMarker(identity),
|
|
154
|
+
body: statusCommentBody(report),
|
|
155
|
+
save: (url) => store.update((state) => withComment(state, identity, url)),
|
|
156
|
+
}));
|
|
157
|
+
return Result.ok({ kind: 'commented', outcome: report.status, url });
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/** Stands in for the revision of a run that has no code yet, so its comment is still keyed. */
|
|
161
|
+
const NO_CODE_REVISION = 'none';
|
|
162
|
+
/**
|
|
163
|
+
* Posts a comment on issue or PR `number` at most once: a saved receipt is reused, and a comment
|
|
164
|
+
* posted before its receipt was saved is found again by its marker instead of being posted twice.
|
|
165
|
+
*/
|
|
166
|
+
function commentOnce({ github, repository, whileOwned }, number, comment) {
|
|
167
|
+
return Result.gen(async function* () {
|
|
168
|
+
if (comment.receipt)
|
|
169
|
+
return Result.ok(comment.receipt);
|
|
170
|
+
const found = yield* Result.await(github.findIssueComment(repository, number, comment.marker));
|
|
171
|
+
const url = found ??
|
|
172
|
+
(yield* Result.await(whileOwned(() => github.commentOnIssue(repository, number, `${comment.body}\n\n${comment.marker}`))));
|
|
173
|
+
yield* Result.await(comment.save(url));
|
|
174
|
+
return Result.ok(url);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
function statusCommentBody(report) {
|
|
178
|
+
return `Run ${report.state.runId}: ${report.status}. ${statusCommentReason(report)}`;
|
|
179
|
+
}
|
|
180
|
+
function statusCommentReason(report) {
|
|
181
|
+
switch (report.status) {
|
|
182
|
+
case 'blocked':
|
|
183
|
+
return report.reason;
|
|
184
|
+
case 'halted':
|
|
185
|
+
return 'Paused at the design checkpoint.';
|
|
186
|
+
case 'stuck':
|
|
187
|
+
return stuckReason(report.state);
|
|
188
|
+
case 'completed':
|
|
189
|
+
case 'done':
|
|
190
|
+
return 'No changes were committed.';
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/** Records the refusal before announcing it, so a crash in between still never publishes the code. */
|
|
194
|
+
function refuse(store, target, refusal) {
|
|
195
|
+
return Result.gen(async function* () {
|
|
196
|
+
const { key } = target;
|
|
197
|
+
const state = yield* Result.await(store.update((state) => withRefusal(state, key, refusal)));
|
|
198
|
+
const recorded = refusalOf(state, key, refusal.revision) ?? refusal;
|
|
199
|
+
const url = yield* Result.await(commentOnce(target, target.issueNumber, {
|
|
200
|
+
receipt: refusalCommentReceipt(state, key, refusal.revision),
|
|
201
|
+
marker: refusalMarker(key, refusal.revision),
|
|
202
|
+
body: refusalCommentBody(recorded),
|
|
203
|
+
save: (url) => store.update((state) => withRefusalComment(state, key, refusal.revision, url)),
|
|
204
|
+
}));
|
|
205
|
+
return Result.ok({ kind: 'refused', paths: recorded.paths, url });
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
function refusalCommentBody({ runId, revision, paths }) {
|
|
209
|
+
const listed = paths.map((path) => `- \`${path}\``).join('\n');
|
|
210
|
+
return `Run ${runId} was not published: revision ${revision} changes GitHub workflows, which jig does not push for review.\n\n${listed}`;
|
|
211
|
+
}
|
|
212
|
+
function pullRequestFor(report, { title, issueNumber }, draft) {
|
|
213
|
+
return {
|
|
214
|
+
head: runBranchFor(report.state.runId),
|
|
215
|
+
base: report.state.baseBranch,
|
|
216
|
+
title,
|
|
217
|
+
body: `${report.state.prDescription ?? ''}\n\nCloses #${issueNumber}`,
|
|
218
|
+
draft,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
function publishReady(store, target, publication) {
|
|
222
|
+
return Result.gen(async function* () {
|
|
223
|
+
const { receipt, reused } = yield* Result.await(openPullRequestOnce(store, target, publication));
|
|
224
|
+
return Result.ok({ kind: 'pullRequest', url: receipt.url, reused });
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
/** Reuses the issue's recorded PR, or one open for the same head, before opening a new one. */
|
|
228
|
+
function openPullRequestOnce(store, { github, repository, key, whileOwned }, publication) {
|
|
229
|
+
return Result.gen(async function* () {
|
|
230
|
+
const state = yield* Result.await(store.read());
|
|
231
|
+
const recorded = pullRequestReceipt(state, key);
|
|
232
|
+
if (recorded)
|
|
233
|
+
return Result.ok({ receipt: recorded, reused: true });
|
|
234
|
+
const found = yield* Result.await(github.findOpenPullRequestByHead(repository, publication.head));
|
|
235
|
+
const pr = found ??
|
|
236
|
+
(yield* Result.await(whileOwned(() => github.openPullRequest(repository, publication))));
|
|
237
|
+
yield* Result.await(store.update((state) => withPullRequest(state, key, pr)));
|
|
238
|
+
return Result.ok({ receipt: { number: pr.number, url: pr.url }, reused: !!found });
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Opens the draft, then makes sure it carries its findings comment: a PR whose comment failed, or
|
|
243
|
+
* one recorded before findings were posted, still gets exactly one.
|
|
244
|
+
*/
|
|
245
|
+
function publishDraft(store, target, draft) {
|
|
246
|
+
return Result.gen(async function* () {
|
|
247
|
+
const { report, revision } = draft;
|
|
248
|
+
const opened = await openPullRequestOnce(store, target, draft.pullRequest);
|
|
249
|
+
if (opened.isErr())
|
|
250
|
+
return Result.err(explainRefusedDraft(opened.error, draft));
|
|
251
|
+
const { receipt, reused } = opened.value;
|
|
252
|
+
const identity = {
|
|
253
|
+
repository: target.repository,
|
|
254
|
+
pullRequest: receipt.number,
|
|
255
|
+
runId: report.state.runId,
|
|
256
|
+
outcome: report.status,
|
|
257
|
+
revision,
|
|
258
|
+
};
|
|
259
|
+
const state = yield* Result.await(store.read());
|
|
260
|
+
const findingsUrl = yield* Result.await(commentOnce(target, receipt.number, {
|
|
261
|
+
receipt: findingsCommentReceipt(state, identity),
|
|
262
|
+
marker: findingsMarker(identity),
|
|
263
|
+
body: findingsCommentBody(report.state, revision),
|
|
264
|
+
save: (url) => store.update((state) => withFindingsComment(state, identity, url)),
|
|
265
|
+
}));
|
|
266
|
+
return Result.ok({
|
|
267
|
+
kind: 'draftPullRequest',
|
|
268
|
+
url: receipt.url,
|
|
269
|
+
reused,
|
|
270
|
+
findingsUrl,
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
/** GitHub may refuse a PR whose commits change no files; say so rather than leave it a puzzle. */
|
|
275
|
+
function explainRefusedDraft(error, { pullRequest, changesFiles }) {
|
|
276
|
+
if (changesFiles || error._tag !== 'GithubRefused')
|
|
277
|
+
return error;
|
|
278
|
+
return new GithubRefused({
|
|
279
|
+
message: `${error.message} The commits on ${pullRequest.head} change no files against ${pullRequest.base}, so there is no diff for a draft PR; inspect the branch by hand.`,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Octokit } from '@octokit/rest';
|
|
2
|
+
import { Result } from 'better-result';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { retryResult } from '../../domain/retry.js';
|
|
5
|
+
import { GithubNotFound, GithubRefused, GithubUnavailable } from './errors.js';
|
|
6
|
+
export class OctokitGithub {
|
|
7
|
+
client;
|
|
8
|
+
constructor(client) {
|
|
9
|
+
this.client = client;
|
|
10
|
+
}
|
|
11
|
+
static authenticated(token) {
|
|
12
|
+
return new OctokitGithub(new Octokit({ auth: token }).rest);
|
|
13
|
+
}
|
|
14
|
+
getIssue(repository, number) {
|
|
15
|
+
return this.call(`getIssue ${repository.owner}/${repository.repo}#${number}`, async () => {
|
|
16
|
+
const { data } = await this.client.issues.get({ ...repository, issue_number: number });
|
|
17
|
+
return { number: data.number, title: data.title, body: data.body ?? '', url: data.html_url };
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
call(subject, operation) {
|
|
21
|
+
return retryResult(() => this.attempt(subject, operation), {
|
|
22
|
+
retries: GITHUB_RETRIES,
|
|
23
|
+
delayMs: GITHUB_RETRY_DELAY_MS,
|
|
24
|
+
shouldRetry: (error) => error._tag === 'GithubUnavailable',
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
attempt(subject, operation) {
|
|
28
|
+
return Result.tryPromise({ try: operation, catch: (cause) => githubFailure(subject, cause) });
|
|
29
|
+
}
|
|
30
|
+
getDefaultBranch(repository) {
|
|
31
|
+
return this.call(`getDefaultBranch ${repository.owner}/${repository.repo}`, async () => (await this.client.repos.get({ ...repository })).data.default_branch);
|
|
32
|
+
}
|
|
33
|
+
findOpenPullRequestByHead(repository, head) {
|
|
34
|
+
return this.call(`findOpenPullRequestByHead ${repository.owner}/${repository.repo}:${head}`, async () => {
|
|
35
|
+
const { data } = await this.client.pulls.list({
|
|
36
|
+
...repository,
|
|
37
|
+
head: `${repository.owner}:${head}`,
|
|
38
|
+
state: 'open',
|
|
39
|
+
});
|
|
40
|
+
return data[0] ? { number: data[0].number, url: data[0].html_url } : undefined;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
openPullRequest(repository, input) {
|
|
44
|
+
return this.call(`openPullRequest ${repository.owner}/${repository.repo}: ${input.head} -> ${input.base}`, async () => {
|
|
45
|
+
const { data } = await this.client.pulls.create({ ...repository, ...input });
|
|
46
|
+
return { number: data.number, url: data.html_url };
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
commentOnIssue(repository, number, body) {
|
|
50
|
+
return this.attempt(`commentOnIssue ${repository.owner}/${repository.repo}#${number}`, async () => {
|
|
51
|
+
const { data } = await this.client.issues.createComment({
|
|
52
|
+
...repository,
|
|
53
|
+
issue_number: number,
|
|
54
|
+
body,
|
|
55
|
+
});
|
|
56
|
+
return data.html_url;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
findIssueComment(repository, number, marker) {
|
|
60
|
+
return this.call(`findIssueComment ${repository.owner}/${repository.repo}#${number}`, async () => {
|
|
61
|
+
for (let page = 1;; page++) {
|
|
62
|
+
const { data } = await this.client.issues.listComments({
|
|
63
|
+
...repository,
|
|
64
|
+
issue_number: number,
|
|
65
|
+
per_page: COMMENTS_PER_PAGE,
|
|
66
|
+
page,
|
|
67
|
+
});
|
|
68
|
+
const found = data.find(({ body }) => body?.includes(marker));
|
|
69
|
+
if (found)
|
|
70
|
+
return found.html_url;
|
|
71
|
+
if (data.length < COMMENTS_PER_PAGE)
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** Short outages should not abort an unattended job, while retries remain bounded. */
|
|
78
|
+
const GITHUB_RETRIES = 3;
|
|
79
|
+
/** Back off from an unavailable API rather than immediately sending the request again. */
|
|
80
|
+
const GITHUB_RETRY_DELAY_MS = 1000;
|
|
81
|
+
function githubFailure(subject, cause) {
|
|
82
|
+
const parsed = z
|
|
83
|
+
.object({
|
|
84
|
+
status: z.number().default(0),
|
|
85
|
+
message: z.string().optional(),
|
|
86
|
+
response: z.object({ data: z.object({ message: z.string().optional() }) }).optional(),
|
|
87
|
+
})
|
|
88
|
+
.safeParse(cause);
|
|
89
|
+
const status = parsed.success ? parsed.data.status : 0;
|
|
90
|
+
const reason = parsed.success
|
|
91
|
+
? (parsed.data.response?.data.message ?? parsed.data.message)
|
|
92
|
+
: undefined;
|
|
93
|
+
const context = `GitHub ${subject} failed${status ? ` (${status})` : ''}`;
|
|
94
|
+
if (status >= 400 && status < 500) {
|
|
95
|
+
const message = `${context}: ${reason ?? 'Request refused.'}`;
|
|
96
|
+
return status === 404 ? new GithubNotFound({ message }) : new GithubRefused({ message });
|
|
97
|
+
}
|
|
98
|
+
return new GithubUnavailable({ message: `${context}: GitHub is unavailable; try again later.` });
|
|
99
|
+
}
|
|
100
|
+
/** GitHub's largest page of issue comments, so a lookup takes as few requests as it can. */
|
|
101
|
+
const COMMENTS_PER_PAGE = 100;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { runIdSchema } from '../../domain/run-id.js';
|
|
3
|
+
const requestSchema = z.object({ runId: runIdSchema, text: z.string(), title: z.string() });
|
|
4
|
+
const receiptSchema = z.object({
|
|
5
|
+
number: z.number().int().positive(),
|
|
6
|
+
url: z.string(),
|
|
7
|
+
});
|
|
8
|
+
const refusalSchema = z.object({
|
|
9
|
+
runId: runIdSchema,
|
|
10
|
+
revision: z.string(),
|
|
11
|
+
paths: z.array(z.string()).min(1),
|
|
12
|
+
});
|
|
13
|
+
const ownershipSchema = z.object({ token: z.string(), expiresAt: z.number() });
|
|
14
|
+
/** Fields added after version 1 default to empty, so state saved before them still loads. */
|
|
15
|
+
export const orchestrationSchema = z.object({
|
|
16
|
+
version: z.literal(1),
|
|
17
|
+
requests: z.record(requestSchema),
|
|
18
|
+
receipts: z.record(receiptSchema),
|
|
19
|
+
comments: z.record(z.string()).default({}),
|
|
20
|
+
refusals: z.record(refusalSchema).default({}),
|
|
21
|
+
refusalComments: z.record(z.string()).default({}),
|
|
22
|
+
findingsComments: z.record(z.string()).default({}),
|
|
23
|
+
owners: z.record(ownershipSchema).default({}),
|
|
24
|
+
});
|
|
25
|
+
export function emptyOrchestration() {
|
|
26
|
+
return {
|
|
27
|
+
version: 1,
|
|
28
|
+
requests: {},
|
|
29
|
+
receipts: {},
|
|
30
|
+
comments: {},
|
|
31
|
+
refusals: {},
|
|
32
|
+
refusalComments: {},
|
|
33
|
+
findingsComments: {},
|
|
34
|
+
owners: {},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function acceptedRequest(state, key) {
|
|
38
|
+
return state.requests[key];
|
|
39
|
+
}
|
|
40
|
+
export function acceptRequest(state, key, request) {
|
|
41
|
+
return state.requests[key]
|
|
42
|
+
? state
|
|
43
|
+
: { ...state, requests: { ...state.requests, [key]: request } };
|
|
44
|
+
}
|
|
45
|
+
export function pullRequestReceipt(state, key) {
|
|
46
|
+
return state.receipts[key];
|
|
47
|
+
}
|
|
48
|
+
export function withPullRequest(state, key, receipt) {
|
|
49
|
+
if (state.receipts[key])
|
|
50
|
+
return state;
|
|
51
|
+
return { ...state, receipts: { ...state.receipts, [key]: receipt } };
|
|
52
|
+
}
|
|
53
|
+
export function withComment(state, identity, url) {
|
|
54
|
+
if (commentReceipt(state, identity))
|
|
55
|
+
return state;
|
|
56
|
+
return { ...state, comments: { ...state.comments, [commentKey(identity)]: url } };
|
|
57
|
+
}
|
|
58
|
+
export function commentReceipt(state, identity) {
|
|
59
|
+
return state.comments[commentKey(identity)];
|
|
60
|
+
}
|
|
61
|
+
function commentKey({ runId, outcome, revision }) {
|
|
62
|
+
return `${runId}:${outcome}:${revision}`;
|
|
63
|
+
}
|
|
64
|
+
/** The marker a status comment carries, so a comment whose receipt was lost can be found again. */
|
|
65
|
+
export function commentMarker(identity) {
|
|
66
|
+
return `<!-- jig:status:${commentKey(identity)} -->`;
|
|
67
|
+
}
|
|
68
|
+
export function withRefusal(state, key, refusal) {
|
|
69
|
+
const id = refusalKey(key, refusal.revision);
|
|
70
|
+
if (state.refusals[id])
|
|
71
|
+
return state;
|
|
72
|
+
return { ...state, refusals: { ...state.refusals, [id]: refusal } };
|
|
73
|
+
}
|
|
74
|
+
function refusalKey(key, revision) {
|
|
75
|
+
return `${key}@${revision}`;
|
|
76
|
+
}
|
|
77
|
+
export function refusalOf(state, key, revision) {
|
|
78
|
+
return state.refusals[refusalKey(key, revision)];
|
|
79
|
+
}
|
|
80
|
+
export function withRefusalComment(state, key, revision, url) {
|
|
81
|
+
const id = refusalKey(key, revision);
|
|
82
|
+
if (state.refusalComments[id])
|
|
83
|
+
return state;
|
|
84
|
+
return { ...state, refusalComments: { ...state.refusalComments, [id]: url } };
|
|
85
|
+
}
|
|
86
|
+
export function refusalCommentReceipt(state, key, revision) {
|
|
87
|
+
return state.refusalComments[refusalKey(key, revision)];
|
|
88
|
+
}
|
|
89
|
+
/** The marker a refusal comment carries, so a comment whose receipt was lost can be found again. */
|
|
90
|
+
export function refusalMarker(key, revision) {
|
|
91
|
+
return `<!-- jig:refusal:${refusalKey(key, revision)} -->`;
|
|
92
|
+
}
|
|
93
|
+
export function withFindingsComment(state, identity, url) {
|
|
94
|
+
if (findingsCommentReceipt(state, identity))
|
|
95
|
+
return state;
|
|
96
|
+
return {
|
|
97
|
+
...state,
|
|
98
|
+
findingsComments: { ...state.findingsComments, [findingsKey(identity)]: url },
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export function findingsCommentReceipt(state, identity) {
|
|
102
|
+
return state.findingsComments[findingsKey(identity)];
|
|
103
|
+
}
|
|
104
|
+
function findingsKey({ repository, pullRequest, ...comment }) {
|
|
105
|
+
return `findings:${repository.owner}/${repository.repo}#${pullRequest}:${commentKey(comment)}`;
|
|
106
|
+
}
|
|
107
|
+
/** The marker a findings comment carries, so a comment whose receipt was lost can be found again. */
|
|
108
|
+
export function findingsMarker(identity) {
|
|
109
|
+
return `<!-- jig:${findingsKey(identity)} -->`;
|
|
110
|
+
}
|
|
111
|
+
/** Takes publication of `key` for `claim` unless another owner holds an unexpired lease. */
|
|
112
|
+
export function claimPublication(state, key, claim, now) {
|
|
113
|
+
const owner = state.owners[key];
|
|
114
|
+
if (owner && owner.token !== claim.token && owner.expiresAt > now)
|
|
115
|
+
return state;
|
|
116
|
+
return { ...state, owners: { ...state.owners, [key]: claim } };
|
|
117
|
+
}
|
|
118
|
+
/** Extends the lease of `claim`'s owner; a lease another job has taken stays with that job. */
|
|
119
|
+
export function renewPublication(state, key, claim) {
|
|
120
|
+
if (!ownsPublication(state, key, claim.token))
|
|
121
|
+
return state;
|
|
122
|
+
return { ...state, owners: { ...state.owners, [key]: claim } };
|
|
123
|
+
}
|
|
124
|
+
export function ownsPublication(state, key, token) {
|
|
125
|
+
return state.owners[key]?.token === token;
|
|
126
|
+
}
|
|
127
|
+
export function releasePublication(state, key, token) {
|
|
128
|
+
if (!ownsPublication(state, key, token))
|
|
129
|
+
return state;
|
|
130
|
+
const { [key]: _released, ...owners } = state.owners;
|
|
131
|
+
return { ...state, owners };
|
|
132
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
4
|
+
import { RemoteRef } from '../../adapters/git/remote-ref.js';
|
|
5
|
+
import { reportCleanup } from '../../app/report-cleanup.js';
|
|
6
|
+
import { messageOf, } from '../../domain/errors.js';
|
|
7
|
+
import { retryResult } from '../../domain/retry.js';
|
|
8
|
+
import { OrchestrationStateInvalid, PublicationInProgress } from './errors.js';
|
|
9
|
+
import { claimPublication, emptyOrchestration, orchestrationSchema, ownsPublication, releasePublication, renewPublication, } from './orchestration-state.js';
|
|
10
|
+
export class OrchestrationStore {
|
|
11
|
+
now;
|
|
12
|
+
refs;
|
|
13
|
+
constructor(repoRoot, now = Date.now) {
|
|
14
|
+
this.now = now;
|
|
15
|
+
this.refs = new RemoteRef(repoRoot);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Runs `use` as the only job publishing `key`, under a lease another job may take over once it
|
|
19
|
+
* expires. `use` renews the lease through `whileOwned` before each effect, and the lease is
|
|
20
|
+
* released however `use` ends, a throw included.
|
|
21
|
+
*/
|
|
22
|
+
async withPublicationOwnership(key, logger, use) {
|
|
23
|
+
const token = randomUUID();
|
|
24
|
+
const claimed = await this.update((state) => claimPublication(state, key, this.leaseFor(token), this.now()));
|
|
25
|
+
if (claimed.isErr())
|
|
26
|
+
return Result.err(claimed.error);
|
|
27
|
+
if (!ownsPublication(claimed.value, key, token))
|
|
28
|
+
return Result.err(new PublicationInProgress({
|
|
29
|
+
message: `Another job is publishing ${key}; run the command again once it finishes.`,
|
|
30
|
+
}));
|
|
31
|
+
const whileOwned = async (step) => {
|
|
32
|
+
const renewed = await this.renew(key, token);
|
|
33
|
+
return renewed.isErr() ? Result.err(renewed.error) : step();
|
|
34
|
+
};
|
|
35
|
+
let used;
|
|
36
|
+
let released;
|
|
37
|
+
try {
|
|
38
|
+
used = await use(whileOwned);
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
released = await this.update((state) => releasePublication(state, key, token));
|
|
42
|
+
}
|
|
43
|
+
return reportCleanup(used, released.map(() => undefined), logger);
|
|
44
|
+
}
|
|
45
|
+
leaseFor(token) {
|
|
46
|
+
return { token, expiresAt: this.now() + OWNERSHIP_LEASE_MS };
|
|
47
|
+
}
|
|
48
|
+
async renew(key, token) {
|
|
49
|
+
const renewed = await this.update((state) => renewPublication(state, key, this.leaseFor(token)));
|
|
50
|
+
if (renewed.isErr())
|
|
51
|
+
return Result.err(renewed.error);
|
|
52
|
+
if (ownsPublication(renewed.value, key, token))
|
|
53
|
+
return Result.ok();
|
|
54
|
+
return Result.err(new PublicationInProgress({
|
|
55
|
+
message: `Another job took over publishing ${key} after this job's lease expired; this job stopped publishing.`,
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
read() {
|
|
59
|
+
return this.load().then((result) => result.map(({ state }) => state));
|
|
60
|
+
}
|
|
61
|
+
load() {
|
|
62
|
+
return Result.gen(async function* () {
|
|
63
|
+
const listed = yield* Result.await(this.refs.list([ORCHESTRATION_REF]));
|
|
64
|
+
if (!listed.has(ORCHESTRATION_REF))
|
|
65
|
+
return Result.ok({ state: emptyOrchestration() });
|
|
66
|
+
yield* Result.await(this.refs.fetch([`+${ORCHESTRATION_REF}:${ORCHESTRATION_REF}`]));
|
|
67
|
+
const expected = await this.refs.revision(ORCHESTRATION_REF);
|
|
68
|
+
const contents = yield* Result.await(this.refs.required(['show', `${expected}:${ORCHESTRATION_FILE}`]));
|
|
69
|
+
const state = yield* Result.try({
|
|
70
|
+
try: () => orchestrationSchema.parse(JSON.parse(contents)),
|
|
71
|
+
catch: (cause) => new OrchestrationStateInvalid({
|
|
72
|
+
message: `Invalid GitHub orchestration state: ${messageOf(cause)}`,
|
|
73
|
+
}),
|
|
74
|
+
});
|
|
75
|
+
return Result.ok({ state, expected });
|
|
76
|
+
}, this);
|
|
77
|
+
}
|
|
78
|
+
async update(decide) {
|
|
79
|
+
const result = await retryResult(() => this.updateOnce(decide), {
|
|
80
|
+
retries: ORCHESTRATION_ATTEMPTS - 1,
|
|
81
|
+
delayMs: 0,
|
|
82
|
+
shouldRetry: (error) => error._tag === 'RunStateConflict',
|
|
83
|
+
});
|
|
84
|
+
return result.mapError((error) => error._tag === 'RunStateConflict'
|
|
85
|
+
? new OrchestrationStateInvalid({
|
|
86
|
+
message: `Competing jobs kept updating GitHub orchestration ref ${ORCHESTRATION_REF}; try again.`,
|
|
87
|
+
})
|
|
88
|
+
: error);
|
|
89
|
+
}
|
|
90
|
+
updateOnce(decide) {
|
|
91
|
+
return Result.gen(async function* () {
|
|
92
|
+
const { state, expected } = yield* Result.await(this.load());
|
|
93
|
+
const next = decide(state);
|
|
94
|
+
if (isDeepStrictEqual(next, state))
|
|
95
|
+
return Result.ok(state);
|
|
96
|
+
const commit = yield* Result.await(this.refs.commitFile(ORCHESTRATION_REF, ORCHESTRATION_FILE, `${JSON.stringify(next, null, 2)}\n`, 'jig github: update orchestration'));
|
|
97
|
+
yield* Result.await(this.refs.pushLeased([{ ref: ORCHESTRATION_REF, source: commit, expected }]));
|
|
98
|
+
return Result.ok(next);
|
|
99
|
+
}, this);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** How long a publication owner holds its lease unrenewed; a crashed job's lease lapses after this. */
|
|
103
|
+
export const OWNERSHIP_LEASE_MS = 15 * 60 * 1000;
|
|
104
|
+
/** A hidden ref outside the branch list shares orchestration across every repository job. */
|
|
105
|
+
const ORCHESTRATION_REF = 'refs/jig/_orchestration';
|
|
106
|
+
/** Every repository job reads the shared receipts from this file in the hidden ref. */
|
|
107
|
+
const ORCHESTRATION_FILE = 'orchestration.json';
|
|
108
|
+
/** Competing jobs re-read the winner's state, within a bounded contention budget. */
|
|
109
|
+
const ORCHESTRATION_ATTEMPTS = 5;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { gitCommand, openRepo } from '../../adapters/git/repository.js';
|
|
4
|
+
import { GithubSetupInvalid } from './errors.js';
|
|
5
|
+
export async function repositoryIdentity(repoRoot, environment) {
|
|
6
|
+
const configured = environment.GITHUB_REPOSITORY;
|
|
7
|
+
if (configured !== undefined)
|
|
8
|
+
return parseRepository(configured);
|
|
9
|
+
const origin = await gitCommand('remote get-url', () => openRepo(repoRoot).remote(['get-url', 'origin']));
|
|
10
|
+
if (origin.isErr())
|
|
11
|
+
return Result.err(new GithubSetupInvalid({ message: 'Set GITHUB_REPOSITORY or configure a GitHub origin.' }));
|
|
12
|
+
const name = /^(?:https:\/\/github\.com\/|git@github\.com:|ssh:\/\/git@github\.com\/)([^\s]+?)\/?$/.exec(origin.value?.trim() ?? '')?.[1];
|
|
13
|
+
return parseRepository(name?.replace(/\.git$/, ''));
|
|
14
|
+
}
|
|
15
|
+
function parseRepository(value) {
|
|
16
|
+
const parsed = repositorySchema.safeParse(value);
|
|
17
|
+
return parsed.success
|
|
18
|
+
? Result.ok(parsed.data)
|
|
19
|
+
: Result.err(new GithubSetupInvalid({ message: 'Repository must identify a GitHub owner/repository.' }));
|
|
20
|
+
}
|
|
21
|
+
const repositorySchema = z
|
|
22
|
+
.string()
|
|
23
|
+
.regex(/^[\w.-]+\/[\w.-]+$/)
|
|
24
|
+
.transform((value) => {
|
|
25
|
+
const [owner, repo] = value.split('/');
|
|
26
|
+
return { owner, repo };
|
|
27
|
+
});
|