@tobi-or-not/quorum 0.1.0 → 0.3.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 (66) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +5 -0
  3. package/dist/bench/fingerprint.d.ts +36 -0
  4. package/dist/bench/fingerprint.js +79 -0
  5. package/dist/bench/records.d.ts +62 -0
  6. package/dist/bench/records.js +161 -0
  7. package/dist/bench/report.d.ts +110 -0
  8. package/dist/bench/report.js +332 -0
  9. package/dist/bench/run-dir.d.ts +101 -0
  10. package/dist/bench/run-dir.js +379 -0
  11. package/dist/bench/runner.d.ts +80 -0
  12. package/dist/bench/runner.js +160 -0
  13. package/dist/bench/score.d.ts +30 -0
  14. package/dist/bench/score.js +38 -0
  15. package/dist/bench/stats.d.ts +51 -0
  16. package/dist/bench/stats.js +83 -0
  17. package/dist/bench/suite.d.ts +87 -0
  18. package/dist/bench/suite.js +286 -0
  19. package/dist/cli/bench.d.ts +32 -0
  20. package/dist/cli/bench.js +463 -0
  21. package/dist/cli/bin.js +9 -1
  22. package/dist/cli/io.d.ts +45 -0
  23. package/dist/cli/io.js +45 -0
  24. package/dist/cli/main.d.ts +7 -37
  25. package/dist/cli/main.js +47 -60
  26. package/dist/cli/print-config.js +38 -9
  27. package/dist/cli/render.d.ts +10 -0
  28. package/dist/cli/render.js +37 -16
  29. package/dist/config/claude-code.js +42 -11
  30. package/dist/config/env.js +135 -32
  31. package/dist/config/from-config.js +43 -9
  32. package/dist/config/load.d.ts +43 -0
  33. package/dist/config/load.js +205 -46
  34. package/dist/config/profiles.js +87 -20
  35. package/dist/config/resolve.js +194 -47
  36. package/dist/config/schema.js +98 -23
  37. package/dist/core/diff.d.ts +10 -0
  38. package/dist/core/diff.js +90 -17
  39. package/dist/core/errors.js +14 -5
  40. package/dist/core/flags.js +30 -6
  41. package/dist/core/output.js +49 -13
  42. package/dist/core/policy.js +6 -2
  43. package/dist/core/profile.js +42 -15
  44. package/dist/core/prompts.d.ts +3 -1
  45. package/dist/core/prompts.js +28 -8
  46. package/dist/core/quorum.js +128 -33
  47. package/dist/core/sanitize.d.ts +16 -1
  48. package/dist/core/sanitize.js +31 -5
  49. package/dist/core/stages/context.js +30 -5
  50. package/dist/core/stages/panel.js +55 -9
  51. package/dist/core/stages/research.js +13 -3
  52. package/dist/core/verify.js +6 -2
  53. package/dist/index.js +5 -1
  54. package/dist/providers/anthropic.js +28 -5
  55. package/dist/providers/build.js +57 -16
  56. package/dist/providers/claude-code.js +206 -51
  57. package/dist/providers/gemini.js +61 -14
  58. package/dist/providers/http.js +32 -9
  59. package/dist/providers/messages.js +14 -2
  60. package/dist/providers/ollama.js +31 -8
  61. package/dist/providers/openai-compatible.js +51 -15
  62. package/dist/providers/options.js +120 -40
  63. package/dist/providers/presets.js +143 -16
  64. package/dist/providers/reply.js +15 -5
  65. package/dist/providers/size.js +11 -3
  66. package/package.json +6 -5
@@ -7,7 +7,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
7
7
  return path;
8
8
  };
9
9
  import { constants } from 'node:fs';
10
- import { open } from 'node:fs/promises';
10
+ import { lstat, open } from 'node:fs/promises';
11
11
  import { dirname, extname, isAbsolute, resolve } from 'node:path';
12
12
  import { pathToFileURL } from 'node:url';
13
13
  import { isMap, isPair, isScalar, isSeq, LineCounter, parseDocument } from 'yaml';
