@tobi-or-not/quorum 0.4.0 → 0.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.
Files changed (111) hide show
  1. package/README.md +3 -1
  2. package/dist/bench/fingerprint.d.ts +8 -5
  3. package/dist/bench/fingerprint.js +28 -9
  4. package/dist/bench/records.d.ts +168 -0
  5. package/dist/bench/records.js +106 -1
  6. package/dist/bench/runner.d.ts +18 -0
  7. package/dist/bench/runner.js +30 -4
  8. package/dist/cli/bench.d.ts +11 -4
  9. package/dist/cli/bench.js +34 -17
  10. package/dist/cli/bin.js +2 -2
  11. package/dist/cli/io.d.ts +5 -0
  12. package/dist/cli/main.d.ts +67 -24
  13. package/dist/cli/main.js +288 -59
  14. package/dist/cli/print-config.d.ts +8 -6
  15. package/dist/cli/print-config.js +26 -8
  16. package/dist/cli/render.d.ts +9 -3
  17. package/dist/cli/render.js +43 -2
  18. package/dist/config/claude-code.d.ts +2 -6
  19. package/dist/config/claude-code.js +6 -44
  20. package/dist/config/command.d.ts +10 -0
  21. package/dist/config/command.js +47 -0
  22. package/dist/config/env.d.ts +32 -19
  23. package/dist/config/env.js +95 -32
  24. package/dist/config/from-config.js +21 -3
  25. package/dist/config/resolve.d.ts +13 -1
  26. package/dist/config/resolve.js +27 -1
  27. package/dist/config/schema.d.ts +152 -5
  28. package/dist/config/schema.js +156 -12
  29. package/dist/core/clock.d.ts +17 -0
  30. package/dist/core/clock.js +14 -0
  31. package/dist/core/coverage.d.ts +74 -0
  32. package/dist/core/coverage.js +221 -0
  33. package/dist/core/diff.d.ts +101 -0
  34. package/dist/core/diff.js +178 -35
  35. package/dist/core/errors.d.ts +12 -3
  36. package/dist/core/errors.js +21 -2
  37. package/dist/core/estimate.d.ts +45 -0
  38. package/dist/core/estimate.js +188 -0
  39. package/dist/core/exclude.d.ts +42 -0
  40. package/dist/core/exclude.js +189 -0
  41. package/dist/core/flags.d.ts +14 -1
  42. package/dist/core/flags.js +56 -0
  43. package/dist/core/inputs.d.ts +38 -0
  44. package/dist/core/inputs.js +228 -0
  45. package/dist/core/limits.d.ts +83 -0
  46. package/dist/core/limits.js +169 -0
  47. package/dist/core/output.d.ts +3 -2
  48. package/dist/core/output.js +3 -2
  49. package/dist/core/parts.d.ts +146 -0
  50. package/dist/core/parts.js +388 -0
  51. package/dist/core/ports.d.ts +67 -2
  52. package/dist/core/ports.js +36 -1
  53. package/dist/core/prompts.d.ts +22 -3
  54. package/dist/core/prompts.js +43 -7
  55. package/dist/core/quorum.d.ts +26 -8
  56. package/dist/core/quorum.js +310 -73
  57. package/dist/core/retry.d.ts +36 -0
  58. package/dist/core/retry.js +46 -0
  59. package/dist/core/secrets.d.ts +98 -0
  60. package/dist/core/secrets.js +507 -0
  61. package/dist/core/stages/context.d.ts +131 -23
  62. package/dist/core/stages/context.js +331 -46
  63. package/dist/core/stages/panel.d.ts +13 -6
  64. package/dist/core/stages/panel.js +79 -19
  65. package/dist/core/stages/research.d.ts +110 -25
  66. package/dist/core/stages/research.js +338 -44
  67. package/dist/core/tokens.d.ts +31 -0
  68. package/dist/core/tokens.js +81 -0
  69. package/dist/core/types.d.ts +280 -25
  70. package/dist/core/verify.d.ts +6 -0
  71. package/dist/core/verify.js +6 -3
  72. package/dist/git/run.d.ts +223 -0
  73. package/dist/git/run.js +608 -0
  74. package/dist/git/tree.d.ts +43 -0
  75. package/dist/git/tree.js +171 -0
  76. package/dist/index.d.ts +26 -2
  77. package/dist/index.js +34 -2
  78. package/dist/providers/anthropic.d.ts +3 -1
  79. package/dist/providers/anthropic.js +10 -3
  80. package/dist/providers/claude-code.d.ts +56 -11
  81. package/dist/providers/claude-code.js +70 -25
  82. package/dist/providers/gemini.d.ts +5 -1
  83. package/dist/providers/gemini.js +65 -11
  84. package/dist/providers/http.d.ts +49 -4
  85. package/dist/providers/http.js +155 -14
  86. package/dist/providers/ollama.d.ts +4 -1
  87. package/dist/providers/ollama.js +13 -5
  88. package/dist/providers/openai-compatible.d.ts +4 -1
  89. package/dist/providers/openai-compatible.js +13 -5
  90. package/dist/providers/options.d.ts +12 -3
  91. package/dist/providers/options.js +30 -10
  92. package/dist/providers/size.d.ts +19 -15
  93. package/dist/providers/size.js +33 -73
  94. package/dist/sources/added.d.ts +49 -0
  95. package/dist/sources/added.js +134 -0
  96. package/dist/sources/diff.d.ts +13 -0
  97. package/dist/sources/diff.js +63 -0
  98. package/dist/sources/local.d.ts +41 -0
  99. package/dist/sources/local.js +251 -0
  100. package/dist/sources/paths.d.ts +45 -0
  101. package/dist/sources/paths.js +230 -0
  102. package/dist/sources/range.d.ts +21 -0
  103. package/dist/sources/range.js +75 -0
  104. package/dist/sources/read.d.ts +69 -0
  105. package/dist/sources/read.js +37 -0
  106. package/dist/sources/repository.d.ts +9 -0
  107. package/dist/sources/repository.js +28 -0
  108. package/dist/sources/skips.d.ts +49 -0
  109. package/dist/sources/skips.js +126 -0
  110. package/package.json +1 -1
  111. package/schema/quorum.schema.json +49 -1
