next-issue 16.3.0 → 16.5.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.
@@ -1,17 +1,37 @@
1
1
  import { randomUUID } from 'node:crypto';
2
+ import { givenStartFlags } from '../shared/args.mjs';
2
3
  import { isNewer } from '../shared/protocol.mjs';
3
4
  import { packageVersion } from '../shared/version.mjs';
4
- import { going, reachWorker, startedRun } from '../shared/worker-client.mjs';
5
- const RUN_GOING = 'a run is going in this repository already';
5
+ import { ANSWER_TIMEOUT_MS, CLOSED, going, reachWorker, startedRun, } from '../shared/worker-client.mjs';
6
+ import { statusText, switched } from './switch-run.mjs';
7
+ const REFUSED_GOING = [
8
+ 'a run is going in this repository already',
9
+ 'the worker is restarting, try again soon',
10
+ ];
11
+ const SILENT = 'the next-issue worker did not answer';
6
12
  const STARTED = 'next-issue: the run started in the worker; follow it with next-issue logs and stop it with next-issue stop\n';
7
13
  export async function startInWorker(root, argv) {
8
14
  const version = await packageVersion();
15
+ const given = givenStartFlags(argv);
9
16
  const connection = await reachWorker(root, version);
10
17
  try {
11
- if (going(connection.runs[root])) {
12
- throw new Error(isNewer(version, connection.version)
13
- ? `${RUN_GOING}; the worker updates to ${version} after the running issues`
14
- : RUN_GOING);
18
+ const progress = connection.runs[root];
19
+ if (going(progress)) {
20
+ const join = await joined(connection, root, argv, progress);
21
+ if (join.joined) {
22
+ return statusText(connection.version, join.progress);
23
+ }
24
+ if (!isNewer(version, connection.version) || !REFUSED_GOING.includes(join.refusal)) {
25
+ throw new Error(join.refusal);
26
+ }
27
+ let latest = join.progress;
28
+ if (given.watch !== undefined) {
29
+ latest = await switched(connection, root, 'watch', given.watch);
30
+ }
31
+ if (given.hunt !== undefined) {
32
+ latest = await switched(connection, root, 'hunt', given.hunt);
33
+ }
34
+ return statusText(connection.version, latest);
15
35
  }
16
36
  const id = randomUUID();
17
37
  const accepted = startedRun(connection, root, id);
@@ -23,3 +43,32 @@ export async function startInWorker(root, argv) {
23
43
  connection.close();
24
44
  }
25
45
  }
46
+ function joined(connection, root, argv, known) {
47
+ const id = randomUUID();
48
+ let latest = known;
49
+ return new Promise((resolve, reject) => {
50
+ const timer = setTimeout(() => reject(new Error(SILENT)), ANSWER_TIMEOUT_MS);
51
+ const settle = (join) => {
52
+ clearTimeout(timer);
53
+ resolve(join);
54
+ };
55
+ connection.listen((message) => {
56
+ if (message.type === 'runs' && message.runs[root] !== undefined) {
57
+ latest = message.runs[root];
58
+ if (message.id === id) {
59
+ settle({ joined: true, progress: latest });
60
+ }
61
+ }
62
+ else if (message.type === 'error' &&
63
+ message.root === root &&
64
+ (message.id === id || message.id === undefined)) {
65
+ settle({ joined: false, progress: latest, refusal: message.message });
66
+ }
67
+ });
68
+ void connection.closed.then(() => {
69
+ clearTimeout(timer);
70
+ reject(new Error(CLOSED));
71
+ });
72
+ connection.send({ type: 'start', root, args: argv, id });
73
+ });
74
+ }
@@ -1,7 +1,7 @@
1
1
  import { randomUUID } from 'node:crypto';
2
2
  import { isNewer } from '../shared/protocol.mjs';
3
3
  import { packageVersion } from '../shared/version.mjs';
4
- import { CLOSED, findWorker, going } from '../shared/worker-client.mjs';
4
+ import { ANSWER_TIMEOUT_MS, CLOSED, findWorker, going, } from '../shared/worker-client.mjs';
5
5
  const FIELDS = { watch: 'watching', hunt: 'hunting' };
6
6
  const NO_RUN = 'next-issue: no run is going in this repository\n';
7
7
  function onOffText(on) {
@@ -32,7 +32,7 @@ async function inWorker(root, act) {
32
32
  connection.close();
33
33
  }
34
34
  }
35
- function switched(connection, root, name, on) {
35
+ export function switched(connection, root, name, on) {
36
36
  const id = randomUUID();
37
37
  let sent = false;
38
38
  return new Promise((resolve, reject) => {
@@ -58,30 +58,63 @@ export function stopInWorker(root) {
58
58
  if (progress.status === 'stopping' && progress.upgrade === undefined) {
59
59
  return 'next-issue: the run is stopping already\n';
60
60
  }
61
- connection.send({ type: 'stop', root, after: 'issue' });
62
- await connection.end();
63
- if (progress.upgrade !== undefined) {
64
- return 'next-issue: the run will stop after its current issues and not start again\n';
65
- }
66
- return 'next-issue: the run will stop after its current issues\n';
61
+ const reply = await stopReply(connection, root);
62
+ return `next-issue: ${stopText(progress, reply)}\n`;
67
63
  });
68
64
  }
69
- export function statusInWorker(root) {
70
- return inWorker(root, async (connection, progress) => {
71
- const lines = [
72
- `next-issue: the run is ${progress.status} in worker ${connection.version}`,
73
- `watching ${onOffText(progress.watching)}, hunting ${onOffText(progress.hunting)}`,
74
- ];
75
- if (progress.upgrade !== undefined) {
76
- lines.push(`stopping for an upgrade to ${progress.upgrade.to}`);
77
- }
78
- if (progress.issues.length === 0) {
79
- lines.push('no issue is running');
80
- }
81
- lines.push(...progress.issues.map(issueLine));
82
- return `${lines.join('\n')}\n`;
65
+ function stopReply(connection, root) {
66
+ const id = randomUUID();
67
+ return new Promise((resolve) => {
68
+ const timer = setTimeout(() => resolve(undefined), ANSWER_TIMEOUT_MS);
69
+ connection.listen((message) => {
70
+ if (message.type === 'stopping' && message.id === id) {
71
+ clearTimeout(timer);
72
+ resolve(message);
73
+ }
74
+ });
75
+ void connection.closed.then(() => {
76
+ clearTimeout(timer);
77
+ resolve(undefined);
78
+ });
79
+ connection.send({ type: 'stop', root, after: 'issue', id });
83
80
  });
84
81
  }
82
+ function stopText(progress, reply) {
83
+ const upgrading = progress.upgrade !== undefined;
84
+ const ending = upgrading ? ' and not start again' : '';
85
+ if (reply === undefined) {
86
+ return `the run will stop after its current issues${ending}`;
87
+ }
88
+ const waits = progress.issues.map(({ issue }) => `#${issue}`);
89
+ if (reply.hunting) {
90
+ waits.push('its current hunt');
91
+ }
92
+ if (waits.length > 0) {
93
+ return `the run will stop after ${listText(waits)}${ending}`;
94
+ }
95
+ const state = reply.waiting ? 'the run stopped' : 'the run is stopping';
96
+ return upgrading ? `${state} and will not start again` : state;
97
+ }
98
+ function listText(items) {
99
+ return items.length === 1 ? items[0] : `${items.slice(0, -1).join(', ')} and ${items.at(-1)}`;
100
+ }
101
+ export function statusInWorker(root) {
102
+ return inWorker(root, async (connection, progress) => statusText(connection.version, progress));
103
+ }
104
+ export function statusText(version, progress) {
105
+ const lines = [
106
+ `next-issue: the run is ${progress.status} in worker ${version}`,
107
+ `watching ${onOffText(progress.watching)}, hunting ${onOffText(progress.hunting)}`,
108
+ ];
109
+ if (progress.upgrade !== undefined) {
110
+ lines.push(`stopping for an upgrade to ${progress.upgrade.to}`);
111
+ }
112
+ if (progress.issues.length === 0) {
113
+ lines.push('no issue is running');
114
+ }
115
+ lines.push(...progress.issues.map(issueLine));
116
+ return `${lines.join('\n')}\n`;
117
+ }
85
118
  function issueLine({ issue, step, round }) {
86
119
  const roundText = round === undefined ? '' : ` (round ${round})`;
87
120
  return `#${issue} ${step?.replaceAll('-', ' ') ?? 'starting'}${roundText}`;
@@ -104,7 +104,7 @@ function readArgs(passed) {
104
104
  }
105
105
  args.watch = given.watch ?? true;
106
106
  args.hunt = given.hunt ?? false;
107
- return { args, flag, runFlag };
107
+ return { args, flag, runFlag, given };
108
108
  }
109
109
  function command(arg) {
110
110
  const found = COMMANDS.find((name) => name === arg);
@@ -128,3 +128,7 @@ export function parseStartArgs(argv) {
128
128
  }
129
129
  return { ...args, command: 'start' };
130
130
  }
131
+ export function givenStartFlags(argv) {
132
+ parseStartArgs(argv);
133
+ return readArgs(argv).given;
134
+ }
@@ -7,7 +7,7 @@ import { WebSocket } from 'ws';
7
7
  import { lockHolder } from './lock.mjs';
8
8
  import { isNewer, } from './protocol.mjs';
9
9
  import { checkSocketPath, workerPaths } from './worker-paths.mjs';
10
- const ANSWER_TIMEOUT_MS = 5_000;
10
+ export const ANSWER_TIMEOUT_MS = 5_000;
11
11
  const START_TIMEOUT_MS = 120_000;
12
12
  const HOLDER_GRACE_MS = 2_000;
13
13
  const POLL_MS = 200;
@@ -72,12 +72,13 @@ export function watchRun(args, loop, context, controls, print) {
72
72
  const { interrupts } = controls;
73
73
  let passStart = recorder.summary();
74
74
  const hunt = hunter(context);
75
+ controls.onHunter?.(() => hunt.busy());
75
76
  return watchIssues({
76
77
  ...loop,
77
78
  wait: (minutes, woken) => {
78
79
  recorder.event('watch.wait', { minutes });
79
80
  passStart = recorder.summary();
80
- return interrupts.sleep(minutesToMs(minutes), () => hunt.busy(), woken);
81
+ return interrupts.sleep(minutesToMs(minutes), woken);
81
82
  },
82
83
  passEnd: (issues) => print({ ...recorder.summary(passStart), issues }),
83
84
  hunter: hunt,
@@ -2,7 +2,7 @@ import { mkdir, rename, rm } from 'node:fs/promises';
2
2
  import { createServer, STATUS_CODES } from 'node:http';
3
3
  import { basename, dirname, join } from 'node:path';
4
4
  import { WebSocketServer } from 'ws';
5
- import { parseStartArgs } from '../shared/args.mjs';
5
+ import { givenStartFlags, parseStartArgs } from '../shared/args.mjs';
6
6
  import { stopChildren, withRunChildren } from '../shared/children.mjs';
7
7
  import { message } from '../shared/errors.mjs';
8
8
  import { lock } from '../shared/lock.mjs';
@@ -14,7 +14,7 @@ import { resumeOnSuccessor } from '../shared/worker-client.mjs';
14
14
  import { latestVersion, NPM_LATEST, npxLaunch } from './latest-version.mjs';
15
15
  import { keepLogTrimmed, LOG_CHECK_MS } from './worker-log.mjs';
16
16
  import { checkSocketPath, workerPaths } from '../shared/worker-paths.mjs';
17
- import { active, dropUpgrade, endStatus, newRun, requestStop, requestUpgrade, runControls, runsMessage, setHunting, setWatching, startRefusal, stoppingMessage, stopPoint, switchRefusal, } from './worker-run.mjs';
17
+ import { active, dropUpgrade, endStatus, joinRun, newRun, requestStop, requestUpgrade, runControls, runsMessage, setHunting, setWatching, startRefusal, stoppingMessage, stopPoint, switchRefusal, } from './worker-run.mjs';
18
18
  import { broadcast, closer, fileId, isLaunch, listen, parseMessage, removeOwned, send, sendAll, sendAllSent, shutdown, stopListening, } from './worker-server.mjs';
19
19
  const LOCK_WAIT_MS = 5_000;
20
20
  const IDLE_MS = 30_000;
@@ -92,8 +92,29 @@ export async function startWorker(options) {
92
92
  }
93
93
  upgradeBetweenIssues(run, true);
94
94
  };
95
- const start = (refuse, root, argv, resume) => {
96
- const refusal = startRefusal(runs.get(root), restart !== undefined);
95
+ const joinGoing = (socket, refuse, run, argv, id) => {
96
+ let refusal;
97
+ try {
98
+ refusal = joinRun(run, givenStartFlags(argv));
99
+ }
100
+ catch (error) {
101
+ refusal = message(error);
102
+ }
103
+ if (refusal !== undefined) {
104
+ refuse(refusal);
105
+ return;
106
+ }
107
+ broadcast(sockets, runs);
108
+ upgradeBetweenIssues(run);
109
+ send(socket, runsMessage(runs, id));
110
+ };
111
+ const start = (socket, refuse, root, argv, resume, id) => {
112
+ const going = runs.get(root);
113
+ if (active(going) && !resume) {
114
+ joinGoing(socket, refuse, going, argv, id);
115
+ return;
116
+ }
117
+ const refusal = startRefusal(going, restart !== undefined);
97
118
  if (refusal !== undefined) {
98
119
  if (!resume) {
99
120
  refuse(refusal);
@@ -193,7 +214,7 @@ export async function startWorker(options) {
193
214
  refuse(`the worker serves ${options.root} only`);
194
215
  }
195
216
  else if (value.type === 'start') {
196
- start(refuse, value.root, value.args ?? [], value.resume === true);
217
+ start(socket, refuse, value.root, value.args ?? [], value.resume === true, value.id);
197
218
  }
198
219
  else if (value.type === 'stop') {
199
220
  stop(socket, refuse, value.root, value.after, value.id);
@@ -29,12 +29,30 @@ export function startRefusal(run, restart) {
29
29
  }
30
30
  return undefined;
31
31
  }
32
- export function runsMessage(runs) {
32
+ export function runsMessage(runs, id) {
33
33
  return {
34
34
  type: 'runs',
35
35
  runs: Object.fromEntries([...runs].map(([root, run]) => [root, run.progress])),
36
+ ...(id === undefined ? {} : { id }),
36
37
  };
37
38
  }
39
+ export function joinRun(run, given) {
40
+ if (run.stoppedByPerson === true) {
41
+ if (run.stop !== 'issue') {
42
+ return 'the run is stopping';
43
+ }
44
+ run.stop = undefined;
45
+ run.stoppedByPerson = false;
46
+ run.progress = { ...run.progress, status: 'running' };
47
+ }
48
+ if (given.watch !== undefined) {
49
+ setWatching(run, given.watch);
50
+ }
51
+ if (given.hunt !== undefined) {
52
+ setHunting(run, given.hunt);
53
+ }
54
+ return undefined;
55
+ }
38
56
  export function stopPoint(after) {
39
57
  return STOP_POINTS.find((point) => point === (after ?? 'issue'));
40
58
  }
@@ -59,7 +77,7 @@ export function stoppingMessage(root, run, id) {
59
77
  type: 'stopping',
60
78
  root,
61
79
  waiting,
62
- hunting: waiting && run.huntBusy?.() === true,
80
+ hunting: run.huntBusy?.() === true,
63
81
  ...(id === undefined ? {} : { id }),
64
82
  };
65
83
  }
@@ -109,8 +127,7 @@ function runInterrupts(run) {
109
127
  return {
110
128
  afterIssue: () => run.stop !== undefined,
111
129
  afterRound: () => run.stop === 'round' || run.stop === 'now',
112
- sleep: (ms, hunting, woken) => {
113
- run.huntBusy = hunting;
130
+ sleep: (ms, woken) => {
114
131
  return wakeableSleep(run, ms, run.stop !== undefined, woken);
115
132
  },
116
133
  };
@@ -121,5 +138,8 @@ export function runControls(run, update) {
121
138
  hunting: run.hunting,
122
139
  watching: run.watching,
123
140
  onEvent: (record) => update(applyEvent(run.progress, record)),
141
+ onHunter: (busy) => {
142
+ run.huntBusy = busy;
143
+ },
124
144
  };
125
145
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-issue",
3
- "version": "16.3.0",
3
+ "version": "16.5.0",
4
4
  "description": "Autonomous issue harness driven by the Claude Agent SDK",
5
5
  "keywords": [
6
6
  "github",