@tobi-or-not/quorum 0.5.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.
- package/README.md +2 -0
- package/dist/bench/fingerprint.d.ts +6 -5
- package/dist/bench/fingerprint.js +11 -5
- package/dist/bench/records.d.ts +100 -2
- package/dist/bench/records.js +80 -5
- package/dist/bench/runner.js +1 -1
- package/dist/cli/io.d.ts +5 -0
- package/dist/cli/main.d.ts +36 -13
- package/dist/cli/main.js +194 -44
- package/dist/cli/print-config.d.ts +5 -5
- package/dist/cli/print-config.js +10 -5
- package/dist/cli/render.d.ts +7 -1
- package/dist/cli/render.js +25 -3
- package/dist/config/claude-code.d.ts +2 -6
- package/dist/config/claude-code.js +6 -44
- package/dist/config/command.d.ts +10 -0
- package/dist/config/command.js +47 -0
- package/dist/config/from-config.js +8 -2
- package/dist/config/resolve.d.ts +4 -0
- package/dist/config/resolve.js +10 -0
- package/dist/config/schema.d.ts +13 -2
- package/dist/config/schema.js +36 -5
- package/dist/core/coverage.d.ts +38 -13
- package/dist/core/coverage.js +105 -28
- package/dist/core/diff.d.ts +77 -0
- package/dist/core/diff.js +122 -24
- package/dist/core/estimate.d.ts +45 -0
- package/dist/core/estimate.js +188 -0
- package/dist/core/flags.d.ts +14 -1
- package/dist/core/flags.js +56 -0
- package/dist/core/inputs.d.ts +38 -0
- package/dist/core/inputs.js +228 -0
- package/dist/core/limits.d.ts +5 -4
- package/dist/core/limits.js +11 -5
- package/dist/core/parts.d.ts +146 -0
- package/dist/core/parts.js +388 -0
- package/dist/core/ports.d.ts +10 -0
- package/dist/core/prompts.d.ts +22 -3
- package/dist/core/prompts.js +43 -7
- package/dist/core/quorum.d.ts +7 -5
- package/dist/core/quorum.js +164 -58
- package/dist/core/secrets.d.ts +98 -0
- package/dist/core/secrets.js +507 -0
- package/dist/core/stages/context.d.ts +16 -3
- package/dist/core/stages/context.js +44 -17
- package/dist/core/stages/panel.d.ts +8 -2
- package/dist/core/stages/panel.js +50 -9
- package/dist/core/stages/research.d.ts +107 -31
- package/dist/core/stages/research.js +290 -53
- package/dist/core/types.d.ts +217 -29
- package/dist/core/verify.d.ts +6 -0
- package/dist/core/verify.js +6 -3
- package/dist/git/run.d.ts +223 -0
- package/dist/git/run.js +608 -0
- package/dist/git/tree.d.ts +43 -0
- package/dist/git/tree.js +171 -0
- package/dist/index.d.ts +26 -2
- package/dist/index.js +34 -2
- package/dist/providers/anthropic.d.ts +2 -1
- package/dist/providers/anthropic.js +4 -2
- package/dist/providers/claude-code.d.ts +5 -0
- package/dist/providers/claude-code.js +11 -1
- package/dist/providers/gemini.d.ts +2 -1
- package/dist/providers/gemini.js +4 -2
- package/dist/providers/ollama.d.ts +2 -1
- package/dist/providers/ollama.js +4 -2
- package/dist/providers/openai-compatible.d.ts +2 -1
- package/dist/providers/openai-compatible.js +4 -2
- package/dist/providers/size.d.ts +9 -1
- package/dist/providers/size.js +18 -4
- package/dist/sources/added.d.ts +49 -0
- package/dist/sources/added.js +134 -0
- package/dist/sources/diff.d.ts +13 -0
- package/dist/sources/diff.js +63 -0
- package/dist/sources/local.d.ts +41 -0
- package/dist/sources/local.js +251 -0
- package/dist/sources/paths.d.ts +45 -0
- package/dist/sources/paths.js +230 -0
- package/dist/sources/range.d.ts +21 -0
- package/dist/sources/range.js +75 -0
- package/dist/sources/read.d.ts +69 -0
- package/dist/sources/read.js +37 -0
- package/dist/sources/repository.d.ts +9 -0
- package/dist/sources/repository.js +28 -0
- package/dist/sources/skips.d.ts +49 -0
- package/dist/sources/skips.js +126 -0
- package/package.json +1 -1
- package/schema/quorum.schema.json +15 -0
package/dist/core/diff.js
CHANGED
|
@@ -2,6 +2,17 @@ const GIT_HEADER = 'diff --git ';
|
|
|
2
2
|
const QUOTED_NEW_SIDE = ' "b/';
|
|
3
3
|
const NEW_SIDE_MARKERS = [QUOTED_NEW_SIDE, ' b/'];
|
|
4
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';
|
|
5
16
|
const PATH_PREFIX = /^(?:[ab]\/|\.\/)/;
|
|
6
17
|
const HUNK_HEADER = /^@@ -\d+(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
|
|
7
18
|
/** How many diff lines of a hunk, either side of the cited one, make up a finding's evidence. */
|
|
@@ -29,6 +40,12 @@ const NAME_DECODER = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
|
|
|
29
40
|
/** A byte of a name that is not valid UTF-8 becomes the lone surrogate at this code plus the byte. */
|
|
30
41
|
const SURROGATE_ESCAPE = 0xDC00;
|
|
31
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
|
+
}
|
|
32
49
|
/** A name without the double quotes git puts around one with special characters, escapes untouched. */
|
|
33
50
|
function unquote(name) {
|
|
34
51
|
return isQuoted(name)
|
|
@@ -49,7 +66,7 @@ function withoutPrefix(path) {
|
|
|
49
66
|
* `JSON.stringify` writes the surrogate as an escape. One pass over the name and no pattern that
|
|
50
67
|
* can backtrack.
|
|
51
68
|
*/
|
|
52
|
-
function decodeQuoted(quoted) {
|
|
69
|
+
export function decodeQuoted(quoted) {
|
|
53
70
|
const bytes = [];
|
|
54
71
|
let name = '';
|
|
55
72
|
const flush = () => {
|
|
@@ -138,10 +155,7 @@ function renamedPath(names) {
|
|
|
138
155
|
}
|
|
139
156
|
/** The path a `diff --git` header names, or undefined when it names none. */
|
|
140
157
|
function headerPath(header) {
|
|
141
|
-
const
|
|
142
|
-
? header.slice(0, -1)
|
|
143
|
-
: header;
|
|
144
|
-
const names = line.slice(GIT_HEADER.length);
|
|
158
|
+
const names = withoutCarriageReturn(header).slice(GIT_HEADER.length);
|
|
145
159
|
return sharedPath(names) || renamedPath(names) || undefined;
|
|
146
160
|
}
|
|
147
161
|
/**
|
|
@@ -155,19 +169,78 @@ function headerPath(header) {
|
|
|
155
169
|
export function movedTo(text) {
|
|
156
170
|
const paths = [];
|
|
157
171
|
for (const raw of text.split('\n')) {
|
|
158
|
-
const line = raw
|
|
159
|
-
? raw.slice(0, -1)
|
|
160
|
-
: raw;
|
|
172
|
+
const line = withoutCarriageReturn(raw);
|
|
161
173
|
const prefix = MOVED_TO_PREFIXES.find(start => line.startsWith(start));
|
|
162
174
|
if (prefix === undefined)
|
|
163
175
|
continue;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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);
|
|
168
205
|
}
|
|
169
206
|
return paths;
|
|
170
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
|
+
}
|
|
171
244
|
/**
|
|
172
245
|
* Every section of a git-style unified diff, in order. Only `diff --git` lines open a section, so the
|
|
173
246
|
* text before the first one is a section of its own, of path '', and a diff without such headers
|
|
@@ -302,12 +375,9 @@ export function locate(sections, file, line) {
|
|
|
302
375
|
if (!section)
|
|
303
376
|
return undefined;
|
|
304
377
|
for (const hunk of hunksOf(sections, section.path, section.text)) {
|
|
305
|
-
if (
|
|
378
|
+
if (!holds(hunk, line))
|
|
306
379
|
continue;
|
|
307
|
-
const
|
|
308
|
-
if (at === -1 && hunk.count > 0)
|
|
309
|
-
continue;
|
|
310
|
-
const center = Math.max(at, 0);
|
|
380
|
+
const center = hunk.rowOfLine[line - hunk.start] ?? 0;
|
|
311
381
|
const rows = hunk.rows.slice(Math.max(0, center - EVIDENCE_RADIUS), center + EVIDENCE_RADIUS + 1);
|
|
312
382
|
return {
|
|
313
383
|
path: section.path,
|
|
@@ -382,7 +452,7 @@ export function addedLines(sections, path) {
|
|
|
382
452
|
* stays true, and the cache is dropped with the map.
|
|
383
453
|
*/
|
|
384
454
|
const parsedSections = new WeakMap();
|
|
385
|
-
function hunksOf(sections, path, text) {
|
|
455
|
+
export function hunksOf(sections, path, text) {
|
|
386
456
|
let parsed = parsedSections.get(sections);
|
|
387
457
|
if (!parsed) {
|
|
388
458
|
parsed = new Map();
|
|
@@ -397,8 +467,19 @@ function hunksOf(sections, path, text) {
|
|
|
397
467
|
}
|
|
398
468
|
/** The merged spans of each section, built once, in a cache that is dropped with the sections map like `parsedSections`. */
|
|
399
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
|
+
}
|
|
400
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. */
|
|
401
|
-
function heldSpan(hunk) {
|
|
482
|
+
export function heldSpan(hunk) {
|
|
402
483
|
if (hunk.rows.length === 0)
|
|
403
484
|
return undefined;
|
|
404
485
|
const shown = hunk.count === 0
|
|
@@ -442,8 +523,11 @@ function spansOf(sections, path, text) {
|
|
|
442
523
|
* headers and `` markers are never mistaken for hunk lines, and a line
|
|
443
524
|
* that cannot belong to a hunk closes it even when the counts promised more. The empty element
|
|
444
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.
|
|
445
529
|
*/
|
|
446
|
-
function parseHunks(section) {
|
|
530
|
+
export function parseHunks(section) {
|
|
447
531
|
const hunks = [];
|
|
448
532
|
let current;
|
|
449
533
|
let oldLeft = 0;
|
|
@@ -451,7 +535,7 @@ function parseHunks(section) {
|
|
|
451
535
|
const lines = section.split('\n');
|
|
452
536
|
if (lines.at(-1) === '')
|
|
453
537
|
lines.pop();
|
|
454
|
-
for (const raw of lines) {
|
|
538
|
+
for (const [at, raw] of lines.entries()) {
|
|
455
539
|
const text = raw.endsWith('\r')
|
|
456
540
|
? raw.slice(0, -1)
|
|
457
541
|
: raw;
|
|
@@ -478,10 +562,19 @@ function parseHunks(section) {
|
|
|
478
562
|
}
|
|
479
563
|
const header = HUNK_HEADER.exec(text);
|
|
480
564
|
if (header) {
|
|
481
|
-
|
|
482
|
-
|
|
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;
|
|
483
575
|
current = {
|
|
484
|
-
|
|
576
|
+
at,
|
|
577
|
+
start,
|
|
485
578
|
count: newLeft,
|
|
486
579
|
rows: [],
|
|
487
580
|
rowOfLine: [],
|
|
@@ -491,6 +584,11 @@ function parseHunks(section) {
|
|
|
491
584
|
}
|
|
492
585
|
return hunks;
|
|
493
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
|
+
}
|
|
494
592
|
/** The most diff quorum reads, from stdin, a file or a suite, in bytes: a larger one is refused and reading stops there. */
|
|
495
593
|
export const MAX_DIFF_BYTES = 16 * 1024 * 1024;
|
|
496
594
|
/**
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { ProfileRun } from './stages/context.ts';
|
|
2
|
+
import type { Estimate } from './types.ts';
|
|
3
|
+
/**
|
|
4
|
+
* How many times the bound of a research request over-counts the tokens of code. The bound is one token per
|
|
5
|
+
* UTF-8 byte, and code diffs run 2.9 to 3.8 bytes per token across the tokenizers measured, so a third of the
|
|
6
|
+
* bound is what code most likely takes, and a diff of prose or of encoded data takes more. It only scales the
|
|
7
|
+
* expected spend of an estimate: the ledger reserves the whole bound at run time.
|
|
8
|
+
*/
|
|
9
|
+
export declare const BOUND_OVER_CODE = 3;
|
|
10
|
+
/** What the estimate reads of a quorum's settings. */
|
|
11
|
+
export interface EstimateSettings {
|
|
12
|
+
/** Whether a file's path is left out of every request. */
|
|
13
|
+
readonly excluded: (path: string) => boolean;
|
|
14
|
+
/** The most tokens of diff text one research part holds. */
|
|
15
|
+
readonly partTokens: number;
|
|
16
|
+
/** How many findings of a research reply each profile sends to the panel. */
|
|
17
|
+
readonly maxFindings: number;
|
|
18
|
+
/** How many provider calls run at once in one review. */
|
|
19
|
+
readonly concurrency: number;
|
|
20
|
+
/** The token cap of one review. */
|
|
21
|
+
readonly maxReviewTokens: number;
|
|
22
|
+
/** The deadline of one review, in milliseconds. */
|
|
23
|
+
readonly reviewTimeoutMs: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* What a review of `diff` by `runs` may cost, before any call: nothing is sent, and no provider's `complete` is
|
|
27
|
+
* called, only the `room` and the `worstCase` it states, which are what the ledger reserves with. It reads the
|
|
28
|
+
* diff as `review` does, less the sections `excluded` leaves out and the files held back as secrets, except the
|
|
29
|
+
* ones `sent` names, and plans each profile's research the same way, so the reservation it states for each
|
|
30
|
+
* research part is the one the ledger takes for it. A diff with nothing left to read makes no call and costs
|
|
31
|
+
* nothing.
|
|
32
|
+
*
|
|
33
|
+
* A vote's reservation depends on a finding that does not exist yet, so each lens's is a bound: the request for
|
|
34
|
+
* a finding at the caps the harness cleans a finder's text to, citing the largest section of the filtered
|
|
35
|
+
* diff, whole for a voter that states no room or whose room holds it, else the partial context filled to its
|
|
36
|
+
* room. A review may cast `maxFindings` votes of each lens in each profile.
|
|
37
|
+
*
|
|
38
|
+
* `expected` counts the research parts at `1 / BOUND_OVER_CODE` of their reservation, rounded up, and the votes
|
|
39
|
+
* not at all: how many findings research reports is not known before it runs, so the votes are in `worst`,
|
|
40
|
+
* at their bound, alone. `inFlight` is the sum of the `concurrency` largest bounds among the research parts
|
|
41
|
+
* and the votes, each lens's counted `maxFindings` times, since the review's one limit shares its slots
|
|
42
|
+
* between every profile. `fits` is `expected + inFlight <= maxReviewTokens`. It is an estimate: the ledger
|
|
43
|
+
* still holds the cap at run time, whatever it says.
|
|
44
|
+
*/
|
|
45
|
+
export declare function estimateReview(runs: readonly ProfileRun[], settings: EstimateSettings, diff: string, sent?: (path: string) => boolean): Estimate;
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { reviewScope } from "./coverage.js";
|
|
2
|
+
import { diffSections, splitDiff } from "./diff.js";
|
|
3
|
+
import { partText } from "./parts.js";
|
|
4
|
+
import { researchRequest, voteRequest } from "./prompts.js";
|
|
5
|
+
import { reservationOf, roomOf } from "./stages/context.js";
|
|
6
|
+
import { planResearch } from "./stages/research.js";
|
|
7
|
+
import { tokenBound } from "./tokens.js";
|
|
8
|
+
import { MAX_CATEGORY, MAX_EVIDENCE, MAX_SUMMARY } from "./verify.js";
|
|
9
|
+
/**
|
|
10
|
+
* How many times the bound of a research request over-counts the tokens of code. The bound is one token per
|
|
11
|
+
* UTF-8 byte, and code diffs run 2.9 to 3.8 bytes per token across the tokenizers measured, so a third of the
|
|
12
|
+
* bound is what code most likely takes, and a diff of prose or of encoded data takes more. It only scales the
|
|
13
|
+
* expected spend of an estimate: the ledger reserves the whole bound at run time.
|
|
14
|
+
*/
|
|
15
|
+
export const BOUND_OVER_CODE = 3;
|
|
16
|
+
/**
|
|
17
|
+
* What a review of `diff` by `runs` may cost, before any call: nothing is sent, and no provider's `complete` is
|
|
18
|
+
* called, only the `room` and the `worstCase` it states, which are what the ledger reserves with. It reads the
|
|
19
|
+
* diff as `review` does, less the sections `excluded` leaves out and the files held back as secrets, except the
|
|
20
|
+
* ones `sent` names, and plans each profile's research the same way, so the reservation it states for each
|
|
21
|
+
* research part is the one the ledger takes for it. A diff with nothing left to read makes no call and costs
|
|
22
|
+
* nothing.
|
|
23
|
+
*
|
|
24
|
+
* A vote's reservation depends on a finding that does not exist yet, so each lens's is a bound: the request for
|
|
25
|
+
* a finding at the caps the harness cleans a finder's text to, citing the largest section of the filtered
|
|
26
|
+
* diff, whole for a voter that states no room or whose room holds it, else the partial context filled to its
|
|
27
|
+
* room. A review may cast `maxFindings` votes of each lens in each profile.
|
|
28
|
+
*
|
|
29
|
+
* `expected` counts the research parts at `1 / BOUND_OVER_CODE` of their reservation, rounded up, and the votes
|
|
30
|
+
* not at all: how many findings research reports is not known before it runs, so the votes are in `worst`,
|
|
31
|
+
* at their bound, alone. `inFlight` is the sum of the `concurrency` largest bounds among the research parts
|
|
32
|
+
* and the votes, each lens's counted `maxFindings` times, since the review's one limit shares its slots
|
|
33
|
+
* between every profile. `fits` is `expected + inFlight <= maxReviewTokens`. It is an estimate: the ledger
|
|
34
|
+
* still holds the cap at run time, whatever it says.
|
|
35
|
+
*/
|
|
36
|
+
export function estimateReview(runs, settings, diff, sent = () => false) {
|
|
37
|
+
const scope = reviewScope(diff, settings.excluded, sent);
|
|
38
|
+
const filtered = diffSections(scope.diff);
|
|
39
|
+
const largest = scope.diff.trim() === ''
|
|
40
|
+
? undefined
|
|
41
|
+
: largestSection(splitDiff(scope.diff), scope.diff);
|
|
42
|
+
const estimates = runs.map(run => ({
|
|
43
|
+
id: run.id,
|
|
44
|
+
estimate: largest === undefined
|
|
45
|
+
? noCall()
|
|
46
|
+
: profileEstimate(run, planResearch(run, scope.diff, filtered, settings.partTokens), scope.diff, largest, settings.maxFindings),
|
|
47
|
+
}));
|
|
48
|
+
const profiles = estimates.map(({ estimate }) => estimate);
|
|
49
|
+
const expected = sum(profiles.map(profile => profile.expected));
|
|
50
|
+
const inFlight = reservedAtOnce(profiles, settings.maxFindings, settings.concurrency);
|
|
51
|
+
return {
|
|
52
|
+
profiles: Object.fromEntries(estimates.map(({ id, estimate }) => [id, estimate])),
|
|
53
|
+
expected,
|
|
54
|
+
worst: sum(profiles.map(profile => profile.worst)),
|
|
55
|
+
inFlight,
|
|
56
|
+
maxReviewTokens: settings.maxReviewTokens,
|
|
57
|
+
reviewTimeoutMs: settings.reviewTimeoutMs,
|
|
58
|
+
fits: expected + inFlight <= settings.maxReviewTokens,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function sum(numbers) {
|
|
62
|
+
return numbers.reduce((total, number) => total + number, 0);
|
|
63
|
+
}
|
|
64
|
+
/** The estimate of a profile whose research makes no call: nothing is reserved, and no vote is cast. */
|
|
65
|
+
function noCall() {
|
|
66
|
+
return {
|
|
67
|
+
parts: [],
|
|
68
|
+
votes: 0,
|
|
69
|
+
vote: {},
|
|
70
|
+
expected: 0,
|
|
71
|
+
worst: 0,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** The largest section of the filtered diff, as `splitDiff` keys them; the whole diff when it keeps none, as a vote falls back on. */
|
|
75
|
+
function largestSection(sections, diff) {
|
|
76
|
+
let largest;
|
|
77
|
+
for (const [path, text] of sections) {
|
|
78
|
+
const tokens = tokenBound(text);
|
|
79
|
+
if (largest === undefined || tokens > largest.tokens)
|
|
80
|
+
largest = { path, text, tokens };
|
|
81
|
+
}
|
|
82
|
+
return largest ?? {
|
|
83
|
+
path: '',
|
|
84
|
+
text: diff,
|
|
85
|
+
tokens: tokenBound(diff),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** One profile's estimate, from the plan its research follows. */
|
|
89
|
+
function profileEstimate(run, plan, diff, largest, maxFindings) {
|
|
90
|
+
const parts = researchReservations(run, plan, diff);
|
|
91
|
+
if (parts.length === 0)
|
|
92
|
+
return noCall();
|
|
93
|
+
const bounds = run.lenses.map(lens => voteBound(run, lens, largest));
|
|
94
|
+
return {
|
|
95
|
+
parts,
|
|
96
|
+
votes: maxFindings * run.lenses.length,
|
|
97
|
+
vote: Object.fromEntries(run.lenses.map((lens, index) => [lens.id, bounds[index]])),
|
|
98
|
+
expected: sum(parts.map(part => Math.ceil(part / BOUND_OVER_CODE))),
|
|
99
|
+
worst: sum(parts) + maxFindings * sum(bounds),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* What the ledger reserves for each research request the review would send, in order, built as `research` builds
|
|
104
|
+
* them: one request holding the whole filtered diff when the plan has no packing, else one per part whose text is
|
|
105
|
+
* not blank, a plan of one part sending its text with no parts block, and a plan of several naming each part's
|
|
106
|
+
* position among them and the change's files. A part that holds only white space is asked of no model.
|
|
107
|
+
*/
|
|
108
|
+
function researchReservations(run, plan, diff) {
|
|
109
|
+
const { instructions } = run.research;
|
|
110
|
+
const reserve = (request) => reservationOf(run.research, request);
|
|
111
|
+
const { packing } = plan;
|
|
112
|
+
if (packing === undefined) {
|
|
113
|
+
return [
|
|
114
|
+
reserve(researchRequest(instructions, diff, run.guidelines)),
|
|
115
|
+
];
|
|
116
|
+
}
|
|
117
|
+
const { parts } = packing;
|
|
118
|
+
const { files, unlisted } = plan.listed ?? { files: [], unlisted: 0 };
|
|
119
|
+
return parts.flatMap((part, index) => {
|
|
120
|
+
const text = partText(plan.sections, packing, part);
|
|
121
|
+
if (text.trim() === '')
|
|
122
|
+
return [];
|
|
123
|
+
return [
|
|
124
|
+
reserve(parts.length === 1
|
|
125
|
+
? researchRequest(instructions, text, run.guidelines)
|
|
126
|
+
: researchRequest(instructions, text, run.guidelines, {
|
|
127
|
+
index: index + 1,
|
|
128
|
+
count: parts.length,
|
|
129
|
+
files,
|
|
130
|
+
unlisted,
|
|
131
|
+
})),
|
|
132
|
+
];
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
/** A finding as the harness leaves it at its longest: every text the finder or the diff writes at its cap, in the one character that costs a token a character. */
|
|
136
|
+
function longestFinding(file) {
|
|
137
|
+
return {
|
|
138
|
+
id: '',
|
|
139
|
+
profile: '',
|
|
140
|
+
file,
|
|
141
|
+
line: Number.MAX_SAFE_INTEGER,
|
|
142
|
+
category: 'x'.repeat(MAX_CATEGORY),
|
|
143
|
+
summary: 'x'.repeat(MAX_SUMMARY),
|
|
144
|
+
evidence: 'x'.repeat(MAX_EVIDENCE),
|
|
145
|
+
quoteMatched: true,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The most the ledger reserves for one vote of `lens`: the request for the longest finding citing the largest
|
|
150
|
+
* section. The section is sent whole, unless the lens's voter states a room the section exceeds, as the panel
|
|
151
|
+
* decides it: the room of the request with an empty diff block, and the room of the partial request, whose
|
|
152
|
+
* block takes the section's header and the hunks nearest the cited line that fit. Then the vote is the partial
|
|
153
|
+
* request filled to that room, the most such a context holds. A room of the partial request that states
|
|
154
|
+
* nothing, or less than 0, sends the whole section, which the provider refuses as too large.
|
|
155
|
+
*/
|
|
156
|
+
function voteBound(run, lens, largest) {
|
|
157
|
+
const finding = longestFinding(largest.path);
|
|
158
|
+
const request = (context, partial = false) => voteRequest(lens.instructions, finding, context, run.guidelines, partial);
|
|
159
|
+
const room = roomOf(lens, request(''));
|
|
160
|
+
if (room !== undefined && largest.tokens > room) {
|
|
161
|
+
const partialRoom = roomOf(lens, request('', true));
|
|
162
|
+
if (partialRoom !== undefined && partialRoom >= 0)
|
|
163
|
+
return reservationOf(lens, request('x'.repeat(Math.min(partialRoom, largest.tokens)), true));
|
|
164
|
+
}
|
|
165
|
+
return reservationOf(lens, request(largest.text));
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* What the calls that run at once reserve: the sum of the `concurrency` largest bounds among every research part
|
|
169
|
+
* and every vote of every profile, a lens's vote counted `maxFindings` times, once per finding it may judge. The
|
|
170
|
+
* review's concurrency limit is one for all its profiles, so the bounds are taken from all of them together; with
|
|
171
|
+
* fewer calls than slots, every call counts.
|
|
172
|
+
*/
|
|
173
|
+
function reservedAtOnce(profiles, maxFindings, concurrency) {
|
|
174
|
+
const calls = profiles.flatMap(({ parts, vote }) => [
|
|
175
|
+
...parts.map(bound => ({ bound, count: 1 })),
|
|
176
|
+
...Object.values(vote).map(bound => ({ bound, count: maxFindings })),
|
|
177
|
+
]).sort((a, b) => b.bound - a.bound);
|
|
178
|
+
let slots = concurrency;
|
|
179
|
+
let reserved = 0;
|
|
180
|
+
for (const { bound, count } of calls) {
|
|
181
|
+
const taken = Math.min(count, slots);
|
|
182
|
+
reserved += bound * taken;
|
|
183
|
+
slots -= taken;
|
|
184
|
+
if (slots === 0)
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
return reserved;
|
|
188
|
+
}
|
package/dist/core/flags.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Flag } from './types.ts';
|
|
1
|
+
import type { Flag, LeftOutFile } from './types.ts';
|
|
2
2
|
/**
|
|
3
3
|
* The advisory flags of a diff, read by code from its sections before any model sees it. They tell
|
|
4
4
|
* a human where to look and never change a verdict, a confidence or an exit code.
|
|
@@ -22,3 +22,16 @@ import type { Flag } from './types.ts';
|
|
|
22
22
|
* zero-width characters, inside a word or between words, is found and its line raises both flags.
|
|
23
23
|
*/
|
|
24
24
|
export declare function diffFlags(sections: ReadonlyMap<string, string>): Flag[];
|
|
25
|
+
/**
|
|
26
|
+
* The flags of the files left out as secrets, in the order they are listed: one `secret-file` flag for
|
|
27
|
+
* each `skipped` entry of the rule `secret` (a source skipped it, by its name or, when its name is no
|
|
28
|
+
* secret's, by a credential it holds), for each `secret` entry (held back from the diff, by its name or by
|
|
29
|
+
* the kind of credential it holds) and for each `deleted-secret` entry (the change deletes it whole: the
|
|
30
|
+
* history still holds it, so rotate what it held). Each is at the file's own path, absent for the empty
|
|
31
|
+
* name that stands for the text no header names, and with no line, since no model saw the file and the
|
|
32
|
+
* flag quotes none of it. A person should look at whether the file belongs in the change, or rotate what
|
|
33
|
+
* it held. An entry an `exclude` pattern matched first is `excluded`, so it raises none: leaving it out
|
|
34
|
+
* was the operator's choice, and a file the input sends is never held. Like every flag, it changes no
|
|
35
|
+
* verdict and no exit code.
|
|
36
|
+
*/
|
|
37
|
+
export declare function secretFlags(leftOut: readonly LeftOutFile[]): Flag[];
|
package/dist/core/flags.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { addedLines } from "./diff.js";
|
|
2
2
|
import { hiddenLabels, spaceHidden, stripHidden } from "./sanitize.js";
|
|
3
|
+
import { CREDENTIAL_PHRASES, isSecretName } from "./secrets.js";
|
|
3
4
|
/** How many code points or patterns a flag's detail names before it counts the rest. */
|
|
4
5
|
const MAX_LISTED = 8;
|
|
5
6
|
/** A byte-order mark: an editor may write one at the start of a file, so it is not flagged there. */
|
|
@@ -11,6 +12,15 @@ const TAB = 'U+0009';
|
|
|
11
12
|
* characters belong to the emoji, so they are not flagged. Outside such a sequence they are.
|
|
12
13
|
*/
|
|
13
14
|
const EMOJI = /\p{RGI_Emoji}/gv;
|
|
15
|
+
/**
|
|
16
|
+
* What a `secret-file` flag says, in words the code writes: a file a path review skipped, by its name or by
|
|
17
|
+
* a credential it holds, and a file held back from the review of a diff, held or deleted whole, by its name
|
|
18
|
+
* or by a kind of credential. It names the kind and never a line of the file.
|
|
19
|
+
*/
|
|
20
|
+
const SKIPPED_BY_NAME = 'named as a secret, so a path review sent none of it: check that it belongs in the repository';
|
|
21
|
+
const SKIPPED_BY_CONTENT = 'holds what reads as a credential, so a path review sent none of it: remove it from the repository, and rotate it if it is real';
|
|
22
|
+
const HELD_BY_NAME = 'named as a secret, so no model was sent any of it: check that it belongs in the change';
|
|
23
|
+
const DELETED_BY_NAME = 'deletes a file named as a secret, so no model was sent any of it: the history still holds the file, so rotate what it held';
|
|
14
24
|
/**
|
|
15
25
|
* Text that speaks to an AI reviewer instead of a person, in forms rare in ordinary code: the
|
|
16
26
|
* markup of chat templates and tool calls (in the research run, only LLM libraries held any),
|
|
@@ -130,3 +140,49 @@ export function diffFlags(sections) {
|
|
|
130
140
|
}
|
|
131
141
|
return flags;
|
|
132
142
|
}
|
|
143
|
+
/** What a `secret-file` flag says of a file left out, or undefined when the file is no secret. */
|
|
144
|
+
function secretDetail(entry) {
|
|
145
|
+
switch (entry.reason) {
|
|
146
|
+
case 'skipped':
|
|
147
|
+
if (entry.rule !== 'secret')
|
|
148
|
+
return undefined;
|
|
149
|
+
return isSecretName(entry.file)
|
|
150
|
+
? SKIPPED_BY_NAME
|
|
151
|
+
: SKIPPED_BY_CONTENT;
|
|
152
|
+
case 'secret':
|
|
153
|
+
return entry.rule === 'name'
|
|
154
|
+
? HELD_BY_NAME
|
|
155
|
+
: `holds what reads as ${CREDENTIAL_PHRASES[entry.rule]}, so no model was sent any of it: keep it out of the repository, and rotate it if it is real`;
|
|
156
|
+
case 'deleted-secret':
|
|
157
|
+
return entry.rule === 'name'
|
|
158
|
+
? DELETED_BY_NAME
|
|
159
|
+
: `deletes a file that holds what reads as ${CREDENTIAL_PHRASES[entry.rule]}, so no model was sent any of it: the history still holds it, so rotate it`;
|
|
160
|
+
default:
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* The flags of the files left out as secrets, in the order they are listed: one `secret-file` flag for
|
|
166
|
+
* each `skipped` entry of the rule `secret` (a source skipped it, by its name or, when its name is no
|
|
167
|
+
* secret's, by a credential it holds), for each `secret` entry (held back from the diff, by its name or by
|
|
168
|
+
* the kind of credential it holds) and for each `deleted-secret` entry (the change deletes it whole: the
|
|
169
|
+
* history still holds it, so rotate what it held). Each is at the file's own path, absent for the empty
|
|
170
|
+
* name that stands for the text no header names, and with no line, since no model saw the file and the
|
|
171
|
+
* flag quotes none of it. A person should look at whether the file belongs in the change, or rotate what
|
|
172
|
+
* it held. An entry an `exclude` pattern matched first is `excluded`, so it raises none: leaving it out
|
|
173
|
+
* was the operator's choice, and a file the input sends is never held. Like every flag, it changes no
|
|
174
|
+
* verdict and no exit code.
|
|
175
|
+
*/
|
|
176
|
+
export function secretFlags(leftOut) {
|
|
177
|
+
return leftOut.flatMap((entry) => {
|
|
178
|
+
const detail = secretDetail(entry);
|
|
179
|
+
if (detail === undefined)
|
|
180
|
+
return [];
|
|
181
|
+
const where = entry.file === ''
|
|
182
|
+
? {}
|
|
183
|
+
: { file: entry.file };
|
|
184
|
+
return [
|
|
185
|
+
{ code: 'secret-file', ...where, detail },
|
|
186
|
+
];
|
|
187
|
+
});
|
|
188
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ConfigIssue } from './errors.ts';
|
|
2
|
+
import type { LeftOutFile, ReviewSource } from './types.ts';
|
|
3
|
+
/** The rules a path review skips a file by: the bench's schema takes the same list. */
|
|
4
|
+
export declare const SKIP_RULES: readonly ["vendored", "lockfile", "minified", "secret", "binary", "submodule", "lfs"];
|
|
5
|
+
/**
|
|
6
|
+
* Every problem with a value given as a review's `source`, so an untyped caller gets issues, never a
|
|
7
|
+
* TypeError: none when it is unset, `source: must be an object`, `source.kind` when the kind is not one
|
|
8
|
+
* of the five, else `source.objectFormat`, and, once the format is known, each id field that is not a full
|
|
9
|
+
* commit id of it (a local or staged source's `head` may be null), a `paths` that is not an array of
|
|
10
|
+
* strings and a `skips` that is not a boolean. A field it does not know is no problem: `sourceOf` leaves it
|
|
11
|
+
* out of the record. No message repeats a value.
|
|
12
|
+
*/
|
|
13
|
+
export declare function sourceIssues(value: unknown): ConfigIssue[];
|
|
14
|
+
/**
|
|
15
|
+
* Every problem with a value given as a review's `leftOut`: none when it is unset, `leftOut: must be an
|
|
16
|
+
* array`, else for each entry `leftOut.<index>` when it is no object, its `file` that is not a string, its
|
|
17
|
+
* `reason` that is none of `excluded`, `binary`, `submodule`, `skipped`, `secret` and `deleted-secret`, and
|
|
18
|
+
* its `rule`: one of the seven a skipped file has, one of the nine a held file has (`secret` and
|
|
19
|
+
* `deleted-secret`), which no other reason has. A reason or a rule is a value of this release: a reader of
|
|
20
|
+
* a result takes one it does not know as a file left out, but a caller of `review` names ones it can.
|
|
21
|
+
*/
|
|
22
|
+
export declare function leftOutIssues(value: unknown): ConfigIssue[];
|
|
23
|
+
/**
|
|
24
|
+
* Every problem with a value given as a review's `send`: none when it is unset, `send: must be an array`,
|
|
25
|
+
* else `send.<index>: must name a file` for each entry that is not a non-empty string, a hole included. The
|
|
26
|
+
* empty name is the text no header names, which no file of the diff is, so it is no file to send.
|
|
27
|
+
*/
|
|
28
|
+
export declare function sendIssues(value: unknown): ConfigIssue[];
|
|
29
|
+
/**
|
|
30
|
+
* A checked source as the review records it: a copy of its known fields, so the caller's later changes to
|
|
31
|
+
* the object, or to its `paths`, never reach the result, and `{ kind: 'diff' }` for an unset source.
|
|
32
|
+
*/
|
|
33
|
+
export declare function sourceOf(value: ReviewSource | undefined): ReviewSource;
|
|
34
|
+
/**
|
|
35
|
+
* The files a review leaves out as one list: the source's first, then the diff's, each file, reason and rule
|
|
36
|
+
* once, the first kept. Every entry is a copy of its known fields, `rule` only for a skipped or a held file.
|
|
37
|
+
*/
|
|
38
|
+
export declare function mergeLeftOut(source: readonly LeftOutFile[], diff: readonly LeftOutFile[]): LeftOutFile[];
|