next-issue 5.4.0 → 5.6.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
@@ -259,7 +259,7 @@ get the full summary object there instead, so the harness fits in a pipe. All
259
259
  progress goes to standard error.
260
260
 
261
261
  Recorded for each step: `claim`, `comments`, `reply`, `worktree`, `setup`,
262
- `implement`, `review`, `fix`, `commit-fix`, `rebase`, `land`, `main-checks` and `revert`, each with a
262
+ `implement`, `review`, `fix`, `commit-fix`, `rebase`, `land`, `main-checks`, `revert` and `hunt`, each with a
263
263
  `.start`, `.end` or `.error` event and a duration in `ms`. Every `git` and `gh`
264
264
  command is recorded with its exit code and duration. A `tool` event holds the
265
265
  tool name, and for a `bash` tool the command too. Each agent call adds a `usage`
@@ -325,6 +325,7 @@ because no pull request is opened.
325
325
  "remote": "origin",
326
326
  "issueLimit": 100,
327
327
  "concurrency": 1,
328
+ "huntCap": 1,
328
329
  "maxCiFixes": 5,
329
330
  "maxCommitFixes": 10,
330
331
  "maxReviewRounds": 5,
@@ -358,6 +359,7 @@ because no pull request is opened.
358
359
  | `remote` | `origin` | The git remote for the repository and the branches |
359
360
  | `issueLimit` | `100` | The maximum number of open issues to read. With `labels.ready` set, only issues with the ready, in-progress or in-review label count |
360
361
  | `concurrency` | `1` | The most issues that a run handles at the same time |
362
+ | `huntCap` | `1` | With `--hunt`, the hunter hunts only while fewer ready issues than this wait besides the running ones |
361
363
  | `maxCiFixes` | `5` | The budget for check fixes per issue |
362
364
  | `maxCommitFixes` | `10` | The budget for fixes of a commit that a git hook stopped, per issue |
363
365
  | `maxReviewRounds` | `5` | The budget for review rounds per issue |
@@ -443,21 +445,22 @@ state of an issue that a label still holds back stays as it was.
443
445
 
444
446
  ## Commands and options
445
447
 
