next-issue 2.20.0 → 2.21.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
@@ -86,9 +86,19 @@ the run handles one issue. For each issue:
86
86
  check keeps asking for `checkGraceSeconds`, because a new pull request often
87
87
  shows no check yet, and only then goes straight to the review. The later
88
88
  gates of the same issue ask once. GitHub runs no check on a pull request
89
- that conflicts with its base, so no check and a conflict hand the issue to
90
- a person. While GitHub still works out whether the pull request can merge,
91
- the run asks again until `checkTimeoutMinutes`.
89
+ that conflicts with its base, so no check and a conflict start a merge of
90
+ the remote base into the branch. While GitHub still works out whether the
91
+ pull request can merge, the run asks again until `checkTimeoutMinutes`.
92
+ When git merges the base cleanly, the harness commits the merge with no
93
+ agent. When git reports conflicted files, the fixer gets the list, removes
94
+ the conflict markers, and the harness commits the merge. The harness then
95
+ pushes without force, and the run goes to step 6 again. The merge commit
96
+ runs the `pre-commit` and `commit-msg` hooks like any other commit. When
97
+ the branch already holds the base, the harness only pushes. Each attempt
98
+ comes from the check budget, `maxCiFixes`. With the budget used up, or when
99
+ the fixer leaves a conflict marker or the merge unresolved, or the fixer
100
+ fails, the harness undoes the merge and hands the issue to a person with the
101
+ reason `merge conflict`.
92
102
  7. On a red build, give the failed job logs to the fixer agent, then go to step
93
103
  6 again. The budget is `maxCiFixes`.
94
104
  8. Let the reviewer agent judge the diff, at most `diffMaxChars` of it. The
@@ -108,22 +118,25 @@ the run handles one issue. For each issue:
108
118
  finding.
109
119
  10. No open finding means approval: mark the pull request ready for review and
110
120
  set `status:done`. The merge stays with you, unless you pass `--merge`:
111
- then the harness squash-merges the pull request, deletes the remote
112
- branch, waits until the pull request is merged, for example through a
113
- merge queue, and then waits for the checks that the merge commit starts on
114
- the base branch. A cancelled run there is no failure. When those checks
115
- fail, the fixer gets their logs on a branch reset to the base branch, from
116
- the check budget, and the harness force-pushes the fix, reopens the issue
117
- and opens a new pull request that goes to step 6 again, with fresh review
118
- rounds. The next issue waits for this, and a second ctrl-c stops only after
119
- it. The worktree, the local branch and the state file go away. A resume
120
- whose pull request is already merged goes straight to the wait for the
121
- checks on the base branch, and opens no new pull request. A resume that
122
- stopped inside the fix of the base branch carries on with that fix, and
123
- uses no more of the check budget. Without `--merge`, the remote branch
124
- stays, and a resume whose pull request a person merged sets
125
- `status:done` and waits for no check. A problem that the reviewer leaves out
126
- of the findings thus gets an approval: only the findings count.
121
+ then the harness first asks whether the pull request conflicts with its
122
+ base. A conflict starts the merge of the base from step 6, and the run
123
+ goes to step 6 again. With no conflict, the harness squash-merges the pull
124
+ request, deletes the remote branch, waits until the pull request is merged,
125
+ for example through a merge queue, and then waits for the checks that the
126
+ merge commit starts on the base branch. A cancelled run there is no
127
+ failure. When those checks fail, the fixer gets their logs on a branch
128
+ reset to the base branch, from the check budget, and the harness
129
+ force-pushes the fix, reopens the issue and opens a new pull request that
130
+ goes to step 6 again, with fresh review rounds. The next issue waits for
131
+ this, and a second ctrl-c stops only after it. The worktree, the local
132
+ branch and the state file go away. A resume whose pull request is already
133
+ merged goes straight to the wait for the checks on the base branch, and
134
+ opens no new pull request. A resume that stopped inside the fix of the
135
+ base branch carries on with that fix, and uses no more of the check
136
+ budget. Without `--merge`, the remote branch stays, and a resume whose
137
+ pull request a person merged sets `status:done` and waits for no check. A
138
+ problem that the reviewer leaves out of the findings thus gets an
139
+ approval: only the findings count.
127
140
  11. With an open finding, run the fixer agent and go to step 6 again. The
128
141
  budget is `maxReviewRounds`. A fixer that holds every finding to be wrong
