next-issue 10.6.0 → 10.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.
- package/README.md +12 -9
- package/dist/cli.mjs +6 -12
- package/dist/interrupt.mjs +25 -11
- package/dist/serve.mjs +9 -3
- package/dist/start.mjs +95 -0
- package/dist/worker-client.mjs +189 -0
- package/dist/worker-run.mjs +14 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,7 +34,9 @@ flowchart TD
|
|
|
34
34
|
|
|
35
35
|
Before each issue, fetch the open issues again and take the oldest one not yet seen in this run. An issue seen once in a run is not tried again in that run while its labels stay as the first fetch after its attempt found them. When its labels change, for example once a person answers a hand-over or makes a skipped issue ready, the next fetch claims it again in the usual order. An issue that is done, or that failed with an error and was not handed over, is not tried again in that run. The run ends when no unseen issue is left. When the fetch fails, the run claims no new issue, ends once the running issues end, writes the summary of the issues it handled and exits with code 1.
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
`next-issue start` runs the work in the worker of the repository, see [Worker](#worker). It connects to the worker, or starts one in the background (`next-issue serve` of the same version, with its output in `.next-issue/worker.log`) when none answers, and then starts the run there with its flags. It prints the output of the run on standard error and the summary on standard output, and exits with the exit code of the run. When the terminal closes or the command ends some other way, the run keeps going in the worker, and pi and `next-issue logs` still show it. pi shows the run in `/next-issue` and the dashboard, and its Stop, Watch, Hunt and Refresh work on it. When the worker is older than `start` and no run is going, it closes and `start` starts a new one. When a run is going in the worker already, `start` exits with code 1 and the error `a run is going in this repository already`, and leaves that run alone: it does not tell an older worker its version, so the worker does not stop a watching run for an upgrade. `start` ignores `--force`, as before.
|
|
38
|
+
|
|
39
|
+
Only one worker goes at a time in a repository. It holds the lock `.next-issue/worker.lock`, with the process id and the start time of that process, for its whole life. A run that the worker starts takes no lock of its own, since the worker runs at most one run at a time. When a process that does not take connections on the worker socket holds the lock, for example a command-line run of an older version, `start` claims no issue and exits with code 1 and the error `another next-issue worker or run (process id <n>) is going in this repository`. A lock whose process is gone, or started at another time, is stale and taken over.
|
|
38
40
|
|
|
39
41
|
Every command uses the main worktree of the repository as its root, the first one that `git worktree list` shows, even when it runs in a linked worktree. So `.next-issue/`, with the config, the locks, the worker socket, the saved state and the notes, and `.worktrees/` are always in the main worktree, and a run or worker started from a linked worktree gets the same refusal as one started from the main worktree.
|
|
40
42
|
|
|
@@ -101,13 +103,13 @@ Each comment that the worker posts on the issue starts with one heading line of
|
|
|
101
103
|
|
|
102
104
|
## Stop a run
|
|
103
105
|
|
|
104
|
-
Press `Ctrl-C` to stop a `next-issue start` run step by step:
|
|
106
|
+
Press `Ctrl-C` to stop a `next-issue start` run step by step. Each press asks the worker to stop the run, as `stop` does (see [Worker](#worker)):
|
|
105
107
|
|
|
106
108
|
1. The first press stops new claims. The harness finishes every issue that it holds and then writes the summary as usual.
|
|
107
109
|
2. The second press stops every running issue after its current round: the implementation or one review round (review, then fix). An issue that lands goes on until its checks on the base branch end. The issue ends as `stopped`. Its state, worktree and labels stay, so the next run carries on from that point.
|
|
108
|
-
3. The third press stops every agent, kills the command that runs and every child of it, so
|
|
110
|
+
3. The third press stops every agent, kills the command that runs and every child of it, so each running issue stops in the middle of a step and ends as `stopped`. The saved state lets the next run continue the issue. A fourth press ends `next-issue start` at once, and the run ends in the worker as after the third. Edits that an agent did not commit stay in the worktree. When the issue resumes in review, the harness drops them before the reviewer starts, so the reviewer sees only the commits of the branch. Files that git ignores, such as build output, stay. When it resumes before the first commit, the implementer runs again and the commit holds them. A review round or a check fix whose reviewer, fixer or read of the failed logs this press kills does not count, so the next run runs that step again with the same budget. A rerun of the failed jobs on the base branch that this press kills before any job restarted runs again in the next run, before any fixer.
|
|
109
111
|
|
|
110
|
-
`SIGTERM` or `SIGHUP` (its terminal or ssh session closes) to
|
|
112
|
+
`SIGTERM` or `SIGHUP` (its terminal or ssh session closes) to `next-issue start` ends only that command; the run goes on in the worker. `SIGTERM`, `SIGHUP` or `SIGINT` to the worker (`next-issue stop` sends `SIGTERM`) act like the third press: they stop the running harness commands and agents, with the commands the agents started, and give back the review round, the check fix and the rerun in the same way.
|
|
111
113
|
|
|
112
114
|
When the reviewer approved the change and the run stopped before its commit reached the base branch, the next run drops those edits and any unfinished merge of the base branch, then lands the change with no new review round. A hand-over of the issue cancels that approval, so the issue resumes in review. A revert of the landed change cancels it too, even when the hand-over after it does not finish.
|
|
113
115
|
|
|
@@ -344,11 +346,11 @@ With `next-issue start --hunt`, which turns on `--watch`, the harness runs a hun
|
|
|
344
346
|
|
|
345
347
|
With `huntCap` at 1 and no ready issue, a hunt files at most one issue. The hunter ends its reply with one `issue:` line for each issue it filed, or one `issue: none` line; a url that is not an issue of the repository of `remote` does not count as filed. When the `remote` url holds an old owner or name that GitHub redirects after a rename or transfer, the harness asks GitHub for the new one, so an issue url with the new name counts too. The run log holds one `hunt.issue` event with the `url` for each, or one with `none`. A hunt that fails writes a `hunt.error` event and the watch goes on. The hunter then waits before the next hunt, with a `hunt.wait` event and the same back-off, so a failure, or a hunt that files nothing, that repeats waits 1, 2, 3, 5, 10, then 10 minutes, and the back-off goes back to 1 minute after a hunt that files at least one issue. The next pass claims the new issues like any other. At the first `Ctrl-C` the hunter stops after its current hunt, and the watch ends once the hunt and the running issues end. A first `Ctrl-C` during the watch wait tells you so while a hunt runs, and the third press stops the hunter at once. The hunter stops too when the watch ends for another reason.
|
|
346
348
|
|
|
347
|
-
In a run of the `serve` worker, a client can switch hunting and watching on or off while the run goes on, with no restart, for example with Hunt on or Watch on in `/next-issue` of the pi extension, see [Worker](#worker). This works
|
|
349
|
+
In a run of the `serve` worker, a client can switch hunting and watching on or off while the run goes on, with no restart, for example with Hunt on or Watch on in `/next-issue` of the pi extension, see [Worker](#worker). This works for every run, since `next-issue start` runs in the worker too.
|
|
348
350
|
|
|
349
351
|
## Worker
|
|
350
352
|
|
|
351
|
-
`next-issue serve` runs one worker for the repository it is run in. The [`@ravshansbox/pi-next-issue`](../pi-next-issue) extension
|
|
353
|
+
`next-issue serve` runs one worker for the repository it is run in. The [`@ravshansbox/pi-next-issue`](../pi-next-issue) extension and `next-issue start` start it and talk to it. `serve` takes no flags apart from `--help` and refuses any other with an error, because runs start from a client, not from `serve`. Each repository has its own worker, so the runs, restarts and upgrades of one repository never affect another. The worker:
|
|
352
354
|
|
|
353
355
|
- listens with WebSocket on the socket file `.next-issue/worker.sock` in the repository. Only your user can open the file. It works on macOS and Linux. When the path of the socket file is too long for the system, about 103 bytes on macOS and 107 on Linux, it exits with an error that says so;
|
|
354
356
|
- holds `.next-issue/worker.lock` in the repository, with its process id and the start time of that process, so only one worker or command-line run goes per repository. A worker that starts while a command-line run holds the lock exits with `another worker is running`. A lock is stale, and is taken over, when no process has that id or the process with that id started at another time, for example because the system gave the id of a worker that was killed or lost in a reboot to another process. A lock without a start time, from an older version, is held while a process with that id is alive, and stale otherwise. A worker that cannot read its own start time exits with an error and takes no lock. When several workers start at once, one takes the lock and the others exit with `another worker is running`. A worker that stops removes the lock file only while it still holds the lock, and the socket file only while it is still the socket it bound;
|
|
@@ -357,12 +359,13 @@ In a run of the `serve` worker, a client can switch hunting and watching on or o
|
|
|
357
359
|
- runs at most one run at a time. A client starts it with `{ "type": "start", "root": "<path>", "args": [...] }`. Each run is like `next-issue start` with those args, or with none when `args` is left out (the pi extension sends none). It takes `--max <n>`, `--watch`, `--hunt`, `--reset`, `--verbose` and `--quiet`, and refuses a command (`start` too), `--json`, `--force`, `--help` or an unknown argument with an `error` message;
|
|
358
360
|
- sends every client the progress of every run when it connects and on every change: for each running issue the issue, the step, the tool count and the last tool, and the handled issues (an issue handled in more than one pass appears once for each);
|
|
359
361
|
- sends every client `{ "type": "output", "root": "<path>", "text": "<lines>" }` for each line of a run that `next-issue start` prints on standard error, as the run prints it and at the level of the run (`--verbose`, `--quiet` or the default). The same lines still go to `.next-issue/worker.log`;
|
|
362
|
+
- sends every client `{ "type": "pass", "root": "<path>", "summary": {...} }` after each pass of a run started with `--watch` or `--hunt` that handled an issue, with the summary of that pass that `next-issue start` prints;
|
|
360
363
|
- sends every client one `{ "type": "end", "root": "<path>", "code": <n>, "summary": {...} }` message when a run ends. `code` is the exit code that `next-issue start` would return for the run, and `summary` is the summary of the whole run that `next-issue start --json` prints (in a watch run, all passes). A run that fails has `code` 1, no `summary`, and the text that `next-issue start` prints after `next-issue failed: ` in `error`;
|
|
361
364
|
- sends every client a `questions` message for each repository: the open issues with the `needsHuman` label, each with its number, title, the reason of the hand-over, the url of the comment that holds it, the `owner/name` repository, and the `needsHuman`, `skip` and `ready` labels. It reads them from GitHub, so the list is the same after a restart. It sends them when a client connects, for its repository, after each issue of a run ends, and when a client sends `{ "type": "questions", "root": "<path>" }`. An issue that no longer has the label leaves the list, and so does an issue whose hand-over has an answer from a writer, the same answer that lets a run claim it. An issue that a person holds with the label stays in the list, even when an earlier hand-over has an answer. An issue with the label but no hand-over comment is in the list with an empty reason and url. The issues are read one at a time. A client never gets an older list after a newer one. When the client adds an `"id"` to its message, the list that answers it carries the same `id` and is read after the message arrived. When the list cannot be read, the client gets `{ "type": "questions-error", "root": "<path>", "message": "<text>" }`;
|
|
362
365
|
- answers `{ "type": "upcoming", "root": "<path>" }` with `{ "type": "upcoming", "root": "<path>", "claimable": [...], "blocked": [...] }`: the open issues with the `ready` label that a run would claim, oldest first, each with its `issue` number and `title`, without issues that have saved state or a label that stops a claim. An issue blocked by open issues is in `blocked` with their references in `blockers`. When the list cannot be read, the client gets `{ "type": "upcoming-error", "root": "<path>", "message": "<text>" }`;
|
|
363
|
-
- stops a run when a client sends `{ "type": "stop", "root": "<path>", "after": "issue" | "round" | "now" }`. With no `after`, it stops after `issue`. `issue` works like a first `Ctrl-C`: no new claim or hunt, and the held issues and a going hunt finish. `round` works like a second `Ctrl-C`: each running issue stops after its current round, with the state kept, and a going hunt ends first. `now` stops every agent, command and hunt of the run at once, and each running issue ends as `stopped` with its state and labels kept; the worker keeps running. A later point than one already asked for changes nothing, and an earlier one replaces it. An unknown `after` gets an `error` message;
|
|
366
|
+
- stops a run when a client sends `{ "type": "stop", "root": "<path>", "after": "issue" | "round" | "now" }`. With no `after`, it stops after `issue`. `issue` works like a first `Ctrl-C`: no new claim or hunt, and the held issues and a going hunt finish. `round` works like a second `Ctrl-C`: each running issue stops after its current round, with the state kept, and a going hunt ends first. `now` stops every agent, command and hunt of the run at once, and each running issue ends as `stopped` with its state and labels kept; the worker keeps running. A later point than one already asked for changes nothing, and an earlier one replaces it. An unknown `after` gets an `error` message. While a run is going, the client that sent the stop gets `{ "type": "stopping", "root": "<path>", "waiting": <bool>, "hunting": <bool> }`, with the `id` of the stop when it has one: `waiting` is `true` when the run waited between passes, and `hunting` is `true` when a hunt was going then. `next-issue start` picks its note from it;
|
|
364
367
|
- switches watching of a run on or off when a client sends `{ "type": "watch", "root": "<path>", "on": true | false }`. The run keeps its issues, worktrees and progress, and nothing restarts. A run started with `--watch` or `--hunt` starts with watching on, and any other run with it off. On keeps the run going after the current pass, as with `--watch`. Off ends the run after the current pass, or at once when it waits between passes, and also turns hunting off. With no run going, the message gets an `error` and starts nothing;
|
|
365
|
-
- switches hunting of a run on or off when a client sends `{ "type": "hunt", "root": "<path>", "on": true | false }`. The run keeps its issues, worktrees and progress, and nothing restarts. Every run of the worker has a hunter: a run started with `--hunt` starts with hunting on, and any other run with it off. Off lets a hunt that is going finish, so the issues it files stay, and starts no new hunt. On wakes the hunter at once, with no back-off wait, and also turns watching on. With no run going, the message gets an `error` and starts nothing. The settings last for the run only: a new run takes them from its own flags.
|
|
368
|
+
- switches hunting of a run on or off when a client sends `{ "type": "hunt", "root": "<path>", "on": true | false }`. The run keeps its issues, worktrees and progress, and nothing restarts. Every run of the worker has a hunter: a run started with `--hunt` starts with hunting on, and any other run with it off. Off lets a hunt that is going finish, so the issues it files stay, and starts no new hunt. On wakes the hunter at once, with no back-off wait, and also turns watching on. With no run going, the message gets an `error` and starts nothing. The settings last for the run only: a new run takes them from its own flags. The progress of each run carries `hunting`, `true` while hunting is on, and `watching`, `true` when the run watches for issues;
|
|
366
369
|
- asks a watching run to check for new issues now when a client sends `{ "type": "refresh", "root": "<path>" }`. A run that waits between passes starts a new pass at once, and its next wait starts again at 1 minute; a waiting hunter checks the ready count again. A refresh while a pass runs, for a run that is stopping or does not watch, or with no run going, changes nothing and gets no answer;
|
|
367
370
|
- exits, and removes its socket and lock files, when no run is going and no client is connected, or when no client connects in the 30 seconds after it starts;
|
|
368
371
|
- waits up to 5 seconds for the lock when a worker that is closing still holds it;
|
|
@@ -370,7 +373,7 @@ In a run of the `serve` worker, a client can switch hunting and watching on or o
|
|
|
370
373
|
- stops every agent and command and exits on `SIGTERM`, `SIGHUP` or `SIGINT`, and removes its socket and lock files;
|
|
371
374
|
- keeps `.next-issue/worker.log`, where the extension writes the output of the worker, within 10 MiB: when it starts and once a minute while it runs, it cuts a log of more than 10 MiB to its newest 5 MiB, from the start of a line. It skips the cut while a command that writes to the log runs. The run logs under `.next-issue/runs/` hold the same events.
|
|
372
375
|
|
|
373
|
-
Stop the worker of a repository with `next-issue stop` in that repository. Workers of other repositories carry on. This sends `SIGTERM` to the process in the lock file when the lock is not stale and the worker socket file takes a connection, so the worker stops at once and a run that is going resumes from its saved state next time. When no worker is running, the command says so and exits with 0. When a command-line run holds the lock, the command sends no signal, says `no worker is running; a command-line run (process id <n>) is going, press Ctrl-C there to stop it` and exits with 0.
|
|
376
|
+
Stop the worker of a repository with `next-issue stop` in that repository. Workers of other repositories carry on. This sends `SIGTERM` to the process in the lock file when the lock is not stale and the worker socket file takes a connection, so the worker stops at once and a run that is going resumes from its saved state next time. When no worker is running, the command says so and exits with 0. When a command-line run of an older version holds the lock, the command sends no signal, says `no worker is running; a command-line run (process id <n>) is going, press Ctrl-C there to stop it` and exits with 0.
|
|
374
377
|
|
|
375
378
|
Versions before one worker per repository ran one worker for the machine, with `next-issue.sock`, `next-issue.lock` and `next-issue.roots.json` in `~/.pi/agent`, or in the folder that `PI_CODING_AGENT_DIR` names. That worker does not stop by itself, `next-issue stop` does not reach it, and a run in it can claim issues alongside the new worker of the same repository. Stop it once after you upgrade:
|
|
376
379
|
|
package/dist/cli.mjs
CHANGED
|
@@ -3,16 +3,13 @@ import { parseArgs, USAGE } from './args.mjs';
|
|
|
3
3
|
import { moveOldConfig, rejectOldConfig, writeDefaultConfig } from './config.mjs';
|
|
4
4
|
import { OLD_CONFIG_FILE } from './config-fields.mjs';
|
|
5
5
|
import { message } from './errors.mjs';
|
|
6
|
-
import { prettyJson } from './files.mjs';
|
|
7
6
|
import { repoRoot } from './git.mjs';
|
|
8
7
|
import { ensureExcluded, excludeFile, removeIgnored } from './ignore.mjs';
|
|
9
|
-
import { listenForInterrupts } from './interrupt.mjs';
|
|
10
8
|
import { stopWorker } from './lock.mjs';
|
|
11
9
|
import { followLog, newestRunLog } from './logs.mjs';
|
|
12
|
-
import { runLocked } from './run.mjs';
|
|
13
10
|
import { serve } from './serve.mjs';
|
|
11
|
+
import { startInWorker } from './start.mjs';
|
|
14
12
|
import { STATE_DIR } from './state.mjs';
|
|
15
|
-
import { formatSummary } from './summary.mjs';
|
|
16
13
|
import { workerPaths } from './worker-paths.mjs';
|
|
17
14
|
import { WORKTREE_DIR } from './worktree.mjs';
|
|
18
15
|
function write(stream, text) {
|
|
@@ -21,7 +18,8 @@ function write(stream, text) {
|
|
|
21
18
|
});
|
|
22
19
|
}
|
|
23
20
|
async function main() {
|
|
24
|
-
const
|
|
21
|
+
const argv = process.argv.slice(2);
|
|
22
|
+
const args = parseArgs(argv);
|
|
25
23
|
if (args.help) {
|
|
26
24
|
await write(process.stdout, USAGE);
|
|
27
25
|
return 0;
|
|
@@ -45,15 +43,11 @@ async function main() {
|
|
|
45
43
|
if (args.command === 'serve') {
|
|
46
44
|
return serve(root);
|
|
47
45
|
}
|
|
48
|
-
return run(root, args);
|
|
46
|
+
return run(root, args, argv);
|
|
49
47
|
}
|
|
50
|
-
function run(root, args) {
|
|
48
|
+
function run(root, args, argv) {
|
|
51
49
|
process.stderr.on('error', () => { });
|
|
52
|
-
|
|
53
|
-
return runLocked(root, args, {
|
|
54
|
-
interrupts,
|
|
55
|
-
print: (summary) => write(process.stdout, args.json ? prettyJson(summary) : formatSummary(summary)),
|
|
56
|
-
});
|
|
50
|
+
return startInWorker(root, args, argv.slice(1).filter((arg) => arg !== '--json' && arg !== '--force'));
|
|
57
51
|
}
|
|
58
52
|
const STOPPED = {
|
|
59
53
|
none: () => 'next-issue: no worker is running\n',
|
package/dist/interrupt.mjs
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { stopChildren } from './children.mjs';
|
|
2
2
|
import { wakeableSleep } from './durations.mjs';
|
|
3
|
-
export
|
|
3
|
+
export const NOTES = {
|
|
4
|
+
hunt: 'Stopping after the current hunt. Press Ctrl-C twice more to stop now.',
|
|
5
|
+
wait: 'Stopping now.',
|
|
6
|
+
issue: 'Stopping after the running issues. Press Ctrl-C again to stop after the current round.',
|
|
7
|
+
round: 'Stopping after the current round. Press Ctrl-C again to stop now.',
|
|
8
|
+
};
|
|
9
|
+
export function onPresses(handle) {
|
|
4
10
|
let presses = 0;
|
|
5
11
|
let lastPress = -Infinity;
|
|
6
|
-
const sleeper = {};
|
|
7
|
-
let hunting = () => false;
|
|
8
12
|
const press = () => {
|
|
9
13
|
const now = performance.now();
|
|
10
14
|
if (now - lastPress < 100) {
|
|
@@ -12,24 +16,34 @@ export function listenForInterrupts(say) {
|
|
|
12
16
|
}
|
|
13
17
|
lastPress = now;
|
|
14
18
|
presses += 1;
|
|
19
|
+
if (presses >= 3) {
|
|
20
|
+
process.off('SIGINT', press);
|
|
21
|
+
}
|
|
22
|
+
handle(presses);
|
|
23
|
+
};
|
|
24
|
+
process.on('SIGINT', press);
|
|
25
|
+
return () => process.off('SIGINT', press);
|
|
26
|
+
}
|
|
27
|
+
export function listenForInterrupts(say) {
|
|
28
|
+
let presses = 0;
|
|
29
|
+
const sleeper = {};
|
|
30
|
+
let hunting = () => false;
|
|
31
|
+
onPresses((count) => {
|
|
32
|
+
presses = count;
|
|
15
33
|
if (presses === 1 && sleeper.wake !== undefined) {
|
|
16
|
-
say(hunting()
|
|
17
|
-
? 'Stopping after the current hunt. Press Ctrl-C twice more to stop now.'
|
|
18
|
-
: 'Stopping now.');
|
|
34
|
+
say(hunting() ? NOTES.hunt : NOTES.wait);
|
|
19
35
|
sleeper.wake();
|
|
20
36
|
}
|
|
21
37
|
else if (presses === 1) {
|
|
22
|
-
say(
|
|
38
|
+
say(NOTES.issue);
|
|
23
39
|
}
|
|
24
40
|
else if (presses === 2) {
|
|
25
|
-
say(
|
|
41
|
+
say(NOTES.round);
|
|
26
42
|
}
|
|
27
43
|
else {
|
|
28
|
-
process.off('SIGINT', press);
|
|
29
44
|
stopChildren('SIGINT');
|
|
30
45
|
}
|
|
31
|
-
};
|
|
32
|
-
process.on('SIGINT', press);
|
|
46
|
+
});
|
|
33
47
|
return {
|
|
34
48
|
afterIssue: () => presses >= 1,
|
|
35
49
|
afterRound: () => presses >= 2,
|
package/dist/serve.mjs
CHANGED
|
@@ -15,7 +15,7 @@ import { listUpcoming, sendUpcoming } from './upcoming.mjs';
|
|
|
15
15
|
import { packageVersion } from './version.mjs';
|
|
16
16
|
import { keepLogTrimmed, LOG_CHECK_MS } from './worker-log.mjs';
|
|
17
17
|
import { checkSocketPath, workerPaths } from './worker-paths.mjs';
|
|
18
|
-
import { active, dropUpgrade, endStatus, newRun, requestRefresh, requestStop, requestUpgrade, runControls, runsMessage, setHunting, setWatching, startRefusal, stopPoint, switchRefusal, } from './worker-run.mjs';
|
|
18
|
+
import { active, dropUpgrade, endStatus, newRun, requestRefresh, requestStop, requestUpgrade, runControls, runsMessage, setHunting, setWatching, startRefusal, stoppingMessage, stopPoint, switchRefusal, } from './worker-run.mjs';
|
|
19
19
|
import { broadcast, closer, fileId, listen, parseMessage, removeOwned, sendAll, shutdown, } from './worker-server.mjs';
|
|
20
20
|
const LOCK_WAIT_MS = 5_000;
|
|
21
21
|
const IDLE_MS = 30_000;
|
|
@@ -82,6 +82,9 @@ export async function startWorker(options) {
|
|
|
82
82
|
let summary;
|
|
83
83
|
withRunChildren(run.children, () => options.runRepo(root, args, {
|
|
84
84
|
...runControls(run, update, () => outsideRuns(() => void sendQuestions(root, () => sockets.clients))),
|
|
85
|
+
print: args.watch
|
|
86
|
+
? async (pass) => sendAll(sockets, { type: 'pass', root, summary: pass })
|
|
87
|
+
: undefined,
|
|
85
88
|
output: (text) => {
|
|
86
89
|
process.stderr.write(text);
|
|
87
90
|
sendAll(sockets, { type: 'output', root, text });
|
|
@@ -104,13 +107,16 @@ export async function startWorker(options) {
|
|
|
104
107
|
})
|
|
105
108
|
.finally(closeWhenIdle);
|
|
106
109
|
};
|
|
107
|
-
const stop = (refuse, root, after) => {
|
|
110
|
+
const stop = (socket, refuse, root, after, id) => {
|
|
108
111
|
const point = stopPoint(after);
|
|
109
112
|
if (point === undefined) {
|
|
110
113
|
refuse(`stop does not take --after ${String(after)}`);
|
|
111
114
|
return;
|
|
112
115
|
}
|
|
113
116
|
const run = runs.get(root);
|
|
117
|
+
if (run !== undefined && active(run)) {
|
|
118
|
+
send(socket, stoppingMessage(root, run, id));
|
|
119
|
+
}
|
|
114
120
|
const dropped = dropUpgrade(run);
|
|
115
121
|
if (requestStop(run, point) || dropped) {
|
|
116
122
|
broadcast(sockets, runs);
|
|
@@ -145,7 +151,7 @@ export async function startWorker(options) {
|
|
|
145
151
|
start(refuse, value.root, value.args ?? [], value.resume === true);
|
|
146
152
|
}
|
|
147
153
|
else if (value.type === 'stop') {
|
|
148
|
-
stop(refuse, value.root, value.after);
|
|
154
|
+
stop(socket, refuse, value.root, value.after, value.id);
|
|
149
155
|
}
|
|
150
156
|
else if (value.type === 'hunt') {
|
|
151
157
|
toggle(refuse, value.root, value.on, setHunting);
|
package/dist/start.mjs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { prettyJson } from './files.mjs';
|
|
3
|
+
import { NOTES, onPresses } from './interrupt.mjs';
|
|
4
|
+
import { formatSummary } from './summary.mjs';
|
|
5
|
+
import { packageVersion } from './version.mjs';
|
|
6
|
+
import { CLOSED, going, reachWorker } from './worker-client.mjs';
|
|
7
|
+
const RUN_GOING = 'a run is going in this repository already';
|
|
8
|
+
function output() {
|
|
9
|
+
let queue = Promise.resolve();
|
|
10
|
+
return {
|
|
11
|
+
print: (stream, text) => {
|
|
12
|
+
queue = queue.then(() => new Promise((resolve) => stream.write(text, () => resolve())));
|
|
13
|
+
},
|
|
14
|
+
flushed: () => queue,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function firstNote(message) {
|
|
18
|
+
if (!message.waiting) {
|
|
19
|
+
return NOTES.issue;
|
|
20
|
+
}
|
|
21
|
+
return message.hunting ? NOTES.hunt : NOTES.wait;
|
|
22
|
+
}
|
|
23
|
+
export async function startInWorker(root, args, argv) {
|
|
24
|
+
const connection = await reachWorker(root, await packageVersion());
|
|
25
|
+
if (going(connection.runs[root])) {
|
|
26
|
+
connection.close();
|
|
27
|
+
throw new Error(RUN_GOING);
|
|
28
|
+
}
|
|
29
|
+
const out = output();
|
|
30
|
+
const code = await follow(connection, root, args, argv, out);
|
|
31
|
+
connection.close();
|
|
32
|
+
await out.flushed();
|
|
33
|
+
return code;
|
|
34
|
+
}
|
|
35
|
+
function follow(connection, root, args, argv, out) {
|
|
36
|
+
const id = randomUUID();
|
|
37
|
+
const stopId = randomUUID();
|
|
38
|
+
const summaryText = (summary) => args.json ? prettyJson(summary) : formatSummary(summary);
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
const stopListening = onPresses((presses) => {
|
|
41
|
+
if (presses === 1) {
|
|
42
|
+
connection.send({ type: 'stop', root, after: 'issue', id: stopId });
|
|
43
|
+
}
|
|
44
|
+
else if (presses === 2) {
|
|
45
|
+
out.print(process.stderr, `${NOTES.round}\n`);
|
|
46
|
+
connection.send({ type: 'stop', root, after: 'round' });
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
connection.send({ type: 'stop', root, after: 'now' });
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
let settled = false;
|
|
53
|
+
const settle = (code) => {
|
|
54
|
+
if (!settled) {
|
|
55
|
+
settled = true;
|
|
56
|
+
stopListening();
|
|
57
|
+
resolve(code);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const fail = (text) => {
|
|
61
|
+
if (!settled) {
|
|
62
|
+
out.print(process.stderr, `next-issue failed: ${text}\n`);
|
|
63
|
+
}
|
|
64
|
+
settle(1);
|
|
65
|
+
};
|
|
66
|
+
connection.listen((message) => {
|
|
67
|
+
if (!('root' in message) || message.root !== root) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (message.type === 'output') {
|
|
71
|
+
out.print(process.stderr, message.text);
|
|
72
|
+
}
|
|
73
|
+
else if (message.type === 'pass') {
|
|
74
|
+
out.print(process.stdout, summaryText(message.summary));
|
|
75
|
+
}
|
|
76
|
+
else if (message.type === 'stopping' && message.id === stopId) {
|
|
77
|
+
out.print(process.stderr, `${firstNote(message)}\n`);
|
|
78
|
+
}
|
|
79
|
+
else if (message.type === 'error' && message.id === id) {
|
|
80
|
+
fail(message.message);
|
|
81
|
+
}
|
|
82
|
+
else if (message.type === 'end' && message.error !== undefined) {
|
|
83
|
+
fail(message.error);
|
|
84
|
+
}
|
|
85
|
+
else if (message.type === 'end') {
|
|
86
|
+
if (!args.watch && message.summary !== undefined) {
|
|
87
|
+
out.print(process.stdout, summaryText(message.summary));
|
|
88
|
+
}
|
|
89
|
+
settle(message.code);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
void connection.closed.then(() => fail(CLOSED));
|
|
93
|
+
connection.send({ type: 'start', root, args: argv, id });
|
|
94
|
+
});
|
|
95
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { closeSync, mkdirSync, openSync } from 'node:fs';
|
|
3
|
+
import { createConnection } from 'node:net';
|
|
4
|
+
import { dirname } from 'node:path';
|
|
5
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
6
|
+
import { WebSocket } from 'ws';
|
|
7
|
+
import { lockHolder } from './lock.mjs';
|
|
8
|
+
import { isNewer } from './protocol.mjs';
|
|
9
|
+
import { checkSocketPath, workerPaths } from './worker-paths.mjs';
|
|
10
|
+
const ANSWER_TIMEOUT_MS = 5_000;
|
|
11
|
+
const START_TIMEOUT_MS = 30_000;
|
|
12
|
+
const HOLDER_GRACE_MS = 2_000;
|
|
13
|
+
const POLL_MS = 200;
|
|
14
|
+
const UPGRADE_TRIES = 3;
|
|
15
|
+
export const CLOSED = 'the worker closed the connection';
|
|
16
|
+
class SilentWorker extends Error {
|
|
17
|
+
constructor() {
|
|
18
|
+
super('the next-issue worker did not answer');
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function going(progress) {
|
|
22
|
+
return progress?.status === 'running' || progress?.status === 'stopping';
|
|
23
|
+
}
|
|
24
|
+
function runGoing(pid) {
|
|
25
|
+
return new Error(`another next-issue worker or run (process id ${pid}) is going in this repository`);
|
|
26
|
+
}
|
|
27
|
+
function parse(text) {
|
|
28
|
+
try {
|
|
29
|
+
return JSON.parse(text);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function open(socketPath, version) {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
const socket = new WebSocket('ws://localhost/', {
|
|
38
|
+
createConnection: () => createConnection(socketPath),
|
|
39
|
+
});
|
|
40
|
+
const closed = new Promise((done) => socket.once('close', () => done()));
|
|
41
|
+
const queued = [];
|
|
42
|
+
let listener;
|
|
43
|
+
let workerVersion = '0.0.0';
|
|
44
|
+
let ready = false;
|
|
45
|
+
const timer = setTimeout(() => {
|
|
46
|
+
socket.terminate();
|
|
47
|
+
reject(new SilentWorker());
|
|
48
|
+
}, ANSWER_TIMEOUT_MS);
|
|
49
|
+
const fail = (error) => {
|
|
50
|
+
if (!ready) {
|
|
51
|
+
clearTimeout(timer);
|
|
52
|
+
reject(error);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
socket.on('error', fail);
|
|
56
|
+
socket.once('close', () => fail(new Error(CLOSED)));
|
|
57
|
+
socket.on('message', (data) => {
|
|
58
|
+
const message = parse(String(data));
|
|
59
|
+
if (message === undefined) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (ready) {
|
|
63
|
+
if (listener === undefined) {
|
|
64
|
+
queued.push(message);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
listener(message);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (message.type === 'hello') {
|
|
71
|
+
workerVersion = message.version;
|
|
72
|
+
}
|
|
73
|
+
else if (message.type === 'runs') {
|
|
74
|
+
ready = true;
|
|
75
|
+
clearTimeout(timer);
|
|
76
|
+
if (!Object.values(message.runs).some(going)) {
|
|
77
|
+
socket.send(JSON.stringify({ type: 'hello', version }));
|
|
78
|
+
}
|
|
79
|
+
resolve({
|
|
80
|
+
version: workerVersion,
|
|
81
|
+
runs: message.runs,
|
|
82
|
+
send: (value) => socket.send(JSON.stringify(value)),
|
|
83
|
+
listen: (next) => {
|
|
84
|
+
listener = next;
|
|
85
|
+
queued.splice(0).forEach(next);
|
|
86
|
+
},
|
|
87
|
+
closed,
|
|
88
|
+
close: () => socket.terminate(),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
async function tryOpen(socketPath, version) {
|
|
95
|
+
try {
|
|
96
|
+
return await open(socketPath, version);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
if (error instanceof SilentWorker) {
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async function waitForHolder(paths, version) {
|
|
106
|
+
const deadline = Date.now() + HOLDER_GRACE_MS;
|
|
107
|
+
for (;;) {
|
|
108
|
+
const pid = await lockHolder(paths.lockPath);
|
|
109
|
+
if (pid === undefined) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
if (Date.now() >= deadline) {
|
|
113
|
+
throw runGoing(pid);
|
|
114
|
+
}
|
|
115
|
+
await delay(POLL_MS);
|
|
116
|
+
const opened = await tryOpen(paths.socketPath, version);
|
|
117
|
+
if (opened !== undefined) {
|
|
118
|
+
return opened;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async function waitForStart(paths, version, exited) {
|
|
123
|
+
const deadline = Date.now() + START_TIMEOUT_MS;
|
|
124
|
+
for (;;) {
|
|
125
|
+
await delay(POLL_MS);
|
|
126
|
+
const reason = exited();
|
|
127
|
+
const opened = await tryOpen(paths.socketPath, version);
|
|
128
|
+
if (opened !== undefined) {
|
|
129
|
+
return opened;
|
|
130
|
+
}
|
|
131
|
+
if (reason !== undefined) {
|
|
132
|
+
const pid = await lockHolder(paths.lockPath);
|
|
133
|
+
throw pid === undefined
|
|
134
|
+
? new Error(`the next-issue worker exited before it listened: ${reason}`)
|
|
135
|
+
: runGoing(pid);
|
|
136
|
+
}
|
|
137
|
+
if (Date.now() >= deadline) {
|
|
138
|
+
throw new Error('the next-issue worker did not start (see .next-issue/worker.log)');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async function launch(root, version) {
|
|
143
|
+
const paths = workerPaths(root);
|
|
144
|
+
const held = await waitForHolder(paths, version);
|
|
145
|
+
if (held !== undefined) {
|
|
146
|
+
return held;
|
|
147
|
+
}
|
|
148
|
+
let exited;
|
|
149
|
+
spawnServe(root, (reason) => {
|
|
150
|
+
exited ??= reason;
|
|
151
|
+
});
|
|
152
|
+
return waitForStart(paths, version, () => exited);
|
|
153
|
+
}
|
|
154
|
+
export async function reachWorker(root, version) {
|
|
155
|
+
const { socketPath } = workerPaths(root);
|
|
156
|
+
checkSocketPath(socketPath);
|
|
157
|
+
for (let tries = 1;; tries += 1) {
|
|
158
|
+
const found = (await tryOpen(socketPath, version)) ?? (await launch(root, version));
|
|
159
|
+
const busy = Object.values(found.runs).some(going);
|
|
160
|
+
if (!isNewer(version, found.version) || busy || tries >= UPGRADE_TRIES) {
|
|
161
|
+
return found;
|
|
162
|
+
}
|
|
163
|
+
await Promise.race([found.closed, delay(ANSWER_TIMEOUT_MS)]);
|
|
164
|
+
found.close();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function exitText(code, signal) {
|
|
168
|
+
return signal === null ? `exit code ${code}` : `signal ${signal}`;
|
|
169
|
+
}
|
|
170
|
+
function spawnServe(root, onExit) {
|
|
171
|
+
const { logPath } = workerPaths(root);
|
|
172
|
+
mkdirSync(dirname(logPath), { recursive: true });
|
|
173
|
+
const log = openSync(logPath, 'a');
|
|
174
|
+
try {
|
|
175
|
+
const child = spawn(process.execPath, [...process.execArgv, process.argv[1], 'serve'], {
|
|
176
|
+
cwd: root,
|
|
177
|
+
detached: true,
|
|
178
|
+
stdio: ['ignore', log, log],
|
|
179
|
+
});
|
|
180
|
+
child.on('error', (error) => onExit(`the worker failed to start: ${error.message}`));
|
|
181
|
+
child.on('exit', (code, signal) => {
|
|
182
|
+
onExit(`${exitText(code, signal)} (see .next-issue/worker.log)`);
|
|
183
|
+
});
|
|
184
|
+
child.unref();
|
|
185
|
+
}
|
|
186
|
+
finally {
|
|
187
|
+
closeSync(log);
|
|
188
|
+
}
|
|
189
|
+
}
|
package/dist/worker-run.mjs
CHANGED
|
@@ -58,6 +58,16 @@ export function requestStop(run, after) {
|
|
|
58
58
|
run.progress = { ...run.progress, status: 'stopping' };
|
|
59
59
|
return true;
|
|
60
60
|
}
|
|
61
|
+
export function stoppingMessage(root, run, id) {
|
|
62
|
+
const waiting = run.wake !== undefined;
|
|
63
|
+
return {
|
|
64
|
+
type: 'stopping',
|
|
65
|
+
root,
|
|
66
|
+
waiting,
|
|
67
|
+
hunting: waiting && run.huntBusy?.() === true,
|
|
68
|
+
...(id === undefined ? {} : { id }),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
61
71
|
export function requestUpgrade(run, upgrade) {
|
|
62
72
|
if (!run.watching.on() || run.stop !== undefined || !requestStop(run, 'issue')) {
|
|
63
73
|
return false;
|
|
@@ -102,7 +112,10 @@ function runInterrupts(run) {
|
|
|
102
112
|
return {
|
|
103
113
|
afterIssue: () => run.stop !== undefined,
|
|
104
114
|
afterRound: () => run.stop === 'round' || run.stop === 'now',
|
|
105
|
-
sleep: (ms,
|
|
115
|
+
sleep: (ms, hunting, refreshed) => {
|
|
116
|
+
run.huntBusy = hunting;
|
|
117
|
+
return wakeableSleep(run, ms, run.stop !== undefined, refreshed);
|
|
118
|
+
},
|
|
106
119
|
};
|
|
107
120
|
}
|
|
108
121
|
export function runControls(run, update, issueEnded) {
|