@tobi-or-not/quorum 0.4.0 → 0.5.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 (71) hide show
  1. package/README.md +1 -1
  2. package/dist/bench/fingerprint.d.ts +7 -5
  3. package/dist/bench/fingerprint.js +22 -9
  4. package/dist/bench/records.d.ts +70 -0
  5. package/dist/bench/records.js +30 -0
  6. package/dist/bench/runner.d.ts +18 -0
  7. package/dist/bench/runner.js +29 -3
  8. package/dist/cli/bench.d.ts +11 -4
  9. package/dist/cli/bench.js +34 -17
  10. package/dist/cli/bin.js +2 -2
  11. package/dist/cli/main.d.ts +34 -14
  12. package/dist/cli/main.js +99 -20
  13. package/dist/cli/print-config.d.ts +8 -6
  14. package/dist/cli/print-config.js +21 -8
  15. package/dist/cli/render.d.ts +2 -2
  16. package/dist/cli/render.js +21 -2
  17. package/dist/config/env.d.ts +32 -19
  18. package/dist/config/env.js +95 -32
  19. package/dist/config/from-config.js +15 -3
  20. package/dist/config/resolve.d.ts +9 -1
  21. package/dist/config/resolve.js +17 -1
  22. package/dist/config/schema.d.ts +141 -5
  23. package/dist/config/schema.js +125 -12
  24. package/dist/core/clock.d.ts +17 -0
  25. package/dist/core/clock.js +14 -0
  26. package/dist/core/coverage.d.ts +49 -0
  27. package/dist/core/coverage.js +144 -0
  28. package/dist/core/diff.d.ts +24 -0
  29. package/dist/core/diff.js +63 -18
  30. package/dist/core/errors.d.ts +12 -3
  31. package/dist/core/errors.js +21 -2
  32. package/dist/core/exclude.d.ts +42 -0
  33. package/dist/core/exclude.js +189 -0
  34. package/dist/core/limits.d.ts +82 -0
  35. package/dist/core/limits.js +163 -0
  36. package/dist/core/output.d.ts +3 -2
  37. package/dist/core/output.js +3 -2
  38. package/dist/core/ports.d.ts +57 -2
  39. package/dist/core/ports.js +36 -1
  40. package/dist/core/quorum.d.ts +24 -8
  41. package/dist/core/quorum.js +187 -56
  42. package/dist/core/retry.d.ts +36 -0
  43. package/dist/core/retry.js +46 -0
  44. package/dist/core/stages/context.d.ts +118 -23
  45. package/dist/core/stages/context.js +304 -46
  46. package/dist/core/stages/panel.d.ts +5 -4
  47. package/dist/core/stages/panel.js +30 -11
  48. package/dist/core/stages/research.d.ts +15 -6
  49. package/dist/core/stages/research.js +75 -18
  50. package/dist/core/tokens.d.ts +31 -0
  51. package/dist/core/tokens.js +81 -0
  52. package/dist/core/types.d.ts +77 -10
  53. package/dist/index.d.ts +1 -1
  54. package/dist/providers/anthropic.d.ts +2 -1
  55. package/dist/providers/anthropic.js +8 -3
  56. package/dist/providers/claude-code.d.ts +51 -11
  57. package/dist/providers/claude-code.js +60 -25
  58. package/dist/providers/gemini.d.ts +4 -1
  59. package/dist/providers/gemini.js +63 -11
  60. package/dist/providers/http.d.ts +49 -4
  61. package/dist/providers/http.js +155 -14
  62. package/dist/providers/ollama.d.ts +3 -1
  63. package/dist/providers/ollama.js +11 -5
  64. package/dist/providers/openai-compatible.d.ts +3 -1
  65. package/dist/providers/openai-compatible.js +11 -5
  66. package/dist/providers/options.d.ts +12 -3
  67. package/dist/providers/options.js +30 -10
  68. package/dist/providers/size.d.ts +10 -14
  69. package/dist/providers/size.js +15 -69
  70. package/package.json +1 -1
  71. package/schema/quorum.schema.json +34 -1
package/dist/cli/main.js CHANGED
@@ -7,6 +7,7 @@ import { loadConfig } from "../config/load.js";
7
7
  import { decodeDiff } from "../core/diff.js";
8
8
  import { ConfigError } from "../core/errors.js";
9
9
  import { inertJson, inertLine } from "../core/sanitize.js";
10
+ import { ownData } from "../core/stages/context.js";
10
11
  import { runBench } from "./bench.js";
11
12
  import { errorLines, errorMessage, EXIT, parseFlags } from "./io.js";
12
13
  import { printConfig } from "./print-config.js";
@@ -20,12 +21,14 @@ const USAGE = [
20
21
  'Options:',
21
22
  ' -c, --config <path> Read this config file, over QUORUM_CONFIG',
22
23
  ' -p, --profile <id> Run only this profile; repeat it to run several',
23
- ' --allow-incomplete Exit 0 instead of 4 when the review is incomplete, except for a request too large for its model',
24
+ ' --allow-incomplete Exit 0 instead of 4 when every gap is a vote that failed in passing: a dropped connection, a timeout, or an HTTP 408, 409, 429 or 5xx',
24
25
  ' --print-config Print the resolved config, keys redacted, and exit',
25
26
  ' -h, --help Print this help and exit',
26
27
  ' -v, --version Print the version and exit',
27
28
  '',
28
29
  ].join('\n');