129
142
  changes nothing and gives its reason instead. The reason goes on the pull
@@ -0,0 +1,57 @@
1
+ import { CODING } from './agents.mjs';
2
+ import { commitWork } from './fix.mjs';
3
+ import { escalate, finish } from './handover.mjs';
4
+ import { fixPrompt, parseCommitSubject } from './prompts.mjs';
5
+ import { writeState } from './state.mjs';
6
+ export async function conflicts(job) {
7
+ const { repo, config, ports } = job.context;
8
+ return ports.prConflicts(repo, job.state.pr, {
9
+ intervalSeconds: config.checkIntervalSeconds,
10
+ timeoutMs: config.checkTimeoutMinutes * 60_000,
11
+ });
12
+ }
13
+ export async function resolveConflict(job) {
14
+ const { context, log, state, worktree, branch } = job;
15
+ const { repo, config, ports } = context;
16
+ if (state.ciFixes >= config.maxCiFixes) {
17
+ await escalate(job, `The pull request conflicts with its base, and the limit of ${config.maxCiFixes} check fixes was reached.`);
18
+ return { done: true, report: await finish(job, 'needs-human', 'merge conflict') };
19
+ }
20
+ state.ciFixes += 1;
21
+ await writeState(repo, state);
22
+ const before = await ports.revision(worktree);
23
+ const files = await log.step('merge-base', { base: context.base }, () => ports.startMerge(worktree, context.base, config.remote));
24
+ log.event('merge-base.conflicts', { files }, 'quiet');
25
+ if (files !== undefined && !(await mergeBase(job, before, files))) {
26
+ await ports.abortMerge(worktree, before);
27
+ await escalate(job, 'The fixer left the conflict with the base unresolved.');
28
+ return { done: true, report: await finish(job, 'needs-human', 'merge conflict') };
29
+ }
30
+ await ports.push(worktree, config.remote, branch);
31
+ return { done: false, fixed: true };
32
+ }
33
+ async function mergeBase(job, before, files) {
34
+ const { context, issue, log, state, worktree } = job;
35
+ const { config, ports } = context;
36
+ try {
37
+ let text = '';
38
+ if (files.length > 0) {
39
+ const reason = 'The pull request conflicts with its base.';
40
+ const result = await log.step('fix', { reason, cause: 'conflict', round: state.ciFixes, limit: config.maxCiFixes }, () => ports.runAgent(log, {
41
+ name: 'fixer',
42
+ cwd: worktree,
43
+ prompt: fixPrompt(issue, reason, files.join('\n'), [], 'conflict', job.reply),
44
+ profile: CODING,
45
+ model: config.models.fixer,
46
+ timeoutMs: config.agentTimeoutMinutes * 60_000,
47
+ }));
48
+ text = result.text;
49
+ }
50
+ const subject = parseCommitSubject(text, `chore: merge ${config.remote}/${context.base}`);
51
+ return await commitWork(job, before, subject, 'fixer', (cwd, message) => ports.commitMerge(cwd, files, message));
52
+ }
53
+ catch (error) {
54
+ await ports.abortMerge(worktree, before);
55
+ throw error;
56
+ }
57
+ }
package/dist/fix.mjs CHANGED
@@ -39,12 +39,13 @@ export async function fixRound(job, reason, detail, earlier, kind, force = false
39
39
  await ports.push(worktree, config.remote, branch, force);
40
40
  return { committed: true };
41
41
  }
