1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//! Map-reduce review configuration and mode selector (Phase 1, #690 / #680).
//!
//! Why: the unified-diff review path silently drops files on large PRs because
//! `render_for_prompt` breaks at `MAX_DIFF_CHARS` (160 K chars, ~40 K tokens).
//! A per-file map-reduce path reviews every file independently; this module
//! defines the configuration knobs that control it and the pure selector
//! function that decides which path to take. Phase 1 introduces the config +
//! selector only — nothing in the live review runner consumes the selector yet
//! (wired in Phase 5, #680).
//!
//! What: `MapMode` enum, `MapReduceConfig` struct (9 env-var knobs), and
//! `select_review_mode` pure function. No I/O; fully unit-testable.
//!
//! Test: `mapreduce_config_defaults`, `mapreduce_env_overrides`,
//! `mapreduce_malformed_env_fallback`, `select_review_mode_*` decision table.
use warn;
use crateMAX_DIFF_CHARS;
// ─── Env-var name constants ───────────────────────────────────────────────────
const ENV_MAP_MODE: &str = "TRUSTY_REVIEW_MAP_MODE";
const ENV_FILE_THRESHOLD: &str = "TRUSTY_REVIEW_MAP_FILE_THRESHOLD";
const ENV_PER_FILE_CHARS: &str = "TRUSTY_REVIEW_MAP_PER_FILE_CHARS";
const ENV_CONCURRENCY: &str = "TRUSTY_REVIEW_MAP_CONCURRENCY";
const ENV_TOTAL_CHAR_BUDGET: &str = "TRUSTY_REVIEW_MAP_TOTAL_CHAR_BUDGET";
const ENV_MAX_CALLS: &str = "TRUSTY_REVIEW_MAP_MAX_CALLS";
const ENV_MAX_FINDINGS: &str = "TRUSTY_REVIEW_MAP_MAX_FINDINGS";
const ENV_SYNTHESIS: &str = "TRUSTY_REVIEW_MAP_SYNTHESIS";
const ENV_PER_FILE_SEARCH: &str = "TRUSTY_REVIEW_MAP_PER_FILE_SEARCH";
// ─── Config defaults ──────────────────────────────────────────────────────────
/// Default file-count threshold above which `auto` mode selects map-reduce.
///
/// Why: PRs with more than this many changed files are expensive to review in a
/// single unified call even before the char budget is exhausted; map-reduce
/// distributes the cognitive load per-file.
/// What: matches `TRUSTY_REVIEW_MAP_FILE_THRESHOLD` default documented in #680.
pub const DEFAULT_MAP_FILE_THRESHOLD: usize = 12;
/// Default per-unit char budget for a single file's map call.
///
/// Why: each map call gets its own context window; capping per-file input keeps
/// individual map calls well within the model's context limit.
/// What: 120 000 chars ≈ 30 K tokens — comfortably below the 200 K-token window.
pub const DEFAULT_MAP_PER_FILE_CHARS: usize = 120_000;
/// Default concurrency for parallel map calls.
///
/// Why: bounded fan-out prevents rate-limit hammering; 4 mirrors the verifier's
/// `buffer_unordered(4)` pattern in `verify.rs:147`.
/// What: `TRUSTY_REVIEW_MAP_CONCURRENCY` default.
pub const DEFAULT_MAP_CONCURRENCY: usize = 4;
/// Default total character budget summed across all map inputs.
///
/// Why: a global budget caps the worst-case cost of a map-reduce review
/// independent of per-file caps. 1 M chars ≈ 250 K tokens.
/// What: `TRUSTY_REVIEW_MAP_TOTAL_CHAR_BUDGET` default.
pub const DEFAULT_MAP_TOTAL_CHAR_BUDGET: usize = 1_000_000;
/// Default hard ceiling on map LLM calls per review.
///
/// Why: prevents runaway cost on a PR with hundreds of files.
/// What: `TRUSTY_REVIEW_MAP_MAX_CALLS` default.
pub const DEFAULT_MAP_MAX_CALLS: usize = 40;
/// Default cap on findings surfaced in the reduce output.
///
/// Why: the PR comment must remain actionable; too many findings overwhelm
/// the author and bury the critical ones.
/// What: `TRUSTY_REVIEW_MAP_MAX_FINDINGS` default.
pub const DEFAULT_MAP_MAX_FINDINGS: usize = 50;
// ─── MapMode enum ─────────────────────────────────────────────────────────────
/// Controls when map-reduce review is used instead of unified-diff review.
///
/// Why: operators need an escape hatch (`never` = today's behaviour exactly)
/// and a forced path (`always`) for testing, while `auto` provides intelligent
/// switching based on diff size + file count.
/// What: parsed from `TRUSTY_REVIEW_MAP_MODE` env var (case-insensitive).
/// `auto` is the default; malformed values fall back to `auto`.
/// Test: `map_mode_parse_auto`, `map_mode_parse_always`, `map_mode_parse_never`,
/// `map_mode_parse_garbage_falls_back_to_auto`.
// ─── ReviewPath (selector output) ────────────────────────────────────────────
/// Decision output of `select_review_mode`.
///
/// Why: a typed enum prevents the caller from comparing strings or booleans;
/// pattern-matching on this enum will produce a compile error if new variants
/// are added without handling them.
/// What: `Unified` = current path; `MapReduce` = per-file fan-out path.
/// Test: returned by `select_review_mode`; asserted in decision-table tests.
// ─── MapReduceConfig ──────────────────────────────────────────────────────────
/// Configuration for the map-reduce review path (Phase 1, #690).
///
/// Why: aggregates all 9 `TRUSTY_REVIEW_MAP_*` knobs into a single owned struct
/// so the pipeline has one place to read map-reduce configuration, matching the
/// pattern used by `VerificationConfig` (#583).
/// What: loaded via `MapReduceConfig::from_env`; all fields have documented
/// defaults; malformed env values fall back to default (never panic).
/// Test: `mapreduce_config_defaults`, `mapreduce_env_overrides`,
/// `mapreduce_malformed_env_fallback`.
// ─── Mode selector ────────────────────────────────────────────────────────────
/// Diff statistics passed to `select_review_mode`.
///
/// Why: bundles the two inputs the selector needs so the signature stays clean
/// and the decision table in tests reads clearly.
/// What: `diff_chars` is the length of the rendered unified diff (before any
/// truncation); `file_count` is the number of surviving files in `FilteredDiff`.
/// Test: constructed inline in `select_review_mode_*` unit tests.
/// Select the review path (unified vs. map-reduce) from diff statistics + config.
///
/// Why: centralises the trigger heuristic documented in #680 so it is tested
/// independently of the runner. Phase 1 only; nothing in the live runner
/// consumes the return value yet (wired in Phase 5).
/// What: implements the decision table:
/// - `Never` → `Unified` (always, regardless of diff stats)
/// - `Always` → `MapReduce` (always, regardless of diff stats)
/// - `Auto` → `MapReduce` if `diff_chars > MAX_DIFF_CHARS`
/// OR `file_count > config.file_threshold`
/// - `Auto` → `Unified` otherwise
///
/// The "would truncate" predicate uses `diff_chars > MAX_DIFF_CHARS` (strict
/// greater-than) because `render_for_prompt` and `truncate_diff` both use
/// `<= MAX_DIFF_CHARS` as the "fits" condition (see `diff.rs:93`).
///
/// Test: `select_never_always_unified`, `select_always_forces_mapreduce`,
/// `select_auto_small_diff_is_unified`, `select_auto_truncates_triggers_mapreduce`,
/// `select_auto_many_files_triggers_mapreduce`,
/// `select_auto_boundary_at_exactly_max_chars`,
/// `select_auto_boundary_at_exactly_threshold`.
// ─── Private helpers ──────────────────────────────────────────────────────────
/// Parse a `usize` env var, falling back to `default` on unset/malformed.
///
/// Why: all numeric knobs need the same parse-or-warn-and-fallback pattern;
/// extracting it here keeps `MapReduceConfig::from_env` readable.
/// What: reads `var`, trims whitespace, parses as `usize`. Empty or unset
/// returns `default` silently; unparseable returns `default` with a warning.
/// Test: covered indirectly by `mapreduce_malformed_env_fallback`.
/// Parse a boolean env var with default, using lenient truthiness rules.
///
/// Why: boolean knobs (`synthesis`, `per_file_search`) need the same
/// true/false/1/0/yes/no/on/off parsing as `verification.rs` with an explicit
/// default when unset.
/// What: `Some(false)` for false/0/no/off, `Some(true)` for true/1/yes/on,
/// `None` (→ default) for unset/empty, `None` (with warning → default) for
/// unrecognised.
/// Test: covered by `mapreduce_synthesis_env_toggle`.
// ─── Unit tests ───────────────────────────────────────────────────────────────
// Why: tests are kept in a separate file to honour the 500-line cap on this
// file (CLAUDE.md §"500-line file size hard cap").
// What: `mapreduce_tests.rs` contains all unit tests for this module.
// Test: run `cargo test -p trusty-review -- config::mapreduce` to execute.