next-issue 6.0.1 → 6.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
@@ -128,8 +128,9 @@ the run handles one issue. For each issue:
128
128
  budget runs out, the harness reverts every commit of the change on the
129
129
  base branch, pushes the revert and hands the issue to a person. A resume
130
130
  that stopped while the checks of a landed commit ran waits for them
131
- again, and a fix that was committed but not landed lands first. A problem that the reviewer leaves
132
- out of the findings thus gets an approval: only the findings count.
131
+ again, even when more commits landed on the base since, and a fix that
132
+ was committed but not landed lands first. A problem that the reviewer
133
+ leaves out of the findings thus gets an approval: only the findings count.
133
134
  8. With an open finding, run the fixer agent and go to step 5 again. The
134
135
  budget is `maxReviewRounds`. A fixer that holds every finding to be wrong
135
136
  changes nothing and gives its reason instead. The reason goes on the issue
@@ -510,21 +511,23 @@ The watch stops:
510
511
 
511
512
  ## Hunt for bugs
512
513
 
513
- With `--hunt`, which turns on `--watch`, the harness runs a hunter agent at the same time as the issues. Before each hunt it reads the open issues with the `labels.ready` label and counts those that are ready to claim, have no open blocker and are not running. It hunts only while that count is less than `huntCap`, 1 by default. Otherwise it waits and checks again, with the same back-off as the watch: 1, 2, 3, 5, 10, then 10 minutes, back to 1 minute after a hunt. Each wait writes a `hunt.wait` event with the `minutes` to the run log. A hunt that files nothing does not stop the next one. The hunter:
514
+ With `--hunt`, which turns on `--watch`, the harness runs a hunter agent at the same time as the issues. Before each hunt it reads the open issues with the `labels.ready` label and counts those that are ready to claim, have no open blocker and are not running. It hunts only while that count is less than `huntCap`, 1 by default. Otherwise it waits and checks again, with the same back-off as the watch: 1, 2, 3, 5, 10, then 10 minutes, back to 1 minute after a hunt that succeeds. Each wait writes a `hunt.wait` event with the `minutes` to the run log. A hunt that files nothing does not stop the next one. The hunter:
514
515
 
515
516
  - works in its own worktree at `.worktrees/hunt`, at the remote base branch, which the harness fetches and resets before each hunt;
516
517
  - follows `next-issue.hunt.md` in the repository root, or a short built-in prompt when the file is missing;
