next-issue 2.21.2 → 4.0.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 +90 -125
- package/dist/agent-reply.mjs +13 -0
- package/dist/agent-session.mjs +56 -0
- package/dist/agents.mjs +47 -88
- package/dist/args.mjs +26 -31
- package/dist/base-checks.mjs +37 -0
- package/dist/budget.mjs +16 -0
- package/dist/check-poll.mjs +73 -0
- package/dist/checks.mjs +67 -0
- package/dist/children.mjs +52 -0
- package/dist/cli.mjs +25 -15
- package/dist/command-records.mjs +15 -0
- package/dist/config-fields.mjs +32 -0
- package/dist/config.mjs +36 -71
- package/dist/conflict.mjs +43 -42
- package/dist/durations.mjs +37 -0
- package/dist/enter-review.mjs +13 -0
- package/dist/errors.mjs +20 -0
- package/dist/exec.mjs +71 -107
- package/dist/files.mjs +28 -0
- package/dist/fix.mjs +61 -50
- package/dist/gate.mjs +31 -43
- package/dist/gh.mjs +55 -0
- package/dist/git-commit.mjs +36 -0
- package/dist/git-land.mjs +30 -0
- package/dist/git-merge.mjs +42 -0
- package/dist/git.mjs +42 -166
- package/dist/github.mjs +31 -388
- package/dist/gitignore.mjs +13 -0
- package/dist/hand-over-comments.mjs +78 -0
- package/dist/handover.mjs +60 -107
- package/dist/implement.mjs +32 -0
- package/dist/interrupt.mjs +6 -19
- package/dist/job.mjs +28 -17
- package/dist/labels.mjs +65 -0
- package/dist/land.mjs +66 -0
- package/dist/lock.mjs +54 -0
- package/dist/logs.mjs +84 -62
- package/dist/loop.mjs +12 -37
- package/dist/observe.mjs +24 -98
- package/dist/open-job.mjs +39 -0
- package/dist/pending.mjs +46 -0
- package/dist/pipeline.mjs +34 -217
- package/dist/poll.mjs +20 -0
- package/dist/ports.mjs +18 -4
- package/dist/progress.mjs +36 -23
- package/dist/prompts.mjs +31 -36
- package/dist/protocol.mjs +17 -11
- package/dist/prune-runs.mjs +33 -0
- package/dist/pull-requests.mjs +60 -0
- package/dist/questions.mjs +45 -0
- package/dist/records.mjs +3 -0
- package/dist/render.mjs +19 -0
- package/dist/revert.mjs +27 -0
- package/dist/review.mjs +63 -83
- package/dist/run-logs.mjs +23 -0
- package/dist/run.mjs +59 -58
- package/dist/serve.mjs +46 -214
- package/dist/setup.mjs +2 -5
- package/dist/skip.mjs +54 -0
- package/dist/state.mjs +27 -39
- package/dist/summary.mjs +11 -7
- package/dist/text.mjs +15 -0
- package/dist/usage.mjs +45 -0
- package/dist/verdict.mjs +20 -13
- package/dist/watch.mjs +38 -0
- package/dist/worker-paths.mjs +16 -0
- package/dist/worker-run.mjs +56 -0
- package/dist/worker-server.mjs +56 -0
- package/dist/worktree-setup.mjs +25 -0
- package/dist/worktree.mjs +58 -0
- package/package.json +1 -1
- package/dist/merge.mjs +0 -75
package/dist/handover.mjs
CHANGED
|
@@ -1,139 +1,92 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { message } from './
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
async function lastHandOver(comments, writer) {
|
|
15
|
-
for (let index = comments.length - 1; index >= 0; index -= 1) {
|
|
16
|
-
const comment = comments[index];
|
|
17
|
-
if (comment.body.startsWith(HAND_OVER) && (await writer(comment.author))) {
|
|
18
|
-
return index;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return -1;
|
|
1
|
+
import { elapsedMs } from './durations.mjs';
|
|
2
|
+
import { message } from './errors.mjs';
|
|
3
|
+
import { HAND_OVER } from './hand-over-comments.mjs';
|
|
4
|
+
import { setStatus, } from './job.mjs';
|
|
5
|
+
import { oneAtATime } from './lock.mjs';
|
|
6
|
+
import { revertFailedFix } from './revert.mjs';
|
|
7
|
+
import { logIssueError } from './skip.mjs';
|
|
8
|
+
import { dropState, writeState } from './state.mjs';
|
|
9
|
+
export async function escalateAndFinish(job, note, reason) {
|
|
10
|
+
const reverted = await revertFailedFix(job);
|
|
11
|
+
await handOver(job.context, job.log, job.issue.number, job.state, `${note}${reverted}`);
|
|
12
|
+
return { done: true, report: await finish(job, 'needs-human', reason) };
|
|
22
13
|
}
|
|
23
|
-
export async function
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const pr = (await readState(repo, issue.number))?.pr;
|
|
29
|
-
const comments = await ports.threadComments(repo, pr ?? issue.number);
|
|
30
|
-
const comment = comments[await lastHandOver(comments, writer)];
|
|
31
|
-
questions.push({
|
|
32
|
-
issue: issue.number,
|
|
33
|
-
title: issue.title,
|
|
34
|
-
reason: comment?.body.slice(HAND_OVER.length).trim() ?? '',
|
|
35
|
-
url: comment?.url ?? '',
|
|
36
|
-
repo: `${repo.owner}/${repo.name}`,
|
|
37
|
-
...(pr === undefined ? {} : { pr }),
|
|
38
|
-
labels: { needsHuman: config.labels.needsHuman, skip: config.labels.skip },
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
return questions;
|
|
42
|
-
}
|
|
43
|
-
export async function writerComments(context, log, issue) {
|
|
44
|
-
const { repo, ports } = context;
|
|
45
|
-
const writer = writers(repo, ports);
|
|
46
|
-
const kept = [];
|
|
47
|
-
for (const comment of await ports.issueComments(repo, issue)) {
|
|
48
|
-
if (await writer(comment.author)) {
|
|
49
|
-
kept.push(`@${comment.author}: ${comment.body}`);
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
log.event('comment.skip', { author: comment.author }, 'quiet');
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return kept;
|
|
14
|
+
export async function finishDone(job) {
|
|
15
|
+
const { context, issue, state } = job;
|
|
16
|
+
await setStatus(context, issue.number, state.pr, context.config.labels.done);
|
|
17
|
+
await context.ports.closeIssue(context.repo, issue.number);
|
|
18
|
+
return { done: true, report: await finish(job, 'done') };
|
|
56
19
|
}
|
|
57
|
-
export
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const writer = writers(repo, ports);
|
|
61
|
-
const last = await lastHandOver(comments, writer);
|
|
62
|
-
if (last === -1) {
|
|
63
|
-
return [];
|
|
64
|
-
}
|
|
65
|
-
const reply = [];
|
|
66
|
-
for (const comment of comments.slice(last + 1)) {
|
|
67
|
-
if (await writer(comment.author)) {
|
|
68
|
-
reply.push(`@${comment.author}: ${comment.body}`);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return reply;
|
|
20
|
+
export function stop(job) {
|
|
21
|
+
job.log.event('issue.stop', { phase: job.state.phase }, 'quiet');
|
|
22
|
+
return jobReport(job, 'stopped');
|
|
72
23
|
}
|
|
73
|
-
|
|
74
|
-
return
|
|
24
|
+
function jobReport(job, outcome, reason) {
|
|
25
|
+
return issueReport(job.issue.number, outcome, job.state, job.started, reason);
|
|
75
26
|
}
|
|
76
|
-
|
|
77
|
-
const { issue, log, state } = job;
|
|
78
|
-
log.event('issue.stop', { phase: state.phase }, 'quiet');
|
|
27
|
+
function issueReport(issue, outcome, state, started, reason) {
|
|
79
28
|
return {
|
|
80
|
-
issue
|
|
81
|
-
outcome
|
|
29
|
+
issue,
|
|
30
|
+
outcome,
|
|
31
|
+
reason,
|
|
82
32
|
pr: state.pr,
|
|
83
33
|
ciFixes: state.ciFixes,
|
|
84
34
|
reviewRounds: state.reviewRounds,
|
|
85
|
-
ms:
|
|
35
|
+
ms: elapsedMs(started),
|
|
86
36
|
};
|
|
87
37
|
}
|
|
88
38
|
export async function finish(job, outcome, reason) {
|
|
89
|
-
const { context, issue, log,
|
|
90
|
-
const { repo
|
|
39
|
+
const { context, issue, log, branch } = job;
|
|
40
|
+
const { repo } = context;
|
|
91
41
|
await cleanUp(context, log, branch);
|
|
92
42
|
if (outcome === 'done') {
|
|
93
|
-
|
|
94
|
-
if (!(await oneAtATime(repo.root, () => ports.deleteBranch(repo, name)))) {
|
|
95
|
-
log.event('branch.kept', { branch: name }, 'quiet');
|
|
96
|
-
}
|
|
97
|
-
}
|
|
43
|
+
await deleteBranches(context, log, branch);
|
|
98
44
|
await dropState(repo, issue.number);
|
|
99
45
|
}
|
|
100
46
|
return {
|
|
101
|
-
|
|
102
|
-
outcome,
|
|
103
|
-
reason,
|
|
104
|
-
pr: state.pr,
|
|
105
|
-
ciFixes: state.ciFixes,
|
|
106
|
-
reviewRounds: state.reviewRounds,
|
|
107
|
-
ms: Date.now() - job.started,
|
|
47
|
+
...jobReport(job, outcome, reason),
|
|
108
48
|
...(job.mainBroken && outcome !== 'done' ? { halt: true } : {}),
|
|
109
49
|
};
|
|
110
50
|
}
|
|
111
|
-
|
|
51
|
+
async function deleteBranches(context, log, branch) {
|
|
52
|
+
const { repo, ports } = context;
|
|
53
|
+
for (const name of [branch, ...(await ports.disputeBranches(repo, branch))]) {
|
|
54
|
+
await removeOrKeep(context, log, 'branch.kept', { branch: name }, () => ports.deleteBranch(repo, name));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async function removeOrKeep(context, log, kind, fields, remove) {
|
|
58
|
+
if (!(await oneAtATime(context.repo.root, remove))) {
|
|
59
|
+
log.event(kind, fields, 'quiet');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function cleanUp(context, log, branch) {
|
|
63
|
+
const { repo, ports } = context;
|
|
112
64
|
try {
|
|
113
|
-
const path = await
|
|
65
|
+
const path = await ports.findWorktree(repo, branch);
|
|
114
66
|
if (path === undefined) {
|
|
115
67
|
return;
|
|
116
68
|
}
|
|
117
|
-
|
|
118
|
-
log.event('worktree.kept', { path }, 'quiet');
|
|
119
|
-
}
|
|
69
|
+
await removeOrKeep(context, log, 'worktree.kept', { path }, () => ports.removeWorktree(repo, path));
|
|
120
70
|
}
|
|
121
71
|
catch (error) {
|
|
122
72
|
log.event('worktree.error', { error: message(error) }, 'quiet');
|
|
123
73
|
}
|
|
124
74
|
}
|
|
125
|
-
|
|
75
|
+
async function handOver(context, log, issue, state, reason) {
|
|
76
|
+
const { repo, ports, config } = context;
|
|
126
77
|
log.event('hand-over', { reason }, 'quiet');
|
|
127
|
-
await writeState(
|
|
128
|
-
await setStatus(context, issue, state.pr,
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
78
|
+
await writeState(repo, { ...state, handedOver: true });
|
|
79
|
+
await setStatus(context, issue, state.pr, config.labels.needsHuman);
|
|
80
|
+
await ports.commentOnIssue(repo, issue, `${HAND_OVER} ${reason}`);
|
|
81
|
+
}
|
|
82
|
+
export async function claimedError(context, log, issue, state, error, started) {
|
|
83
|
+
const { reason } = logIssueError(log, error, started);
|
|
84
|
+
try {
|
|
85
|
+
await handOver(context, log, issue.number, state, `The run failed: ${reason}`);
|
|
132
86
|
}
|
|
133
|
-
|
|
134
|
-
|
|
87
|
+
catch (failure) {
|
|
88
|
+
log.event('hand-over.error', { error: message(failure) }, 'quiet');
|
|
135
89
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return { issue: issue.number, outcome: 'skipped', ciFixes: 0, reviewRounds: 0, ms: 0 };
|
|
90
|
+
await cleanUp(context, log, state.branch);
|
|
91
|
+
return issueReport(issue.number, 'error', state, started, reason);
|
|
139
92
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { parseCommitSubject } from './agent-reply.mjs';
|
|
2
|
+
import { CODING } from './agents.mjs';
|
|
3
|
+
import { commitWork } from './fix.mjs';
|
|
4
|
+
import { escalateAndFinish } from './handover.mjs';
|
|
5
|
+
import { fallbackTitle, ROUND_CONTINUE, runRole } from './job.mjs';
|
|
6
|
+
import { implementPrompt } from './prompts.mjs';
|
|
7
|
+
import { writeState } from './state.mjs';
|
|
8
|
+
export async function implement(job) {
|
|
9
|
+
const { context, issue, log, state, worktree, comments, reply } = job;
|
|
10
|
+
const before = await context.ports.revision(worktree);
|
|
11
|
+
const result = await log.step('implement', {}, () => runRole(job, 'implementer', implementPrompt(issue, comments, reply), CODING));
|
|
12
|
+
const subject = parseCommitSubject(result.text, fallbackTitle(issue));
|
|
13
|
+
if (!(await commitWork(job, before, subject, 'implementer'))) {
|
|
14
|
+
const escalated = await useEarlierCommit(job);
|
|
15
|
+
if (escalated !== undefined) {
|
|
16
|
+
return escalated;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
state.phase = 'implemented';
|
|
20
|
+
await writeState(context.repo, state);
|
|
21
|
+
return ROUND_CONTINUE;
|
|
22
|
+
}
|
|
23
|
+
async function useEarlierCommit(job) {
|
|
24
|
+
const { context, log, state, worktree } = job;
|
|
25
|
+
const earlier = await context.ports.latestSubject(worktree, context.base, context.config.remote);
|
|
26
|
+
if (earlier === undefined) {
|
|
27
|
+
return escalateAndFinish(job, 'The implementer produced no change.', 'no implementation commit');
|
|
28
|
+
}
|
|
29
|
+
log.event('implement.earlier', { subject: earlier }, 'quiet');
|
|
30
|
+
state.title = earlier;
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
package/dist/interrupt.mjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { stopChildren } from './
|
|
1
|
+
import { stopChildren } from './children.mjs';
|
|
2
|
+
import { wakeableSleep } from './durations.mjs';
|
|
2
3
|
export function listenForInterrupts(say) {
|
|
3
4
|
let presses = 0;
|
|
4
|
-
|
|
5
|
+
const sleeper = {};
|
|
5
6
|
const press = () => {
|
|
6
7
|
presses += 1;
|
|
7
|
-
if (presses === 1 && wake !== undefined) {
|
|
8
|
+
if (presses === 1 && sleeper.wake !== undefined) {
|
|
8
9
|
say('Stopping now.');
|
|
9
|
-
wake();
|
|
10
|
+
sleeper.wake();
|
|
10
11
|
}
|
|
11
12
|
else if (presses === 1) {
|
|
12
13
|
say('Stopping after the running issues. Press Ctrl-C again to stop after the current round.');
|
|
@@ -23,20 +24,6 @@ export function listenForInterrupts(say) {
|
|
|
23
24
|
return {
|
|
24
25
|
afterIssue: () => presses >= 1,
|
|
25
26
|
afterRound: () => presses >= 2,
|
|
26
|
-
sleep: (ms) =>
|
|
27
|
-
if (presses >= 1) {
|
|
28
|
-
resolve(false);
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
const timer = setTimeout(() => {
|
|
32
|
-
wake = undefined;
|
|
33
|
-
resolve(true);
|
|
34
|
-
}, ms);
|
|
35
|
-
wake = () => {
|
|
36
|
-
clearTimeout(timer);
|
|
37
|
-
wake = undefined;
|
|
38
|
-
resolve(false);
|
|
39
|
-
};
|
|
40
|
-
}),
|
|
27
|
+
sleep: (ms) => wakeableSleep(sleeper, ms, presses >= 1),
|
|
41
28
|
};
|
|
42
29
|
}
|
package/dist/job.mjs
CHANGED
|
@@ -1,10 +1,28 @@
|
|
|
1
1
|
import { managedLabels } from './config.mjs';
|
|
2
|
+
import { minutesToMs } from './durations.mjs';
|
|
2
3
|
import { writeState } from './state.mjs';
|
|
3
|
-
const
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
export const ROUND_CONTINUE = { done: false };
|
|
5
|
+
export const GATE_PASSED = { done: false, fixed: false };
|
|
6
|
+
export const GATE_FIXED = { done: false, fixed: true };
|
|
7
|
+
export function gateRound(gate) {
|
|
8
|
+
if (gate.done) {
|
|
9
|
+
return gate;
|
|
10
|
+
}
|
|
11
|
+
return gate.fixed ? ROUND_CONTINUE : undefined;
|
|
12
|
+
}
|
|
13
|
+
export function runRole(job, role, prompt, profile) {
|
|
14
|
+
const { config, ports } = job.context;
|
|
15
|
+
return ports.runAgent(job.log, {
|
|
16
|
+
name: role,
|
|
17
|
+
cwd: job.worktree,
|
|
18
|
+
prompt,
|
|
19
|
+
profile,
|
|
20
|
+
model: config.models[role],
|
|
21
|
+
timeoutMs: minutesToMs(config.agentTimeoutMinutes),
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
export function fallbackTitle(issue) {
|
|
25
|
+
return `fix: resolve issue #${issue.number}`;
|
|
8
26
|
}
|
|
9
27
|
export async function openPr(job) {
|
|
10
28
|
const { context, issue, log, state, branch } = job;
|
|
@@ -12,24 +30,17 @@ export async function openPr(job) {
|
|
|
12
30
|
const pr = await log.step('pull-request', { branch }, async () => {
|
|
13
31
|
const existing = await ports.findPr(repo, branch);
|
|
14
32
|
return (existing ??
|
|
15
|
-
ports.createPr(repo, branch, context.base, state.title ??
|
|
33
|
+
ports.createPr(repo, branch, context.base, state.title ?? fallbackTitle(issue), `Closes #${issue.number}\n\nOpened by next-issue.`, config.draftPullRequest));
|
|
16
34
|
});
|
|
17
35
|
state.pr = pr;
|
|
18
36
|
await writeState(repo, state);
|
|
19
37
|
log.event('pull-request.ready', { pr }, 'quiet');
|
|
20
38
|
}
|
|
21
39
|
export async function setStatus(context, issue, pr, label) {
|
|
22
|
-
const
|
|
23
|
-
|
|
40
|
+
const { repo, config, ports } = context;
|
|
41
|
+
const managed = managedLabels(config);
|
|
42
|
+
await ports.setLabel(repo, 'issue', issue, label, managed);
|
|
24
43
|
if (pr !== undefined) {
|
|
25
|
-
await
|
|
44
|
+
await ports.setLabel(repo, 'pr', pr, label, managed);
|
|
26
45
|
}
|
|
27
46
|
}
|
|
28
|
-
export function history(rounds) {
|
|
29
|
-
return rounds.map((round) => {
|
|
30
|
-
const head = `Round ${round.round}:\n${round.findings}`;
|
|
31
|
-
return round.dispute === undefined
|
|
32
|
-
? head
|
|
33
|
-
: `${head}\nThe fixer disputed every finding of this round: ${round.dispute}`;
|
|
34
|
-
});
|
|
35
|
-
}
|
package/dist/labels.mjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { asError } from './errors.mjs';
|
|
2
|
+
import { gh, ghJson, ghOnce, names, slug, viewJson } from './gh.mjs';
|
|
3
|
+
const KNOWN_LABELS = new Map();
|
|
4
|
+
async function labelsOf(repo, kind, number) {
|
|
5
|
+
const parsed = await viewJson(repo, kind, number, 'labels');
|
|
6
|
+
return names(parsed.labels);
|
|
7
|
+
}
|
|
8
|
+
export function labelArgs(add, remove, current) {
|
|
9
|
+
const stale = remove.filter((item) => item !== add && current.includes(item));
|
|
10
|
+
return ['--add-label', add, ...stale.flatMap((name) => ['--remove-label', name])];
|
|
11
|
+
}
|
|
12
|
+
export async function setLabel(repo, kind, number, add, remove) {
|
|
13
|
+
await createUnknownLabel(repo, add);
|
|
14
|
+
const current = await labelsOf(repo, kind, number);
|
|
15
|
+
const args = [kind, 'edit', String(number), ...labelArgs(add, remove, current)];
|
|
16
|
+
try {
|
|
17
|
+
await gh(repo, args);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
await retryWithLabel(repo, args, add, error);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
async function retryWithLabel(repo, args, label, error) {
|
|
24
|
+
KNOWN_LABELS.delete(slug(repo));
|
|
25
|
+
const created = await createUnknownLabel(repo, label);
|
|
26
|
+
if (created === 'known') {
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
if (created instanceof Error) {
|
|
30
|
+
throw created;
|
|
31
|
+
}
|
|
32
|
+
await gh(repo, args);
|
|
33
|
+
}
|
|
34
|
+
async function knownLabels(repo) {
|
|
35
|
+
const repoSlug = slug(repo);
|
|
36
|
+
const cached = KNOWN_LABELS.get(repoSlug);
|
|
37
|
+
if (cached !== undefined) {
|
|
38
|
+
return cached;
|
|
39
|
+
}
|
|
40
|
+
const listed = await ghJson(repo, [
|
|
41
|
+
'label',
|
|
42
|
+
'list',
|
|
43
|
+
'--limit',
|
|
44
|
+
'1000',
|
|
45
|
+
'--json',
|
|
46
|
+
'name',
|
|
47
|
+
]);
|
|
48
|
+
const known = new Set(names(listed));
|
|
49
|
+
KNOWN_LABELS.set(repoSlug, known);
|
|
50
|
+
return known;
|
|
51
|
+
}
|
|
52
|
+
async function createUnknownLabel(repo, label) {
|
|
53
|
+
const known = await knownLabels(repo);
|
|
54
|
+
if (known.has(label)) {
|
|
55
|
+
return 'known';
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
await ghOnce(repo, ['label', 'create', label]);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
return asError(error);
|
|
62
|
+
}
|
|
63
|
+
known.add(label);
|
|
64
|
+
return 'created';
|
|
65
|
+
}
|
package/dist/land.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { rebase } from './conflict.mjs';
|
|
2
|
+
import { checkGate } from './gate.mjs';
|
|
3
|
+
import { escalateAndFinish, finishDone } from './handover.mjs';
|
|
4
|
+
import { oneAtATime } from './lock.mjs';
|
|
5
|
+
import { revertFailedFix } from './revert.mjs';
|
|
6
|
+
import { writeState } from './state.mjs';
|
|
7
|
+
const PUSH_TRIES = 3;
|
|
8
|
+
export function landApproved(job) {
|
|
9
|
+
return landing(job, () => landLoop(job));
|
|
10
|
+
}
|
|
11
|
+
export function resumeLanding(job, sha) {
|
|
12
|
+
return landing(job, async () => {
|
|
13
|
+
if ((await job.context.ports.revision(job.worktree)) !== sha) {
|
|
14
|
+
job.mainBroken = true;
|
|
15
|
+
job.repairing = true;
|
|
16
|
+
return landLoop(job);
|
|
17
|
+
}
|
|
18
|
+
const gate = await checkGate(job, sha);
|
|
19
|
+
if (gate.done) {
|
|
20
|
+
return gate;
|
|
21
|
+
}
|
|
22
|
+
return gate.fixed ? landLoop(job) : finishDone(job);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function landing(job, work) {
|
|
26
|
+
return oneAtATime(`${job.context.repo.root}#land`, work);
|
|
27
|
+
}
|
|
28
|
+
async function landLoop(job) {
|
|
29
|
+
for (;;) {
|
|
30
|
+
const pushed = await pushChange(job).catch(async (error) => {
|
|
31
|
+
await revertFailedFix(job);
|
|
32
|
+
throw error;
|
|
33
|
+
});
|
|
34
|
+
if (typeof pushed !== 'string') {
|
|
35
|
+
return pushed;
|
|
36
|
+
}
|
|
37
|
+
const gate = await checkGate(job, pushed);
|
|
38
|
+
if (gate.done) {
|
|
39
|
+
return gate;
|
|
40
|
+
}
|
|
41
|
+
if (!gate.fixed) {
|
|
42
|
+
return finishDone(job);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async function pushChange(job) {
|
|
47
|
+
const { context, log, state, worktree } = job;
|
|
48
|
+
const { repo, config, ports, base } = context;
|
|
49
|
+
for (let attempt = 1;; attempt += 1) {
|
|
50
|
+
const rebased = await rebase(job);
|
|
51
|
+
if (rebased !== undefined) {
|
|
52
|
+
return rebased;
|
|
53
|
+
}
|
|
54
|
+
const sha = await ports.revision(worktree);
|
|
55
|
+
const pushed = await log.step('land', { base, sha }, () => ports.pushToBase(worktree, config.remote, base));
|
|
56
|
+
if (pushed) {
|
|
57
|
+
state.landed = [...(state.landed ?? []), sha];
|
|
58
|
+
await writeState(repo, state);
|
|
59
|
+
job.repairing = false;
|
|
60
|
+
return sha;
|
|
61
|
+
}
|
|
62
|
+
if (attempt >= PUSH_TRIES) {
|
|
63
|
+
return escalateAndFinish(job, `The push to ${base} failed ${PUSH_TRIES} times.`, 'push failed');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
package/dist/lock.mjs
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { rm, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
3
|
+
import { hasCode } from './errors.mjs';
|
|
4
|
+
import { readTextOrEmpty } from './files.mjs';
|
|
5
|
+
const LOCK_POLL_MS = 100;
|
|
6
|
+
const worktreeQueues = new Map();
|
|
7
|
+
export function oneAtATime(root, work) {
|
|
8
|
+
const result = (worktreeQueues.get(root) ?? Promise.resolve()).then(work);
|
|
9
|
+
worktreeQueues.set(root, result.catch(() => undefined));
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
function alive(pid) {
|
|
13
|
+
try {
|
|
14
|
+
process.kill(pid, 0);
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
return hasCode(error, 'EPERM');
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async function holder(lockPath) {
|
|
22
|
+
const pid = Number((await readTextOrEmpty(lockPath)).trim());
|
|
23
|
+
return pid > 0 && alive(pid) ? pid : undefined;
|
|
24
|
+
}
|
|
25
|
+
export async function stopWorker(lockPath) {
|
|
26
|
+
const pid = await holder(lockPath);
|
|
27
|
+
if (pid !== undefined) {
|
|
28
|
+
process.kill(pid, 'SIGTERM');
|
|
29
|
+
}
|
|
30
|
+
return pid;
|
|
31
|
+
}
|
|
32
|
+
export async function lock(path, waitMs) {
|
|
33
|
+
const deadline = Date.now() + waitMs;
|
|
34
|
+
for (;;) {
|
|
35
|
+
try {
|
|
36
|
+
await writeFile(path, `${process.pid}\n`, { flag: 'wx', mode: 0o600 });
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (!hasCode(error, 'EEXIST')) {
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if ((await holder(path)) === undefined) {
|
|
45
|
+
await rm(path, { force: true });
|
|
46
|
+
}
|
|
47
|
+
else if (Date.now() >= deadline) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
await delay(LOCK_POLL_MS);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|