next-issue 13.0.0 → 13.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 +4 -3
- package/dist/args.mjs +1 -1
- package/dist/serve.mjs +47 -8
- package/dist/worker-server.mjs +20 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -297,7 +297,7 @@ To drop an issue instead, close it. The next run that finds a saved state for an
|
|
|
297
297
|
| `watch on\|off` | Switch watching of the going run, see [Watch for new issues](#watch-for-new-issues) |
|
|
298
298
|
| `hunt on\|off` | Switch hunting of the going run, see [Hunt for bugs](#hunt-for-bugs) |
|
|
299
299
|
| `init` | Write the config file with the defaults |
|
|
300
|
-
| `serve` | Run the worker of this repository that
|
|
300
|
+
| `serve` | Run the worker of this repository that the clients talk to, see [Worker](#worker) |
|
|
301
301
|
| `stop` | Stop the worker of this repository, see [Worker](#worker) |
|
|
302
302
|
| `logs` | Show the latest run log and follow it, see [Observability](#observability) |
|
|
303
303
|
| `--force` | Replace an existing config file, with `init` |
|
|
@@ -352,9 +352,10 @@ In a run of the `serve` worker, a client can switch hunting and watching on or o
|
|
|
352
352
|
|
|
353
353
|
## Worker
|
|
354
354
|
|
|
355
|
-
`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:
|
|
355
|
+
`next-issue serve` runs one worker for the repository it is run in. The [`@ravshansbox/pi-next-issue`](../pi-next-issue) extension, the [Claude Code plugin](../claude-next-issue) 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:
|
|
356
356
|
|
|
357
357
|
- 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;
|
|
358
|
+
- also answers plain HTTP on the same socket file, for a client with no WebSocket. `GET /` answers `[{ "type": "hello", ... }, { "type": "runs", ... }]`. `POST /` takes one client message as its JSON body, such as `{ "type": "start", "root": "<path>" }`, and answers a JSON list of the messages the worker sends that client in reply (an `error`, `stopping`, `questions` or `upcoming` message), then the `runs` message after the change. A body that is not a client message gets status 400 and `[{ "type": "error", "root": "<path>", "message": "the request is not a next-issue message" }]`; another path gets 404 and another method 405. An HTTP client does not get the `output`, `pass` and `end` messages, so it polls `GET /` for the progress;
|
|
358
359
|
- holds `.next-issue/worker.lock` in the repository, with its process id and the start time of that process, so only one worker goes per repository. A worker that starts while another process 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;
|
|
359
360
|
- refuses a `start`, `stop`, `watch`, `hunt`, `questions` or `upcoming` message whose `root` is not the exact path of its repository with an `error` message, and starts or stops nothing;
|
|
360
361
|
- sends each `error` as `{ "type": "error", "root": "<path>", "message": "<text>" }`. When a `start`, `stop`, `watch`, `hunt` or `refresh` message carries an `"id"`, the `error` that refuses it carries the same `id`;
|
|
@@ -369,7 +370,7 @@ In a run of the `serve` worker, a client can switch hunting and watching on or o
|
|
|
369
370
|
- 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 starts with watching on, unless it was started with `--watch off`. On keeps the run going after the current pass, as with `--watch on`. On for a run that a person asked to stop gets an `error` (`the run is stopping`, with the `id` of the request) and changes nothing; on for a run that stops for an upgrade keeps the upgrade, so the run is still resumed. 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;
|
|
370
371
|
- 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 on` 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. On for a run that a person asked to stop gets the same `error` as watch 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;
|
|
371
372
|
- 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 refresh during the last read of the open issues of a pass, or while the pass ends, also starts a new pass at once; a waiting hunter checks the ready count again. While a pass runs, the run reads the open issues again, so its open-issue count is current, and claims a new issue only when a slot is free. A refresh for a run that is stopping or does not watch, or with no run going, changes nothing and gets no answer;
|
|
372
|
-
- exits, and removes its socket and lock files, when no run is going
|
|
373
|
+
- exits, and removes its socket and lock files, when no run is going, no client is connected and no HTTP request came in the last 30 seconds, or when no client connects in the 30 seconds after it starts;
|
|
373
374
|
- waits up to 5 seconds for the lock when a worker that is closing still holds it;
|
|
374
375
|
- closes once no run is going when a client with a newer version connects, so that the client can start the newer worker. The `hello` of a client can say how to start the worker of its version, as `"launch": { "command": "<command>", "args": ["<arg>", ...] }`: pi sends `npx -y next-issue@<version> serve`, and `next-issue start` its own `node <script> serve`. A run that watches is stopped between issues for that, as `stop` with `issue` does: it claims nothing new, and its running issues and its current hunt finish. Its progress then holds `"upgrade": { "from": "<worker version>", "to": "<client version>" }`, with `"resumes": true` when the first newer client gave a `launch`. A stop by a person, or turning watching off, drops that field, so the run does not start again. With `resumes`, once the run ends, the worker stops listening, runs that command in the repository with its output in `.next-issue/worker.log` (or uses a worker that already listens there), waits up to 2 minutes for it to listen, as pi does, since `npx` may first download it, and asks the newer worker to start the run again with `--watch on` and the same `--hunt` setting, before it closes. This works with no client connected. When the newer worker does not start or does not start the run, the worker writes the reason to `.next-issue/worker.log`, and its clients see the run `failed` with an `error` that starts with `the worker could not start next-issue <version>`. Without `resumes`, a client starts the newer worker and the run again. A run that does not watch is left alone; a run that `next-issue start` started is not, and `start` follows it to the newer worker. Meanwhile the worker refuses every start with `the worker is restarting, try again soon`. A `start` with `"resume": true` gets no error when the worker refuses it;
|
|
375
376
|
- stops every agent and command and exits on `SIGTERM`, `SIGHUP` or `SIGINT`, and removes its socket and lock files;
|
package/dist/args.mjs
CHANGED
|
@@ -16,7 +16,7 @@ export const USAGE = `next-issue <command> [options]
|
|
|
16
16
|
watch on|off switch watching of the going run; off also turns hunting off
|
|
17
17
|
hunt on|off switch hunting of the going run; on also turns watching on
|
|
18
18
|
init write ${CONFIG_FILE} with the defaults and add .next-issue/ and .worktrees/ to info/exclude
|
|
19
|
-
serve run the worker of this repository that
|
|
19
|
+
serve run the worker of this repository that the clients talk to
|
|
20
20
|
stop stop the worker of this repository
|
|
21
21
|
logs show the latest run log and follow it
|
|
22
22
|
|
package/dist/serve.mjs
CHANGED
|
@@ -8,8 +8,8 @@ import { stopChildren, withRunChildren } from './children.mjs';
|
|
|
8
8
|
import { message } from './errors.mjs';
|
|
9
9
|
import { lock } from './lock.mjs';
|
|
10
10
|
import { startProgress } from './progress.mjs';
|
|
11
|
-
import { isNewer } from './protocol.mjs';
|
|
12
|
-
import { listQuestions, questionSender, send } from './questions.mjs';
|
|
11
|
+
import { isNewer, } from './protocol.mjs';
|
|
12
|
+
import { listQuestions, questionSender, send, } from './questions.mjs';
|
|
13
13
|
import { runRepo } from './run.mjs';
|
|
14
14
|
import { listUpcoming, sendUpcoming } from './upcoming.mjs';
|
|
15
15
|
import { packageVersion } from './version.mjs';
|
|
@@ -17,7 +17,7 @@ import { resumeOnSuccessor } from './worker-client.mjs';
|
|
|
17
17
|
import { keepLogTrimmed, LOG_CHECK_MS } from './worker-log.mjs';
|
|
18
18
|
import { checkSocketPath, workerPaths } from './worker-paths.mjs';
|
|
19
19
|
import { active, dropUpgrade, endStatus, newRun, requestRefresh, requestStop, requestUpgrade, runControls, runsMessage, setHunting, setWatching, startRefusal, stoppingMessage, stopPoint, switchRefusal, } from './worker-run.mjs';
|
|
20
|
-
import { broadcast, closer, fileId, isLaunch, listen, parseMessage, removeOwned, sendAll, shutdown, stopListening, } from './worker-server.mjs';
|
|
20
|
+
import { broadcast, closer, fileId, isLaunch, listen, parseMessage, readBody, removeOwned, reply, sendAll, shutdown, stopListening, } from './worker-server.mjs';
|
|
21
21
|
const LOCK_WAIT_MS = 5_000;
|
|
22
22
|
const IDLE_MS = 30_000;
|
|
23
23
|
export async function startWorker(options) {
|
|
@@ -44,6 +44,9 @@ export async function startWorker(options) {
|
|
|
44
44
|
const outsideRuns = AsyncLocalStorage.snapshot();
|
|
45
45
|
const busy = () => [...runs.values()].some(active);
|
|
46
46
|
let idleTimer;
|
|
47
|
+
const idleMs = options.idleMs ?? IDLE_MS;
|
|
48
|
+
let polledAt = Number.NEGATIVE_INFINITY;
|
|
49
|
+
const polled = () => Date.now() - polledAt < idleMs;
|
|
47
50
|
const handOver = async (how, run) => {
|
|
48
51
|
const listening = stopListening(server);
|
|
49
52
|
removeOwned(owned);
|
|
@@ -69,7 +72,7 @@ export async function startWorker(options) {
|
|
|
69
72
|
}
|
|
70
73
|
});
|
|
71
74
|
const closeWhenIdle = () => {
|
|
72
|
-
if ((restart !== undefined || sockets.clients.size === 0) && !busy()) {
|
|
75
|
+
if ((restart !== undefined || (sockets.clients.size === 0 && !polled())) && !busy()) {
|
|
73
76
|
void close();
|
|
74
77
|
}
|
|
75
78
|
};
|
|
@@ -200,11 +203,12 @@ export async function startWorker(options) {
|
|
|
200
203
|
requestRefresh(runs.get(value.root));
|
|
201
204
|
}
|
|
202
205
|
else if (value.type === 'questions') {
|
|
203
|
-
|
|
206
|
+
return sendQuestions(value.root, () => [socket], value.id);
|
|
204
207
|
}
|
|
205
208
|
else if (value.type === 'upcoming') {
|
|
206
|
-
|
|
209
|
+
return sendUpcoming(options.upcoming ?? listUpcoming, socket, value.root);
|
|
207
210
|
}
|
|
211
|
+
return undefined;
|
|
208
212
|
};
|
|
209
213
|
sockets.on('connection', (socket) => {
|
|
210
214
|
send(socket, { type: 'hello', version: options.version });
|
|
@@ -219,19 +223,54 @@ export async function startWorker(options) {
|
|
|
219
223
|
}
|
|
220
224
|
const refuse = refuser(socket, value, options.root);
|
|
221
225
|
try {
|
|
222
|
-
receive(socket, value, refuse);
|
|
226
|
+
void receive(socket, value, refuse);
|
|
223
227
|
}
|
|
224
228
|
catch (error) {
|
|
225
229
|
refuse(message(error));
|
|
226
230
|
}
|
|
227
231
|
});
|
|
228
232
|
});
|
|
233
|
+
const answer = async (request, response) => {
|
|
234
|
+
polledAt = Date.now();
|
|
235
|
+
clearTimeout(idleTimer);
|
|
236
|
+
idleTimer = setTimeout(closeWhenIdle, idleMs);
|
|
237
|
+
const refusal = (text) => [
|
|
238
|
+
{ type: 'error', root: options.root, message: text },
|
|
239
|
+
];
|
|
240
|
+
if (request.url !== '/') {
|
|
241
|
+
reply(response, 404, refusal('the worker answers on / only'));
|
|
242
|
+
}
|
|
243
|
+
else if (request.method === 'GET') {
|
|
244
|
+
reply(response, 200, [{ type: 'hello', version: options.version }, runsMessage(runs)]);
|
|
245
|
+
}
|
|
246
|
+
else if (request.method !== 'POST') {
|
|
247
|
+
reply(response, 405, refusal('the worker answers GET and POST only'));
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
const value = parseMessage(await readBody(request));
|
|
251
|
+
if (value === undefined) {
|
|
252
|
+
reply(response, 400, refusal('the request is not a next-issue message'));
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
const replies = [];
|
|
256
|
+
const peer = { send: (data) => replies.push(JSON.parse(data)) };
|
|
257
|
+
const refuse = refuser(peer, value, options.root);
|
|
258
|
+
try {
|
|
259
|
+
await receive(peer, value, refuse);
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
refuse(message(error));
|
|
263
|
+
}
|
|
264
|
+
reply(response, 200, [...replies, runsMessage(runs)]);
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
server.on('request', (request, response) => void answer(request, response));
|
|
229
268
|
const draft = join(dirname(options.socketPath), `~${basename(options.socketPath).slice(1)}`);
|
|
230
269
|
await rm(draft, { force: true });
|
|
231
270
|
await listen(server, draft);
|
|
232
271
|
await rename(draft, options.socketPath);
|
|
233
272
|
owned.socket = fileId(options.socketPath);
|
|
234
|
-
idleTimer = setTimeout(closeWhenIdle,
|
|
273
|
+
idleTimer = setTimeout(closeWhenIdle, idleMs);
|
|
235
274
|
return { closed, close, release: () => removeOwned(owned) };
|
|
236
275
|
}
|
|
237
276
|
function upgradedRun(run) {
|
package/dist/worker-server.mjs
CHANGED
|
@@ -46,6 +46,26 @@ export function sendAll(sockets, value) {
|
|
|
46
46
|
send(socket, value);
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
|
+
const BODY_LIMIT = 1024 * 1024;
|
|
50
|
+
export function readBody(request) {
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
let body = '';
|
|
53
|
+
request.setEncoding('utf8');
|
|
54
|
+
request.on('data', (chunk) => {
|
|
55
|
+
body += chunk;
|
|
56
|
+
if (body.length > BODY_LIMIT) {
|
|
57
|
+
resolve('');
|
|
58
|
+
request.destroy();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
request.on('end', () => resolve(body));
|
|
62
|
+
request.on('error', reject);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
export function reply(response, status, messages) {
|
|
66
|
+
response.writeHead(status, { 'content-type': 'application/json', connection: 'close' });
|
|
67
|
+
response.end(JSON.stringify(messages));
|
|
68
|
+
}
|
|
49
69
|
export function listen(server, path) {
|
|
50
70
|
return new Promise((resolve, reject) => {
|
|
51
71
|
server.once('error', reject);
|