@@ -26,26 +26,75 @@ const MAX_DEPTH = 64;
26
26
  /** How many values a config may hold: more would be a walk and a schema check of unbounded work, and issues of unbounded number. */
27
27
  const MAX_VALUES = 10_000;
28
28
  /** The format of each extension a config may have, read from the map alone, never from an object's prototype. */
29
- const FORMATS = new Map([['.yaml', 'yaml'], ['.yml', 'yaml'], ['.json', 'json'], ['.mts', 'code'], ['.mjs', 'code'], ['.ts', 'code'], ['.js', 'code']]);
29
+ const FORMATS = new Map([
30
+ ['.yaml', 'yaml'],
31
+ ['.yml', 'yaml'],
32
+ ['.json', 'json'],
33
+ ['.mts', 'code'],
34
+ ['.mjs', 'code'],
35
+ ['.ts', 'code'],
36
+ ['.js', 'code'],
37
+ ]);
30
38
  /** A name a message may give what a config threw: a class name, never text the config could have written a secret into. */
31
39
  const ERROR_NAME = /^[A-Z][A-Za-z]{0,63}$/;
32
40
  const STAGES = ['research', 'vote'];
33
41
  function fail(path, message) {
34
- throw new ConfigError([{ path, message }]);
42
+ throw new ConfigError([
43
+ { path, message },
44
+ ]);
45
+ }
46
+ /** What an open that failed means, by its code: the runtime's own message is never kept. */
47
+ function openProblem(error, noFollow) {
48
+ const code = error.code;
49
+ if (code === 'ENOENT' || code === 'ENOTDIR')
50
+ return 'does not exist';
51
+ if (code === 'EISDIR')
52
+ return 'is not a file';
53
+ return code === 'ELOOP' && noFollow
54
+ ? 'is a symbolic link'
55
+ : 'cannot be read';
56
+ }
57
+ /** The problem a path has before an open that must not follow a link: it is one, or it is missing or unreadable; undefined when it is not a link. */
58
+ async function linkProblem(absolute) {
59
+ try {
60
+ return (await lstat(absolute)).isSymbolicLink()
61
+ ? 'is a symbolic link'
62
+ : undefined;
63
+ }
64
+ catch (error) {
65
+ const code = error.code;
66
+ return code === 'ENOENT' || code === 'ENOTDIR'
67
+ ? 'does not exist'
68
+ : 'cannot be read';
69
+ }
35
70
  }
36
71
  /**
37
72
  * The bytes of a regular file of at most `limit` bytes. The file is opened without blocking, so a FIFO
38
73
  * is refused instead of waiting for a writer, and at most one byte past the limit is read, however large
39
- * the file is or grows. The runtime's own message is never kept.
74
+ * the file is or grows. The runtime's own message is never kept. A symbolic link is followed, unless
75
+ * `options.noFollow` is set: then a link is `is a symbolic link`, refused by an `lstat` before the open,
76
+ * and the open itself refuses one with `O_NOFOLLOW` where the platform has it, which closes the race
77
+ * between the two on Linux and macOS (without it, on Windows, a link swapped in after the `lstat` is
78
+ * followed).
40
79
  */
