next-issue 14.8.0 → 14.9.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.
@@ -100,14 +100,16 @@ function follow(first, root, args, argv, out) {
100
100
  else if (message.type === 'end' && message.error !== undefined) {
101
101
  fail(message.error);
102
102
  }
103
- else if (message.type === 'end' && latest?.upgrade !== undefined) {
104
- upgradeEnd = message.code;
105
- }
106
103
  else if (message.type === 'end') {
107
104
  if (!args.watch && message.summary !== undefined) {
108
105
  out.print(process.stdout, formatSummary(message.summary));
109
106
  }
110
- settle(message.code);
107
+ if (latest?.upgrade === undefined) {
108
+ settle(message.code);
109
+ }
110
+ else {
111
+ upgradeEnd = message.code;
112
+ }
111
113
  }
112
114
  };
113
115
  const rejoin = async (upgrade) => {
@@ -123,6 +123,10 @@ async function claimIssues(loop, seen, tried, pool) {
123
123
  if (!pool.slots.open()) {
124
124
  return { claiming: true, unclaimed: true };
125
125
  }
126
+ await loop.beforeClaim?.();
127
+ if (loop.stop()) {
128
+ return { claiming: false, unclaimed: false };
129
+ }
126
130
  seen.add(issue.number);
127
131
  const slot = pool.slots.claim();
128
132
  pool.running.set(issue.number, loop.process(issue, slot).then((report) => ({ issue: issue.number, slot, report }), (failure) => ({ issue: issue.number, slot, failure })));
@@ -202,7 +202,8 @@ export async function rejoinRun(root) {
202
202
  const deadline = Date.now() + START_TIMEOUT_MS;
203
203
  while (Date.now() < deadline) {
204
204
  const found = await tryOpen(socketPath);
205
- if (found !== undefined && going(found.runs[root])) {
205
+ if (found?.runs[root] !== undefined) {
206
+ found.send({ type: 'rejoin', root });
206
207
  return found;
207
208
  }
208
209
  found?.close();
@@ -0,0 +1,18 @@
1
+ export const NPM_LATEST = 'https://registry.npmjs.org/next-issue/latest';
2
+ const LATEST_TIMEOUT_MS = 3_000;
3
+ export async function latestVersion(registry) {
4
+ try {
5
+ const response = await fetch(registry, { signal: AbortSignal.timeout(LATEST_TIMEOUT_MS) });
6
+ if (!response.ok) {
7
+ return undefined;
8
+ }
9
+ const { version } = (await response.json());
10
+ return typeof version === 'string' ? version : undefined;
11
+ }
12
+ catch {
13
+ return undefined;
14
+ }
15
+ }
16
+ export function npxLaunch(version) {
17
+ return { command: 'npx', args: ['-y', `next-issue@${version}`, 'serve'] };
18
+ }
@@ -49,6 +49,7 @@ async function handle(args, config, repo, recorder, controls) {
49
49
  issues: () => pendingIssues(context),
50
50
  process: (issue, slot) => processIssue(context, issue, slot),
51
51
  stop: interrupts.afterIssue,
52
+ beforeClaim: controls.beforeClaim,
52
53
  concurrency: config.concurrency,
53
54
  };
54
55
  const print = controls.print ?? (async () => undefined);
@@ -7,16 +7,18 @@ import { stopChildren, withRunChildren } from '../shared/children.mjs';
7
7
  import { message } from '../shared/errors.mjs';
8
8
  import { lock } from '../shared/lock.mjs';
9
9
  import { startProgress } from './progress.mjs';
10
- import { isNewer } from '../shared/protocol.mjs';
10
+ import { isNewer, } from '../shared/protocol.mjs';
11
11
  import { runRepo } from './run.mjs';
12
12
  import { packageVersion } from '../shared/version.mjs';
13
13
  import { resumeOnSuccessor } from '../shared/worker-client.mjs';
14
+ import { latestVersion, NPM_LATEST, npxLaunch } from './latest-version.mjs';
14
15
  import { keepLogTrimmed, LOG_CHECK_MS } from './worker-log.mjs';
15
16
  import { checkSocketPath, workerPaths } from '../shared/worker-paths.mjs';
16
17
  import { active, dropUpgrade, endStatus, newRun, requestRefresh, requestStop, requestUpgrade, runControls, runsMessage, setHunting, setWatching, startRefusal, stoppingMessage, stopPoint, switchRefusal, } from './worker-run.mjs';
17
18
  import { broadcast, closer, fileId, isLaunch, listen, parseMessage, removeOwned, send, sendAll, sendAllSent, shutdown, stopListening, } from './worker-server.mjs';
18
19
  const LOCK_WAIT_MS = 5_000;
19
20
  const IDLE_MS = 30_000;
21
+ const REJOIN_GRACE_MS = 5_000;
20
22
  export async function startWorker(options) {
21
23
  checkSocketPath(options.socketPath);
22
24
  await mkdir(dirname(options.lockPath), { recursive: true });
@@ -36,6 +38,23 @@ export async function startWorker(options) {
36
38
  const server = createServer();
37
39
  const sockets = new WebSocketServer({ server });
38
40
  const busy = () => [...runs.values()].some(active);
41
+ let followed = false;
42
+ let lateEnd;
43
+ const awaitRejoin = (end) => {
44
+ if (followed) {
45
+ return Promise.resolve();
46
+ }
47
+ return new Promise((resolve) => {
48
+ const timer = setTimeout(resolve, REJOIN_GRACE_MS);
49
+ lateEnd = {
50
+ end,
51
+ sent: () => {
52
+ clearTimeout(timer);
53
+ resolve();
54
+ },
55
+ };
56
+ });
57
+ };
39
58
  let idleTimer;
40
59
  const idleMs = options.idleMs ?? IDLE_MS;
41
60
  const handOver = async (how, run) => {
@@ -63,11 +82,11 @@ export async function startWorker(options) {
63
82
  }
64
83
  });
65
84
  const closeWhenIdle = () => {
66
- if ((restart !== undefined || sockets.clients.size === 0) && !busy()) {
85
+ if ((restart !== undefined || sockets.clients.size === 0) && !busy() && lateEnd === undefined) {
67
86
  void close();
68
87
  }
69
88
  };
70
- const upgradeBetweenIssues = (run) => {
89
+ const upgradeBetweenIssues = (run, everyRun = false) => {
71
90
  if (restart === undefined) {
72
91
  return;
73
92
  }
@@ -76,12 +95,25 @@ export async function startWorker(options) {
76
95
  to: restart,
77
96
  ...(successor === undefined ? {} : { resumes: true }),
78
97
  };
79
- if (requestUpgrade(run, upgrade)) {
98
+ if (requestUpgrade(run, upgrade, everyRun)) {
80
99
  broadcast(sockets, runs);
81
100
  }
82
101
  };
102
+ const upgradeToLatest = async (run) => {
103
+ const latest = await latestVersion(options.registry ?? NPM_LATEST);
104
+ if (latest === undefined || !isNewer(latest, options.version)) {
105
+ return;
106
+ }
107
+ if (restart === undefined) {
108
+ restart = latest;
109
+ successor = (options.successorLaunch ?? npxLaunch)(latest);
110
+ }
111
+ upgradeBetweenIssues(run, true);
112
+ };
83
113
  const start = (refuse, root, argv, resume) => {
84
- const refusal = startRefusal(runs.get(root), restart !== undefined);
114
+ const refusal = lateEnd === undefined
115
+ ? startRefusal(runs.get(root), restart !== undefined)
116
+ : 'the worker is closing, try again soon';
85
117
  if (refusal !== undefined) {
86
118
  if (!resume) {
87
119
  refuse(refusal);
@@ -104,8 +136,15 @@ export async function startWorker(options) {
104
136
  };
105
137
  broadcast(sockets, runs);
106
138
  let summary;
139
+ const ended = async (end) => {
140
+ await sendAllSent(sockets, end);
141
+ if (resume) {
142
+ await awaitRejoin(end);
143
+ }
144
+ };
107
145
  withRunChildren(run.children, () => options.runRepo(root, args, {
108
146
  ...runControls(run, update),
147
+ beforeClaim: () => upgradeToLatest(run),
109
148
  print: args.watch
110
149
  ? async (pass) => sendAll(sockets, { type: 'pass', root, summary: pass })
111
150
  : undefined,
@@ -119,7 +158,7 @@ export async function startWorker(options) {
119
158
  }))
120
159
  .then((code) => {
121
160
  update({ ...run.progress, status: endStatus(run) });
122
- return sendAllSent(sockets, {
161
+ return ended({
123
162
  type: 'end',
124
163
  root,
125
164
  code,
@@ -127,7 +166,7 @@ export async function startWorker(options) {
127
166
  });
128
167
  }, (error) => {
129
168
  update({ ...run.progress, status: 'failed', error: message(error) });
130
- return sendAllSent(sockets, { type: 'end', root, code: 1, error: message(error) });
169
+ return ended({ type: 'end', root, code: 1, error: message(error) });
131
170
  })
132
171
  .finally(() => void close());
133
172
  };
@@ -171,7 +210,7 @@ export async function startWorker(options) {
171
210
  restart = value.version;
172
211
  successor = value.launch;
173
212
  }
174
- runs.forEach(upgradeBetweenIssues);
213
+ runs.forEach((run) => upgradeBetweenIssues(run));
175
214
  closeWhenIdle();
176
215
  }
177
216
  }
@@ -193,6 +232,13 @@ export async function startWorker(options) {
193
232
  else if (value.type === 'refresh') {
194
233
  requestRefresh(runs.get(value.root));
195
234
  }
235
+ else if (value.type === 'rejoin') {
236
+ followed = true;
237
+ if (lateEnd !== undefined) {
238
+ const { end, sent } = lateEnd;
239
+ socket.send(JSON.stringify(end), () => sent());
240
+ }
241
+ }
196
242
  };
197
243
  sockets.on('connection', (socket) => {
198
244
  send(socket, { type: 'hello', version: options.version });
@@ -229,7 +275,12 @@ function upgradedRun(run) {
229
275
  return run.progress.upgrade !== undefined && run.progress.status === 'stopped';
230
276
  }
231
277
  function resumeArgs(run) {
232
- return ['--watch', 'on', '--hunt', run.progress.hunting ? 'on' : 'off'];
278
+ return [
279
+ '--watch',
280
+ run.progress.watching === true ? 'on' : 'off',
281
+ '--hunt',
282
+ run.progress.hunting ? 'on' : 'off',
283
+ ];
233
284
  }
234
285
  function refuser(socket, value, served) {
235
286
  const root = value.type === 'hello' ? served : value.root;
@@ -65,9 +65,9 @@ export function stoppingMessage(root, run, id) {
65
65
  ...(id === undefined ? {} : { id }),
66
66
  };
67
67
  }
68
- export function requestUpgrade(run, upgrade) {
68
+ export function requestUpgrade(run, upgrade, everyRun = false) {
69
69
  if (!active(run) ||
70
- !run.watching.on() ||
70
+ (!everyRun && !run.watching.on()) ||
71
71
  run.stoppedByPerson === true ||
72
72
  run.progress.upgrade !== undefined) {
73
73
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-issue",
3
- "version": "14.8.0",
3
+ "version": "14.9.0",
4
4
  "description": "Autonomous issue harness driven by the Claude Agent SDK",
5
5
  "keywords": [
6
6
  "github",