@@ -0,0 +1,221 @@
1
+ import { diffSections, movedTo } from "./diff.js";
2
+ import { isWholeDeletion, secretRuleOf } from "./secrets.js";
3
+ const BINARY_FILES_START = 'Binary files ';
4
+ const BINARY_FILES_END = ' differ';
5
+ const BINARY_PATCH = 'GIT binary patch';
6
+ const SUBPROJECT_COMMIT = 'Subproject commit ';
7
+ const SUBMODULE_MODES = new Set([
8
+ 'new file mode 160000',
9
+ 'deleted file mode 160000',
10
+ 'old mode 160000',
11
+ 'new mode 160000',
12
+ ]);
13
+ const INDEX_START = 'index ';
14
+ const GITLINK_MODE_END = ' 160000';
15
+ /** The line git's `log` and `diff` submodule formats print in place of a header, at the end of the section before it. */
16
+ const SUBMODULE_LINE_START = 'Submodule ';
17
+ /** The characters a hunk row starts with, the only ones that can come before `Subproject commit`. */
18
+ const ROW_MARKERS = '+- ';
19
+ /** Whether `line` (less its `\r`) is a line of the section's own submodule. */
20
+ function isOwnSubmodule(line) {
21
+ return line.startsWith(SUBPROJECT_COMMIT)
22
+ || (line.startsWith(SUBPROJECT_COMMIT, 1) && ROW_MARKERS.includes(line[0]))
23
+ || SUBMODULE_MODES.has(line)
24
+ || (line.startsWith(INDEX_START) && line.endsWith(GITLINK_MODE_END));
25
+ }
26
+ /** The markers of one section, each line read on its own, with no hunk parsing, and at most one marker for a line: binary first, then the section's own submodule, then a `Submodule` line. */
27
+ function markersOf(text) {
28
+ const markers = { binary: false, submodule: false, submoduleLine: false };
29
+ for (const raw of text.split('\n')) {
30
+ const line = raw.endsWith('\r')
31
+ ? raw.slice(0, -1)
32
+ : raw;
33
+ if ((line.startsWith(BINARY_FILES_START) && line.endsWith(BINARY_FILES_END)) || line.startsWith(BINARY_PATCH))
34
+ markers.binary = true;
35
+ else if (isOwnSubmodule(line))
36
+ markers.submodule = true;
37
+ else if (line.startsWith(SUBMODULE_LINE_START))
38
+ markers.submoduleLine = true;
39
+ }
40
+ return markers;
41
+ }
42
+ /**
43
+ * The files to hold back among the sections to judge, by file (the sections of one path; '' for the text
44
+ * no header names). A file is held by the first rule found, in the diff's order, one section's name before
45
+ * its content, and holds every section of it. It is a deletion, which is no gap, only when its rule is
46
+ * `name` and every section of it is a whole deletion: a file named as a secret holds no code a review
47
+ * would read. A file held for a credential in it may be code, and the removed lines of a deleted source
48
+ * file are the code a review would read, so a deletion of one is held as a gap like any other.
49
+ */
50
+ function heldFiles(judged) {
51
+ const held = new Map();
52
+ for (const section of judged) {
53
+ const file = section.path ?? '';
54
+ if (held.has(file))
55
+ continue;
56
+ const rule = secretRuleOf(section);
57
+ if (rule !== undefined)
58
+ held.set(file, { rule, deletion: rule === 'name' });
59
+ }
60
+ for (const { path, text } of judged) {
61
+ const file = held.get(path ?? '');
62
+ if (file !== undefined && file.deletion)
63
+ file.deletion = isWholeDeletion(text);
64
+ }
65
+ return held;
66
+ }
67
+ /**
68
+ * What a review covers of a diff: the files `excluded` leaves out, the files held back as secrets, and the
69
+ * files whose change the diff does not carry, found from the markers git prints in their place, each
70
+ * section read line by line (a trailing `\r` is dropped), so a Windows-made diff reads as any other. In
71
+ * each section:
72
+ * - a `binary` file shows as a line that starts with `Binary files ` and ends with ` differ`, or one
73
+ * that starts with `GIT binary patch`;
74
+ * - a `submodule` shows as a `Subproject commit` row (past at most one leading `+`, `-` or space), a
75
+ * header of mode 160000 (`new file mode`, `deleted file mode`, `old mode` and `new mode`), or an
76
+ * `index` line that ends in ` 160000`;
77
+ * - a `Submodule` line, which git's `log` and `diff` formats print in place of a header, at the end of
78
+ * the section before it, lists a `submodule` under the empty name, whatever section holds it.
79
+ *
80
+ * A file is listed by the diff's own path, decoded, in the diff's order, once per file and reason.
81
+ * `excluded` is asked once per distinct path a header names; a section it takes is listed as
82
+ * `excluded`, leaves the scope's `diff` and has its own markers left unread, but its `Submodule`
83
+ * lines still list their submodule, since such a line belongs to no section. Nothing a header does
84
+ * not name is ever excluded: the text before the first header, a diff without such headers and a
85
+ * header that names no path quorum reads.
86
+ *
87
+ * A header can read two ways: git does not quote a path that holds a space, so a rename's header
88
+ * (`a/src/auth.js b/src/x b/dist/auth.js`) can name a path the file does not have. A section is
89
+ * therefore excluded only when every `rename to` and `copy to` line it holds names the header's
90
+ * path, as `movedTo` reads them; otherwise it is kept, as a header quorum cannot read is, its markers
91
+ * read and its text sent. A forged line, such as a `format-patch` series carries in the next mail's
92
+ * message, can then only make quorum exclude less. That next mail's headers and message sit at the
93
+ * end of the previous file's section and leave with it when that file is excluded: no code hides
94
+ * there, since `git am` applies only the patch sections, and a `Submodule` line still counts.
95
+ *
96
+ * The empty name stands for a part of the diff that no `diff --git` header names: the text before the
97
+ * first header, a diff without such headers, a header that names no path quorum reads, and a `Submodule`
98
+ * line. A readable header never names the empty path, so it is never a file's.
99
+ *
100
+ * A file is held back when its name is a secret's, by any path its section names (its header, its `---`
101
+ * and `+++` lines, its `rename` and `copy` lines), or a line of it, removed and context lines included,
102
+ * holds a credential quorum recognizes (`secretRuleOf`). One file has one verdict: the sections of one
103
+ * path are one file, held by the first rule found in the diff's order, so when any of them is held every
104
+ * one leaves the scope's `diff`, and a credential in one holds the others. The file is listed once, at
105
+ * its first section, as `secret` with its `rule`, or as `deleted-secret` when its rule is `name` and every
106
+ * section of it is a whole deletion (`isWholeDeletion`): the change then adds no code, and the file is no
107
+ * gap. A deletion of a file held for a credential in it is `secret` and a gap, since the code it removes
108
+ * would go unread, as an edit of it would. A held section's own markers are left unread, as an excluded
109
+ * one's are, so a held binary file is listed `secret` only; its `Submodule` lines still list their
110
+ * submodule. The empty name is a file like any
111
+ * other: the text before the first header, a diff without such headers and a header quorum cannot read
112
+ * are held whole by one verdict.
113
+ *
114
+ * The order is: `excluded` first, so an excluded file is listed `excluded` only and judged no further;
115
+ * then `sent`, asked of each path a header names and never of the empty name, which sends a file whatever
116
+ * would hold it, so it is neither held nor listed; then the hold.
117
+ *
118
+ * The scope's `files` names every section's file as it is read, once each, in the diff's order, the held
119
+ * ones too: the empty name for the text no header names, and again, after the section that holds one, for
120
+ * a `Submodule` line; a preamble that is blank names no file.
121
+ *
122
+ * Detection is generous: a line of the same form elsewhere, such as a commit message line of a
123
+ * `format-patch` series, lists the file whose section holds it, and a hit anywhere in a file holds all
124
+ * of it, since a gap it should not have stops a review for a person to look, where a missed one would
125
+ * pass a change unread. The scope's `diff` is the given string itself when no section was excluded or
126
+ * held, the kept sections joined by line feeds when some were, and '' when every section was.
127
+ */
128
+ export function reviewScope(diff, excluded = () => false, sent = () => false) {
129
+ const leftOut = [];
130
+ const listed = {
131
+ excluded: new Set(),
132
+ binary: new Set(),
133
+ submodule: new Set(),
134
+ secret: new Set(),
135
+ 'deleted-secret': new Set(),
136
+ };
137
+ const list = (entry) => {
138
+ if (listed[entry.reason].has(entry.file))
139
+ return;
140
+ listed[entry.reason].add(entry.file);
141
+ leftOut.push(entry);
142
+ };
143
+ const answers = new Map();
144
+ const isExcluded = (path) => {
145
+ let answer = answers.get(path);
146
+ if (answer === undefined) {
147
+ answer = excluded(path);
148
+ answers.set(path, answer);
149
+ }
150
+ return answer;
151
+ };
152
+ /** The path of a section `excluded` takes, or undefined for any other. */
153
+ const excludedPath = ({ path, text }) => path !== undefined && path !== '' && isExcluded(path) && movedTo(text).every(moved => moved === path)
154
+ ? path
155
+ : undefined;
156
+ /** Whether the input sends the file of a section: only a path a header names can be sent, never the empty name. */
157
+ const isSent = ({ path }) => path !== undefined && path !== '' && sent(path);
158
+ const files = new Set();
159
+ const sections = diffSections(diff);
160
+ const held = heldFiles(sections.filter(section => excludedPath(section) === undefined && !isSent(section)));
161
+ const kept = [];
162
+ for (const section of sections) {
163
+ const { path, text } = section;
164
+ const markers = markersOf(text);
165
+ if (path !== '' || text.trim() !== '')
166
+ files.add(path ?? '');
167
+ const gone = excludedPath(section);
168
+ const hold = held.get(path ?? '');
169
+ if (gone !== undefined) {
170
+ list({ file: gone, reason: 'excluded' });
171
+ }
172
+ else if (hold !== undefined) {
173
+ list({
174
+ file: path ?? '',
175
+ reason: hold.deletion ? 'deleted-secret' : 'secret',
176
+ rule: hold.rule,
177
+ });
178
+ }
179
+ else {
180
+ kept.push(text);
181
+ if (markers.binary)
182
+ list({ file: path ?? '', reason: 'binary' });
183
+ if (markers.submodule)
184
+ list({ file: path ?? '', reason: 'submodule' });
185
+ }
186
+ if (markers.submoduleLine) {
187
+ list({ file: '', reason: 'submodule' });
188
+ files.add('');
189
+ }
190
+ }
191
+ return {
192
+ diff: kept.length === sections.length
193
+ ? diff
194
+ : kept.join('\n'),
195
+ leftOut,
196
+ files: [...files],
197
+ };
198
+ }
199
+ /** The reasons of the gaps a diff's own files make for a profile, in the order they are recorded. A file excluded, skipped, or named as a secret and deleted whole, is no gap. */
200
+ const GAP_REASONS = ['binary', 'submodule', 'secret'];
201
+ /** What a gap's detail says of its `count` files. */
202
+ const GAP_DETAILS = {
203
+ binary: count => `the diff carries no content for ${count} binary file(s), listed in meta.leftOut`,
204
+ submodule: count => `the diff carries no content for ${count} submodule(s), listed in meta.leftOut`,
205
+ secret: count => `no model was sent ${count} file(s) held as secrets, listed in meta.leftOut`,
206
+ };
207
+ /** The gaps the files left out make for one profile: one per reason, binary then submodule then secret, counting its files, and none for a reason with no file. A `deleted-secret` file is no gap. */
208
+ export function leftOutGaps(profile, leftOut) {
209
+ return GAP_REASONS.flatMap((reason) => {
210
+ const count = leftOut.filter(entry => entry.reason === reason).length;
211
+ return count === 0
212
+ ? []
213
+ : [
214
+ {
215
+ profile,
216
+ reason,
217
+ detail: GAP_DETAILS[reason](count),
218
+ },
219
+ ];
220
+ });
221
+ }
@@ -1,3 +1,67 @@
1
+ /**
2
+ * Decode the inside of a quoted name (`core.quotePath` writes every non-ASCII byte as `\NNN`): a
3
+ * backslash escape stands for one character (`\n`, `\t`, `\"`, `\\`, ...) and the `\NNN` bytes of a
4
+ * run are decoded together as UTF-8. An escape git never writes is kept as it is. A run of bytes
5
+ * that is not valid UTF-8 becomes one lone surrogate per byte (U+DC00 plus the byte, as Python
6
+ * names a file it cannot decode): no valid name holds a lone surrogate, so two such names never
7
+ * share a key, nor does one share a key with a name that spells out its escapes, and
8
+ * `JSON.stringify` writes the surrogate as an escape. One pass over the name and no pattern that
9
+ * can backtrack.
10
+ */
11
+ export declare function decodeQuoted(quoted: string): string;
12
+ /**
13
+ * The paths the `rename to ` and `copy to ` lines of a section name, in order, each less a trailing
14
+ * `\r` and decoded when git quoted it. Git does not quote a path that holds a space, so a path with a
15
+ * space before `b/` makes a rename's `diff --git` header read two ways (`a/src/auth.js b/src/x
16
+ * b/dist/auth.js` reads as a rename to `dist/auth.js`), while these lines name the new path whole. A
17
+ * hunk row starts with a space, `+`, `-` or `\`, never with these words, so the section's lines are
18
+ * read with no hunk parsing.
19
+ */
20
+ export declare function movedTo(text: string): string[];
21
+ /**
22
+ * The paths a section's own lines name, in the order of the lines, each as its line writes it (an
23
+ * `a/` or `b/` prefix included) and decoded when git quoted it: the path of each `--- `, `+++ `,
24
+ * `rename from `, `rename to `, `copy from ` and `copy to ` line, less a trailing `\r` and cut at a
25
+ * tab (git ends a `---` or `+++` line with one when the name holds a space, and `diff -u` writes a
26
+ * timestamp after one). `/dev/null` and an empty name are no path. The header's own path is the
27
+ * section's `path`, and is not read again. A hunk row that reads like one of these lines (an added
28
+ * `++ b/.env` shows as `+++ b/.env`) names a path too: the lines are read with no hunk parsing, so
29
+ * the answer is generous and fails closed.
30
+ */
31
+ export declare function namedPaths(text: string): string[];
32
+ /**
33
+ * The names the first line of a section holds when it is a `diff --git` header: each space-separated
34
+ * word after the keyword, none empty, less a trailing `\r` and decoded when quoted, as `namedPaths`
35
+ * decodes. The header's own path (`path` of the section) is one reading of these words, and a header
36
+ * that is forged or cannot be read gives the words and no path, so the hold reads each of them: a
37
+ * header such as `diff --git a/.env` or `diff --git a/.env b/.env c` names `.env` to a reader that
38
+ * splits it on its spaces. A path that holds a space is cut, so this is generous and fails closed.
39
+ * None for a section that opens with another line.
40
+ */
41
+ export declare function headerNames(text: string): string[];
42
+ /**
43
+ * Whether a `--- ` line writes the old side of a `diff --git` header that names one path on both
44
+ * sides, as the header of a file git deletes whole does: the header's first half, cut at the middle
45
+ * space as `sharedPath` cuts it, and the line's path (up to a tab), are one name once decoded. A
46
+ * header whose two sides differ, or that holds a third name, has no such old side, so no line agrees
47
+ * with it.
48
+ */
49
+ export declare function namesOldSide(header: string, minusLine: string): boolean;
50
+ /** One section of a diff: the text from a `diff --git` header to the next, or the text before the first one. */
51
+ export interface DiffSection {
52
+ /** The path the header names; '' for the text before the first header or a diff without headers; undefined for a header that names no readable path. */
53
+ readonly path: string | undefined;
54
+ readonly text: string;
55
+ }
56
+ /**
57
+ * Every section of a git-style unified diff, in order. Only `diff --git` lines open a section, so the
58
+ * text before the first one is a section of its own, of path '', and a diff without such headers
59
+ * (plain `diff -u` output) is one section of path ''. A header that names no path quorum can read
60
+ * gives a section of path `undefined`, kept here whatever `splitDiff` does with it. A section holds
61
+ * at least one line, so a diff that starts with its header has no section before it, and the texts
62
+ * joined by line feeds are the diff.
63
+ */
64
+ export declare function diffSections(diff: string): DiffSection[];
1
65
  /**
2
66
  * Split a git-style unified diff into sections keyed by file path. Only `diff --git` lines open a
3
67
  * section: inside a hunk every line starts with ' ', '+', '-' or '\', so that text cannot appear
@@ -77,6 +141,43 @@ export interface AddedLine {
77
141
  * `format-patch` series.
78
142
  */
