next-issue 6.6.118 → 6.6.120

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.
@@ -5,7 +5,9 @@ import { commitEverything } from './git-commit.mjs';
5
5
  import { fetchBase, git, isAncestor, resetHard, succeeds } from './git.mjs';
6
6
  export async function startMerge(cwd, base, remote) {
7
7
  const target = await git(cwd, ['rev-parse', await fetchBase(cwd, base, remote)]);
8
- const result = await run('git', ['merge', '--no-ff', '--no-commit', target], { cwd });
8
+ const result = await run('git', [...NO_RERERE, 'merge', '--no-ff', '--no-commit', target], {
9
+ cwd,
10
+ });
9
11
  if (succeeded(result)) {
10
12
  return { target, files: (await merging(cwd)) ? [] : undefined, unmarked: [] };
11
13
  }
@@ -15,12 +17,14 @@ export async function startMerge(cwd, base, remote) {
15
17
  }
16
18
  const unmarked = [];
17
19
  for (const file of files) {
18
- if (!(await hasConflictMarkers(cwd, [file]))) {
20
+ if (!(await hasConflictMarkers(cwd, [file], 'HEAD'))) {
19
21
  unmarked.push(file);
20
22
  }
21
23
  }
22
24
  return { target, files, unmarked };
23
25
  }
26
+ // A recorded resolution would hide the conflict markers from the fixer.
27
+ const NO_RERERE = ['-c', 'rerere.enabled=false', '-c', 'rerere.autoUpdate=false'];
24
28
  async function unmergedFiles(cwd) {
25
29
  const args = ['diff', '--name-only', '-z', '--diff-filter=U'];
26
30
  const listed = await run('git', args, { cwd });
@@ -32,13 +36,30 @@ async function unmergedFiles(cwd) {
32
36
  async function merging(cwd) {
33
37
  return succeeds(cwd, ['rev-parse', '--quiet', '--verify', 'MERGE_HEAD']);
34
38
  }
35
- const CONFLICT_MARKER = /^(?:<{7}|>{7})(?: |$)/m;
39
+ const DEFAULT_MARKER_SIZE = 7;
40
+ function conflictMarker(size) {
41
+ return new RegExp(`^(?:<{${size}}|>{${size}})(?: |$)`, 'm');
42
+ }
43
+ // git writes the markers with the attributes of the branch's own commit, while the merged
44
+ // base or the fixer can change them in the worktree, so both sizes count.
45
+ async function markerSizes(cwd, file, before) {
46
+ return new Set([await markerSize(cwd, file, ['--source', before]), await markerSize(cwd, file)]);
47
+ }
48
+ async function markerSize(cwd, file, source = []) {
49
+ const args = ['check-attr', '-z', ...source, 'conflict-marker-size', '--', file];
50
+ const result = await run('git', args, { cwd });
51
+ if (!succeeded(result)) {
52
+ throw new Error(failedWith(`git ${args.join(' ')}`, result));
53
+ }
54
+ const value = Number(result.stdout.split('\0')[2]);
55
+ return Number.isInteger(value) && value > 0 ? value : DEFAULT_MARKER_SIZE;
56
+ }
36
57
  export async function commitMerge(cwd, merge, message) {
37
58
  if (!(await merging(cwd))) {
38
59
  await refuseUnmerged(cwd, merge);
39
60
  return false;
40
61
  }
41
- if (await hasConflictMarkers(cwd, merge.files)) {
62
+ if (await hasConflictMarkers(cwd, merge.files, merge.before)) {
42
63
  return false;
43
64
  }
44
65
  await commitEverything(cwd, message);
@@ -46,15 +67,17 @@ export async function commitMerge(cwd, merge, message) {
46
67
  }
47
68
  async function refuseUnmerged(cwd, merge) {
48
69
  const holdsBase = await isAncestor(cwd, merge.target, 'HEAD');
49
- if (!holdsBase || (await hasConflictMarkers(cwd, merge.files))) {
70
+ if (!holdsBase || (await hasConflictMarkers(cwd, merge.files, merge.before))) {
50
71
  await resetHard(cwd, merge.before);
51
72
  }
52
73
  }
53
- async function hasConflictMarkers(cwd, files) {
74
+ async function hasConflictMarkers(cwd, files, before) {
54
75
  for (const file of files) {
55
76
  const text = await readTextOrEmpty(join(cwd, file));
56
- if (CONFLICT_MARKER.test(text)) {
57
- return true;
77
+ for (const size of await markerSizes(cwd, file, before)) {
78
+ if (conflictMarker(size).test(text)) {
79
+ return true;
80
+ }
58
81
  }
59
82
  }
60
83
  return false;
package/dist/observe.mjs CHANGED
@@ -14,14 +14,27 @@ class Sink {
14
14
  level;
15
15
  file;
16
16
  listener;
17
+ /** Set once the run log fails: the run goes on, and its events reach only the listener. */
18
+ failed = false;
17
19
  constructor(stream, level, file, listener) {
18
20
  this.stream = stream;
19
21
  this.level = level;
20
22
  this.file = file;
21
23
  this.listener = listener;
24
+ // With no listener, a failed open or write (`ENOSPC`, `EIO`, `EMFILE`) would crash the process.
25
+ stream.on('error', (error) => this.fail(error));
26
+ }
27
+ fail(error) {
28
+ if (this.failed) {
29
+ return;
30
+ }
31
+ this.failed = true;
32
+ process.stderr.write(`next-issue: the run log ${this.file} cannot be written (${message(error)}); the run goes on without it\n`);
22
33
  }
23
34
  write(record) {
24
- this.stream.write(`${JSON.stringify(record)}\n`);
35
+ if (!this.failed) {
36
+ this.stream.write(`${JSON.stringify(record)}\n`);
37
+ }
25
38
  this.listener?.(record);
26
39
  }
27
40
  count(kind) {
@@ -35,7 +48,15 @@ class Sink {
35
48
  this.totals.set(role, current);
36
49
  }
37
50
  close() {
38
- return new Promise((resolve) => this.stream.end(resolve));
51
+ return new Promise((resolve) => {
52
+ if (this.stream.closed) {
53
+ resolve();
54
+ return;
55
+ }
56
+ // `close` comes after the last write is flushed, and also after an error.
57
+ this.stream.once('close', () => resolve());
58
+ this.stream.end();
59
+ });
39
60
  }
40
61
  }
41
62
  export class Recorder {
@@ -47,12 +68,12 @@ export class Recorder {
47
68
  this.base = base;
48
69
  this.runId = runId;
49
70
  }
50
- static async create(root, level, listener) {
71
+ static async create(root, level, listener, open = (file) => createWriteStream(file, { flags: 'a' })) {
51
72
  const runId = new Date().toISOString().replace(/[:.]/g, '-');
52
73
  const dir = runsDir(root);
53
74
  await mkdir(dir, { recursive: true });
54
75
  const file = join(dir, `${runId}.jsonl`);
55
- const sink = new Sink(createWriteStream(file, { flags: 'a' }), level, file, listener);
76
+ const sink = new Sink(open(file), level, file, listener);
56
77
  return new Recorder(sink, {}, runId);
57
78
  }
58
79
  get logFile() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-issue",
3
- "version": "6.6.118",
3
+ "version": "6.6.120",
4
4
  "description": "Autonomous issue harness driven by the pi SDK",
5
5
  "keywords": [
6
6
  "github",