42
- export async function commitWork(job, before, subject, role) {
42
+ export async function commitWork(job, before, subject, role, commit) {
43
43
  const { context, issue, log, state, worktree, comments } = job;
44
44
  const { repo, config, ports } = context;
45
+ const commitChanges = commit ?? ports.commitAll;
45
46
  for (;;) {
46
47
  try {
47
- if (await ports.commitAll(worktree, `${subject}\n\nRefs #${issue.number}`)) {
48
+ if (await commitChanges(worktree, `${subject}\n\nRefs #${issue.number}`)) {
48
49
  log.event('commit', { subject });
49
50
  }
50
51
  if ((await ports.revision(worktree)) === before) {
package/dist/gate.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { conflicts, resolveConflict } from './conflict.mjs';
1
2
  import { fixRound } from './fix.mjs';
2
3
  import { escalate, finish } from './handover.mjs';
3
4
  import { history } from './job.mjs';
@@ -19,13 +20,8 @@ export async function checkGate(job) {
19
20
  await escalate(job, 'The checks did not finish in time.');
20
21
  return { done: true, report: await finish(job, 'needs-human', 'checks timeout') };
21
22
  }
22
- if (checks === 'none' &&
23
- (await ports.prConflicts(repo, state.pr, {
24
- intervalSeconds: config.checkIntervalSeconds,
25
- timeoutMs: config.checkTimeoutMinutes * 60_000,
26
- }))) {
27
- await escalate(job, 'The pull request conflicts with its base, so no checks ran.');
28
- return { done: true, report: await finish(job, 'needs-human', 'merge conflict') };
23
+ if (checks === 'none' && (await conflicts(job))) {
24
+ return resolveConflict(job);
29
25
  }
30
26
  if (checks === 'moved') {
31
27
  await escalate(job, 'Someone pushed to the pull request while the harness waited for the checks.');
package/dist/git.mjs CHANGED
@@ -115,6 +115,10 @@ export async function commitAll(cwd, message) {
115
115
  return false;
116
116
  }
117
117
  await must('git', ['add', '-A'], { cwd });
118
+ await commitStaged(cwd, message);
119
+ return true;
120
+ }
121
+ async function commitStaged(cwd, message) {
118
122
  const folder = await mkdtemp(join(tmpdir(), 'next-issue-message-'));
119
123
  const file = join(folder, 'COMMIT_EDITMSG');
120
124
  try {
@@ -126,7 +130,6 @@ export async function commitAll(cwd, message) {
126
130
  finally {
127
131
  await rm(folder, { recursive: true, force: true });
128
132
  }
129
- return true;
130
133
  }
131
134
  async function runHook(cwd, name, ...args) {
132
135
  try {
@@ -163,6 +166,44 @@ export async function resetToBase(cwd, base, remote) {
163
166
  await must('git', ['fetch', remote, base], { cwd });
164
167
  await must('git', ['reset', '--hard', `${remote}/${base}`], { cwd });
165
168
  }
169
+ export async function startMerge(cwd, base, remote) {
170
+ await must('git', ['fetch', remote, base], { cwd });
171
+ const result = await run('git', ['merge', '--no-ff', '--no-commit', `${remote}/${base}`], {
172
+ cwd,
173
+ });
174
+ if (result.code === 0) {
175
+ return (await merging(cwd)) ? [] : undefined;
176
+ }
177
+ const listed = await must('git', ['diff', '--name-only', '--diff-filter=U'], { cwd });
178
+ const files = listed.split('\n').filter((line) => line.length > 0);
179
+ if (files.length === 0) {
180
+ throw new Error(`git merge ${remote}/${base} failed: ${result.stderr.trim()}`);
181
+ }
182
+ return files;
183
+ }
184
+ async function merging(cwd) {
185
+ const result = await run('git', ['rev-parse', '--quiet', '--verify', 'MERGE_HEAD'], { cwd });
186
+ return result.code === 0;
187
+ }
188
+ const CONFLICT_MARKER = /^(?:<{7}|>{7})(?: |$)/m;
189
+ export async function commitMerge(cwd, files, message) {
190
+ if (!(await merging(cwd))) {
191
+ return false;
192
+ }
193
+ for (const file of files) {
194
+ const text = await readFile(join(cwd, file), 'utf8').catch(() => '');
195
+ if (CONFLICT_MARKER.test(text)) {
196
+ return false;
197
+ }
198
+ }
199
+ await must('git', ['add', '-A'], { cwd });
200
+ await commitStaged(cwd, message);
201
+ return true;
202
+ }
203
+ export async function abortMerge(cwd, target) {
204
+ await must('git', ['reset', '--hard', target], { cwd });
205
+ await must('git', ['clean', '-fd'], { cwd });
206
+ }
166
207
  export async function latestSubject(cwd, base, remote) {
167
208
  const subject = await must('git', ['log', '-1', '--format=%s', `${remote}/${base}..HEAD`], {
168
209
  cwd,
package/dist/ports.mjs CHANGED
@@ -1,14 +1,16 @@
1
1
  import { runAgent } from './agents.mjs';
2
2
  import { run } from './exec.mjs';
3
- import { addWorktree, commitAll, deleteBranch, diff, discardChanges, disputeBranches, findWorktree, isDirty, latestSubject, push, removeWorktree, resetToBase, revision, setAside, } from './git.mjs';
3
+ import { abortMerge, addWorktree, commitAll, commitMerge, deleteBranch, diff, discardChanges, disputeBranches, findWorktree, isDirty, latestSubject, push, removeWorktree, resetToBase, revision, setAside, startMerge, } from './git.mjs';
4
4
  import { canWrite, commentOnIssue, commentOnPr, createPr, failedBaseLogs, failedCheckLogs, findPr, issueComments, markPrReady, mergeCommit, mergePr, openBlockers, openIssues, prConflicts, prMerged, reopenIssue, setLabel, threadComments, viewIssue, waitForBaseChecks, waitForChecks, } from './github.mjs';
5
5
  import { detectSetupCommand } from './setup.mjs';
6
6
  export const PORTS = {
7
+ abortMerge,
7
8
  addWorktree,
8
9
  canWrite,
9
10
  commentOnIssue,
10
11
  commentOnPr,
11
12
  commitAll,
13
+ commitMerge,
12
14
  createPr,
13
15
  deleteBranch,
14
16
  detectSetupCommand,
@@ -38,6 +40,7 @@ export const PORTS = {
38
40
  runSetup: async (command, cwd, timeoutMs) => run('bash', ['-lc', command], { cwd, timeoutMs }),
39
41
  setAside,
40
42
  setLabel,
43
+ startMerge,
41
44
  threadComments,
42
45
  viewIssue,
43
46
  waitForBaseChecks,
package/dist/prompts.mjs CHANGED
@@ -87,6 +87,16 @@ const DISPUTE_RULE = [
87
87
  'If only some of the findings are wrong, fix the correct ones and change nothing for the wrong ones. Write no `unrelated` line in that round. The next round holds only the findings that are left, and you can dispute them there.',
88
88
  'The reviewer reads that line in the next round. Do not make a change that you hold to be wrong only to end the round.',
89
89
  ].join('\n');
90
+ const CONFLICT_RULE = [
91
+ 'The harness merged the base branch into this branch, and git left conflict markers in the files listed under Detail.',
92
+ 'Resolve each conflict so that both the change of this issue and the change on the base branch survive, and remove every conflict marker.',
93
+ 'Leave the merge in progress. The harness commits the merge and pushes it.',
94
+ ].join('\n');
95
+ const KIND_RULES = {
96
+ checks: UNRELATED_RULE,
97
+ review: DISPUTE_RULE,
98
+ conflict: CONFLICT_RULE,
99
+ };
90
100
  export function fixPrompt(issue, reason, detail, earlier, kind, reply = []) {
91
101
  return [
92
102
  `You are the fixer. Correct the work for issue #${issue.number}: ${issue.title}.`,
@@ -102,7 +112,7 @@ export function fixPrompt(issue, reason, detail, earlier, kind, reply = []) {
102
112
  replyBlock(reply),
103
113
  '## Rules',
104
114
  'Fix the cause, not the symptom. Keep the change minimal.',
105
- kind === 'checks' ? UNRELATED_RULE : DISPUTE_RULE,
115
+ KIND_RULES[kind],
106
116
  COMMIT_RULES,
107
117
  ]
108
118
  .filter((part) => part.length > 0)
package/dist/review.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import { REVIEWING } from './agents.mjs';
2
+ import { conflicts, resolveConflict } from './conflict.mjs';
2
3
  import { fixRound } from './fix.mjs';
3
4
  import { checkGate } from './gate.mjs';
4
5
  import { escalate, finish } from './handover.mjs';
@@ -65,6 +66,10 @@ export async function reviewRound(job) {
65
66
  log.event('pull-request.undraft.fail', { pr }, 'quiet');
66
67
  }
67
68
  if (context.merge) {
69
+ if (await conflicts(job)) {
70
+ const resolved = await resolveConflict(job);
71
+ return resolved.done ? { done: true, report: resolved.report } : { done: false };
72
+ }
68
73
  if (!(await ports.mergePr(repo, pr))) {
69
74
  await escalate(job, 'The merge of the pull request failed.');
70
75
  return { done: true, report: await finish(job, 'needs-human', 'merge failed') };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-issue",
3
- "version": "2.20.0",
3
+ "version": "2.21.0",
4
4
  "description": "Autonomous issue harness driven by the pi SDK",
5
5
  "keywords": [
6
6
  "github",