79
143
  export declare function addedLines(sections: ReadonlyMap<string, string>, path: string): AddedLine[];
144
+ export interface Hunk {
145
+ /** The index of the hunk's `@@` line among the section's lines split at line feeds. */
146
+ at: number;
147
+ /** First new-file line of the hunk. */
148
+ start: number;
149
+ /** Number of new-file lines the header promises. */
150
+ count: number;
151
+ /** The hunk's lines (with their +, - or space marker), in order. */
152
+ rows: string[];
153
+ /** For each new-file line from `start`, the index in `rows` of the row that shows it. */
154
+ rowOfLine: number[];
155
+ }
156
+ export declare function hunksOf(sections: ReadonlyMap<string, string>, path: string, text: string): Hunk[];
157
+ /** A run of new-file lines, both ends included. */
158
+ export interface Span {
159
+ readonly start: number;
160
+ readonly end: number;
161
+ }
162
+ /**
163
+ * Whether `hunk` holds the new-file line `line`, as `locate` takes it: the line is within the hunk's
164
+ * start and its count (a count of 0 holds its start alone), the hunk shows a row, and, for a hunk
165
+ * that counts new lines, a row shows the line, so a hunk cut short of its count does not hold the
166
+ * lines it never shows.
167
+ */
168
+ export declare function holds(hunk: Hunk, line: number): boolean;
169
+ /** The new-file lines a hunk holds for `locate`: those it shows from its start, up to its count, or its start alone for a hunk that counts no new line; none for a hunk that shows no row. */
170
+ export declare function heldSpan(hunk: Hunk): Span | undefined;
171
+ /**
172
+ * Read the hunks of one file section. The header's line counts say where a hunk ends, so file
173
+ * headers and `` markers are never mistaken for hunk lines, and a line
174
+ * that cannot belong to a hunk closes it even when the counts promised more. The empty element
175
+ * after a final newline is not a line; an empty line before it is a blank context row.
176
+ *
177
+ * This is the one reading of a hunk: the packer of a large change, `locate` and a vote's context
178
+ * share it, so they agree on where each hunk opens, at the line `at` of its `@@` header.
179
+ */
180
+ export declare function parseHunks(section: string): Hunk[];
80
181
  /** The most diff quorum reads, from stdin, a file or a suite, in bytes: a larger one is refused and reading stops there. */
