next-issue 16.3.0 → 16.4.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
+ }
@@ -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) => {
@@ -67,20 +67,21 @@ export function stopInWorker(root) {
67
67
  });
68
68
  }
69
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`;
83
- });
70
+ return inWorker(root, async (connection, progress) => statusText(connection.version, progress));
71
+ }
72
+ export function statusText(version, progress) {
73
+ const lines = [
74
+ `next-issue: the run is ${progress.status} in worker ${version}`,
75
+ `watching ${onOffText(progress.watching)}, hunting ${onOffText(progress.hunting)}`,
76
+ ];
77
+ if (progress.upgrade !== undefined) {
78
+ lines.push(`stopping for an upgrade to ${progress.upgrade.to}`);
79
+ }
80
+ if (progress.issues.length === 0) {
81
+ lines.push('no issue is running');
82
+ }
83
+ lines.push(...progress.issues.map(issueLine));
84
+ return `${lines.join('\n')}\n`;
84
85
  }
85
86
  function issueLine({ issue, step, round }) {
86
87
  const roundText = round === undefined ? '' : ` (round ${round})`;
@@ -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;
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-issue",
3
- "version": "16.3.0",
3
+ "version": "16.4.0",
4
4
  "description": "Autonomous issue harness driven by the Claude Agent SDK",
5
5
  "keywords": [
6
6
  "github",