next-issue 12.0.2 → 12.1.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 CHANGED
@@ -369,7 +369,7 @@ In a run of the `serve` worker, a client can switch hunting and watching on or o
369
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. 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;
370
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;
371
371
  - waits up to 5 seconds for the lock when a worker that is closing still holds it;
372
- - closes once no run is going when a client with a newer version connects, so that the client can start the newer worker. 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>" }`. A stop by a person, or turning watching off, drops that field, so no client starts the run again. A run that does not watch, or that `next-issue start` started, is left alone. 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;
372
+ - 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), 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, or that `next-issue start` started, is left alone. 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;
373
373
  - stops every agent and command and exits on `SIGTERM`, `SIGHUP` or `SIGINT`, and removes its socket and lock files;
374
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.
375
375
 
package/dist/serve.mjs CHANGED
@@ -13,10 +13,11 @@ 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';
16
+ import { resumeOnSuccessor } from './worker-client.mjs';
16
17
  import { keepLogTrimmed, LOG_CHECK_MS } from './worker-log.mjs';
17
18
  import { checkSocketPath, workerPaths } from './worker-paths.mjs';
18
19
  import { active, dropUpgrade, endStatus, newRun, requestRefresh, requestStop, requestUpgrade, runControls, runsMessage, setHunting, setWatching, startRefusal, stoppingMessage, stopPoint, switchRefusal, } from './worker-run.mjs';
19
- import { broadcast, closer, fileId, listen, parseMessage, removeOwned, sendAll, shutdown, } from './worker-server.mjs';
20
+ import { broadcast, closer, fileId, isLaunch, listen, parseMessage, removeOwned, sendAll, shutdown, stopListening, } from './worker-server.mjs';
20
21
  const LOCK_WAIT_MS = 5_000;
21
22
  const IDLE_MS = 30_000;