81
182
  export declare const MAX_DIFF_BYTES: number;
82
183
  /**
package/dist/core/diff.js CHANGED
@@ -1,6 +1,18 @@
1
1
  const GIT_HEADER = 'diff --git ';
2
2
  const QUOTED_NEW_SIDE = ' "b/';
3
3
  const NEW_SIDE_MARKERS = [QUOTED_NEW_SIDE, ' b/'];
4
+ const MOVED_TO_PREFIXES = ['rename to ', 'copy to '];
5
+ /** The lines, outside a header, that name a path: a file's two sides, and where git moved or copied it from and to. */
6
+ const NAMING_PREFIXES = [
7
+ '--- ',
8
+ '+++ ',
9
+ 'rename from ',
10
+ 'rename to ',
11
+ 'copy from ',
12
+ 'copy to ',
13
+ ];
14
+ /** What git writes for the side of a file that does not exist: no path. */
15
+ const NO_FILE = '/dev/null';
4
16
  const PATH_PREFIX = /^(?:[ab]\/|\.\/)/;
5
17
  const HUNK_HEADER = /^@@ -\d+(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
6
18
  /** How many diff lines of a hunk, either side of the cited one, make up a finding's evidence. */
@@ -28,6 +40,12 @@ const NAME_DECODER = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
28
40
  /** A byte of a name that is not valid UTF-8 becomes the lone surrogate at this code plus the byte. */
29
41
  const SURROGATE_ESCAPE = 0xDC00;
30
42
  const isQuoted = (name) => name.startsWith('"') && name.endsWith('"');
43
+ /** A line less the carriage return a Windows-made diff ends it with. */
44
+ function withoutCarriageReturn(line) {
45
+ return line.endsWith('\r')
46
+ ? line.slice(0, -1)
47
+ : line;
48
+ }
31
49
  /** A name without the double quotes git puts around one with special characters, escapes untouched. */
32
50
  function unquote(name) {
33
51
  return isQuoted(name)
@@ -48,7 +66,7 @@ function withoutPrefix(path) {
48
66
  * `JSON.stringify` writes the surrogate as an escape. One pass over the name and no pattern that
49
67
  * can backtrack.
50
68
  */
51
- function decodeQuoted(quoted) {
69
+ export function decodeQuoted(quoted) {
52
70
  const bytes = [];
53
71
  let name = '';
54
72
  const flush = () => {
@@ -137,12 +155,123 @@ function renamedPath(names) {
137
155
  }
138
156
  /** The path a `diff --git` header names, or undefined when it names none. */
139
157
  function headerPath(header) {
140
- const line = header.endsWith('\r')
141
- ? header.slice(0, -1)
142
- : header;
143
- const names = line.slice(GIT_HEADER.length);
158
+ const names = withoutCarriageReturn(header).slice(GIT_HEADER.length);
144
159
  return sharedPath(names) || renamedPath(names) || undefined;
145
160
  }
161
+ /**
162
+ * The paths the `rename to ` and `copy to ` lines of a section name, in order, each less a trailing
163
+ * `\r` and decoded when git quoted it. Git does not quote a path that holds a space, so a path with a
164
+ * space before `b/` makes a rename's `diff --git` header read two ways (`a/src/auth.js b/src/x
165
+ * b/dist/auth.js` reads as a rename to `dist/auth.js`), while these lines name the new path whole. A
166
+ * hunk row starts with a space, `+`, `-` or `\`, never with these words, so the section's lines are
167
+ * read with no hunk parsing.
168
+ */
169
+ export function movedTo(text) {
170
+ const paths = [];
171
+ for (const raw of text.split('\n')) {
172
+ const line = withoutCarriageReturn(raw);
173
+ const prefix = MOVED_TO_PREFIXES.find(start => line.startsWith(start));
174
+ if (prefix === undefined)
175
+ continue;
176
+ paths.push(decodedName(line.slice(prefix.length)));
177
+ }
178
+ return paths;
179
+ }
180
+ /** A name as a line writes it, decoded when git quoted it. */
181
+ function decodedName(name) {
182
+ return isQuoted(name)
183
+ ? decodeQuoted(name.slice(1, -1))
184
+ : name;
185
+ }
186
+ /**
187
+ * The paths a section's own lines name, in the order of the lines, each as its line writes it (an
188
+ * `a/` or `b/` prefix included) and decoded when git quoted it: the path of each `--- `, `+++ `,
189
+ * `rename from `, `rename to `, `copy from ` and `copy to ` line, less a trailing `\r` and cut at a
190
+ * tab (git ends a `---` or `+++` line with one when the name holds a space, and `diff -u` writes a
191
+ * timestamp after one). `/dev/null` and an empty name are no path. The header's own path is the
192
+ * section's `path`, and is not read again. A hunk row that reads like one of these lines (an added
193
+ * `++ b/.env` shows as `+++ b/.env`) names a path too: the lines are read with no hunk parsing, so
194
+ * the answer is generous and fails closed.
195
+ */
196
+ export function namedPaths(text) {
197
+ const paths = [];
198
+ for (const raw of text.split('\n')) {
199
+ const prefix = NAMING_PREFIXES.find(start => raw.startsWith(start));
200
+ if (prefix === undefined)
201
+ continue;
202
+ const path = pathOfLine(raw, prefix);
203
+ if (path !== '' && path !== NO_FILE)
204
+ paths.push(path);
205
+ }
206
+ return paths;
207
+ }
208
+ /** The path a naming line writes after its prefix: less a trailing `\r`, cut at a tab, decoded when git quoted it. */
209
+ function pathOfLine(raw, prefix) {
210
+ const line = withoutCarriageReturn(raw);
211
+ return decodedName(line.slice(prefix.length).split('\t', 1)[0] ?? '');
212
+ }
213
+ /**
214
+ * The names the first line of a section holds when it is a `diff --git` header: each space-separated
215
+ * word after the keyword, none empty, less a trailing `\r` and decoded when quoted, as `namedPaths`
216
+ * decodes. The header's own path (`path` of the section) is one reading of these words, and a header
217
+ * that is forged or cannot be read gives the words and no path, so the hold reads each of them: a
218
+ * header such as `diff --git a/.env` or `diff --git a/.env b/.env c` names `.env` to a reader that
219
+ * splits it on its spaces. A path that holds a space is cut, so this is generous and fails closed.
220
+ * None for a section that opens with another line.
221
+ */
222
+ export function headerNames(text) {
223
+ const end = text.indexOf('\n');
224
+ const first = withoutCarriageReturn(end === -1
225
+ ? text
226
+ : text.slice(0, end));
227
+ if (!first.startsWith(GIT_HEADER))
228
+ return [];
229
+ return first.slice(GIT_HEADER.length).split(' ').filter(name => name !== '').map(decodedName);
230
+ }
231
+ /**
232
+ * Whether a `--- ` line writes the old side of a `diff --git` header that names one path on both
233
+ * sides, as the header of a file git deletes whole does: the header's first half, cut at the middle
234
+ * space as `sharedPath` cuts it, and the line's path (up to a tab), are one name once decoded. A
235
+ * header whose two sides differ, or that holds a third name, has no such old side, so no line agrees
236
+ * with it.
237
+ */
238
+ export function namesOldSide(header, minusLine) {
239
+ const names = withoutCarriageReturn(header).slice(GIT_HEADER.length);
240
+ if (sharedPath(names) === '')
241
+ return false;
242
+ return decodedName(names.slice(0, (names.length - 1) / 2)) === pathOfLine(minusLine, '--- ');
243
+ }
244
+ /**
245
+ * Every section of a git-style unified diff, in order. Only `diff --git` lines open a section, so the
246
+ * text before the first one is a section of its own, of path '', and a diff without such headers
247
+ * (plain `diff -u` output) is one section of path ''. A header that names no path quorum can read
248
+ * gives a section of path `undefined`, kept here whatever `splitDiff` does with it. A section holds
249
+ * at least one line, so a diff that starts with its header has no section before it, and the texts
250
+ * joined by line feeds are the diff.
251
+ */
252
+ export function diffSections(diff) {
253
+ const sections = [];
254
+ let path = '';
255
+ let lines = [];
256
+ const flush = () => {
257
+ if (lines.length > 0) {
258
+ sections.push({
259
+ path,
260
+ text: lines.join('\n'),
261
+ });
262
+ }
263
+ };
264
+ for (const line of diff.split('\n')) {
265
+ if (line.startsWith(GIT_HEADER)) {
266
+ flush();
267
+ path = headerPath(line);
268
+ lines = [];
269
+ }
270
+ lines.push(line);
271
+ }
272
+ flush();
273
+ return sections;
274
+ }
146
275
  /**
147
276
  * Split a git-style unified diff into sections keyed by file path. Only `diff --git` lines open a
148
277
  * section: inside a hunk every line starts with ' ', '+', '-' or '\', so that text cannot appear
@@ -160,29 +289,18 @@ function headerPath(header) {
160
289
  */
161
290
  export function splitDiff(diff) {
162
291
  const sections = new Map();
163
- let path = '';
164
- let lines = [];
165
- const flush = () => {
166
- if (path === undefined || lines.length === 0)
167
- return;
168
- const text = lines.join('\n');
292
+ const all = diffSections(diff);
293
+ for (const { path, text } of all) {
294
+ if (path === undefined)
295
+ continue;
169
296
  const previous = sections.get(path);
170
297
  sections.set(path, previous === undefined
171
298
  ? text
172
299
  : `${previous}\n${text}`);
173
- };
174
- for (const line of diff.split('\n')) {
175
- if (line.startsWith(GIT_HEADER)) {
176
- flush();
177
- path = headerPath(line);
178
- lines = [];
179
- }
180
- lines.push(line);
181
300
  }
182
- flush();
183
- // `path` is '' only before the first header: when every header was unreadable, the preamble is
184
- // all that is left, and it must not stand for a file.
185
- if (path !== '' && sections.size === 1)
301
+ // A section whose path is not '' means a header was met, an unreadable one included: when every
302
+ // header was unreadable, the preamble is all that is left, and it must not stand for a file.
303
+ if (all.some(({ path }) => path !== '') && sections.size === 1)
186
304
  sections.delete('');
187
305
  return sections;
188
306
  }
@@ -257,12 +375,9 @@ export function locate(sections, file, line) {
257
375
  if (!section)
258
376
  return undefined;
259
377
  for (const hunk of hunksOf(sections, section.path, section.text)) {
260
- if (hunk.rows.length === 0 || line < hunk.start || line >= hunk.start + Math.max(hunk.count, 1))
378
+ if (!holds(hunk, line))
261
379
  continue;
262
- const at = hunk.rowOfLine[line - hunk.start] ?? -1;
263
- if (at === -1 && hunk.count > 0)
264
- continue;
265
- const center = Math.max(at, 0);
380
+ const center = hunk.rowOfLine[line - hunk.start] ?? 0;
266
381
  const rows = hunk.rows.slice(Math.max(0, center - EVIDENCE_RADIUS), center + EVIDENCE_RADIUS + 1);
267
382
  return {
268
383
  path: section.path,
@@ -337,7 +452,7 @@ export function addedLines(sections, path) {
337
452
  * stays true, and the cache is dropped with the map.
338
453
  */
339
454
  const parsedSections = new WeakMap();
340
- function hunksOf(sections, path, text) {
455
+ export function hunksOf(sections, path, text) {
341
456
  let parsed = parsedSections.get(sections);
342
457
  if (!parsed) {
343
458
  parsed = new Map();
@@ -352,8 +467,19 @@ function hunksOf(sections, path, text) {
352
467
  }
353
468
  /** The merged spans of each section, built once, in a cache that is dropped with the sections map like `parsedSections`. */
354
469
  const coveredSections = new WeakMap();
470
+ /**
471
+ * Whether `hunk` holds the new-file line `line`, as `locate` takes it: the line is within the hunk's
472
+ * start and its count (a count of 0 holds its start alone), the hunk shows a row, and, for a hunk
473
+ * that counts new lines, a row shows the line, so a hunk cut short of its count does not hold the
474
+ * lines it never shows.
475
+ */
476
+ export function holds(hunk, line) {
477
+ if (hunk.rows.length === 0 || line < hunk.start || line >= hunk.start + Math.max(hunk.count, 1))
478
+ return false;
479
+ return hunk.count === 0 || hunk.rowOfLine[line - hunk.start] !== undefined;
480
+ }
355
481
  /** The new-file lines a hunk holds for `locate`: those it shows from its start, up to its count, or its start alone for a hunk that counts no new line; none for a hunk that shows no row. */
356
- function heldSpan(hunk) {
482
+ export function heldSpan(hunk) {
357
483
  if (hunk.rows.length === 0)
358
484
  return undefined;
359
485
  const shown = hunk.count === 0
@@ -397,8 +523,11 @@ function spansOf(sections, path, text) {
397
523
  * headers and `` markers are never mistaken for hunk lines, and a line
398
524
  * that cannot belong to a hunk closes it even when the counts promised more. The empty element
399
525
  * after a final newline is not a line; an empty line before it is a blank context row.
526
+ *
527
+ * This is the one reading of a hunk: the packer of a large change, `locate` and a vote's context
528
+ * share it, so they agree on where each hunk opens, at the line `at` of its `@@` header.
400
529
  */
401
- function parseHunks(section) {
530
+ export function parseHunks(section) {
402
531
  const hunks = [];
403
532
  let current;
404
533
  let oldLeft = 0;
@@ -406,7 +535,7 @@ function parseHunks(section) {
406
535
  const lines = section.split('\n');
407
536
  if (lines.at(-1) === '')
408
537
  lines.pop();
409
- for (const raw of lines) {
538
+ for (const [at, raw] of lines.entries()) {
410
539
  const text = raw.endsWith('\r')
411
540
  ? raw.slice(0, -1)
412
541
  : raw;
@@ -433,10 +562,19 @@ function parseHunks(section) {
433
562
  }
434
563
  const header = HUNK_HEADER.exec(text);
435
564
  if (header) {
436
- oldLeft = Number(header[1] ?? 1);
437
- newLeft = Number(header[3] ?? 1);
565
+ const [oldCount, start, newCount] = [
566
+ Number(header[1] ?? 1),
567
+ Number(header[2]),
568
+ Number(header[3] ?? 1),
569
+ ];
570
+ // A header whose numbers are no line numbers opens no hunk: its line is text outside any hunk.
571
+ if (!isLineNumbers(oldCount, start, newCount))
572
+ continue;
573
+ oldLeft = oldCount;
574
+ newLeft = newCount;
438
575
  current = {
439
- start: Number(header[2]),
576
+ at,
577
+ start,
440
578
  count: newLeft,
441
579
  rows: [],
442
580
  rowOfLine: [],
@@ -446,6 +584,11 @@ function parseHunks(section) {
446
584
  }
447
585
  return hunks;
448
586
  }
587
+ /** Whether a header's old count, new start and new count are safe integers, and the last new line the hunk can hold is one too, so no range read from the hunk is anything but a line number. */
588
+ function isLineNumbers(oldCount, start, newCount) {
589
+ return Number.isSafeInteger(oldCount) && Number.isSafeInteger(start) && Number.isSafeInteger(newCount)
590
+ && start <= Number.MAX_SAFE_INTEGER - Math.max(newCount, 1) + 1;
591
+ }
449
592
  /** The most diff quorum reads, from stdin, a file or a suite, in bytes: a larger one is refused and reading stops there. */
450
593
  export const MAX_DIFF_BYTES = 16 * 1024 * 1024;
451
594
  /**