@tobi-or-not/quorum 0.1.0 → 0.2.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/LICENSE +202 -0
- package/README.md +4 -0
- package/dist/cli/bin.js +9 -1
- package/dist/cli/main.js +49 -12
- package/dist/cli/print-config.js +38 -9
- package/dist/cli/render.js +18 -6
- package/dist/config/claude-code.js +42 -11
- package/dist/config/env.js +135 -32
- package/dist/config/from-config.js +43 -9
- package/dist/config/load.js +163 -37
- package/dist/config/profiles.js +87 -20
- package/dist/config/resolve.js +194 -47
- package/dist/config/schema.js +98 -23
- package/dist/core/diff.js +65 -17
- package/dist/core/errors.js +14 -5
- package/dist/core/flags.js +30 -6
- package/dist/core/output.js +49 -13
- package/dist/core/policy.js +6 -2
- package/dist/core/profile.js +42 -15
- package/dist/core/prompts.js +20 -5
- package/dist/core/quorum.js +128 -33
- package/dist/core/sanitize.js +3 -1
- package/dist/core/stages/context.js +30 -5
- package/dist/core/stages/panel.js +55 -9
- package/dist/core/stages/research.js +13 -3
- package/dist/core/verify.js +6 -2
- package/dist/index.js +5 -1
- package/dist/providers/anthropic.js +28 -5
- package/dist/providers/build.js +57 -16
- package/dist/providers/claude-code.js +206 -51
- package/dist/providers/gemini.js +61 -14
- package/dist/providers/http.js +32 -9
- package/dist/providers/messages.js +14 -2
- package/dist/providers/ollama.js +31 -8
- package/dist/providers/openai-compatible.js +51 -15
- package/dist/providers/options.js +120 -40
- package/dist/providers/presets.js +143 -16
- package/dist/providers/reply.js +15 -5
- package/dist/providers/size.js +11 -3
- package/package.json +5 -4
package/dist/core/diff.js
CHANGED
|
@@ -6,7 +6,17 @@ const HUNK_HEADER = /^@@ -\d+(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
|
|
|
6
6
|
/** How many diff lines of a hunk, either side of the cited one, make up a finding's evidence. */
|
|
7
7
|
const EVIDENCE_RADIUS = 2;
|
|
8
8
|
/** What a backslash and one more character stand for in a name git quotes (octal bytes are read apart). */
|
|
9
|
-
const QUOTED_ESCAPES = new Map([
|
|
9
|
+
const QUOTED_ESCAPES = new Map([
|
|
10
|
+
['a', '\x07'],
|
|
11
|
+
['b', '\b'],
|
|
12
|
+
['f', '\f'],
|
|
13
|
+
['n', '\n'],
|
|
14
|
+
['r', '\r'],
|
|
15
|
+
['t', '\t'],
|
|
16
|
+
['v', '\v'],
|
|
17
|
+
['\\', '\\'],
|
|
18
|
+
['"', '"'],
|
|
19
|
+
]);
|
|
10
20
|
/** One byte as git writes it in a quoted name: a backslash then three octal digits (0 to 255). */
|
|
11
21
|
const OCTAL_BYTE = /^[0-3][0-7]{2}$/;
|
|
12
22
|
/**
|
|
@@ -20,7 +30,9 @@ const SURROGATE_ESCAPE = 0xDC00;
|
|
|
20
30
|
const isQuoted = (name) => name.startsWith('"') && name.endsWith('"');
|
|
21
31
|
/** A name without the double quotes git puts around one with special characters, escapes untouched. */
|
|
22
32
|
function unquote(name) {
|
|
23
|
-
return isQuoted(name)
|
|
33
|
+
return isQuoted(name)
|
|
34
|
+
? name.slice(1, -1)
|
|
35
|
+
: name;
|
|
24
36
|
}
|
|
25
37
|
/** A path without the `a/`, `b/` or `./` prefix a diff, or a model, may put in front of it. */
|
|
26
38
|
function withoutPrefix(path) {
|
|
@@ -87,10 +99,14 @@ function sharedPath(names) {
|
|
|
87
99
|
const second = names.slice(middle + 1);
|
|
88
100
|
const before = unquote(first);
|
|
89
101
|
const after = unquote(second);
|
|
90
|
-
const decode = isQuoted(first) && isQuoted(second)
|
|
102
|
+
const decode = isQuoted(first) && isQuoted(second)
|
|
103
|
+
? decodeQuoted
|
|
104
|
+
: (path) => path;
|
|
91
105
|
if (before === after)
|
|
92
106
|
return decode(before);
|
|
93
|
-
return before[1] === '/' && after[1] === '/' && before.slice(2) === after.slice(2)
|
|
107
|
+
return before[1] === '/' && after[1] === '/' && before.slice(2) === after.slice(2)
|
|
108
|
+
? decode(before.slice(2))
|
|
109
|
+
: '';
|
|
94
110
|
}
|
|
95
111
|
/**
|
|
96
112
|
* The new path of a renamed or copied file, whose two names differ: what follows the last ` b/`
|
|
@@ -112,12 +128,18 @@ function renamedPath(names) {
|
|
|
112
128
|
if (start === -1)
|
|
113
129
|
return '';
|
|
114
130
|
const path = names.slice(start);
|
|
115
|
-
const inside = path.endsWith('"')
|
|
116
|
-
|
|
131
|
+
const inside = path.endsWith('"')
|
|
132
|
+
? path.slice(0, -1)
|
|
133
|
+
: path;
|
|
134
|
+
return quoted
|
|
135
|
+
? decodeQuoted(inside)
|
|
136
|
+
: inside;
|
|
117
137
|
}
|
|
118
138
|
/** The path a `diff --git` header names, or undefined when it names none. */
|
|
119
139
|
function headerPath(header) {
|
|
120
|
-
const line = header.endsWith('\r')
|
|
140
|
+
const line = header.endsWith('\r')
|
|
141
|
+
? header.slice(0, -1)
|
|
142
|
+
: header;
|
|
121
143
|
const names = line.slice(GIT_HEADER.length);
|
|
122
144
|
return sharedPath(names) || renamedPath(names) || undefined;
|
|
123
145
|
}
|
|
@@ -145,7 +167,9 @@ export function splitDiff(diff) {
|
|
|
145
167
|
return;
|
|
146
168
|
const text = lines.join('\n');
|
|
147
169
|
const previous = sections.get(path);
|
|
148
|
-
sections.set(path, previous === undefined
|
|
170
|
+
sections.set(path, previous === undefined
|
|
171
|
+
? text
|
|
172
|
+
: `${previous}\n${text}`);
|
|
149
173
|
};
|
|
150
174
|
for (const line of diff.split('\n')) {
|
|
151
175
|
if (line.startsWith(GIT_HEADER)) {
|
|
@@ -183,9 +207,13 @@ export function sectionFor(sections, file) {
|
|
|
183
207
|
const unkeyed = sections.get('');
|
|
184
208
|
if (sections.size === 1 && unkeyed !== undefined)
|
|
185
209
|
return { path: '', text: unkeyed };
|
|
186
|
-
const candidates = [
|
|
210
|
+
const candidates = [
|
|
211
|
+
...sections.keys(),
|
|
212
|
+
].filter(path => path !== '' && (path.endsWith(`/${wanted}`) || wanted.endsWith(`/${path}`)));
|
|
187
213
|
const [only] = candidates;
|
|
188
|
-
return candidates.length === 1 && only !== undefined
|
|
214
|
+
return candidates.length === 1 && only !== undefined
|
|
215
|
+
? { path: only, text: sections.get(only) ?? '' }
|
|
216
|
+
: undefined;
|
|
189
217
|
}
|
|
190
218
|
/**
|
|
191
219
|
* The spellings a cited file is looked up under, in order: exactly as given, trimmed, without its
|
|
@@ -196,7 +224,11 @@ export function sectionFor(sections, file) {
|
|
|
196
224
|
* preamble.
|
|
197
225
|
*/
|
|
198
226
|
function citedSpellings(given, written) {
|
|
199
|
-
const spellings = [
|
|
227
|
+
const spellings = [
|
|
228
|
+
given,
|
|
229
|
+
written,
|
|
230
|
+
withoutPrefix(written),
|
|
231
|
+
];
|
|
200
232
|
if (written.includes('\\') || isQuoted(written)) {
|
|
201
233
|
const decoded = decodeQuoted(unquote(written));
|
|
202
234
|
spellings.push(decoded, withoutPrefix(decoded));
|
|
@@ -232,7 +264,10 @@ export function locate(sections, file, line) {
|
|
|
232
264
|
continue;
|
|
233
265
|
const center = Math.max(at, 0);
|
|
234
266
|
const rows = hunk.rows.slice(Math.max(0, center - EVIDENCE_RADIUS), center + EVIDENCE_RADIUS + 1);
|
|
235
|
-
return {
|
|
267
|
+
return {
|
|
268
|
+
path: section.path,
|
|
269
|
+
evidence: rows.join('\n'),
|
|
270
|
+
};
|
|
236
271
|
}
|
|
237
272
|
return undefined;
|
|
238
273
|
}
|
|
@@ -251,8 +286,12 @@ export function addedLines(sections, path) {
|
|
|
251
286
|
for (const hunk of hunksOf(sections, path, text)) {
|
|
252
287
|
for (const [offset, at] of hunk.rowOfLine.entries()) {
|
|
253
288
|
const row = hunk.rows[at];
|
|
254
|
-
if (row.startsWith('+'))
|
|
255
|
-
added.push({
|
|
289
|
+
if (row.startsWith('+')) {
|
|
290
|
+
added.push({
|
|
291
|
+
line: hunk.start + offset,
|
|
292
|
+
text: row.slice(1),
|
|
293
|
+
});
|
|
294
|
+
}
|
|
256
295
|
}
|
|
257
296
|
}
|
|
258
297
|
return added;
|
|
@@ -291,8 +330,12 @@ function parseHunks(section) {
|
|
|
291
330
|
if (lines.at(-1) === '')
|
|
292
331
|
lines.pop();
|
|
293
332
|
for (const raw of lines) {
|
|
294
|
-
const text = raw.endsWith('\r')
|
|
295
|
-
|
|
333
|
+
const text = raw.endsWith('\r')
|
|
334
|
+
? raw.slice(0, -1)
|
|
335
|
+
: raw;
|
|
336
|
+
const marker = text === ''
|
|
337
|
+
? ' '
|
|
338
|
+
: text[0];
|
|
296
339
|
if (current && (oldLeft > 0 || newLeft > 0)) {
|
|
297
340
|
if (marker === '\\')
|
|
298
341
|
continue;
|
|
@@ -315,7 +358,12 @@ function parseHunks(section) {
|
|
|
315
358
|
if (header) {
|
|
316
359
|
oldLeft = Number(header[1] ?? 1);
|
|
317
360
|
newLeft = Number(header[3] ?? 1);
|
|
318
|
-
current = {
|
|
361
|
+
current = {
|
|
362
|
+
start: Number(header[2]),
|
|
363
|
+
count: newLeft,
|
|
364
|
+
rows: [],
|
|
365
|
+
rowOfLine: [],
|
|
366
|
+
};
|
|
319
367
|
hunks.push(current);
|
|
320
368
|
}
|
|
321
369
|
}
|
package/dist/core/errors.js
CHANGED
|
@@ -26,7 +26,9 @@ export function isSafeSegment(segment) {
|
|
|
26
26
|
}
|
|
27
27
|
/** A path as an issue names it: its segments joined with `.`, each one that is not a safe segment printed as `*`. */
|
|
28
28
|
export function issuePath(segments) {
|
|
29
|
-
return segments.map(segment => isSafeSegment(segment)
|
|
29
|
+
return segments.map(segment => isSafeSegment(segment)
|
|
30
|
+
? String(segment)
|
|
31
|
+
: '*').join('.');
|
|
30
32
|
}
|
|
31
33
|
/** The issues a `ConfigError` message lists: a config of thousands of values can carry as many issues. */
|
|
32
34
|
const LISTED_ISSUES = 50;
|
|
@@ -37,10 +39,15 @@ const LISTED_ISSUES = 50;
|
|
|
37
39
|
export class ConfigError extends QuorumError {
|
|
38
40
|
issues;
|
|
39
41
|
constructor(issues) {
|
|
40
|
-
const lines = issues.slice(0, LISTED_ISSUES).map(issue => (issue.path
|
|
42
|
+
const lines = issues.slice(0, LISTED_ISSUES).map(issue => (issue.path
|
|
43
|
+
? `${issue.path}: ${issue.message}`
|
|
44
|
+
: issue.message));
|
|
41
45
|
const rest = issues.length - LISTED_ISSUES;
|
|
42
|
-
if (rest > 0)
|
|
43
|
-
lines.push(`and ${rest} more issue${rest === 1
|
|
46
|
+
if (rest > 0) {
|
|
47
|
+
lines.push(`and ${rest} more issue${rest === 1
|
|
48
|
+
? ''
|
|
49
|
+
: 's'}`);
|
|
50
|
+
}
|
|
44
51
|
super(lines.join('\n'));
|
|
45
52
|
this.issues = issues;
|
|
46
53
|
}
|
|
@@ -62,7 +69,9 @@ export class ProviderError extends QuorumError {
|
|
|
62
69
|
/** True for network errors, timeouts, 408, 409, 429 and 5xx; a caller may retry the call. */
|
|
63
70
|
retryable;
|
|
64
71
|
constructor(details) {
|
|
65
|
-
super(`${details.providerId}: ${details.message}`, details.cause === undefined
|
|
72
|
+
super(`${details.providerId}: ${details.message}`, details.cause === undefined
|
|
73
|
+
? undefined
|
|
74
|
+
: { cause: details.cause });
|
|
66
75
|
this.providerId = details.providerId;
|
|
67
76
|
this.kind = details.kind;
|
|
68
77
|
this.status = details.status;
|
package/dist/core/flags.js
CHANGED
|
@@ -31,7 +31,13 @@ const AI_ADDRESSED = [
|
|
|
31
31
|
{ name: 'a forged vote reply', pattern: /"keep"\s*:\s*(?:true|false)\s*,\s*"reasoning"\s*:|"reasoning"\s*:\s*"(?:[^"\\]|\\.)*"\s*,\s*"keep"\s*:\s*(?:true|false)/i },
|
|
32
32
|
{ name: 'a forged research reply', pattern: /\{\s*"findings"\s*:\s*\[\s*\]\s*\}/i },
|
|
33
33
|
];
|
|
34
|
-
|
|
34
|
+
function newTally() {
|
|
35
|
+
return {
|
|
36
|
+
first: 0,
|
|
37
|
+
lines: 0,
|
|
38
|
+
found: new Set(),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
35
41
|
/** Count `line` in `tally` when it held anything. */
|
|
36
42
|
function record(tally, line, items) {
|
|
37
43
|
if (items.length === 0)
|
|
@@ -45,11 +51,15 @@ function record(tally, line, items) {
|
|
|
45
51
|
/** The first `MAX_LISTED` items, then how many more there are. */
|
|
46
52
|
function listed(items) {
|
|
47
53
|
const names = items.slice(0, MAX_LISTED).join(', ');
|
|
48
|
-
return items.length > MAX_LISTED
|
|
54
|
+
return items.length > MAX_LISTED
|
|
55
|
+
? `${names} and ${items.length - MAX_LISTED} more`
|
|
56
|
+
: names;
|
|
49
57
|
}
|
|
50
58
|
/** The labels of the hidden characters of an added line, less the ones the rules leave alone. */
|
|
51
59
|
function lineLabels(text, line) {
|
|
52
|
-
const shown = line === 1 && text.startsWith(BOM)
|
|
60
|
+
const shown = line === 1 && text.startsWith(BOM)
|
|
61
|
+
? text.slice(BOM.length)
|
|
62
|
+
: text;
|
|
53
63
|
// A space, not nothing, so that what surrounds an emoji cannot join: two halves of a surrogate
|
|
54
64
|
// pair, or a byte-order mark that would then start line 1.
|
|
55
65
|
return hiddenLabels(shown.replace(EMOJI, ' ')).filter(label => label !== TAB);
|
|
@@ -60,12 +70,24 @@ function lineLabels(text, line) {
|
|
|
60
70
|
* A pattern that matches both forms counts once.
|
|
61
71
|
*/
|
|
62
72
|
function addressedNames(text) {
|
|
63
|
-
const forms = [
|
|
73
|
+
const forms = [
|
|
74
|
+
stripHidden(text),
|
|
75
|
+
spaceHidden(text),
|
|
76
|
+
];
|
|
64
77
|
return AI_ADDRESSED.filter(({ pattern }) => forms.some(form => pattern.test(form))).map(({ name }) => name);
|
|
65
78
|
}
|
|
66
79
|
/** The flag of `code` at the first line `tally` counted, or none when it counted no line. */
|
|
67
80
|
function flagOf(code, where, tally) {
|
|
68
|
-
return tally.lines === 0
|
|
81
|
+
return tally.lines === 0
|
|
82
|
+
? []
|
|
83
|
+
: [
|
|
84
|
+
{
|
|
85
|
+
code,
|
|
86
|
+
...where,
|
|
87
|
+
line: tally.first,
|
|
88
|
+
detail: `${listed([...tally.found])} on ${tally.lines} added line(s)`,
|
|
89
|
+
},
|
|
90
|
+
];
|
|
69
91
|
}
|
|
70
92
|
/**
|
|
71
93
|
* The advisory flags of a diff, read by code from its sections before any model sees it. They tell
|
|
@@ -92,7 +114,9 @@ function flagOf(code, where, tally) {
|
|
|
92
114
|
export function diffFlags(sections) {
|
|
93
115
|
const flags = [];
|
|
94
116
|
for (const path of sections.keys()) {
|
|
95
|
-
const where = path === ''
|
|
117
|
+
const where = path === ''
|
|
118
|
+
? {}
|
|
119
|
+
: { file: path };
|
|
96
120
|
const nameLabels = hiddenLabels(path);
|
|
97
121
|
if (nameLabels.length > 0)
|
|
98
122
|
flags.push({ code: 'hidden-characters', ...where, detail: `the file name holds ${listed(nameLabels)}` });
|
package/dist/core/output.js
CHANGED
|
@@ -6,7 +6,17 @@ import { HIDDEN_CLASS } from "./sanitize.js";
|
|
|
6
6
|
* `format`. Strict modes refuse some of them (Anthropic documents numeric constraints as unsupported),
|
|
7
7
|
* and zod still enforces each one on the reply.
|
|
8
8
|
*/
|
|
9
|
-
const NON_PORTABLE_KEYWORDS = new Set([
|
|
9
|
+
const NON_PORTABLE_KEYWORDS = new Set([
|
|
10
|
+
'minimum',
|
|
11
|
+
'maximum',
|
|
12
|
+
'exclusiveMinimum',
|
|
13
|
+
'exclusiveMaximum',
|
|
14
|
+
'multipleOf',
|
|
15
|
+
'minLength',
|
|
16
|
+
'maxLength',
|
|
17
|
+
'pattern',
|
|
18
|
+
'format',
|
|
19
|
+
]);
|
|
10
20
|
/** Wire shape of one finding. Semantic checks (line >= 1, non-empty file) run after parsing. */
|
|
11
21
|
const FindingReplySchema = z.object({
|
|
12
22
|
file: z.string(),
|
|
@@ -20,7 +30,9 @@ const VoteReplySchema = z.object({
|
|
|
20
30
|
keep: z.boolean(),
|
|
21
31
|
reasoning: z.string(),
|
|
22
32
|
});
|
|
23
|
-
const ResearchReplySchema = z.object({
|
|
33
|
+
const ResearchReplySchema = z.object({
|
|
34
|
+
findings: z.array(FindingReplySchema),
|
|
35
|
+
});
|
|
24
36
|
/** JSON Schemas shown to the model; they stay free of transforms so zod can always emit them. */
|
|
25
37
|
export const researchReplyJsonSchema = toWireSchema(ResearchReplySchema);
|
|
26
38
|
export const voteReplyJsonSchema = toWireSchema(VoteReplySchema);
|
|
@@ -91,7 +103,9 @@ function syntaxAfterAnswer(visible, first, copies) {
|
|
|
91
103
|
from = copy.end + 1;
|
|
92
104
|
}
|
|
93
105
|
const found = visible.slice(from).search(JSON_SYNTAX);
|
|
94
|
-
return found === -1
|
|
106
|
+
return found === -1
|
|
107
|
+
? -1
|
|
108
|
+
: from + found;
|
|
95
109
|
}
|
|
96
110
|
/**
|
|
97
111
|
* Validate a research reply. The envelope must be exactly `{ findings: [...] }`: a missing or
|
|
@@ -288,8 +302,11 @@ function repeatsKey(json) {
|
|
|
288
302
|
const code = json.charCodeAt(index);
|
|
289
303
|
if (code === QUOTE) {
|
|
290
304
|
let close = index + 1;
|
|
291
|
-
while (close < json.length && json.charCodeAt(close) !== QUOTE)
|
|
292
|
-
close += json.charCodeAt(close) === BACKSLASH
|
|
305
|
+
while (close < json.length && json.charCodeAt(close) !== QUOTE) {
|
|
306
|
+
close += json.charCodeAt(close) === BACKSLASH
|
|
307
|
+
? 2
|
|
308
|
+
: 1;
|
|
309
|
+
}
|
|
293
310
|
const keys = open.at(-1);
|
|
294
311
|
if (expectKey && keys) {
|
|
295
312
|
const key = String(JSON.parse(json.slice(index, close + 1)));
|
|
@@ -301,7 +318,9 @@ function repeatsKey(json) {
|
|
|
301
318
|
index = close;
|
|
302
319
|
}
|
|
303
320
|
else if (code === OPEN_BRACE || code === OPEN_BRACKET) {
|
|
304
|
-
open.push(code === OPEN_BRACE
|
|
321
|
+
open.push(code === OPEN_BRACE
|
|
322
|
+
? new Set()
|
|
323
|
+
: undefined);
|
|
305
324
|
expectKey = code === OPEN_BRACE;
|
|
306
325
|
}
|
|
307
326
|
else if (code === CLOSE_BRACE || code === CLOSE_BRACKET) {
|
|
@@ -319,16 +338,27 @@ function normalizeFinding(item) {
|
|
|
319
338
|
if (!isRecord(item) || typeof item.line !== 'string')
|
|
320
339
|
return item;
|
|
321
340
|
const line = item.line.trim();
|
|
322
|
-
return DIGITS.test(line)
|
|
341
|
+
return DIGITS.test(line)
|
|
342
|
+
? {
|
|
343
|
+
...item,
|
|
344
|
+
line: Number(line),
|
|
345
|
+
}
|
|
346
|
+
: item;
|
|
323
347
|
}
|
|
324
348
|
/** Accept "true" / "false" strings for keep; anything else is left for the schema to reject. */
|
|
325
349
|
function normalizeVote(reply) {
|
|
326
|
-
const keep = typeof reply.keep === 'string'
|
|
327
|
-
|
|
350
|
+
const keep = typeof reply.keep === 'string'
|
|
351
|
+
? reply.keep.trim().toLowerCase()
|
|
352
|
+
: undefined;
|
|
353
|
+
return keep === 'true' || keep === 'false'
|
|
354
|
+
? { ...reply, keep: keep === 'true' }
|
|
355
|
+
: reply;
|
|
328
356
|
}
|
|
329
357
|
function describeIssue(error) {
|
|
330
358
|
const issue = error.issues[0];
|
|
331
|
-
return issue
|
|
359
|
+
return issue
|
|
360
|
+
? `${issue.path.join('.') || '(root)'}: ${issue.message}`
|
|
361
|
+
: 'invalid value';
|
|
332
362
|
}
|
|
333
363
|
/** Whether `value` is a non-null object that is not an array: the shape every untyped input is checked against before its fields are read. */
|
|
334
364
|
export function isRecord(value) {
|
|
@@ -342,9 +372,15 @@ function portable(schema) {
|
|
|
342
372
|
return schema;
|
|
343
373
|
return Object.fromEntries(Object.entries(schema)
|
|
344
374
|
.filter(([key]) => !NON_PORTABLE_KEYWORDS.has(key))
|
|
345
|
-
.map(([key, value]) => [
|
|
346
|
-
|
|
347
|
-
|
|
375
|
+
.map(([key, value]) => [
|
|
376
|
+
key,
|
|
377
|
+
key === 'properties' && isRecord(value)
|
|
378
|
+
? Object.fromEntries(Object.entries(value).map(([name, field]) => [
|
|
379
|
+
name,
|
|
380
|
+
portable(field),
|
|
381
|
+
]))
|
|
382
|
+
: portable(value),
|
|
383
|
+
]));
|
|
348
384
|
}
|
|
349
385
|
function toWireSchema(schema) {
|
|
350
386
|
const { $schema: _dialect, ...wire } = z.toJSONSchema(schema);
|
package/dist/core/policy.js
CHANGED
|
@@ -56,7 +56,9 @@ export function verdictStatus(verdict, thresholds) {
|
|
|
56
56
|
if (confidenceFor(verdict.keptVotes, thresholds) !== undefined)
|
|
57
57
|
return 'kept';
|
|
58
58
|
const failed = verdict.votes.filter(vote => vote.vote === null).length;
|
|
59
|
-
return confidenceFor(verdict.keptVotes + failed, thresholds) === undefined
|
|
59
|
+
return confidenceFor(verdict.keptVotes + failed, thresholds) === undefined
|
|
60
|
+
? 'discarded'
|
|
61
|
+
: 'unreviewed';
|
|
60
62
|
}
|
|
61
63
|
/**
|
|
62
64
|
* The higher level a kept verdict's failed votes leave undecided: the level its kept plus failed
|
|
@@ -69,7 +71,9 @@ export function undecidedLevel(verdict, thresholds) {
|
|
|
69
71
|
if (failed === 0)
|
|
70
72
|
return undefined;
|
|
71
73
|
const reachable = confidenceFor(verdict.keptVotes + failed, thresholds);
|
|
72
|
-
return reachable !== undefined && reachable !== verdict.confidence
|
|
74
|
+
return reachable !== undefined && reachable !== verdict.confidence
|
|
75
|
+
? reachable
|
|
76
|
+
: undefined;
|
|
73
77
|
}
|
|
74
78
|
/**
|
|
75
79
|
* Resolve one rule. `rule` is typed `unknown` on purpose: a value outside the `Rule` type (what
|
package/dist/core/profile.js
CHANGED
|
@@ -13,13 +13,18 @@ const VOTE_VARIABLES = new Set(['lens.id', 'lens.question', 'panel.size']);
|
|
|
13
13
|
* (the profile's place in a larger input, `profile` by default) while the id is invalid.
|
|
14
14
|
*/
|
|
15
15
|
export function profileIssues(profile, fallbackRoot = 'profile') {
|
|
16
|
-
if (!isRecord(profile))
|
|
17
|
-
return [
|
|
16
|
+
if (!isRecord(profile)) {
|
|
17
|
+
return [
|
|
18
|
+
{ path: fallbackRoot, message: 'must be an object' },
|
|
19
|
+
];
|
|
20
|
+
}
|
|
18
21
|
const issues = [];
|
|
19
22
|
const idIssue = idProblem(profile.id);
|
|
20
23
|
if (idIssue)
|
|
21
24
|
issues.push({ path: `${fallbackRoot}.id`, message: idIssue });
|
|
22
|
-
const root = isValidId(profile.id)
|
|
25
|
+
const root = isValidId(profile.id)
|
|
26
|
+
? profile.id
|
|
27
|
+
: fallbackRoot;
|
|
23
28
|
issues.push(...lensIssues(profile.lenses, root));
|
|
24
29
|
issues.push(...policyIssues(profile, root));
|
|
25
30
|
issues.push(...promptIssues(profile.prompts, root));
|
|
@@ -58,12 +63,17 @@ function isValidId(id) {
|
|
|
58
63
|
}
|
|
59
64
|
/** `unknown` on purpose: an untyped caller can hand over a missing id, and `RegExp.test` would read it as the text "undefined". */
|
|
60
65
|
function idProblem(id) {
|
|
61
|
-
return isValidId(id)
|
|
66
|
+
return isValidId(id)
|
|
67
|
+
? undefined
|
|
68
|
+
: `must match ${ID_PATTERN.source}, got ${JSON.stringify(id)}`;
|
|
62
69
|
}
|
|
63
70
|
/** `Array.from` reads a hole in an untyped array as `undefined`, so it is reported like any other lens that is not an object. */
|
|
64
71
|
function lensIssues(lenses, root) {
|
|
65
|
-
if (!Array.isArray(lenses))
|
|
66
|
-
return [
|
|
72
|
+
if (!Array.isArray(lenses)) {
|
|
73
|
+
return [
|
|
74
|
+
{ path: `${root}.lenses`, message: 'must be an array of lenses' },
|
|
75
|
+
];
|
|
76
|
+
}
|
|
67
77
|
const issues = [];
|
|
68
78
|
const seen = new Set();
|
|
69
79
|
Array.from(lenses).forEach((lens, index) => {
|
|
@@ -72,7 +82,9 @@ function lensIssues(lenses, root) {
|
|
|
72
82
|
issues.push({ path, message: 'must be an object with an id and a question' });
|
|
73
83
|
return;
|
|
74
84
|
}
|
|
75
|
-
const lensIssue = idProblem(lens.id) ?? (seen.has(lens.id)
|
|
85
|
+
const lensIssue = idProblem(lens.id) ?? (seen.has(lens.id)
|
|
86
|
+
? `repeats the lens id ${JSON.stringify(lens.id)}`
|
|
87
|
+
: undefined);
|
|
76
88
|
if (lensIssue)
|
|
77
89
|
issues.push({ path: `${path}.id`, message: lensIssue });
|
|
78
90
|
seen.add(lens.id);
|
|
@@ -85,15 +97,21 @@ function lensIssues(lenses, root) {
|
|
|
85
97
|
}
|
|
86
98
|
/** The policy is resolved only when its shape is right and the lenses give it a count to resolve against. */
|
|
87
99
|
function policyIssues(profile, root) {
|
|
88
|
-
if (profile.policy !== undefined && !isRecord(profile.policy))
|
|
89
|
-
return [
|
|
100
|
+
if (profile.policy !== undefined && !isRecord(profile.policy)) {
|
|
101
|
+
return [
|
|
102
|
+
{ path: `${root}.policy`, message: 'must be an object with high, medium and low rules' },
|
|
103
|
+
];
|
|
104
|
+
}
|
|
90
105
|
if (!Array.isArray(profile.lenses))
|
|
91
106
|
return [];
|
|
92
107
|
return resolvePolicy(profile.policy ?? defaultPolicy, profile.lenses.length).issues.map(issue => ({ path: `${root}.${issue.path}`, message: issue.message }));
|
|
93
108
|
}
|
|
94
109
|
function promptIssues(prompts, root) {
|
|
95
|
-
if (!isRecord(prompts))
|
|
96
|
-
return [
|
|
110
|
+
if (!isRecord(prompts)) {
|
|
111
|
+
return [
|
|
112
|
+
{ path: `${root}.prompts`, message: 'must be an object holding the research and vote templates' },
|
|
113
|
+
];
|
|
114
|
+
}
|
|
97
115
|
const templates = [
|
|
98
116
|
{ name: 'research', problem: researchVariableProblem },
|
|
99
117
|
{ name: 'vote', problem: voteVariableProblem },
|
|
@@ -101,7 +119,11 @@ function promptIssues(prompts, root) {
|
|
|
101
119
|
return templates.flatMap(({ name, problem }) => {
|
|
102
120
|
const path = `${root}.prompts.${name}`;
|
|
103
121
|
const template = prompts[name];
|
|
104
|
-
return typeof template === 'string'
|
|
122
|
+
return typeof template === 'string'
|
|
123
|
+
? templateIssues(path, template, problem)
|
|
124
|
+
: [
|
|
125
|
+
{ path, message: 'must be a string' },
|
|
126
|
+
];
|
|
105
127
|
});
|
|
106
128
|
}
|
|
107
129
|
function researchVariableProblem(name) {
|
|
@@ -110,12 +132,17 @@ function researchVariableProblem(name) {
|
|
|
110
132
|
: 'the research template takes no variables';
|
|
111
133
|
}
|
|
112
134
|
function voteVariableProblem(name) {
|
|
113
|
-
return VOTE_VARIABLES.has(name)
|
|
135
|
+
return VOTE_VARIABLES.has(name)
|
|
136
|
+
? undefined
|
|
137
|
+
: 'the vote template takes {{lens.id}}, {{lens.question}} and {{panel.size}}';
|
|
114
138
|
}
|
|
115
139
|
/** A blank template is one issue; otherwise one issue per distinct refused variable, in order of first appearance. */
|
|
116
140
|
function templateIssues(path, template, problem) {
|
|
117
|
-
if (template.trim() === '')
|
|
118
|
-
return [
|
|
141
|
+
if (template.trim() === '') {
|
|
142
|
+
return [
|
|
143
|
+
{ path, message: 'must not be empty' },
|
|
144
|
+
];
|
|
145
|
+
}
|
|
119
146
|
const issues = [];
|
|
120
147
|
const reported = new Set();
|
|
121
148
|
for (const name of placeholderNames(template)) {
|
package/dist/core/prompts.js
CHANGED
|
@@ -18,7 +18,9 @@ export const PLACEHOLDER = /\{\{([^{}]*)\}\}/g;
|
|
|
18
18
|
export function renderTemplate(template, values) {
|
|
19
19
|
return template.replace(PLACEHOLDER, (placeholder, name) => {
|
|
20
20
|
const key = name.trim();
|
|
21
|
-
return Object.hasOwn(values, key)
|
|
21
|
+
return Object.hasOwn(values, key)
|
|
22
|
+
? values[key]
|
|
23
|
+
: placeholder;
|
|
22
24
|
});
|
|
23
25
|
}
|
|
24
26
|
/**
|
|
@@ -51,7 +53,9 @@ const BLOCK_DESCRIPTIONS = {
|
|
|
51
53
|
* message names the block the request carries.
|
|
52
54
|
*/
|
|
53
55
|
export function researchRequest(instructions, diff, guidelines) {
|
|
54
|
-
return buildRequest(instructions, [
|
|
56
|
+
return buildRequest(instructions, [
|
|
57
|
+
{ label: 'diff', content: diff, description: BLOCK_DESCRIPTIONS.researchDiff },
|
|
58
|
+
], guidelines, { name: 'research_findings', schema: researchReplyJsonSchema });
|
|
55
59
|
}
|
|
56
60
|
/**
|
|
57
61
|
* One vote call, built only from the finding, the lens instructions and the diff context: no other
|
|
@@ -73,9 +77,15 @@ export function voteRequest(instructions, finding, diffContext, guidelines) {
|
|
|
73
77
|
}
|
|
74
78
|
function buildRequest(instructions, blocks, guidelines, output) {
|
|
75
79
|
const withGuidelines = guidelines !== undefined && guidelines.trim() !== ''
|
|
76
|
-
? [
|
|
80
|
+
? [
|
|
81
|
+
...blocks,
|
|
82
|
+
{ label: 'guidelines', content: guidelines, description: BLOCK_DESCRIPTIONS.guidelines },
|
|
83
|
+
]
|
|
77
84
|
: blocks;
|
|
78
|
-
const fenced = withGuidelines.map(block => ({
|
|
85
|
+
const fenced = withGuidelines.map(block => ({
|
|
86
|
+
...fence(block.label, block.content),
|
|
87
|
+
description: block.description,
|
|
88
|
+
}));
|
|
79
89
|
const dataNotice = [
|
|
80
90
|
'The user message holds these blocks:',
|
|
81
91
|
...fenced.map(block => `- <${block.tag}>: ${block.description}`),
|
|
@@ -87,7 +97,12 @@ function buildRequest(instructions, blocks, guidelines, output) {
|
|
|
87
97
|
dataNotice,
|
|
88
98
|
UNTRUSTED_CONTENT_NOTICE,
|
|
89
99
|
].join('\n\n'),
|
|
90
|
-
messages: [
|
|
100
|
+
messages: [
|
|
101
|
+
{
|
|
102
|
+
role: 'user',
|
|
103
|
+
content: fenced.map(block => block.text).join('\n\n'),
|
|
104
|
+
},
|
|
105
|
+
],
|
|
91
106
|
output,
|
|
92
107
|
};
|
|
93
108
|
}
|