517
518
  - files one issue, for its most serious finding, with the `labels.ready` label, the `labels.hunt` label and the sections of the [issue format](#issue-format), after a search for duplicates. The `labels.hunt` label, `source:hunt` by default, marks the issue as one the hunter filed, and the harness creates it before the hunt when it is missing. It files nothing when it finds no real problem or a duplicate exists;
518
519
  - runs no test suite and changes no file. It gets the tools of the reviewer.
519
520
 
520
- The hunter ends its reply with an `issue:` line, and the run log holds it as a `hunt.issue` event with the `url`, or `none`. A hunt that fails writes a `hunt.error` event and the watch goes on. The next pass claims the new issue like any other. At the first `Ctrl-C` the hunter stops after its current hunt, and the watch ends once the hunt and the running issues end. A first `Ctrl-C` during the watch wait tells you so while a hunt runs, and the third press stops the hunter at once. The hunter stops too when the watch ends for another reason.
521
+ The hunter ends its reply with an `issue:` line, and the run log holds it as a `hunt.issue` event with the `url`, or `none`. A hunt that fails writes a `hunt.error` event and the watch goes on. The hunter then waits before the next hunt, with a `hunt.wait` event and the same back-off, so a failure that repeats waits 1, 2, 3, 5, 10, then 10 minutes, and the back-off goes back to 1 minute after a hunt that succeeds. The next pass claims the new issue like any other. At the first `Ctrl-C` the hunter stops after its current hunt, and the watch ends once the hunt and the running issues end. A first `Ctrl-C` during the watch wait tells you so while a hunt runs, and the third press stops the hunter at once. The hunter stops too when the watch ends for another reason.
521
522
 
522
523
  ## Worker
523
524
 
524
525
  `next-issue serve` runs one worker for the repository it is run in. The
525
526
  [`@ravshansbox/pi-next-issue`](../pi-next-issue) extension starts it and talks
526
- to it. Each repository has its own worker, so the runs, restarts and upgrades
527
- of one repository never affect another. The worker:
527
+ to it. `serve` takes no flags apart from `--help` and refuses any other with
528
+ an error, because runs start from a client, not from `serve`. Each repository
529
+ has its own worker, so the runs, restarts and upgrades of one repository never
530
+ affect another. The worker:
528
531
 
529
532
  - listens with WebSocket on the socket file `.next-issue/worker.sock` in the
530
533
  repository. Only your user can open the file. It works on macOS and Linux.
@@ -556,8 +559,16 @@ of one repository never affect another. The worker:
556
559
  a newer one. When the client adds an `"id"` to its message, the list that
557
560
  answers it carries the same `id` and is read after the message arrived. When the list cannot be read, the client gets
558
561
  `{ "type": "questions-error", "root": "<path>", "message": "<text>" }`;
559
- - stops a run, when a client asks, like a second `Ctrl-C`: each running issue
560
- after its current round, with the state kept;
562
+ - stops a run when a client sends
563
+ `{ "type": "stop", "root": "<path>", "after": "issue" | "round" | "now" }`.
564
+ With no `after`, it stops after `issue`. `issue` works like a first
565
+ `Ctrl-C`: no new claim or hunt, and the held issues and a going hunt finish.
566
+ `round` works like a second `Ctrl-C`: each running issue stops after its
567
+ current round, with the state kept, and a going hunt ends first. `now`
568
+ stops every agent, command and hunt of the run at once, and each running
569
+ issue ends as `stopped` with its state and labels kept; the worker keeps
570
+ running. A later point than one already asked for changes nothing, and an
571
+ earlier one replaces it. An unknown `after` gets an `error` message;
561
572
  - exits, and removes its socket and lock files, when no run is going and no
562
573
  client is connected, or when no client connects in the 30 seconds after it
563
574
  starts;
package/dist/agents.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { getAgentDir, } from '@earendil-works/pi-coding-agent';
2
2
  import { openSession } from './agent-session.mjs';
3
- import { onStop } from './children.mjs';
3
+ import { onStop, refuseWhenStopped } from './children.mjs';
4
4
  import { deadline, elapsedMs } from './durations.mjs';
5
5
  import { isRecord } from './records.mjs';
6
6
  import { emptyUsage } from './usage.mjs';
@@ -21,6 +21,7 @@ export async function runAgent(recorder, request) {
21
21
  const unsubscribe = session.subscribe((event) => observe(log, event));
22
22
  const release = onStop(() => void session.abort());
23
23
  try {
24
+ refuseWhenStopped();
24
25
  await session.bindExtensions({});
25
26
  await session.prompt(request.prompt, { expandPromptTemplates: false });
26
27
  if (timer.timedOut()) {
package/dist/args.mjs CHANGED
@@ -42,8 +42,12 @@ export function parseArgs(argv) {
42
42
  hunt: false,
43
43
  level: 'normal',
44
44
  };
45
+ let flag;
45
46
  for (let index = 0; index < argv.length; index += 1) {
46
47
  const arg = argv[index];
48
+ if (index !== 0 && arg !== '--help') {
49
+ flag ??= arg;
50
+ }
47
51
  if (index === 0 && !arg.startsWith('-')) {
48
52
  args.command = command(arg);
49
53
  }
@@ -60,6 +64,9 @@ export function parseArgs(argv) {
60
64
  throw new Error(`Unknown argument: ${arg}`);
61
65
  }
62
66
  }
67
+ if (args.command === 'serve' && !args.help && flag !== undefined) {
68
+ throw new Error(`serve does not take ${flag}`);
69
+ }
63
70
  args.watch ||= args.hunt;
64
71
  return args;
65
72
  }
package/dist/children.mjs CHANGED
@@ -1,38 +1,69 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
1
2
  const groups = new Set();
2
3
  const stoppers = new Set();
3
- export function onStop(stopper) {
4
- stoppers.add(stopper);
5
- return () => stoppers.delete(stopper);
4
+ const current = new AsyncLocalStorage();
5
+ export function runChildren() {
6
+ return { groups: new Set(), stoppers: new Set(), stopped: false };
6
7
  }
7
- export function stopChildren(signal) {
8
- for (const stopper of stoppers) {
8
+ export function withRunChildren(children, body) {
9
+ return current.run(children, body);
10
+ }
11
+ function stopAll(children, signal) {
12
+ for (const stopper of children.stoppers) {
9
13
  stopper();
10
14
  }
11
- stoppers.clear();
12
- for (const pid of groups) {
15
+ children.stoppers.clear();
16
+ for (const pid of children.groups) {
13
17
  try {
14
18
  process.kill(-pid, signal);
15
19
  }
16
20
  catch { }
17
21
  }
18
- groups.clear();
22
+ children.groups.clear();
23
+ }
24
+ export function stopRunChildren(children) {
25
+ children.stopped = true;
26
+ stopAll(children, 'SIGINT');
27
+ }
28
+ export function runStopped() {
29
+ return current.getStore()?.stopped === true;
30
+ }
31
+ export function refuseWhenStopped() {
32
+ if (runStopped()) {
33
+ throw new Error('The run stopped.');
34
+ }
35
+ }
36
+ export function onStop(stopper) {
37
+ const children = current.getStore();
38
+ stoppers.add(stopper);
39
+ children?.stoppers.add(stopper);
40
+ return () => {
41
+ stoppers.delete(stopper);
42
+ children?.stoppers.delete(stopper);
43
+ };
44
+ }
45
+ export function stopChildren(signal) {
46
+ stopAll({ groups, stoppers }, signal);
19
47
  process.off('SIGTERM', onTerminate);
20
48
  process.kill(process.pid, signal);
21
49
  }
22
50
  const onTerminate = () => stopChildren('SIGTERM');
23
51
  export function watch(pid) {
24
52
  if (pid === undefined) {
25
- return;
53
+ return () => undefined;
26
54
  }
27
55
  if (groups.size === 0) {
28
56
  process.on('SIGTERM', onTerminate);
29
57
  }
30
58
  groups.add(pid);
59
+ const children = current.getStore();
60
+ children?.groups.add(pid);
61
+ return () => {
62
+ children?.groups.delete(pid);
63
+ unwatch(pid);
64
+ };
31
65
  }
32
- export function unwatch(pid) {
33
- if (pid === undefined) {
34
- return;
35
- }
66
+ function unwatch(pid) {
36
67
  groups.delete(pid);
37
68
  if (groups.size === 0) {
38
69
  process.off('SIGTERM', onTerminate);
package/dist/exec.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { spawn } from 'node:child_process';
3
- import { killTree, unwatch, watch } from './children.mjs';
3
+ import { killTree, refuseWhenStopped, watch } from './children.mjs';
4
4
  import { deadline, elapsedMs, minutesToMs } from './durations.mjs';
5
5
  import { keepTail } from './text.mjs';
6
6
  let observer;
@@ -60,15 +60,15 @@ export function run(command, args, options = {}) {
60
60
  const started = Date.now();
61
61
  const limit = options.timeoutMs ?? defaultTimeout.getStore() ?? DEFAULT_TIMEOUT_MS;
62
62
  return new Promise((resolve, reject) => {
63
+ refuseWhenStopped();
63
64
  const child = spawnChild(command, args, options);
64
- const pid = child.pid;
65
- watch(pid);
65
+ const unwatch = watch(child.pid);
66
66
  const output = collect(child, options);
67
67
  const timer = deadline(limit, () => killTree(child));
68
68
  let done = false;
69
69
  const release = () => {
70
70
  timer.clear();
71
- unwatch(pid);
71
+ unwatch();
72
72
  };
73
73
  const settle = (code) => {
74
74
  release();
package/dist/git.mjs CHANGED
@@ -88,6 +88,9 @@ export async function currentBranch(cwd) {
88
88
  export async function isAncestor(cwd, ancestor, descendant) {
89
89
  return succeeds(cwd, ['merge-base', '--is-ancestor', ancestor, descendant]);
90
90
  }
91
+ export async function isLanded(cwd, base, remote) {
92
+ return isAncestor(cwd, 'HEAD', remoteRef(remote, base));
93
+ }
91
94
  export async function deleteBranch(repo, branch) {
92
95
  return succeeds(repo.root, ['branch', '-D', branch]);
93
96
  }
package/dist/handover.mjs CHANGED
@@ -30,6 +30,10 @@ export function stop(job) {
30
30
  job.log.event('issue.stop', { phase: job.state.phase }, 'quiet');
31
31
  return jobReport(job, 'stopped');
32
32
  }
33
+ export function stopNow(log, issue, state, started) {
34
+ log.event('issue.stop', { phase: state.phase }, 'quiet');
35
+ return issueReport(issue.number, 'stopped', state, started);
36
+ }
33
37
  function jobReport(job, outcome, reason) {
34
38
  return issueReport(job.issue.number, outcome, job.state, job.started, reason);
35
39
  }
package/dist/hunt.mjs CHANGED
@@ -55,7 +55,11 @@ export function hunter(context) {
55
55
  hunt: async () => {
56
56
  hunting = true;
57
57
  try {
58
- await hunt(context).catch(() => undefined);
58
+ await hunt(context);
59
+ return true;
60
+ }
61
+ catch {
62
+ return false;
59
63
  }
60
64
  finally {
61
65
  hunting = false;
package/dist/land.mjs CHANGED
@@ -10,7 +10,7 @@ export function landApproved(job) {
10
10
  }
11
11
  export function resumeLanding(job, sha) {
12
12
  return landing(job, async () => {
13
- if ((await job.context.ports.revision(job.worktree)) !== sha) {
13
+ if (await holdsUnlandedFix(job, sha)) {
14
14
  job.mainBroken = true;
15
15
  job.repairing = true;
16
16
  return landLoop(job);
@@ -22,6 +22,14 @@ export function resumeLanding(job, sha) {
22
22
  return gate.fixed ? landLoop(job) : finishDone(job);
23
23
  });
24
24
  }
25
+ async function holdsUnlandedFix(job, sha) {
26
+ const { context, worktree } = job;
27
+ const { ports, config, base } = context;
28
+ if ((await ports.revision(worktree)) === sha) {
29
+ return false;
30
+ }
31
+ return !(await ports.isLanded(worktree, base, config.remote));
32
+ }
25
33
  function landing(job, work) {
26
34
  return oneAtATime(`${job.context.repo.root}#land`, work);
27
35
  }
package/dist/pipeline.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { enterReview } from './enter-review.mjs';
2
2
  import { branchName } from './git.mjs';
3
- import { claimedError, stop } from './handover.mjs';
3
+ import { runStopped } from './children.mjs';
4
+ import { claimedError, stop, stopNow } from './handover.mjs';
4
5
  import { implement } from './implement.mjs';
5
6
  import { openJob } from './open-job.mjs';
6
7
  import { reviewRound } from './review.mjs';
@@ -24,14 +25,18 @@ export async function processIssue(context, issue) {
24
25
  }
25
26
  log.event('issue.start', { title: issue.title, createdAt: issue.createdAt }, 'quiet');
26
27
  const state = startState(context, issue, saved);
28
+ let report;
27
29
  try {
28
- const report = await work(context, issue, log, state, saved === undefined, saved?.handedOver === true);
29
- log.event('issue.end', { outcome: report.outcome, reason: report.reason, ms: report.ms }, 'quiet');
30
- return report;
30
+ report = await work(context, issue, log, state, saved === undefined, saved?.handedOver === true);
31
31
  }
32
32
  catch (error) {
33
- return claimedError(context, log, issue, state, error, started);
33
+ if (!runStopped()) {
34
+ return claimedError(context, log, issue, state, error, started);
35
+ }
36
+ report = stopNow(log, issue, state, started);
34
37
  }
38
+ log.event('issue.end', { outcome: report.outcome, reason: report.reason, ms: report.ms }, 'quiet');
39
+ return report;
35
40
  }
36
41
  function startState(context, issue, saved) {
37
42
  if (saved === undefined) {
package/dist/ports.mjs CHANGED
@@ -4,7 +4,7 @@ import { run } from './exec.mjs';
4
4
  import { commitAll } from './git-commit.mjs';
5
5
  import { pushToBase, revertOnBase, squashOnto } from './git-land.mjs';
6
6
  import { abortMerge, commitMerge, startMerge } from './git-merge.mjs';
7
- import { deleteBranch, diff, discardChanges, disputeBranches, isDirty, latestSubject, push, resetToBase, revision, setAside, } from './git.mjs';
7
+ import { deleteBranch, diff, discardChanges, disputeBranches, isDirty, isLanded, latestSubject, push, resetToBase, revision, setAside, } from './git.mjs';
8
8
  import { canWrite, closeIssue, commentOnIssue, issueComments, openBlockers, openIssues, reopenIssue, threadComments, viewIssue, } from './github.mjs';
9
9
  import { huntWorktree } from './hunt.mjs';
10
10
  import { ensureLabel, setLabel } from './labels.mjs';
@@ -32,6 +32,7 @@ export const PORTS = {
32
32
  findWorktree,
33
33
  huntWorktree,
34
34
  isDirty,
35
+ isLanded,
35
36
  issueComments,
36
37
  latestSubject,
37
38
  openBlockers,
package/dist/protocol.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ export const STOP_POINTS = ['issue', 'round', 'now'];
1
2
  export function isNewer(candidate, current) {
2
3
  const { core: left, prerelease: leftPrerelease } = parseVersion(candidate);
3
4
  const { core: right, prerelease: rightPrerelease } = parseVersion(current);
package/dist/serve.mjs CHANGED
@@ -4,7 +4,7 @@ import { createServer } from 'node:http';
4
4
  import { dirname } from 'node:path';
5
5
  import { WebSocketServer } from 'ws';
6
6
  import { parseStartArgs } from './args.mjs';
7
- import { stopChildren } from './children.mjs';
7
+ 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';
@@ -13,7 +13,7 @@ import { listQuestions, questionSender, send } from './questions.mjs';
13
13
  import { runRepo } from './run.mjs';
14
14
  import { packageVersion } from './version.mjs';
15
15
  import { checkSocketPath, workerPaths } from './worker-paths.mjs';
16
- import { active, endStatus, requestStop, runControls, runsMessage, startRefusal, } from './worker-run.mjs';
16
+ import { active, endStatus, newRun, requestStop, runControls, runsMessage, startRefusal, stopPoint, } from './worker-run.mjs';
17
17
  import { broadcast, closer, listen, parseMessage, shutdown, } from './worker-server.mjs';
18
18
  const LOCK_WAIT_MS = 5_000;
19
19
  const IDLE_MS = 30_000;
@@ -54,18 +54,26 @@ export async function startWorker(options) {
54
54
  send(socket, { type: 'error', root, message: message(error) });
55
55
  return;
56
56
  }
57
- const run = { progress: startProgress(), stop: false };
57
+ const run = newRun(startProgress());
58
58
  runs.set(root, run);
59
59
  const update = (next) => {
60
60
  run.progress = next;
61
61
  broadcast(sockets, runs);
62
62
  };
63
63
  broadcast(sockets, runs);
64
- options
65
- .runRepo(root, args, runControls(run, update, () => void sendQuestions(root, () => sockets.clients)))
64
+ withRunChildren(run.children, () => options.runRepo(root, args, runControls(run, update, () => void sendQuestions(root, () => sockets.clients))))
66
65
  .then(() => update({ ...run.progress, status: endStatus(run) }), (error) => update({ ...run.progress, status: 'failed', error: message(error) }))
67
66
  .finally(closeWhenIdle);
68
67
  };
68
+ const stop = (socket, root, after) => {
69
+ const point = stopPoint(after);
70
+ if (point === undefined) {
71
+ send(socket, { type: 'error', root, message: `stop does not take --after ${String(after)}` });
72
+ }
73
+ else if (requestStop(runs.get(root), point)) {
74
+ broadcast(sockets, runs);
75
+ }
76
+ };
69
77
  const receive = (socket, value) => {
70
78
  if (value.type === 'hello') {
71
79
  if (isNewer(value.version, options.version)) {
@@ -77,9 +85,7 @@ export async function startWorker(options) {
77
85
  start(socket, value.root, value.args ?? []);
78
86
  }
79
87
  else if (value.type === 'stop') {
80
- if (requestStop(runs.get(value.root))) {
81
- broadcast(sockets, runs);
82
- }
88
+ stop(socket, value.root, value.after);
83
89
  }
84
90
  else if (value.type === 'questions') {
85
91
  void sendQuestions(value.root, () => [socket], value.id);
package/dist/watch.mjs CHANGED
@@ -31,9 +31,10 @@ async function huntIssues(hunter, running, stopped, signal) {
31
31
  const ready = await hunter.ready().catch(() => undefined);
32
32
  const waiting = ready?.filter((issue) => !running.has(issue.number)).length;
33
33
  if (waiting !== undefined && waiting < hunter.cap) {
34
- step = 0;
35
- await hunter.hunt();
36
- continue;
34
+ if (await hunter.hunt()) {
35
+ step = 0;
36
+ continue;
37
+ }
37
38
  }
38
39
  await hunter.wait(backOffMinutes(step), signal);
39
40
  step += 1;
@@ -1,10 +1,15 @@
1
+ import { runChildren, stopRunChildren } from './children.mjs';
1
2
  import { wakeableSleep } from './durations.mjs';
2
3
  import { applyEvent } from './progress.mjs';
4
+ import { STOP_POINTS } from './protocol.mjs';
5
+ export function newRun(progress) {
6
+ return { progress, children: runChildren() };
7
+ }
3
8
  export function active(run) {
4
9
  return run?.progress.status === 'running' || run?.progress.status === 'stopping';
5
10
  }
6
11
  export function endStatus(run) {
7
- if (run.stop) {
12
+ if (run.stop !== undefined) {
8
13
  return 'stopped';
9
14
  }
10
15
  return run.progress.error === undefined ? 'finished' : 'failed';
@@ -27,20 +32,29 @@ export function runsMessage(runs) {
27
32
  function endsIssue(record) {
28
33
  return record['kind'] === 'issue.end' || record['kind'] === 'issue.error';
29
34
  }
30
- export function requestStop(run) {
31
- if (run === undefined || run.progress.status !== 'running') {
35
+ export function stopPoint(after) {
36
+ return STOP_POINTS.find((point) => point === (after ?? 'issue'));
37
+ }
38
+ function earlier(after, than) {
39
+ return than === undefined || STOP_POINTS.indexOf(after) > STOP_POINTS.indexOf(than);
40
+ }
41
+ export function requestStop(run, after) {
42
+ if (run === undefined || !active(run) || !earlier(after, run.stop)) {
32
43
  return false;
33
44
  }
34
- run.stop = true;
45
+ run.stop = after;
35
46
  run.wake?.();
47
+ if (after === 'now') {
48
+ stopRunChildren(run.children);
49
+ }
36
50
  run.progress = { ...run.progress, status: 'stopping' };
37
51
  return true;
38
52
  }
39
53
  function runInterrupts(run) {
40
54
  return {
41
- afterIssue: () => run.stop,
42
- afterRound: () => run.stop,
43
- sleep: (ms) => wakeableSleep(run, ms, run.stop),
55
+ afterIssue: () => run.stop !== undefined,
56
+ afterRound: () => run.stop === 'round' || run.stop === 'now',
57
+ sleep: (ms) => wakeableSleep(run, ms, run.stop !== undefined),
44
58
  };
45
59
  }
46
60
  export function runControls(run, update, issueEnded) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-issue",
3
- "version": "6.0.1",
3
+ "version": "6.1.0",
4
4
  "description": "Autonomous issue harness driven by the pi SDK",
5
5
  "keywords": [
6
6
  "github",