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
//! Letter-grade type for PR reviews — 13 half-step variants A+ through F.
//!
//! Why: a letter grade gives reviewers an at-a-glance quality signal and makes
//! the verdict thresholds explicit. The grade is the LLM's primary quality
//! assessment; the verdict is *derived* from it (with a safety floor applied
//! on top so severity/verification never weaken the verdict below what the
//! grade implies).
//!
//! What: exposes the `Grade` enum (13 variants, A+ through F), `Display`,
//! `FromStr` / serde (using the standard notation "A+", "B-", "C", …, "F"),
//! `PartialOrd` / `Ord` so bands can be compared (A+ > A > … > F),
//! `verdict_for_grade` — the single source of truth for the grade→verdict mapping,
//! and `default_grade_for_verdict` — the inverse used when the LLM omits the grade.
//!
//! Grade → Verdict mapping (FIXED by product decision, APPROVE floor = B-):
//!
//! | Grade band | Verdict |
//! |-------------------|----------------------|
//! | A+, A, A-, B+, B, B- | APPROVE |
//! | C+, C, C- | APPROVE* (approve w/ reservations) |
//! | D+, D, D- | REQUEST_CHANGES |
//! | F | BLOCK |
//!
//! Test: `grade_serde_roundtrip`, `grade_ordering`, `verdict_for_grade_boundaries`,
//! `default_grade_for_verdict_roundtrips`.
use FromStr;
use ;
use crateVerdict;
// ─── Grade enum ───────────────────────────────────────────────────────────────
/// Letter grade for a PR review, with half-step +/- modifiers.
///
/// Why: provides an at-a-glance quality signal and makes the verdict thresholds
/// explicit and tunable. The grade is the LLM's primary quality assessment;
/// `verdict_for_grade` derives the corresponding action-verdict from it.
/// What: 13 ordered variants from A+ (best) to F (worst). Serde uses the
/// standard notation ("A+", "A", "A-", …, "F"). `Ord` is defined so A+ > A >
/// … > F (higher ordinal = higher quality).
/// Test: `grade_serde_roundtrip`, `grade_ordering`.
// ─── Ordering (A+ = best = highest) ──────────────────────────────────────────
// ─── Display ─────────────────────────────────────────────────────────────────
// ─── FromStr ─────────────────────────────────────────────────────────────────
/// Parsing error for `Grade::from_str`.
///
/// Why: `FromStr` requires an `Err` type; a simple tuple-struct wrapping the
/// rejected token is sufficient.
/// What: carries the unrecognised string for diagnostic messages.
/// Test: `grade_from_str_invalid`.
;
// ─── Grade → Verdict mapping ─────────────────────────────────────────────────
/// Derive the review verdict from a letter grade.
///
/// Why: the grade→verdict table is a FIXED product decision (APPROVE floor =
/// B-). This function is the **single source of truth** for that mapping.
/// It is used in two places:
/// 1. After the LLM produces a grade — the verdict is derived here and then
/// reconciled with the severity floor (`grade::derive_verdict_with_grade`).
/// 2. After verification — the grade may be clamped down to stay consistent
/// with a stricter post-verification verdict.
///
/// What: implements the table:
///
/// | Grade band | Verdict |
/// |----------------------|----------------------|
/// | A+, A, A-, B+, B, B- | APPROVE |
/// | C+, C, C- | APPROVE* |
/// | D+, D, D- | REQUEST_CHANGES |
/// | F | BLOCK |
///
/// Test: `verdict_for_grade_boundaries` — covers every grade band edge.
/// Return the default (mildest representative) grade for a verdict.
///
/// Why: when the LLM omits or emits an unparseable grade, the pipeline must
/// still populate `ReviewResult.grade`. Rather than leaving it absent, we
/// derive a conservative default — the mildest grade in the verdict's band —
/// so grade and verdict are always consistent in the output.
/// What: A+ for APPROVE, C for APPROVE*, D for REQUEST_CHANGES, F for BLOCK.
/// UNKNOWN maps to F (maximally conservative — unknown is not a pass).
/// Test: `default_grade_for_verdict_roundtrips`.
/// Clamp a grade down so it is consistent with the given verdict.
///
/// Why: after verification tightens the verdict (e.g. APPROVE → REQUEST_CHANGES
/// due to a confirmed High finding), the original model grade may be inconsistent
/// (e.g. grade "B+" vs verdict REQUEST_CHANGES). This function ensures grade and
/// verdict never disagree in the final output by lowering the grade to the ceiling
/// of the verdict's band when the grade implies a milder verdict than the actual one.
///
/// Precedence rule:
/// final_grade = min(model_grade, ceiling_for_verdict(actual_verdict))
///
/// The ceiling is the BEST (highest) grade still consistent with the verdict:
/// APPROVE → A+ (no clamping needed for any grade)
/// APPROVE* → C+ (clamp any B or above down to C+)
/// REQUEST_CHANGES → D+ (clamp any C or above down to D+)
/// BLOCK → F (clamp anything above F down to F)
/// UNKNOWN → F (same as BLOCK)
///
/// What: returns the grade unchanged when it is already consistent, or the
/// band ceiling when it implies a milder verdict than `actual_verdict`.
/// Test: `clamp_grade_to_verdict_block`, `clamp_grade_to_verdict_request_changes`.
/// Return true if `a` implies a verdict at least as strict as `b`.
///
/// Why: needed by `clamp_grade_to_verdict` to avoid clamping when the grade
/// is already consistent or stricter than the actual verdict.
/// What: uses the verdict ordinal ordering APPROVE(0) < APPROVE*(1) <
/// REQUEST_CHANGES(2) < BLOCK(3). UNKNOWN(4) is treated as maximally strict.
/// Test: transitively covered by `clamp_grade_to_verdict_*`.
/// Ordinal for strict ordering of verdicts (higher = more severe).
///
/// Why: used by `is_at_least_as_strict` to compare strictness.
/// What: APPROVE=0, APPROVE*=1, REQUEST_CHANGES=2, BLOCK=3, UNKNOWN=4.
/// Test: transitively covered.
// ─── Unit tests ─────────────────────────────────────────────────────────────
// Tests extracted to letter_grade_tests.rs to keep this file under the 500-line cap.