22
23
  export async function startWorker(options) {
@@ -34,6 +35,7 @@ export async function startWorker(options) {
34
35
  };
35
36
  const runs = new Map();
36
37
  let restart;
38
+ let successor;
37
39
  const server = createServer();
38
40
  const sockets = new WebSocketServer({ server });
39
41
  const sendQuestions = questionSender(options.questions);
@@ -42,9 +44,29 @@ export async function startWorker(options) {
42
44
  const outsideRuns = AsyncLocalStorage.snapshot();
43
45
  const busy = () => [...runs.values()].some(active);
44
46
  let idleTimer;
47
+ const handOver = async (how, run) => {
48
+ const listening = stopListening(server);
49
+ removeOwned(owned);
50
+ try {
51
+ await resumeOnSuccessor(options.root, how, resumeArgs(run));
52
+ }
53
+ catch (error) {
54
+ const reason = `the worker could not start next-issue ${restart}: ${message(error)}`;
55
+ process.stderr.write(`next-issue: ${reason}\n`);
56
+ run.progress = { ...run.progress, status: 'failed', error: reason };
57
+ broadcast(sockets, runs);
58
+ }
59
+ await shutdown(server, sockets, owned, listening);
60
+ };
45
61
  const { closed, close } = closer(async () => {
46
62
  clearTimeout(idleTimer);
47
- await shutdown(server, sockets, owned);
63
+ const run = runs.get(options.root);
64
+ if (successor !== undefined && run !== undefined && upgradedRun(run)) {
65
+ await handOver(successor, run);
66
+ }
67
+ else {
68
+ await shutdown(server, sockets, owned);
69
+ }
48
70
  });
49
71
  const closeWhenIdle = () => {
50
72
  if ((restart !== undefined || sockets.clients.size === 0) && !busy()) {
@@ -52,7 +74,15 @@ export async function startWorker(options) {
52
74
  }
53
75
  };
54
76
  const upgradeBetweenIssues = (run) => {
55
- if (restart !== undefined && requestUpgrade(run, { from: options.version, to: restart })) {
77
+ if (restart === undefined) {
78
+ return;
79
+ }
80
+ const upgrade = {
81
+ from: options.version,
82
+ to: restart,
83
+ ...(successor === undefined ? {} : { resumes: true }),
84
+ };
85
+ if (requestUpgrade(run, upgrade)) {
56
86
  broadcast(sockets, runs);
57
87
  }
58
88
  };
@@ -138,8 +168,14 @@ export async function startWorker(options) {
138
168
  if (typeof value.version !== 'string') {
139
169
  throw new Error('a hello message needs its version as a string');
140
170
  }
171
+ if (value.launch !== undefined && !isLaunch(value.launch)) {
172
+ throw new Error('a hello message needs its launch as a command and a list of args');
173
+ }
141
174
  if (isNewer(value.version, options.version)) {
142
- restart ??= value.version;
175
+ if (restart === undefined) {
176
+ restart = value.version;
177
+ successor = value.launch;
178
+ }
143
179
  runs.forEach(upgradeBetweenIssues);
144
180
  closeWhenIdle();
145
181
  }
@@ -197,6 +233,12 @@ export async function startWorker(options) {
197
233
  idleTimer = setTimeout(closeWhenIdle, options.idleMs ?? IDLE_MS);
198
234
  return { closed, close, release: () => removeOwned(owned) };
199
235
  }
236
+ function upgradedRun(run) {
237
+ return run.progress.upgrade !== undefined && run.progress.status === 'stopped';
238
+ }
239
+ function resumeArgs(run) {
240
+ return ['--watch', 'on', '--hunt', run.progress.hunting ? 'on' : 'off'];
241
+ }
200
242
  function refuser(socket, value, served) {
201
243
  const root = value.type === 'hello' ? served : value.root;
202
244
  const id = 'id' in value ? value.id : undefined;
@@ -5,7 +5,7 @@ import { dirname } from 'node:path';
5
5
  import { setTimeout as delay } from 'node:timers/promises';
6
6
  import { WebSocket } from 'ws';
7
7
  import { lockHolder } from './lock.mjs';
8
- import { isNewer } from './protocol.mjs';
8
+ import { isNewer, } from './protocol.mjs';
9
9
  import { checkSocketPath, workerPaths } from './worker-paths.mjs';
10
10
  const ANSWER_TIMEOUT_MS = 5_000;
11
11
  const START_TIMEOUT_MS = 30_000;
@@ -29,6 +29,9 @@ function parse(text) {
29
29
  return undefined;
30
30
  }
31
31
  }
32
+ function ownLaunch() {
33
+ return { command: process.execPath, args: [...process.execArgv, process.argv[1], 'serve'] };
34
+ }
32
35
  function open(socketPath, version) {
33
36
  return new Promise((resolve, reject) => {
34
37
  const socket = new WebSocket('ws://localhost/', {
@@ -71,7 +74,7 @@ function open(socketPath, version) {
71
74
  ready = true;
72
75
  clearTimeout(timer);
73
76
  if (version !== undefined && !Object.values(message.runs).some(going)) {
74
- socket.send(JSON.stringify({ type: 'hello', version }));
77
+ socket.send(JSON.stringify({ type: 'hello', version, launch: ownLaunch() }));
75
78
  }
76
79
  resolve({
77
80
  version: workerVersion,
@@ -129,14 +132,14 @@ async function waitForStart(paths, version, exited) {
129
132
  }
130
133
  }
131
134
  }
132
- async function launch(root, version) {
135
+ async function launch(root, version, how) {
133
136
  const paths = workerPaths(root);
134
137
  const held = await waitForHolder(paths, version);
135
138
  if (held !== undefined) {
136
139
  return held;
137
140
  }
138
141
  let exited;
139
- spawnServe(root, (reason) => {
142
+ spawnServe(root, how, (reason) => {
140
143
  exited ??= reason;
141
144
  });
142
145
  return waitForStart(paths, version, () => exited);
@@ -145,7 +148,7 @@ export async function reachWorker(root, version) {
145
148
  const { socketPath } = workerPaths(root);
146
149
  checkSocketPath(socketPath);
147
150
  for (let tries = 1;; tries += 1) {
148
- const found = (await tryOpen(socketPath, version)) ?? (await launch(root, version));
151
+ const found = (await tryOpen(socketPath, version)) ?? (await launch(root, version, ownLaunch()));
149
152
  const busy = Object.values(found.runs).some(going);
150
153
  if (!isNewer(version, found.version) || busy || tries >= UPGRADE_TRIES) {
151
154
  return found;
@@ -154,6 +157,42 @@ export async function reachWorker(root, version) {
154
157
  found.close();
155
158
  }
156
159
  }
160
+ function startedRun(found, root) {
161
+ return new Promise((resolve, reject) => {
162
+ const timer = setTimeout(() => reject(new Error('the newer worker did not start the run')), ANSWER_TIMEOUT_MS);
163
+ const settle = (error) => {
164
+ clearTimeout(timer);
165
+ if (error === undefined) {
166
+ resolve();
167
+ }
168
+ else {
169
+ reject(error);
170
+ }
171
+ };
172
+ found.listen((message) => {
173
+ if (message.type === 'runs' && going(message.runs[root])) {
174
+ settle();
175
+ }
176
+ else if (message.type === 'error' && message.root === root) {
177
+ settle(new Error(message.message));
178
+ }
179
+ });
180
+ void found.closed.then(() => settle(new Error(CLOSED)));
181
+ });
182
+ }
183
+ export async function resumeOnSuccessor(root, how, args) {
184
+ const { socketPath } = workerPaths(root);
185
+ const found = (await tryOpen(socketPath)) ?? (await launch(root, undefined, how));
186
+ try {
187
+ if (!going(found.runs[root])) {
188
+ found.send({ type: 'start', root, args, resume: true });
189
+ await startedRun(found, root);
190
+ }
191
+ }
192
+ finally {
193
+ found.close();
194
+ }
195
+ }
157
196
  export function findWorker(root) {
158
197
  const { socketPath } = workerPaths(root);
159
198
  checkSocketPath(socketPath);
@@ -162,12 +201,12 @@ export function findWorker(root) {
162
201
  function exitText(code, signal) {
163
202
  return signal === null ? `exit code ${code}` : `signal ${signal}`;
164
203
  }
165
- function spawnServe(root, onExit) {
204
+ function spawnServe(root, how, onExit) {
166
205
  const { logPath } = workerPaths(root);
167
206
  mkdirSync(dirname(logPath), { recursive: true });
168
207
  const log = openSync(logPath, 'a');
169
208
  try {
170
- const child = spawn(process.execPath, [...process.execArgv, process.argv[1], 'serve'], {
209
+ const child = spawn(how.command, how.args, {
171
210
  cwd: root,
172
211
  detached: true,
173
212
  stdio: ['ignore', log, log],
@@ -7,6 +7,9 @@ function isRecord(value) {
7
7
  function isStringArray(value) {
8
8
  return Array.isArray(value) && value.every((item) => typeof item === 'string');
9
9
  }
10
+ export function isLaunch(value) {
11
+ return isRecord(value) && typeof value['command'] === 'string' && isStringArray(value['args']);
12
+ }
10
13
  function isClientMessage(value) {
11
14
  if (!isRecord(value) || typeof value['type'] !== 'string') {
12
15
  return false;
@@ -61,6 +64,9 @@ export function listen(server, path) {
61
64
  function whenClosed(close) {
62
65
  return new Promise((resolve) => close(() => resolve()));
63
66
  }
67
+ export function stopListening(server) {
68
+ return whenClosed((done) => server.close(done));
69
+ }
64
70
  export function fileId(path) {
65
71
  const found = statSync(path, { throwIfNoEntry: false });
66
72
  return found?.isSocket() === true ? `${found.dev}:${found.ino}` : '';
@@ -73,12 +79,12 @@ export function removeOwned(owned) {
73
79
  rmSync(owned.lockPath, { force: true });
74
80
  }
75
81
  }
76
- export async function shutdown(server, sockets, owned) {
82
+ export async function shutdown(server, sockets, owned, listening) {
77
83
  for (const socket of sockets.clients) {
78
84
  socket.terminate();
79
85
  }
80
86
  await whenClosed((done) => sockets.close(done));
81
- await whenClosed((done) => server.close(done));
87
+ await (listening ?? stopListening(server));
82
88
  removeOwned(owned);
83
89
  }
84
90
  export function closer(stop) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-issue",
3
- "version": "12.0.2",
3
+ "version": "12.1.0",
4
4
  "description": "Autonomous issue harness driven by the pi SDK",
5
5
  "keywords": [
6
6
  "github",