446
- | Argument | Effect |
447
- | ----------- | ------------------------------------------------------------------------------------ |
448
- | `init` | Write the config file with the defaults |
449
- | `serve` | Run the worker of this repository that pi extensions talk to, see [Worker](#worker) |
450
- | `stop` | Stop the worker of this repository, see [Worker](#worker) |
451
- | `logs` | Show the latest run log and follow it, see [Observability](#observability) |
452
- | `--force` | Replace an existing config file, with `init` |
453
- | `--once` | Stop after the first handled issue |
454
- | `--max <n>` | Handle at most n issues |
455
- | `--watch` | Keep running, and read the open issues again after each pass |
456
- | `--reset` | Drop the saved budgets and findings, and keep the rest of the state |
457
- | `--json` | Print the machine summary instead of the text one, or the raw JSON lines with `logs` |
458
- | `--verbose` | Show every command and all agent output |
459
- | `--quiet` | Show only the milestones and the summary |
460
- | `--help` | Show the option list |
448
+ | Argument | Effect |
449
+ | ----------- | -------------------------------------------------------------------------------------- |
450
+ | `init` | Write the config file with the defaults |
451
+ | `serve` | Run the worker of this repository that pi extensions talk to, see [Worker](#worker) |
452
+ | `stop` | Stop the worker of this repository, see [Worker](#worker) |
453
+ | `logs` | Show the latest run log and follow it, see [Observability](#observability) |
454
+ | `--force` | Replace an existing config file, with `init` |
455
+ | `--once` | Stop after the first handled issue |
456
+ | `--max <n>` | Handle at most n issues |
457
+ | `--watch` | Keep running, and read the open issues again after each pass |
458
+ | `--hunt` | Hunt the repository for bugs alongside the issues, see [Hunt for bugs](#hunt-for-bugs) |
459
+ | `--reset` | Drop the saved budgets and findings, and keep the rest of the state |
460
+ | `--json` | Print the machine summary instead of the text one, or the raw JSON lines with `logs` |
461
+ | `--verbose` | Show every command and all agent output |
462
+ | `--quiet` | Show only the milestones and the summary |
463
+ | `--help` | Show the option list |
461
464
 
462
465
  The exit code is 1 when an issue needs a person or failed, and 0 when it does
463
466
  not, so a cron job or a CI step can read the result of the run.
@@ -490,6 +493,17 @@ The watch stops:
490
493
  branch failed;
491
494
  - when the open issues cannot be read.
492
495
 
496
+ ## Hunt for bugs
497
+
498
+ 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:
499
+
500
+ - works in its own worktree at `.worktrees/hunt`, at the remote base branch, which the harness fetches and resets before each hunt;
501
+ - follows `next-issue.hunt.md` in the repository root, or a short built-in prompt when the file is missing;
502
+ - files one issue, for its most serious finding, with the `labels.ready` label and the sections of the [issue format](#issue-format), after a search for duplicates. It files nothing when it finds no real problem or a duplicate exists;
503
+ - runs no test suite and changes no file. It gets the tools of the reviewer.
504
+
505
+ 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.
506
+
493
507
  ## Worker
494
508
 
495
509
  `next-issue serve` runs one worker for the repository it is run in. The
package/dist/args.mjs CHANGED
@@ -4,6 +4,7 @@ const SWITCHES = new Map([
4
4
  ['--json', 'json'],
5
5
  ['--help', 'help'],
6
6
  ['--watch', 'watch'],
7
+ ['--hunt', 'hunt'],
7
8
  ['--reset', 'reset'],
8
9
  ['--once', 'once'],
9
10
  ]);
@@ -21,6 +22,7 @@ export const USAGE = `next-issue [command] [options]
21
22
  --once stop after the first handled issue
22
23
  --max <n> handle at most n issues
23
24
  --watch keep running and read the issues again after each pass
25
+ --hunt hunt the repository for bugs and file issues alongside the issues, turns on --watch
24
26
  --reset drop the saved budgets and findings first
25
27
  --json print the machine summary instead of the text one, or raw lines with logs
26
28
  --force replace an existing config file, with init
@@ -37,6 +39,7 @@ export function parseArgs(argv) {
37
39
  reset: false,
38
40
  force: false,
39
41
  watch: false,
42
+ hunt: false,
40
43
  level: 'normal',
41
44
  };
42
45
  for (let index = 0; index < argv.length; index += 1) {
@@ -57,6 +60,7 @@ export function parseArgs(argv) {
57
60
  throw new Error(`Unknown argument: ${arg}`);
58
61
  }
59
62
  }
63
+ args.watch ||= args.hunt;
60
64
  return args;
61
65
  }
62
66
  function command(arg) {
package/dist/config.mjs CHANGED
@@ -6,6 +6,7 @@ const DEFAULTS = {
6
6
  remote: 'origin',
7
7
  issueLimit: 100,
8
8
  concurrency: 1,
9
+ huntCap: 1,
9
10
  maxCiFixes: 5,
10
11
  maxCommitFixes: 10,
11
12
  maxReviewRounds: 5,
@@ -35,6 +36,7 @@ const FIELDS = [...Object.keys(DEFAULTS), 'setupCommand'];
35
36
  const WHOLE_FIELDS = [
36
37
  ['issueLimit', 1],
37
38
  ['concurrency', 1],
39
+ ['huntCap', 1],
38
40
  ['maxCiFixes', 0],
39
41
  ['maxCommitFixes', 0],
40
42
  ['maxReviewRounds', 1],
package/dist/hunt.mjs ADDED
@@ -0,0 +1,69 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { setTimeout as delay } from 'node:timers/promises';
4
+ import { REVIEWING } from './agents.mjs';
5
+ import { minutesToMs } from './durations.mjs';
6
+ import { readTextIfPresent } from './files.mjs';
7
+ import { git, remoteRef, resetHard } from './git.mjs';
8
+ import { oneAtATime } from './lock.mjs';
9
+ import { huntPrompt } from './prompts.mjs';
10
+ import { skipReason } from './skip.mjs';
11
+ import { WORKTREE_DIR } from './worktree.mjs';
12
+ export const HUNT_FILE = 'next-issue.hunt.md';
13
+ const FILED = /^issue:\s*(\S+)\s*$/m;
14
+ export async function huntWorktree(repo, base, remote) {
15
+ const path = join(repo.root, WORKTREE_DIR, 'hunt');
16
+ const target = remoteRef(remote, base);
17
+ await git(repo.root, ['fetch', remote]);
18
+ await git(repo.root, ['worktree', 'prune']);
19
+ if (existsSync(join(path, '.git'))) {
20
+ await resetHard(path, target);
21
+ }
22
+ else {
23
+ await git(repo.root, ['worktree', 'add', '--detach', path, target]);
24
+ }
25
+ return path;
26
+ }
27
+ export async function hunt(context) {
28
+ const { repo, config, base, recorder, ports } = context;
29
+ await recorder.step('hunt', {}, async () => {
30
+ const cwd = await oneAtATime(repo.root, () => ports.huntWorktree(repo, base, config.remote));
31
+ const instructions = await readTextIfPresent(join(repo.root, HUNT_FILE));
32
+ const result = await ports.runAgent(recorder, {
33
+ name: 'hunter',
34
+ cwd,
35
+ prompt: huntPrompt(instructions, config.labels.ready),
36
+ profile: REVIEWING,
37
+ timeoutMs: minutesToMs(config.agentTimeoutMinutes),
38
+ });
39
+ recorder.event('hunt.issue', { url: FILED.exec(result.text)?.[1] ?? 'none' }, 'quiet');
40
+ });
41
+ }
42
+ export function hunter(context) {
43
+ const { repo, config, recorder, ports } = context;
44
+ const { ready } = config.labels;
45
+ const labels = ready.length === 0 ? [] : [ready];
46
+ let hunting = false;
47
+ return {
48
+ cap: config.huntCap,
49
+ ready: async () => {
50
+ const open = (await ports.openIssues(repo, config.issueLimit, labels)).filter((issue) => skipReason(issue, config, false) === undefined);
51
+ const blockers = await Promise.all(open.map((issue) => ports.openBlockers(repo, issue.number)));
52
+ return open.filter((_issue, index) => blockers[index].length === 0);
53
+ },
54
+ hunt: async () => {
55
+ hunting = true;
56
+ try {
57
+ await hunt(context).catch(() => undefined);
58
+ }
59
+ finally {
60
+ hunting = false;
61
+ }
62
+ },
63
+ wait: (minutes, signal) => {
64
+ recorder.event('hunt.wait', { minutes });
65
+ return delay(minutesToMs(minutes), undefined, { signal }).catch(() => undefined);
66
+ },
67
+ busy: () => hunting,
68
+ };
69
+ }
@@ -3,10 +3,13 @@ import { wakeableSleep } from './durations.mjs';
3
3
  export function listenForInterrupts(say) {
4
4
  let presses = 0;
5
5
  const sleeper = {};
6
+ let hunting = () => false;
6
7
  const press = () => {
7
8
  presses += 1;
8
9
  if (presses === 1 && sleeper.wake !== undefined) {
9
- say('Stopping now.');
10
+ say(hunting()
11
+ ? 'Stopping after the current hunt. Press Ctrl-C twice more to stop now.'
12
+ : 'Stopping now.');
10
13
  sleeper.wake();
11
14
  }
12
15
  else if (presses === 1) {
@@ -24,6 +27,9 @@ export function listenForInterrupts(say) {
24
27
  return {
25
28
  afterIssue: () => presses >= 1,
26
29
  afterRound: () => presses >= 2,
27
- sleep: (ms) => wakeableSleep(sleeper, ms, presses >= 1),
30
+ sleep: (ms, busy = () => false) => {
31
+ hunting = busy;
32
+ return wakeableSleep(sleeper, ms, presses >= 1);
33
+ },
28
34
  };
29
35
  }
package/dist/ports.mjs CHANGED
@@ -6,6 +6,7 @@ import { pushToBase, revertOnBase, squashOnto } from './git-land.mjs';
6
6
  import { abortMerge, commitMerge, startMerge } from './git-merge.mjs';
7
7
  import { deleteBranch, diff, discardChanges, disputeBranches, isDirty, latestSubject, push, resetToBase, revision, setAside, } from './git.mjs';
8
8
  import { canWrite, closeIssue, commentOnIssue, issueComments, openBlockers, openIssues, reopenIssue, threadComments, viewIssue, } from './github.mjs';
9
+ import { huntWorktree } from './hunt.mjs';
9
10
  import { setLabel } from './labels.mjs';
10
11
  import { detectSetupCommand } from './setup.mjs';
11
12
  import { addWorktree, findWorktree, removeWorktree } from './worktree.mjs';
@@ -27,6 +28,7 @@ export const PORTS = {
27
28
  disputeBranches,
28
29
  failedBaseLogs,
29
30
  findWorktree,
31
+ huntWorktree,
30
32
  isDirty,
31
33
  issueComments,
32
34
  latestSubject,
package/dist/prompts.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { ISSUE_SECTIONS } from './issue-format.mjs';
1
2
  import { paragraphs } from './text.mjs';
2
3
  const COMMIT_RULES = [
3
4
  'Do not run git, gh or any push command. The harness commits for you.',
@@ -149,3 +150,25 @@ export function history(rounds) {
149
150
  : `${head}\nThe fixer disputed every finding of this round: ${round.dispute}`;
150
151
  });
151
152
  }
153
+ const HUNT_DEFAULT = [
154
+ 'Read the whole repository and look for bugs: wrong results, unhandled errors, races, security holes and documentation that contradicts the code.',
155
+ 'Prove each finding from the code. A guess is not a finding.',
156
+ ].join('\n');
157
+ export function huntPrompt(instructions, ready) {
158
+ const kept = instructions?.trim() ?? '';
159
+ const label = ready === '' ? '' : ` --label "${ready}"`;
160
+ return paragraphs([
161
+ 'You are the hunter. Find the most serious problem in this repository and file one issue for it.',
162
+ `## Instructions\n${kept === '' ? HUNT_DEFAULT : kept}`,
163
+ '## Rules',
164
+ [
165
+ 'Run no test suite and no build. Change no file. Do not commit or push.',
166
+ 'File one issue only, for your most serious finding. File nothing when you find no real problem.',
167
+ 'Before you file it, search the open and closed issues for a duplicate, for example with `gh issue list --state all --search "<words>"`. File nothing when a duplicate exists.',
168
+ `File it with \`gh issue create${label} --title "<conventional commit subject>" --body-file -\`.`,
169
+ `The body holds these sections as \`##\` headings, in this order: ${ISSUE_SECTIONS.map((section) => `\`## ${section}\``).join(', ')}.`,
170
+ 'Give the file and line of each piece of evidence. Leave the Open questions section empty.',
171
+ 'End your final message with one line in the form `issue: <url of the new issue>`, or `issue: none` when you filed nothing.',
172
+ ].join('\n'),
173
+ ]);
174
+ }
package/dist/run.mjs CHANGED
@@ -7,6 +7,7 @@ import { writeJson } from './files.mjs';
7
7
  import { slug } from './gh.mjs';
8
8
  import { detectRepo } from './git.mjs';
9
9
  import { defaultBranch } from './github.mjs';
10
+ import { hunter } from './hunt.mjs';
10
11
  import { exitCode, runIssues } from './loop.mjs';
11
12
  import { Recorder } from './observe.mjs';
12
13
  import { pendingIssues } from './pending.mjs';
@@ -45,7 +46,7 @@ async function handle(args, config, repo, recorder, controls) {
45
46
  };
46
47
  const print = controls.print ?? (async () => undefined);
47
48
  const { reports, stoppedAt, error } = args.watch
48
- ? await watchRun(args, loop, recorder, interrupts, print)
49
+ ? await watchRun(args, loop, context, interrupts, print)
49
50
  : await runIssues(args, loop);
50
51
  recordLoopEnd(recorder, stoppedAt, error);
51
52
  const summary = await endRun(recorder, reports);
@@ -54,16 +55,19 @@ async function handle(args, config, repo, recorder, controls) {
54
55
  }
55
56
  return error === undefined ? exitCode(reports) : 1;
56
57
  }
57
- function watchRun(args, loop, recorder, interrupts, print) {
58
+ function watchRun(args, loop, context, interrupts, print) {
59
+ const { recorder } = context;
58
60
  let passStart = recorder.summary();
61
+ const hunting = args.hunt ? hunter(context) : undefined;
59
62
  return watchIssues(args, {
60
63
  ...loop,
61
64
  wait: (minutes) => {
62
65
  recorder.event('watch.wait', { minutes }, 'quiet');
63
66
  passStart = recorder.summary();
64
- return interrupts.sleep(minutesToMs(minutes));
67
+ return interrupts.sleep(minutesToMs(minutes), () => hunting?.busy() === true);
65
68
  },
66
69
  passEnd: (issues) => print({ ...recorder.summary(passStart), issues }),
70
+ ...(hunting === undefined ? {} : { hunter: hunting }),
67
71
  });
68
72
  }
69
73
  function recordLoopEnd(recorder, stoppedAt, error) {
package/dist/watch.mjs CHANGED
@@ -1,6 +1,45 @@
1
1
  import { handled, runIssues } from './loop.mjs';
2
2
  const BACK_OFF_MINUTES = [1, 2, 3, 5, 10];
3
3
  export async function watchIssues(args, watch) {
4
+ const running = new Set();
5
+ const ended = new AbortController();
6
+ const hunting = watch.hunter === undefined
7
+ ? undefined
8
+ : huntIssues(watch.hunter, running, () => ended.signal.aborted || watch.stop(), ended.signal);
9
+ try {
10
+ return await watchPasses(args, {
11
+ ...watch,
12
+ process: async (issue) => {
13
+ running.add(issue.number);
14
+ try {
15
+ return await watch.process(issue);
16
+ }
17
+ finally {
18
+ running.delete(issue.number);
19
+ }
20
+ },
21
+ });
22
+ }
23
+ finally {
24
+ ended.abort();
25
+ await hunting;
26
+ }
27
+ }
28
+ async function huntIssues(hunter, running, stopped, signal) {
29
+ let step = 0;
30
+ while (!stopped()) {
31
+ const ready = await hunter.ready().catch(() => undefined);
32
+ const waiting = ready?.filter((issue) => !running.has(issue.number)).length;
33
+ if (waiting !== undefined && waiting < hunter.cap) {
34
+ step = 0;
35
+ await hunter.hunt();
36
+ continue;
37
+ }
38
+ await hunter.wait(backOffMinutes(step), signal);
39
+ step += 1;
40
+ }
41
+ }
42
+ async function watchPasses(args, watch) {
4
43
  const limit = args.once ? 1 : args.max;
5
44
  const reports = [];
6
45
  const failed = new Set();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-issue",
3
- "version": "5.4.0",
3
+ "version": "5.6.0",
4
4
  "description": "Autonomous issue harness driven by the pi SDK",
5
5
  "keywords": [
6
6
  "github",