30
+ /** The HTTP statuses, besides 5xx, that a call fails with in passing: a request timeout, a conflict and a rate limit. */
31
+ const PASSING_STATUSES = new Set([408, 409, 429]);
29
32
  const OPTIONS = {
30
33
  config: { type: 'string', short: 'c' },
31
34
  profile: { type: 'string', short: 'p', multiple: true },
@@ -36,10 +39,12 @@ const OPTIONS = {
36
39
  };
37
40
  /**
38
41
  * Run one review and return the process exit code. Never throws: usage and configuration problems
39
- * exit 2, runtime failures exit 3, an interrupt exits 130, and an incomplete review (a finding its
40
- * failed votes left undecided, or a research gap) of a gating profile exits 4 unless a `high` finding
41
- * was kept by a gating profile, so CI can tell a broken run and a partial review from a real finding
42
- * (exit 1) and from a clean one (exit 0). `exitCodeFor` maps the result; advisory flags never change it.
42
+ * exit 2, a gating profile's failed research and other runtime failures exit 3, an interrupt exits
43
+ * 130, and an incomplete review (a finding its failed votes left undecided, or a gap) of a gating
44
+ * profile exits 4 unless a `high` finding was kept by a gating profile, so CI can tell a broken run
45
+ * and a partial review from a real finding (exit 1) and from a clean one (exit 0). `--allow-incomplete`
46
+ * turns that exit 4 into 0 only when every failed vote behind it failed in passing (see `exitCodeFor`).
47
+ * `exitCodeFor` maps the result; advisory flags never change it.
43
48
  *
44
49
  * It reads at most 16 MiB of diff, from stdin or a file, counted in bytes: a larger diff is a usage error,
45
50
  * and reading stops at the chunk that crosses the cap.
@@ -147,7 +152,7 @@ function writeWarnings(io, warnings) {
147
152
  }
148
153
  /**
149
154
  * The signal of one review, aborted by the first SIGINT or SIGTERM. The next one calls `exit(130)`: the
150
- * way out of a provider that ignores its signal, since no timer waits for it.
155
+ * way out when what a provider still runs, after the review settled its call, keeps the process alive.
151
156
  */
152
157
  export function interruptSignal(source, exit) {
153
158
  const controller = new AbortController();
@@ -165,27 +170,101 @@ export function interruptSignal(source, exit) {
165
170
  }
166
171
  /**
167
172
  * The exit code of a finished review: 1 when a kept finding is `high` and its profile gates, 4 when
168
- * an unreviewed finding or a gap belongs to a gating profile (unless `allowIncomplete` turns it into
169
- * 0, which it does for every gap but a `request-too-large` one), 0 otherwise. A profile gates
170
- * unless `meta.profiles` holds it with `gating: false`, so a profile missing from the result counts
171
- * as gating and a gap never hides.
173
+ * an unreviewed finding or a gap belongs to a gating profile, 0 otherwise. `allowIncomplete` turns
174
+ * that 4 into 0 only when every gap of a gating profile is a `vote-failed` one (an unreviewed finding
175
+ * is no gap) and, for each gating profile that has a gap, every failed vote of its own kept and
176
+ * unreviewed findings failed in passing, as `failedInPassing` says: a dropped connection, a timeout,
177
+ * or an HTTP 408, 409, 429 or 5xx. A gating profile with no gap blocks no other profile's lift. A gap
178
+ * with no failed vote behind it lifts nothing: an unreviewed finding whose votes hold none, or a
179
+ * `vote-failed` gap beside kept findings that hold none, which no review writes.
180
+ * A profile gates unless `meta.profiles` holds it with `gating: false`, so a profile missing from the
181
+ * result counts as gating and a gap never hides.
172
182
  *
173
- * A `request-too-large` gap of a gating profile exits 4 whatever `allowIncomplete` says: a pull
174
- * request can cause that gap by padding the diff until a request is refused, so no flag lifts it
175
- * (ADR-0012). The flag lifts every other gap, including gaps a pull request can cause: decoy findings
176
- * that push a real one past `maxFindings`, or text that trips a hosted voter's content filter. On a
177
- * gating profile, do not use it for a pull request you do not trust.
183
+ * Why so narrow: a pull request causes the other gaps directly (ADR-0012). It pads the diff until a
184
+ * request is refused as too large, floods the research with decoy findings past `maxFindings`, or
185
+ * plants text that a voter refuses or that cuts its reply off or leaves it unusable, and one
186
+ * `.gitattributes` line, one NUL byte or a submodule's bump leaves a file's change out of the diff.
187
+ * The flag lifts only what the code names, so a reason added later is out of its reach by default,
188
+ * and so is what a result built by hand or by an older quorum leaves unsaid: a failed vote with no
189
+ * `failure`, a verdict whose `votes` is no array, a reason no quorum writes. A gating profile's
190
+ * `research-failed` gap, which no review records, is out too: a review that read nothing is never
191
+ * turned into exit 0. Planted text can still draw out a reply that ends in a lifted failure, a
192
+ * timeout of its own, so on a gating profile do not use the flag for a pull request you do not trust.
178
193
  */
179
194
  export function exitCodeFor(result, options = {}) {
180
195
  const gates = (profile) => !Object.hasOwn(result.meta.profiles, profile) || result.meta.profiles[profile].gating !== false;
181
196
  if (result.kept.some(verdict => verdict.confidence === 'high' && gates(verdict.profile)))
182
197
  return EXIT.finding;
183
- if (result.meta.incomplete.some(entry => entry.reason === 'request-too-large' && gates(entry.profile)))
184
- return EXIT.incomplete;
185
198
  const incomplete = result.unreviewed.some(verdict => gates(verdict.profile)) || result.meta.incomplete.some(entry => gates(entry.profile));
186
- return incomplete && options.allowIncomplete !== true
187
- ? EXIT.incomplete
188
- : EXIT.ok;
199
+ if (!incomplete)
200
+ return EXIT.ok;
201
+ return options.allowIncomplete === true && liftable(result, gates)
202
+ ? EXIT.ok
203
+ : EXIT.incomplete;
204
+ }
205
+ /** True when every gap of a gating profile is a `vote-failed` one, and each gating profile that has a gap passes `liftsProfile`. */
206
+ function liftable(result, gates) {
207
+ const gaps = result.meta.incomplete.filter(entry => gates(entry.profile));
208
+ if (gaps.some(entry => entry.reason !== 'vote-failed'))
209
+ return false;
210
+ const unreviewed = result.unreviewed.filter(verdict => gates(verdict.profile));
211
+ const profiles = new Set([
212
+ ...gaps,
213
+ ...unreviewed,
214
+ ].map(({ profile }) => profile));
215
+ return [...profiles].every(profile => liftsProfile(result, profile, gaps.some(entry => entry.profile === profile)));
216
+ }
217
+ /**
218
+ * True when the failed votes behind `profile`'s gaps all failed in passing: those of its own kept and
219
+ * unreviewed findings, each finding's `votes` an array. Every unreviewed finding holds a failed vote, and
220
+ * so do the kept findings when the profile has a `vote-failed` gap (`gap`): a gap with no failed vote behind
221
+ * it lifts nothing.
222
+ */
223
+ function liftsProfile(result, profile, gap) {
224
+ const kept = failedVotesOf(result.kept.filter(verdict => verdict.profile === profile));
225
+ const unreviewed = failedVotesOf(result.unreviewed.filter(verdict => verdict.profile === profile));
226
+ if (kept === undefined || unreviewed === undefined)
227
+ return false;
228
+ return [
229
+ ...kept,
230
+ ...unreviewed,
231
+ ].every(failed => failed.every(lensVote => failedInPassing(ownData(lensVote, 'failure'))))
232
+ && unreviewed.every(failed => failed.length > 0)
233
+ && (!gap || kept.some(failed => failed.length > 0));
234
+ }
235
+ /**
236
+ * The failed votes of each verdict, in order, or undefined when the `votes` of one is no array. A vote is
237
+ * cast only when its own data `vote` is an object and its own data `failure` is `null` or absent: any other
238
+ * entry is failed, so a vote that records a failure never counts as cast.
239
+ */
240
+ function failedVotesOf(verdicts) {
241
+ const lists = verdicts.map((verdict) => {
242
+ const votes = verdict.votes;
243
+ return Array.isArray(votes)
244
+ ? votes.filter((lensVote) => {
245
+ const cast = ownData(lensVote, 'vote');
246
+ const failure = ownData(lensVote, 'failure');
247
+ return typeof cast !== 'object' || cast === null || (failure !== null && failure !== undefined);
248
+ })
249
+ : undefined;
250
+ });
251
+ return lists.every(list => list !== undefined)
252
+ ? lists
253
+ : undefined;
254
+ }
255
+ /**
256
+ * True for a vote's failure that is infrastructure's: its own data `kind` is `network` or `timeout`, or
257
+ * `http` with an own data `status` of 408, 409, 429 or an integer from 500 to 599. Anything else is false:
258
+ * a refusal, a reply cut off or unusable, a request too large, any other status, a status that is no
259
+ * number, a kind no provider has, `null` and a value that is no object. It reads own data only, never
260
+ * runs a getter and never throws.
261
+ */
262
+ export function failedInPassing(failure) {
263
+ const kind = ownData(failure, 'kind');
264
+ if (kind === 'network' || kind === 'timeout')
265
+ return true;
266
+ const status = ownData(failure, 'status');
267
+ return kind === 'http' && typeof status === 'number' && Number.isInteger(status) && (PASSING_STATUSES.has(status) || (status >= 500 && status <= 599));
189
268
  }
190
269
  /** Write a usage error and the usage, and return its exit code. */
191
270
  function usageError(io, error) {
@@ -1,12 +1,14 @@
1
1
  import type { ResolvedConfig } from '../config/resolve.ts';
2
2
  /**
3
3
  * The resolved config as the YAML `--print-config` writes, for a CI log that anyone can read: the file it
4
- * came from (`null` for none), the providers, the profiles that run, the limits the review applies, and
5
- * the warnings. A key and every value of `headers`, `query` and `extraBody` print as `[redacted]`, a base
6
- * URL as its origin, and a prompt as its SHA-256. A `claude-code` provider prints its protocol, command,
7
- * model, context size, key (redacted), auth mode and args, and no origin: the command and the args are the
8
- * user's own config. The view is an allowlist, so a field added to the config prints nothing until it is
9
- * added here on purpose.
4
+ * came from (`null` for none), the providers, the profiles that run, the limits the review applies
5
+ * (`concurrency`, `maxFindings`, `maxReviewTokens` and `reviewTimeoutMs`, defaults filled in), the
6
+ * patterns it excludes, and the warnings. A key and every value of `headers`, `query` and `extraBody`
7
+ * print as `[redacted]`, a base URL as its origin, and a prompt as its SHA-256. A provider's `timeoutMs` and
8
+ * `retries` print when it sets them, a provider object's when it states them. A `claude-code` provider
9
+ * prints its protocol, command, model, context size, key (redacted), auth mode and args, and no origin:
10
+ * the command and the args are the user's own config. The view is an allowlist, so a field added to the
11
+ * config prints nothing until it is added here on purpose.
10
12
  *
11
13
  * The text ends with a newline, and each line passes through `inertLine`. It is YAML 1.1, so a reader of
12
14
  * that version reads `off` as a string, and no line folds.
@@ -1,8 +1,9 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import { stringify } from 'yaml';
3
3
  import { own } from "../config/profiles.js";
4
- import { isProvider } from "../config/schema.js";
4
+ import { isProvider, statedFacts } from "../config/schema.js";
5
5
  import { DEFAULT_CONCURRENCY } from "../core/concurrency.js";
6
+ import { DEFAULT_MAX_REVIEW_TOKENS, DEFAULT_REVIEW_TIMEOUT_MS } from "../core/limits.js";
6
7
  import { thresholdsFor } from "../core/profile.js";
7
8
  import { inertLine } from "../core/sanitize.js";
8
9
  import { DEFAULT_MAX_FINDINGS } from "../core/stages/research.js";
@@ -66,16 +67,21 @@ function specView(spec) {
66
67
  maxTokens: own(spec, 'maxTokens'),
67
68
  maxTokensField: own(spec, 'maxTokensField'),
68
69
  systemPrompt: systemPromptView(own(spec, 'systemPrompt')),
70
+ timeoutMs: own(spec, 'timeoutMs'),
71
+ retries: own(spec, 'retries'),
69
72
  };
70
73
  }
71
- /** A provider object by what it implements and its model, when that is a string; never its function. */
74
+ /** A provider object by what it implements, its model when that is a string, and the `timeoutMs` and `retries` it states; never its function. */
72
75
  function objectView(provider) {
73
76
  const model = own(provider, 'model');
77
+ const { timeoutMs, retries } = statedFacts(provider).stated;
74
78
  return {
75
79
  implements: 'Provider',
76
80
  model: typeof model === 'string'
77
81
  ? model
78
82
  : undefined,
83
+ timeoutMs,
84
+ retries,
79
85
  };
80
86
  }
81
87
  /** A profile that runs: its providers, the votes its policy needs, and each prompt as its hash. */
@@ -93,12 +99,14 @@ function profileView({ profile, research, voters }) {
93
99
  }
94
100
  /**
95
101
  * The resolved config as the YAML `--print-config` writes, for a CI log that anyone can read: the file it
96
- * came from (`null` for none), the providers, the profiles that run, the limits the review applies, and
97
- * the warnings. A key and every value of `headers`, `query` and `extraBody` print as `[redacted]`, a base
98
- * URL as its origin, and a prompt as its SHA-256. A `claude-code` provider prints its protocol, command,
99
- * model, context size, key (redacted), auth mode and args, and no origin: the command and the args are the
100
- * user's own config. The view is an allowlist, so a field added to the config prints nothing until it is
101
- * added here on purpose.
102
+ * came from (`null` for none), the providers, the profiles that run, the limits the review applies
103
+ * (`concurrency`, `maxFindings`, `maxReviewTokens` and `reviewTimeoutMs`, defaults filled in), the
104
+ * patterns it excludes, and the warnings. A key and every value of `headers`, `query` and `extraBody`
105
+ * print as `[redacted]`, a base URL as its origin, and a prompt as its SHA-256. A provider's `timeoutMs` and
106
+ * `retries` print when it sets them, a provider object's when it states them. A `claude-code` provider
107
+ * prints its protocol, command, model, context size, key (redacted), auth mode and args, and no origin:
108
+ * the command and the args are the user's own config. The view is an allowlist, so a field added to the
109
+ * config prints nothing until it is added here on purpose.
102
110
  *
103
111
  * The text ends with a newline, and each line passes through `inertLine`. It is YAML 1.1, so a reader of
104
112
  * that version reads `off` as a string, and no line folds.
@@ -118,6 +126,11 @@ export function printConfig(config, source) {
118
126
  ])),
119
127
  concurrency: config.concurrency ?? DEFAULT_CONCURRENCY,
120
128
  maxFindings: config.maxFindings ?? DEFAULT_MAX_FINDINGS,
129
+ maxReviewTokens: config.maxReviewTokens ?? DEFAULT_MAX_REVIEW_TOKENS,
130
+ reviewTimeoutMs: config.reviewTimeoutMs ?? DEFAULT_REVIEW_TIMEOUT_MS,
131
+ exclude: [
132
+ ...config.exclude ?? [],
133
+ ],
121
134
  warnings: [...config.warnings],
122
135
  };
123
136
  return stringify(view, { version: '1.1', lineWidth: 0, blockQuote: false }).split('\n').map(line => inertLine(line)).join('\n');
@@ -6,8 +6,8 @@ import type { QuorumEvent } from '../core/types.ts';
6
6
  * are the CLI's progress output on stderr, so a person follows a run without the full event stream.
7
7
  * Each line passes through `inertLine` whole: the reasons, details and file names it carries come
8
8
  * from the finder or the diff, and nothing in them can act on a terminal or a CI log. When `prefixed`,
9
- * a line of an event that names a profile starts with `[<profile>] `: the `flag:` and totals lines
10
- * belong to the whole review and never do.
9
+ * a line of an event that names a profile starts with `[<profile>] `: the `left out:`, `flag:` and totals
10
+ * lines belong to the whole review and never do.
11
11
  */
12
12
  export declare function renderEvent(event: QuorumEvent, prefixed?: boolean): string | undefined;
13
13
  /**
@@ -9,8 +9,8 @@ import { presetOf } from "../providers/presets.js";
9
9
  * are the CLI's progress output on stderr, so a person follows a run without the full event stream.
10
10
  * Each line passes through `inertLine` whole: the reasons, details and file names it carries come
11
11
  * from the finder or the diff, and nothing in them can act on a terminal or a CI log. When `prefixed`,
12
- * a line of an event that names a profile starts with `[<profile>] `: the `flag:` and totals lines
13
- * belong to the whole review and never do.
12
+ * a line of an event that names a profile starts with `[<profile>] `: the `left out:`, `flag:` and totals
13
+ * lines belong to the whole review and never do.
14
14
  */
15
15
  export function renderEvent(event, prefixed = false) {
16
16
  const line = progressLine(event, prefixed);
@@ -19,6 +19,8 @@ export function renderEvent(event, prefixed = false) {
19
19
  : inertLine(line);
20
20
  }
21
21
  const profilePrefix = (profile) => `[${profile}] `;
22
+ /** How many of the files left out the progress line names; the JSON result lists every one. */
23
+ const LISTED_FILES = 5;
22
24
  function progressLine(event, prefixed) {
23
25
  const of = (profile, line) => prefixed
24
26
  ? `${profilePrefix(profile)}${line}`
@@ -32,6 +34,8 @@ function progressLine(event, prefixed) {
32
34
  return of(event.profile, `invalid finding dropped: ${event.reason}`);
33
35
  case 'incomplete':
34
36
  return of(event.entry.profile, `incomplete: ${event.entry.reason} (${event.entry.detail})`);
37
+ case 'left-out':
38
+ return leftOutLine(event.files);
35
39
  case 'flag':
36
40
  return `flag: ${event.flag.code}${where(event.flag)} (${event.flag.detail})`;
37
41
  case 'run:end':
@@ -40,6 +44,21 @@ function progressLine(event, prefixed) {
40
44
  return undefined;
41
45
  }
42
46
  }
47
+ /**
48
+ * The line that names the files a review left out: their count, the first `LISTED_FILES` of them as
49
+ * `<file> (<reason>)`, then how many more. A file is a JSON string, as a flag's is; the empty name, which
50
+ * stands for a part of the diff no header names, is `<no path>`.
51
+ */
52
+ function leftOutLine(files) {
53
+ const named = (file) => file === ''
54
+ ? '<no path>'
55
+ : JSON.stringify(file);
56
+ const entries = files.slice(0, LISTED_FILES).map(({ file, reason }) => `${named(file)} (${reason})`);
57
+ const more = files.length > LISTED_FILES
58
+ ? ` and ${files.length - LISTED_FILES} more`
59
+ : '';
60
+ return `left out: ${files.length} file(s): ${entries.join(', ')}${more}`;
61
+ }
43
62
  /** Where a flag points: the file as a JSON string, with the line when it has one, else the line alone. */
44
63
  function where({ file, line }) {
45
64
  if (file !== undefined && line !== undefined)
@@ -8,13 +8,18 @@ export type Env = Readonly<Record<string, string | undefined>>;
8
8
  * not break a key. A blank variable is unset, and so is one the environment only inherits from its prototype.
9
9
  */
10
10
  export declare function envValue(env: Env, name: string): string | undefined;
11
- /** What the variables of one role set: only the variables that are set. `contextTokens` is `QUORUM_<ROLE>_CONTEXT_TOKENS`, read as a whole number. */
11
+ /**
12
+ * What the variables of one role set: only the variables that are set. `contextTokens`, `timeoutMs` and
13
+ * `retries` are `QUORUM_<ROLE>_CONTEXT_TOKENS`, `_TIMEOUT_MS` and `_RETRIES`, each read as a whole number.
14
+ */
12
15
  export interface RoleOverrides {
13
16
  readonly preset?: string;
14
17
  readonly protocol?: string;
15
18
  readonly baseUrl?: string;
16
19
  readonly model?: string;
17
20
  readonly contextTokens?: number;
21
+ readonly timeoutMs?: number;
22
+ readonly retries?: number;
18
23
  readonly apiKey?: string;
19
24
  }
20
25
  /** What the environment holds for the config layer. */
@@ -25,6 +30,10 @@ export interface EnvLayer {
25
30
  readonly voter: RoleOverrides;
26
31
  /** `QUORUM_CLAUDE_CODE_COMMAND`, trimmed: present only when it is set and `commandProblem` finds nothing wrong with it. */
27
32
  readonly claudeCodeCommand?: string;
33
+ /** `QUORUM_MAX_REVIEW_TOKENS`: present only when it is set and in range. */
34
+ readonly maxReviewTokens?: number;
35
+ /** `QUORUM_REVIEW_TIMEOUT_MS`: present only when it is set and in range. */
36
+ readonly reviewTimeoutMs?: number;
28
37
  /** The deprecation line, when an alias is set, then the line naming the `QUORUM_` variables nothing reads. */
29
38
  readonly warnings: readonly string[];
30
39
  readonly issues: readonly ConfigIssue[];
@@ -37,17 +46,20 @@ export declare const VARIABLE_NAME: RegExp;
37
46
  export declare const CLAUDE_CODE_COMMAND = "QUORUM_CLAUDE_CODE_COMMAND";
38
47
  /**
39
48
  * Every variable the environment layer reads: `QUORUM_CONFIG`, `QUORUM_PROFILES`, `QUORUM_CLAUDE_CODE_COMMAND`, the
40
- * `QUORUM_` form of each role's variables and the deprecated aliases of those that have one. The one list: a
41
- * variable added to `VARIABLES` joins it.
49
+ * review's two limits, the `QUORUM_` form of each role's variables and the deprecated aliases of those that have one.
50
+ * The one list: a variable added to `VARIABLES` or `LIMIT_VARIABLES` joins it.
42
51
  */
43
52
  export declare const READ_VARIABLES: ReadonlySet<string>;
44
53
  /**
45
- * Read the `QUORUM_*` variables: the preset, protocol, base URL, model, context size and key of each role,
46
- * which are `QUORUM_RESEARCH_*` and `QUORUM_VOTER_*`, `QUORUM_PROFILES`, and `QUORUM_CLAUDE_CODE_COMMAND`, the
47
- * executable of the `claude-code` protocol: an absolute path, or a name to find on `PATH`. `RESEARCH_*` and
48
- * `VOTER_*` are read as deprecated aliases of the base URL, model and key, and the `QUORUM_` form wins; the
49
- * context size (`QUORUM_RESEARCH_CONTEXT_TOKENS`, `QUORUM_VOTER_CONTEXT_TOKENS`) has no alias. Any other
50
- * variable set under a `QUORUM_` name is named in one warning. No message repeats a value.
54
+ * Read the `QUORUM_*` variables: the preset, protocol, base URL, model, context size, deadline, retries and key
55
+ * of each role, which are `QUORUM_RESEARCH_*` and `QUORUM_VOTER_*`, `QUORUM_PROFILES`, and
56
+ * `QUORUM_CLAUDE_CODE_COMMAND`, the executable of the `claude-code` protocol: an absolute path, or a name to
57
+ * find on `PATH`; and the review's two limits, the run-level `QUORUM_MAX_REVIEW_TOKENS` and
58
+ * `QUORUM_REVIEW_TIMEOUT_MS`, which belong to no role and are read as strictly as a role's numbers.
59
+ * `RESEARCH_*` and `VOTER_*` are read as deprecated aliases of the base URL, model and key, and
60
+ * the `QUORUM_` form wins; the context size (`QUORUM_RESEARCH_CONTEXT_TOKENS`, `QUORUM_VOTER_CONTEXT_TOKENS`),
61
+ * the deadline (`_TIMEOUT_MS`) and the retries (`_RETRIES`) have no alias. Any other variable set under a
62
+ * `QUORUM_` name is named in one warning. No message repeats a value.
51
63
  */
52
64
  export declare function readEnv(env: Env): EnvLayer;
53
65
  /** A provider of a config: its settings, or an object implementing `Provider`. */
@@ -69,16 +81,17 @@ export declare function setsOnlyBaseUrl(overrides: RoleOverrides): boolean;
69
81
  * Set the variables of each role on its provider. The research variables go on the provider research is
70
82
  * bound to. The voter variables go on the voter's own provider, or, when the voter shares the research
71
83
  * provider, derive a provider named `voter` from it: a copy on the same endpoint, and on another one a copy
72
- * with no key, header, query, body field, model or context size (ADR-0013). A preset or a protocol variable
73
- * builds the role's provider from its variables alone. A context size describes one model on one endpoint,
74
- * so it crosses to neither: a role's variables that name a preset, a protocol, a base URL or a model drop the
75
- * context size of the provider they are set on, the derived voter's included, unless they set one; a key
76
- * variable alone leaves it. A provider object cannot be changed: each variable aimed at one
77
- * is an issue. Nothing it is given changes; nested objects are shared. A role bound to a name that is not an
78
- * own key of `providers` is an issue at that role, and nothing is applied. `builtIn` is true when `providers`
79
- * is the built-in default alone: while the research variables name no preset or protocol, a voter derived from
80
- * it whose variables set a base URL alone speaks OpenAI-compatible to it, as that variable always meant, and
81
- * does not inherit the default's own protocol. A provider of a config keeps what it names.
84
+ * with no key, header, query, body field, model or context size (ADR-0013); its deadline and retries stay, as
85
+ * its `maxTokens` does. A preset or a protocol variable builds the role's provider from its variables alone.
86
+ * A context size describes one model on one endpoint, so it crosses to neither: a role's variables that name a
87
+ * preset, a protocol, a base URL or a model drop the context size of the provider they are set on, the
88
+ * derived voter's included, unless they set one; a variable that sets only a key, a deadline or a retry
89
+ * count leaves it. A provider object cannot be changed: each variable aimed at one is an issue. Nothing it
90
+ * is given changes; nested objects are shared. A role bound to a name that is not an own key of `providers`
91
+ * is an issue at that role, and nothing is applied. `builtIn` is true when `providers` is the built-in
92
+ * default alone: while the research variables name no preset or protocol, a voter derived from it whose
93
+ * variables set a base URL alone speaks OpenAI-compatible to it, as that variable always meant, and does not
94
+ * inherit the default's own protocol. A provider of a config keeps what it names.
82
95
  */
83
96
  export declare function applyRoleOverrides(providers: Readonly<Record<string, ProviderEntry>>, roles: RoleBinding, layer: Pick<EnvLayer, 'research' | 'voter'>, builtIn?: boolean): {
84
97
  providers: Record<string, ProviderEntry>;
@@ -1,3 +1,4 @@
1
+ import { MAX_RETRIES, MAX_TIMEOUT_MS, MIN_TIMEOUT_MS } from "../core/ports.js";
1
2
  import { commandProblem } from "../providers/claude-code.js";
2
3
  import { httpOrigin } from "../providers/http.js";
3
4
  import { wordList } from "../providers/options.js";
@@ -20,6 +21,8 @@ const VARIABLES = [
20
21
  { field: 'baseUrl', suffix: 'BASE_URL', aliased: true },
21
22
  { field: 'model', suffix: 'MODEL', aliased: true },
22
23
  { field: 'contextTokens', suffix: 'CONTEXT_TOKENS', aliased: false },
24
+ { field: 'timeoutMs', suffix: 'TIMEOUT_MS', aliased: false },
25
+ { field: 'retries', suffix: 'RETRIES', aliased: false },
23
26
  { field: 'apiKey', suffix: 'API_KEY', aliased: true },
24
27
  ];
25
28
  const ROLES = ['RESEARCH', 'VOTER'];
@@ -37,24 +40,60 @@ export const VARIABLE_NAME = /^[A-Z][A-Z0-9_]{0,63}$/;
37
40
  const canonicalName = (role, suffix) => `QUORUM_${role}_${suffix}`;
38
41
  /** The variable that names the executable of every `claude-code` provider. */
39
42
  export const CLAUDE_CODE_COMMAND = 'QUORUM_CLAUDE_CODE_COMMAND';
43
+ /** The deadline variables' rule, a role's `_TIMEOUT_MS` and the review's alike: milliseconds in the range a provider's deadline may take. */
44
+ const TIMEOUT_RULE = {
45
+ form: /^[1-9]\d{3,7}$/,
46
+ max: MAX_TIMEOUT_MS,
47
+ message: `must be a whole number of milliseconds, from ${MIN_TIMEOUT_MS} to ${MAX_TIMEOUT_MS}`,
48
+ };
49
+ /** The variables of the review itself, which belong to no role: each by the field it sets and the rule it is read by. */
50
+ const LIMIT_VARIABLES = [
51
+ {
52
+ field: 'maxReviewTokens',
53
+ name: 'QUORUM_MAX_REVIEW_TOKENS',
54
+ rule: { form: /^[1-9]\d{0,15}$/, max: Number.MAX_SAFE_INTEGER, message: `must be a whole number of tokens, from 1 to ${Number.MAX_SAFE_INTEGER}` },
55
+ },
56
+ { field: 'reviewTimeoutMs', name: 'QUORUM_REVIEW_TIMEOUT_MS', rule: TIMEOUT_RULE },
57
+ ];
40
58
  /**
41
59
  * Every variable the environment layer reads: `QUORUM_CONFIG`, `QUORUM_PROFILES`, `QUORUM_CLAUDE_CODE_COMMAND`, the
42
- * `QUORUM_` form of each role's variables and the deprecated aliases of those that have one. The one list: a
43
- * variable added to `VARIABLES` joins it.
60
+ * review's two limits, the `QUORUM_` form of each role's variables and the deprecated aliases of those that have one.
61
+ * The one list: a variable added to `VARIABLES` or `LIMIT_VARIABLES` joins it.
44
62
  */
45
63
  export const READ_VARIABLES = new Set([
46
64
  'QUORUM_CONFIG',
47
65
  'QUORUM_PROFILES',
48
66
  CLAUDE_CODE_COMMAND,
67
+ ...LIMIT_VARIABLES.map(({ name }) => name),
49
68
  ...ROLES.flatMap(role => VARIABLES.map(({ suffix }) => canonicalName(role, suffix))),
50
69
  ...ROLES.flatMap(role => VARIABLES.filter(({ aliased }) => aliased).map(({ suffix }) => `${role}_${suffix}`)),
51
70
  ]);
52
- /** A context size variable: a whole number of tokens, written in decimal digits, from 1 to 999999999. */
53
- const CONTEXT_TOKENS = /^[1-9]\d{0,8}$/;
54
- const CONTEXT_TOKENS_FORM = 'must be a whole number of tokens, from 1 to 999999999';
71
+ /** The variables of a role that hold a whole number, each by the field it sets: decimal digits, no sign, no leading zero. A `Map`, so a field is never found on a prototype. */
72
+ const NUMBERS = new Map([
73
+ [
74
+ 'contextTokens',
75
+ { form: /^[1-9]\d{0,8}$/, max: 999_999_999, message: 'must be a whole number of tokens, from 1 to 999999999' },
76
+ ],
77
+ ['timeoutMs', TIMEOUT_RULE],
78
+ [
79
+ 'retries',
80
+ { form: /^[0-5]$/, max: MAX_RETRIES, message: `must be a whole number, from 0 to ${MAX_RETRIES}` },
81
+ ],
82
+ ]);
83
+ /** The number a variable holds, or the issue at the variable when it is not a whole number in the rule's range, which never repeats it. */
84
+ function readNumber(name, value, rule) {
85
+ return rule.form.test(value) && Number(value) <= rule.max
86
+ ? {
87
+ number: Number(value),
88
+ }
89
+ : {
90
+ issue: { path: name, message: rule.message },
91
+ };
92
+ }
55
93
  /**
56
94
  * The variables of one role that are set, each as its field and value: the `QUORUM_` form, else its alias. A
57
- * context size that is not a whole number in range sets nothing and is an issue at its variable, which never repeats it.
95
+ * number (the context size, the deadline, the retries) that is not a whole number in range sets nothing and is
96
+ * an issue at its variable, which never repeats it.
58
97
  */
59
98
  function readRole(env, role) {
60
99
  const entries = [];
@@ -66,18 +105,16 @@ function readRole(env, role) {
66
105
  : undefined);
67
106
  if (value === undefined)
68
107
  continue;
69
- if (field !== 'contextTokens') {
108
+ const rule = NUMBERS.get(field);
109
+ if (rule === undefined) {
70
110
  entries.push([field, value]);
111
+ continue;
71
112
  }
72
- else if (CONTEXT_TOKENS.test(value)) {
73
- entries.push([
74
- field,
75
- Number(value),
76
- ]);
77
- }
78
- else {
79
- issues.push({ path: name, message: CONTEXT_TOKENS_FORM });
80
- }
113
+ const read = readNumber(name, value, rule);
114
+ if ('issue' in read)
115
+ issues.push(read.issue);
116
+ else
117
+ entries.push([field, read.number]);
81
118
  }
82
119
  return {
83
120
  overrides: Object.fromEntries(entries),
@@ -114,6 +151,25 @@ function unreadWarnings(env) {
114
151
  : `${wordList(printed, 'and')} are not variables quorum reads`,
115
152
  ];
116
153
  }
154
+ /** The review's limits the variables set, each as its field and value, and an issue at each variable that is not a whole number in range, which never repeats it. */
155
+ function readLimits(env) {
156
+ const entries = [];
157
+ const issues = [];
158
+ for (const { field, name, rule } of LIMIT_VARIABLES) {
159
+ const value = envValue(env, name);
160
+ if (value === undefined)
161
+ continue;
162
+ const read = readNumber(name, value, rule);
163
+ if ('issue' in read)
164
+ issues.push(read.issue);
165
+ else
166
+ entries.push([field, read.number]);
167
+ }
168
+ return {
169
+ ...Object.fromEntries(entries),
170
+ issues,
171
+ };
172
+ }
117
173
  /** The profile ids of `QUORUM_PROFILES`, or the one issue that refuses it. Neither repeats the value. */
118
174
  function readProfiles(env) {
119
175
  const value = envValue(env, 'QUORUM_PROFILES');
@@ -151,21 +207,26 @@ function readClaudeCodeCommand(env) {
151
207
  };
152
208
  }
153
209
  /**
154
- * Read the `QUORUM_*` variables: the preset, protocol, base URL, model, context size and key of each role,
155
- * which are `QUORUM_RESEARCH_*` and `QUORUM_VOTER_*`, `QUORUM_PROFILES`, and `QUORUM_CLAUDE_CODE_COMMAND`, the
156
- * executable of the `claude-code` protocol: an absolute path, or a name to find on `PATH`. `RESEARCH_*` and
157
- * `VOTER_*` are read as deprecated aliases of the base URL, model and key, and the `QUORUM_` form wins; the
158
- * context size (`QUORUM_RESEARCH_CONTEXT_TOKENS`, `QUORUM_VOTER_CONTEXT_TOKENS`) has no alias. Any other
159
- * variable set under a `QUORUM_` name is named in one warning. No message repeats a value.
210
+ * Read the `QUORUM_*` variables: the preset, protocol, base URL, model, context size, deadline, retries and key
211
+ * of each role, which are `QUORUM_RESEARCH_*` and `QUORUM_VOTER_*`, `QUORUM_PROFILES`, and
212
+ * `QUORUM_CLAUDE_CODE_COMMAND`, the executable of the `claude-code` protocol: an absolute path, or a name to
213
+ * find on `PATH`; and the review's two limits, the run-level `QUORUM_MAX_REVIEW_TOKENS` and
214
+ * `QUORUM_REVIEW_TIMEOUT_MS`, which belong to no role and are read as strictly as a role's numbers.
215
+ * `RESEARCH_*` and `VOTER_*` are read as deprecated aliases of the base URL, model and key, and
216
+ * the `QUORUM_` form wins; the context size (`QUORUM_RESEARCH_CONTEXT_TOKENS`, `QUORUM_VOTER_CONTEXT_TOKENS`),
217
+ * the deadline (`_TIMEOUT_MS`) and the retries (`_RETRIES`) have no alias. Any other variable set under a
218
+ * `QUORUM_` name is named in one warning. No message repeats a value.
160
219
  */
161
220
  export function readEnv(env) {
162
221
  const { issues: profileIssues, ...profiles } = readProfiles(env);
163
222
  const { issues: commandIssues, ...command } = readClaudeCodeCommand(env);
223
+ const { issues: limitIssues, ...limits } = readLimits(env);
164
224
  const research = readRole(env, 'RESEARCH');
165
225
  const voter = readRole(env, 'VOTER');
166
226
  return {
167
227
  ...profiles,
168
228
  ...command,
229
+ ...limits,
169
230
  research: research.overrides,
170
231
  voter: voter.overrides,
171
232
  warnings: [
@@ -175,6 +236,7 @@ export function readEnv(env) {
175
236
  issues: [
176
237
  ...profileIssues,
177
238
  ...commandIssues,
239
+ ...limitIssues,
178
240
  ...research.issues,
179
241
  ...voter.issues,
180
242
  ],
@@ -290,16 +352,17 @@ function unboundRoles(providers, roles) {
290
352
  * Set the variables of each role on its provider. The research variables go on the provider research is
291
353
  * bound to. The voter variables go on the voter's own provider, or, when the voter shares the research
292
354
  * provider, derive a provider named `voter` from it: a copy on the same endpoint, and on another one a copy
293
- * with no key, header, query, body field, model or context size (ADR-0013). A preset or a protocol variable
294
- * builds the role's provider from its variables alone. A context size describes one model on one endpoint,
295
- * so it crosses to neither: a role's variables that name a preset, a protocol, a base URL or a model drop the
296
- * context size of the provider they are set on, the derived voter's included, unless they set one; a key
297
- * variable alone leaves it. A provider object cannot be changed: each variable aimed at one
298
- * is an issue. Nothing it is given changes; nested objects are shared. A role bound to a name that is not an
299
- * own key of `providers` is an issue at that role, and nothing is applied. `builtIn` is true when `providers`
300
- * is the built-in default alone: while the research variables name no preset or protocol, a voter derived from
301
- * it whose variables set a base URL alone speaks OpenAI-compatible to it, as that variable always meant, and
302
- * does not inherit the default's own protocol. A provider of a config keeps what it names.
355
+ * with no key, header, query, body field, model or context size (ADR-0013); its deadline and retries stay, as
356
+ * its `maxTokens` does. A preset or a protocol variable builds the role's provider from its variables alone.
357
+ * A context size describes one model on one endpoint, so it crosses to neither: a role's variables that name a
358
+ * preset, a protocol, a base URL or a model drop the context size of the provider they are set on, the
359
+ * derived voter's included, unless they set one; a variable that sets only a key, a deadline or a retry
360
+ * count leaves it. A provider object cannot be changed: each variable aimed at one is an issue. Nothing it
361
+ * is given changes; nested objects are shared. A role bound to a name that is not an own key of `providers`
362
+ * is an issue at that role, and nothing is applied. `builtIn` is true when `providers` is the built-in
363
+ * default alone: while the research variables name no preset or protocol, a voter derived from it whose
364
+ * variables set a base URL alone speaks OpenAI-compatible to it, as that variable always meant, and does not
365
+ * inherit the default's own protocol. A provider of a config keeps what it names.
303
366
  */
304
367
  export function applyRoleOverrides(providers, roles, layer, builtIn = false) {
305
368
  const unbound = unboundRoles(providers, roles);