41
- async function readBounded(absolute, limit, size) {
80
+ export async function readBounded(absolute, limit, size, options = {}) {
81
+ const noFollow = options.noFollow === true;
82
+ if (noFollow) {
83
+ const linked = await linkProblem(absolute);
84
+ if (linked !== undefined)
85
+ return { problem: linked };
86
+ }
87
+ const flags = noFollow && typeof constants.O_NOFOLLOW === 'number'
88
+ ? constants.O_RDONLY | constants.O_NONBLOCK | constants.O_NOFOLLOW
89
+ : constants.O_RDONLY | constants.O_NONBLOCK;
42
90
  let handle;
43
91
  try {
44
- handle = await open(absolute, constants.O_RDONLY | constants.O_NONBLOCK);
92
+ handle = await open(absolute, flags);
45
93
  }
46
94
  catch (error) {
47
- const code = error.code;
48
- return { problem: code === 'ENOENT' || code === 'ENOTDIR' ? 'does not exist' : code === 'EISDIR' ? 'is not a file' : 'cannot be read' };
95
+ return {
96
+ problem: openProblem(error, noFollow),
97
+ };
49
98
  }
50
99
  try {
51
100
  const stats = await handle.stat();
@@ -56,7 +105,11 @@ async function readBounded(absolute, limit, size) {
56
105
  for (let read = -1; read !== 0 && length < buffer.length; length += read) {
57
106
  read = (await handle.read(buffer, length, buffer.length - length, length)).bytesRead;
58
107
  }
59
- return length > limit ? { problem: `is over ${size}`, counted: limit } : { value: buffer.subarray(0, length) };
108
+ return length > limit
109
+ ? { problem: `is over ${size}`, counted: limit }
110
+ : {
111
+ value: buffer.subarray(0, length),
112
+ };
60
113
  }
61
114
  catch {
62
115
  return { problem: 'cannot be read' };
@@ -66,9 +119,11 @@ async function readBounded(absolute, limit, size) {
66
119
  }
67
120
  }
68
121
  /** The UTF-8 text of `bytes`, a leading byte-order mark dropped (the decoder's default). */
69
- function decode(bytes) {
122
+ export function decode(bytes) {
70
123
  try {
71
- return { value: new TextDecoder('utf-8', { fatal: true }).decode(bytes) };
124
+ return {
125
+ value: new TextDecoder('utf-8', { fatal: true }).decode(bytes),
126
+ };
72
127
  }
73
128
  catch {
74
129
  return { problem: 'is not UTF-8 text' };
@@ -77,7 +132,9 @@ function decode(bytes) {
77
132
  /** The UTF-8 text of a regular file of at most `limit` bytes. */
78
133
  async function readText(absolute, limit, size) {
79
134
  const bytes = await readBounded(absolute, limit, size);
80
- return 'problem' in bytes ? bytes : decode(bytes.value);
135
+ return 'problem' in bytes
136
+ ? bytes
137
+ : decode(bytes.value);
81
138
  }
82
139
  /** Where `offset` sits in the text, as a line and a column. */
83
140
  function position(lines, offset) {
@@ -100,7 +157,9 @@ function yamlProblem(error, lines) {
100
157
  * `MAX_DEPTH` keys deep: `dataIssues` refuses that data whatever the walk finds there.
101
158
  */
102
159
  function repeatedKeyAt(doc) {
103
- const stack = [{ node: doc.contents, depth: 0 }];
160
+ const stack = [
161
+ { node: doc.contents, depth: 0 },
162
+ ];
104
163
  for (let entry = stack.pop(); entry !== undefined; entry = stack.pop()) {
105
164
  const { node, depth } = entry;
106
165
  if (depth > MAX_DEPTH)
@@ -112,9 +171,14 @@ function repeatedKeyAt(doc) {
112
171
  if (isMap(node)) {
113
172
  const seen = new Set();
114
173
  for (const { key } of node.items) {
115
- const value = isScalar(key) ? key.value : key;
116
- if (seen.has(value))
117
- return (isScalar(key) ? key.range?.[0] : undefined) ?? 0;
174
+ const value = isScalar(key)
175
+ ? key.value
176
+ : key;
177
+ if (seen.has(value)) {
178
+ return (isScalar(key)
179
+ ? key.range?.[0]
180
+ : undefined) ?? 0;
181
+ }
118
182
  seen.add(value);
119
183
  }
120
184
  }
@@ -130,22 +194,42 @@ function repeatedKeyAt(doc) {
130
194
  * are not resolved; any error, any warning (an unknown tag is only a warning to the parser), a key that
131
195
  * repeats one of its mapping and any alias refuse the file; no problem carries source text.
132
196
  */
133
- function parseYaml(text) {
197
+ export function parseYaml(text) {
134
198
  const lines = new LineCounter();
135
199
  try {
136
- const doc = parseDocument(text, { schema: 'core', version: '1.2', merge: false, resolveKnownTags: false, uniqueKeys: false, stringKeys: true, prettyErrors: false, logLevel: 'error', strict: true, lineCounter: lines });
200
+ const doc = parseDocument(text, {
201
+ schema: 'core',
202
+ version: '1.2',
203
+ merge: false,
204
+ resolveKnownTags: false,
205
+ uniqueKeys: false,
206
+ stringKeys: true,
207
+ prettyErrors: false,
208
+ logLevel: 'error',
209
+ strict: true,
210
+ lineCounter: lines,
211
+ });
137
212
  const first = doc.errors[0] ?? doc.warnings[0];
138
- if (first !== undefined)
139
- return { problem: yamlProblem(first, lines) };
213
+ if (first !== undefined) {
214
+ return {
215
+ problem: yamlProblem(first, lines),
216
+ };
217
+ }
140
218
  const repeated = repeatedKeyAt(doc);
141
219
  if (repeated !== undefined)
142
220
  return { problem: `is not valid YAML at ${position(lines, repeated)} (DUPLICATE_KEY)` };
143
221
  const value = doc.toJS({ maxAliasCount: 0 });
144
- return value === null ? { problem: 'is empty' } : { value };
222
+ return value === null
223
+ ? { problem: 'is empty' }
224
+ : { value };
145
225
  }
146
226
  catch (error) {
147
227
  // An alias throws a ReferenceError once aliases are disabled; deep nesting can throw a RangeError.
148
- return { problem: error instanceof ReferenceError ? 'uses an alias, which quorum does not read' : 'is not valid YAML' };
228
+ return {
229
+ problem: error instanceof ReferenceError
230
+ ? 'uses an alias, which quorum does not read'
231
+ : 'is not valid YAML',
232
+ };
149
233
  }
150
234
  }
151
235
  /**
@@ -165,19 +249,27 @@ function parseJson(text) {
165
249
  }
166
250
  const lines = new LineCounter();
167
251
  try {
168
- const doc = parseDocument(text, { schema: 'json', uniqueKeys: false, prettyErrors: false, logLevel: 'error', lineCounter: lines });
252
+ const doc = parseDocument(text, {
253
+ schema: 'json',
254
+ uniqueKeys: false,
255
+ prettyErrors: false,
256
+ logLevel: 'error',
257
+ lineCounter: lines,
258
+ });
169
259
  const first = doc.errors[0];
170
260
  if (first !== undefined)
171
261
  return { problem: `is not valid JSON at ${position(lines, first.pos[0])} (${first.code})` };
172
262
  const repeated = repeatedKeyAt(doc);
173
- return repeated === undefined ? { value } : { problem: `has a duplicate key at ${position(lines, repeated)}` };
263
+ return repeated === undefined
264
+ ? { value }
265
+ : { problem: `has a duplicate key at ${position(lines, repeated)}` };
174
266
  }
175
267
  catch {
176
268
  return { problem: 'is not valid JSON' };
177
269
  }
178
270
  }
179
271
  /** Whether `value` is an object of data: an object whose prototype is `Object.prototype` or none, never a class instance. */
180
- function isPlainObject(value) {
272
+ export function isPlainObject(value) {
181
273
  if (typeof value !== 'object' || value === null || Array.isArray(value))
182
274
  return false;
183
275
  const prototype = Object.getPrototypeOf(value);
@@ -194,28 +286,57 @@ function isPlainObject(value) {
194
286
  * `MAX_VALUES` it stops and the config has that one issue, at the file: the schema never sees more data
195
287
  * than that, and an object that refers to itself ends the walk instead of repeating it.
196
288
  */
197
- function dataIssues(value, file) {
289
+ export function dataIssues(value, file) {
198
290
  const budget = { left: MAX_VALUES };
199
291
  const issues = valueIssues(value, [], file, 0, budget);
200
- return budget.left < 0 ? [{ path: file ?? 'config', message: `holds more than ${MAX_VALUES} values` }] : issues;
292
+ return budget.left < 0
293
+ ? [
294
+ { path: file ?? 'config', message: `holds more than ${MAX_VALUES} values` },
295
+ ]
296
+ : issues;
201
297
  }
202
298
  function valueIssues(value, segments, file, depth, budget) {
203
299
  if (--budget.left < 0)
204
300
  return [];
205
- if (depth > MAX_DEPTH)
206
- return [{ path: configPath(segments, file), message: `is nested more than ${MAX_DEPTH} keys deep` }];
301
+ if (depth > MAX_DEPTH) {
302
+ return [
303
+ {
304
+ path: configPath(segments, file),
305
+ message: `is nested more than ${MAX_DEPTH} keys deep`,
306
+ },
307
+ ];
308
+ }
207
309
  if (value === null || value === undefined || typeof value === 'string' || typeof value === 'boolean')
208
310
  return [];
209
- if (typeof value === 'number')
210
- return Number.isFinite(value) ? [] : [{ path: configPath(segments, file), message: 'must be a finite number' }];
311
+ if (typeof value === 'number') {
312
+ return Number.isFinite(value)
313
+ ? []
314
+ : [
315
+ {
316
+ path: configPath(segments, file),
317
+ message: 'must be a finite number',
318
+ },
319
+ ];
320
+ }
211
321
  if (isProvider(value))
212
322
  return [];
213
323
  if (Array.isArray(value))
214
324
  return value.flatMap((item, index) => valueIssues(item, [...segments, index], file, depth + 1, budget));
215
- if (!isPlainObject(value))
216
- return [{ path: configPath(segments, file), message: 'must be plain data: an object, an array, a string, a finite number, true, false or null' }];
325
+ if (!isPlainObject(value)) {
326
+ return [
327
+ {
328
+ path: configPath(segments, file),
329
+ message: 'must be plain data: an object, an array, a string, a finite number, true, false or null',
330
+ },
331
+ ];
332
+ }
217
333
  return Object.keys(value).flatMap(key => key === '__proto__'
218
- ? [{ path: configPath([...segments, key], file), message: 'is not allowed as a key: it can replace the prototype of an object' }]
334
+ ? [
335
+ {
336
+ path: configPath([...segments, key], file),
337
+ message: 'is not allowed as a key: it can replace the prototype of an object',
338
+ },
339
+ ]
219
340
  : valueIssues(value[key], [...segments, key], file, depth + 1, budget));
220
341
  }
221
342
  /** The name a message gives what a config threw. Reading it can run the config's code too, so nothing it throws gets out. */
@@ -224,7 +345,9 @@ function thrownName(error) {
224
345
  if (typeof error !== 'object' || error === null)
225
346
  return 'a value that is not an error';
226
347
  const { name } = error;
227
- return typeof name === 'string' && ERROR_NAME.test(name) ? name : 'an error';
348
+ return typeof name === 'string' && ERROR_NAME.test(name)
349
+ ? name
350
+ : 'an error';
228
351
  }
229
352
  catch {
230
353
  return 'an error';
@@ -260,7 +383,13 @@ function nameProviders(config) {
260
383
  const providers = config.providers;
261
384
  if (typeof providers !== 'object' || providers === null)
262
385
  return config;
263
- return { ...config, providers: Object.fromEntries(Object.entries(providers).map(([name, entry]) => [name, nameSnapshot(name, entry)])) };
386
+ return {
387
+ ...config,
388
+ providers: Object.fromEntries(Object.entries(providers).map(([name, entry]) => [
389
+ name,
390
+ nameSnapshot(name, entry),
391
+ ])),
392
+ };
264
393
  }
265
394
  /**
266
395
  * The plain object a config's code hands over, checked by `schema`: `read` returns it, then it must be a
@@ -279,9 +408,19 @@ export function checkCodeValue(schema, read, words) {
279
408
  let checked;
280
409
  try {
281
410
  const value = read();
282
- const issues = isPlainObject(value) ? dataIssues(value, words.file) : [{ path: at, message: words.notAnObject }];
283
- const result = issues.length > 0 ? { issues } : checkConfig(schema, value, words.file);
284
- checked = 'issues' in result ? result : { config: nameProviders(result.config) };
411
+ const issues = isPlainObject(value)
412
+ ? dataIssues(value, words.file)
413
+ : [
414
+ { path: at, message: words.notAnObject },
415
+ ];
416
+ const result = issues.length > 0
417
+ ? { issues }
418
+ : checkConfig(schema, value, words.file);
419
+ checked = 'issues' in result
420
+ ? result
421
+ : {
422
+ config: nameProviders(result.config),
423
+ };
285
424
  }
286
425
  catch (error) {
287
426
  fail(at, `${words.threw} (${thrownName(error)})`);
@@ -345,15 +484,29 @@ async function readPromptFiles(config, directory, path) {
345
484
  const prompt = prompts[stage];
346
485
  if (typeof prompt !== 'object')
347
486
  continue;
348
- const at = configPath(['profiles', id, 'prompts', stage, 'file'], path);
487
+ const at = configPath([
488
+ 'profiles',
489
+ id,
490
+ 'prompts',
491
+ stage,
492
+ 'file',
493
+ ], path);
349
494
  const absolute = resolve(directory, prompt.file);
350
495
  let text = reads.get(absolute);
351
496
  if (text === undefined) {
352
497
  const bytes = await readBounded(absolute, PROMPT_LIMIT, '64 KiB');
353
- total += 'problem' in bytes ? bytes.counted ?? 0 : bytes.value.length;
354
- if (total > PROMPT_TOTAL_LIMIT)
355
- throw new ConfigError([...issues, { path: at, message: 'takes the prompt files over 1 MiB in total' }]);
356
- text = 'problem' in bytes ? bytes : decode(bytes.value);
498
+ total += 'problem' in bytes
499
+ ? bytes.counted ?? 0
500
+ : bytes.value.length;
501
+ if (total > PROMPT_TOTAL_LIMIT) {
502
+ throw new ConfigError([
503
+ ...issues,
504
+ { path: at, message: 'takes the prompt files over 1 MiB in total' },
505
+ ]);
506
+ }
507
+ text = 'problem' in bytes
508
+ ? bytes
509
+ : decode(bytes.value);
357
510
  reads.set(absolute, text);
358
511
  }
359
512
  if ('problem' in text)
@@ -388,7 +541,11 @@ export async function readConfigFile(path, options = {}) {
388
541
  const text = await readText(source, CONFIG_LIMIT, '1 MiB');
389
542
  if ('problem' in text)
390
543
  fail(path, text.problem);
391
- const config = format === 'code' ? await loadCode(source, path) : checkData(format === 'yaml' ? parseYaml(text.value) : parseJson(text.value), path);
544
+ const config = format === 'code'
545
+ ? await loadCode(source, path)
546
+ : checkData(format === 'yaml'
547
+ ? parseYaml(text.value)
548
+ : parseJson(text.value), path);
392
549
  return { config: await readPromptFiles(config, dirname(source), path), source };
393
550
  }
394
551
  /**
@@ -411,7 +568,9 @@ export async function loadConfig(options = {}) {
411
568
  if (named !== undefined && !isAbsolute(named))
412
569
  fail('QUORUM_CONFIG', 'must be an absolute path');
413
570
  const path = options.configPath ?? named;
414
- const loaded = path === undefined ? undefined : await readConfigFile(path, { cwd: options.cwd });
571
+ const loaded = path === undefined
572
+ ? undefined
573
+ : await readConfigFile(path, { cwd: options.cwd });
415
574
  const config = resolveConfig(loaded?.config ?? {}, { env, selection: options.selection });
416
575
  return { config, source: loaded?.source, warnings: config.warnings };
417
576
  }
@@ -4,31 +4,53 @@ import { profileIssues } from "../core/profile.js";
4
4
  import { securityProfile } from "../profiles/security.js";
5
5
  import { unknownName } from "./schema.js";
6
6
  /** The built-in profiles a config can run or extend, by id. */
7
- const BUILT_IN = new Map([[securityProfile.id, securityProfile]]);
7
+ const BUILT_IN = new Map([
8
+ [securityProfile.id, securityProfile],
9
+ ]);
8
10
  const NEEDS_BASE = 'is required: the profile extends no built-in profile';
9
11
  const LEVELS = ['high', 'medium', 'low'];
10
12
  /** A field a record holds itself: a prototype, a polluted one included, fills none. */
11
13
  export function own(record, key) {
12
- return record !== undefined && Object.hasOwn(record, key) ? record[key] : undefined;
14
+ return record !== undefined && Object.hasOwn(record, key)
15
+ ? record[key]
16
+ : undefined;
13
17
  }
14
18
  /** A rule as a new frozen value: the config's own objects stay out of a frozen profile. */
15
19
  function copyRule(rule) {
16
- return typeof rule === 'object' ? Object.freeze({ ...rule }) : rule;
20
+ return typeof rule === 'object'
21
+ ? Object.freeze({ ...rule })
22
+ : rule;
17
23
  }
18
24
  /** The base's policy, else the default one, with the levels the entry sets (not `undefined`) over it. */
19
25
  function policyOf(base, entry) {
20
26
  const set = Object.fromEntries(LEVELS.flatMap((level) => {
21
27
  const rule = own(entry, level);
22
- return rule === undefined ? [] : [[level, rule]];
28
+ return rule === undefined
29
+ ? []
30
+ : [
31
+ [level, rule],
32
+ ];
23
33
  }));
24
- const merged = { ...base?.policy ?? defaultPolicy, ...set };
25
- return Object.freeze({ high: copyRule(merged.high), medium: copyRule(merged.medium), low: copyRule(merged.low) });
34
+ const merged = {
35
+ ...base?.policy ?? defaultPolicy,
36
+ ...set,
37
+ };
38
+ return Object.freeze({
39
+ high: copyRule(merged.high),
40
+ medium: copyRule(merged.medium),
41
+ low: copyRule(merged.low),
42
+ });
26
43
  }
27
44
  /** The lenses of the entry, else of the base: the fields the check reads, each read once, whatever the lens holds besides. */
28
45
  function lensesOf(entry, base) {
29
46
  // `profileIssues` refuses a lens whose id or question is not a string, so the casts hide nothing.
30
- if (entry !== undefined)
31
- return entry.map(lens => ({ id: own(lens, 'id'), question: own(lens, 'question'), provider: own(lens, 'provider') }));
47
+ if (entry !== undefined) {
48
+ return entry.map(lens => ({
49
+ id: own(lens, 'id'),
50
+ question: own(lens, 'question'),
51
+ provider: own(lens, 'provider'),
52
+ }));
53
+ }
32
54
  return base?.lenses.map(({ id, question }) => ({ id, question, provider: undefined }));
33
55
  }
34
56
  /** What one profile of the config resolves to, or every issue that keeps it from running. */
@@ -40,8 +62,17 @@ function resolveEntry(id, entry, { roles, providers, known }) {
40
62
  const vote = own(entryPrompts, 'vote') ?? base?.prompts.vote;
41
63
  const lenses = lensesOf(own(entry, 'lenses'), base);
42
64
  if (research === undefined || vote === undefined || lenses === undefined) {
43
- const needed = [[research, 'prompts', 'research'], [vote, 'prompts', 'vote'], [lenses, 'lenses']];
44
- return { issues: needed.filter(([value]) => value === undefined).map(([, ...segments]) => ({ path: at(...segments), message: NEEDS_BASE })) };
65
+ const needed = [
66
+ [research, 'prompts', 'research'],
67
+ [vote, 'prompts', 'vote'],
68
+ [lenses, 'lenses'],
69
+ ];
70
+ return {
71
+ issues: needed.filter(([value]) => value === undefined).map(([, ...segments]) => ({
72
+ path: at(...segments),
73
+ message: NEEDS_BASE,
74
+ })),
75
+ };
45
76
  }
46
77
  const profile = Object.freeze({
47
78
  id,
@@ -54,9 +85,18 @@ function resolveEntry(id, entry, { roles, providers, known }) {
54
85
  const issues = profileIssues(profile, id).map(issue => ({ path: `${at()}${issue.path.slice(id.length)}`, message: issue.message }));
55
86
  const entryRoles = own(entry, 'roles');
56
87
  const references = [
57
- { path: at('roles', 'research'), name: own(entryRoles, 'research') },
58
- { path: at('roles', 'voter'), name: own(entryRoles, 'voter') },
59
- ...lenses.map((lens, index) => ({ path: at('lenses', index, 'provider'), name: lens.provider })),
88
+ {
89
+ path: at('roles', 'research'),
90
+ name: own(entryRoles, 'research'),
91
+ },
92
+ {
93
+ path: at('roles', 'voter'),
94
+ name: own(entryRoles, 'voter'),
95
+ },
96
+ ...lenses.map((lens, index) => ({
97
+ path: at('lenses', index, 'provider'),
98
+ name: lens.provider,
99
+ })),
60
100
  ];
61
101
  for (const { path, name } of references) {
62
102
  if (name !== undefined && !known.has(name))
@@ -87,11 +127,33 @@ function resolveEntry(id, entry, { roles, providers, known }) {
87
127
  */
88
128
  export function resolveProfiles(inputs) {
89
129
  const configured = new Map(Object.entries(inputs.profiles ?? {}));
90
- const selected = inputs.selection?.ids ?? (inputs.profiles === undefined ? [securityProfile.id] : [...configured.keys()]);
91
- const bindings = { roles: inputs.roles, providers: inputs.providers, known: new Set(inputs.providers) };
92
- const defined = [...configured.keys(), ...[...BUILT_IN.keys()].filter(id => !configured.has(id))];
93
- const issues = selected.length > 0 ? [] : [inputs.selection === undefined ? { path: 'profiles', message: 'must hold at least one profile, or be left out' } : { path: inputs.selection.path, message: 'must name at least one profile' }];
94
- const resolutions = new Map([...configured].map(([id, entry]) => [id, resolveEntry(id, entry, bindings)]));
130
+ const selected = inputs.selection?.ids ?? (inputs.profiles === undefined
131
+ ? [securityProfile.id]
132
+ : [
133
+ ...configured.keys(),
134
+ ]);
135
+ const bindings = {
136
+ roles: inputs.roles,
137
+ providers: inputs.providers,
138
+ known: new Set(inputs.providers),
139
+ };
140
+ const defined = [
141
+ ...configured.keys(),
142
+ ...[
143
+ ...BUILT_IN.keys(),
144
+ ].filter(id => !configured.has(id)),
145
+ ];
146
+ const issues = selected.length > 0
147
+ ? []
148
+ : [
149
+ inputs.selection === undefined
150
+ ? { path: 'profiles', message: 'must hold at least one profile, or be left out' }
151
+ : { path: inputs.selection.path, message: 'must name at least one profile' },
152
+ ];
153
+ const resolutions = new Map([...configured].map(([id, entry]) => [
154
+ id,
155
+ resolveEntry(id, entry, bindings),
156
+ ]));
95
157
  for (const id of selected) {
96
158
  if (resolutions.has(id))
97
159
  continue;
@@ -100,6 +162,11 @@ export function resolveProfiles(inputs) {
100
162
  else
101
163
  issues.push(unknownName(inputs.selection?.path ?? 'profiles', 'profile', id, defined));
102
164
  }
103
- issues.push(...[...resolutions.values()].flatMap(resolution => resolution.issues));
104
- return { profiles: selected.flatMap(id => resolutions.get(id)?.resolved ?? []), issues };
165
+ issues.push(...[
166
+ ...resolutions.values(),
167
+ ].flatMap(resolution => resolution.issues));
168
+ return {
169
+ profiles: selected.flatMap(id => resolutions.get(id)?.resolved ?? []),
170
+ issues,
171
+ };
105
172
  }