prview 0.4.0

PR Review & Artifact Generator - cross-language PR analysis tool
Documentation
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use serde::{Deserialize, Serialize};

use crate::check_id::check_id_from_name;
use crate::checks::{CheckResult, CheckStatus, SkippedCheck};
use crate::config::Config;
use crate::policy::{GateClass, PolicySeverity};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolOutcome {
    Passed,
    FindingsFailed,
    FindingsWarning,
    SystemError,
    Skipped,
    Unavailable,
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PolicyConclusion {
    Satisfied,
    Advisory,
    Blocked,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AnalysisStatus {
    Complete,
    Degraded,
    Incomplete,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MergeRecommendation {
    Approve,
    ReviewRequired,
    Block,
}

impl MergeRecommendation {
    /// Legacy single-field verdict, unified to the machine vocabulary
    /// `PASS`/`CONDITIONAL`/`BLOCK` (PV-03/04). `CONDITIONAL` replaces the former
    /// `HOLD` synonym so there is one word for "review required / degraded" on
    /// every decision surface. Old `HOLD` runs are still tolerated by the MCP
    /// adapter and the schema validator during read-back.
    pub fn legacy_verdict(
        self,
        analysis_status: AnalysisStatus,
        quality_pass: bool,
    ) -> &'static str {
        match self {
            MergeRecommendation::Block => "BLOCK",
            MergeRecommendation::ReviewRequired => "CONDITIONAL",
            MergeRecommendation::Approve => {
                if analysis_status == AnalysisStatus::Complete && quality_pass {
                    "PASS"
                } else {
                    "CONDITIONAL"
                }
            }
        }
    }

    pub fn legacy_recommended_merge(self) -> bool {
        self == MergeRecommendation::Approve
    }

    pub fn machine_status(
        self,
        analysis_status: AnalysisStatus,
        quality_pass: bool,
    ) -> &'static str {
        if self == MergeRecommendation::Approve
            && analysis_status == AnalysisStatus::Complete
            && quality_pass
        {
            "ok"
        } else {
            "fail"
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CheckExecutionState {
    Executed,
    Skipped,
    Unavailable,
    Unknown,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckEvaluation {
    pub check_id: String,
    pub name: String,
    pub raw_status: String,
    pub execution_state: CheckExecutionState,
    pub gate_class: GateClass,
    pub severity: PolicySeverity,
    pub outcome: ToolOutcome,
    pub conclusion: PolicyConclusion,
    pub confidence_impact: AnalysisStatus,
    pub merge_impact: MergeRecommendation,
    pub reason: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PolicyRunSummary {
    pub analysis_status: AnalysisStatus,
    pub merge_recommendation: MergeRecommendation,
    pub evaluations: Vec<CheckEvaluation>,
    pub blocking_issues: Vec<String>,
    pub review_caveats: Vec<String>,
}

pub struct PolicyEngine<'a> {
    config: &'a Config,
}

impl<'a> PolicyEngine<'a> {
    pub fn new(config: &'a Config) -> Self {
        Self { config }
    }

    pub fn evaluate_run(&self, result: &CheckResult) -> CheckEvaluation {
        let id = check_id_from_name(&result.name);
        let severity = self.config.policy.severity_for(&id);

        // A check that RAN but returned Skipped (e.g. a required tool that
        // spawned then failed and was downgraded to Skipped, or a degenerate
        // no-op) must route through the SAME policy logic as a pre-run skip.
        // Otherwise a check REQUIRED by policy that vanishes at runtime is
        // scored as a silent Satisfied/Approve and the gate goes falsely green
        // — the fail-open class at the core of the merge-gate doctrine
        // (PR #12 review #1).
        if result.status == CheckStatus::Skipped {
            let reason = result.output.to_lowercase();
            let execution_state = classify_skip_execution_state(&reason);
            let (conclusion, confidence_impact, merge_impact) =
                self.skip_policy_outcome(severity, &reason);
            return CheckEvaluation {
                check_id: id,
                name: result.name.clone(),
                raw_status: result.status.as_str().to_string(),
                execution_state,
                gate_class: GateClass::Skip,
                severity,
                outcome: skip_outcome_for(execution_state),
                conclusion,
                confidence_impact,
                merge_impact,
                reason: (!result.output.is_empty()).then(|| result.output.clone()),
            };
        }

        let outcome = match result.status {
            CheckStatus::Passed => ToolOutcome::Passed,
            CheckStatus::Failed => ToolOutcome::FindingsFailed,
            CheckStatus::Warnings => ToolOutcome::FindingsWarning,
            CheckStatus::Error => ToolOutcome::SystemError,
            CheckStatus::Skipped => ToolOutcome::Skipped,
        };

        let class = match result.status {
            CheckStatus::Passed => GateClass::Pass,
            CheckStatus::Failed | CheckStatus::Error => GateClass::Fail,
            CheckStatus::Warnings => GateClass::Info,
            CheckStatus::Skipped => GateClass::Skip,
        };
        let is_blocking = self.config.policy.is_blocking(severity, class);

        let conclusion = if is_blocking {
            PolicyConclusion::Blocked
        } else if class == GateClass::Fail || class == GateClass::Info {
            PolicyConclusion::Advisory
        } else {
            PolicyConclusion::Satisfied
        };

        let confidence_impact = if result.status == CheckStatus::Error {
            AnalysisStatus::Incomplete
        } else {
            AnalysisStatus::Complete
        };

        let merge_impact = if conclusion == PolicyConclusion::Blocked {
            MergeRecommendation::Block
        } else if conclusion == PolicyConclusion::Advisory {
            MergeRecommendation::ReviewRequired
        } else {
            MergeRecommendation::Approve
        };

        let has_hard_fails = result
            .provenance
            .as_ref()
            .is_some_and(|p| !p.hard_fail_signatures.is_empty());
        let final_merge_impact = if has_hard_fails && merge_impact != MergeRecommendation::Block {
            MergeRecommendation::ReviewRequired
        } else {
            merge_impact
        };

        CheckEvaluation {
            check_id: id,
            name: result.name.clone(),
            raw_status: result.status.as_str().to_string(),
            execution_state: CheckExecutionState::Executed,
            gate_class: class,
            severity,
            outcome,
            conclusion,
            confidence_impact,
            merge_impact: final_merge_impact,
            reason: None,
        }
    }

    pub fn evaluate_skip(&self, skipped: &SkippedCheck) -> CheckEvaluation {
        let id = check_id_from_name(&skipped.name);
        let reason = skipped.reason.to_lowercase();
        let severity = self.config.policy.severity_for(&id);
        let execution_state = classify_skip_execution_state(&reason);
        let (conclusion, confidence_impact, merge_impact) =
            self.skip_policy_outcome(severity, &reason);

        CheckEvaluation {
            check_id: id,
            name: skipped.name.clone(),
            raw_status: CheckStatus::Skipped.as_str().to_string(),
            execution_state,
            gate_class: GateClass::Skip,
            severity,
            outcome: skip_outcome_for(execution_state),
            conclusion,
            confidence_impact,
            merge_impact,
            reason: Some(skipped.reason.clone()),
        }
    }

    /// Policy outcome for a check that produced no usable signal (skipped —
    /// either pre-run via `evaluate_skip`, or downgraded at runtime via the
    /// Skipped branch of `evaluate_run`). Shared so a check REQUIRED by policy
    /// cannot silently pass the gate just because it vanished at runtime
    /// (fail-open). `reason` must already be lowercased.
    fn skip_policy_outcome(
        &self,
        severity: PolicySeverity,
        reason: &str,
    ) -> (PolicyConclusion, AnalysisStatus, MergeRecommendation) {
        if reason.starts_with("profile") {
            // Profile mismatches (e.g. running a rust check on a JS repo) are
            // totally fine — the check simply does not apply here.
            (
                PolicyConclusion::Satisfied,
                AnalysisStatus::Complete,
                MergeRecommendation::Approve,
            )
        } else if severity == PolicySeverity::Block {
            if reason.contains("fast remote-only preset") && self.config.remote_only {
                // Strictly required check skipped by the fast remote preset: a
                // degraded signal that requires review, not an outright block.
                (
                    PolicyConclusion::Advisory,
                    AnalysisStatus::Degraded,
                    MergeRecommendation::ReviewRequired,
                )
            } else {
                // Required but skipped for any other reason (missing tool,
                // explicitly disabled, runtime spawn failure): the gate cannot
                // be trusted, so block.
                (
                    PolicyConclusion::Blocked,
                    AnalysisStatus::Incomplete,
                    MergeRecommendation::Block,
                )
            }
        } else if severity == PolicySeverity::Warn {
            // "Warn" severity skipping means it's an optional extra layer.
            (
                PolicyConclusion::Advisory,
                AnalysisStatus::Degraded,
                MergeRecommendation::ReviewRequired,
            )
        } else {
            // "Ignore" severity skipping.
            (
                PolicyConclusion::Satisfied,
                AnalysisStatus::Complete,
                MergeRecommendation::Approve,
            )
        }
    }

    pub fn evaluate_all(
        &self,
        checks: &[CheckResult],
        skipped_checks: &[SkippedCheck],
    ) -> PolicyRunSummary {
        let mut evaluations = Vec::with_capacity(checks.len() + skipped_checks.len());
        let mut analysis_status = AnalysisStatus::Complete;
        let mut merge_recommendation = MergeRecommendation::Approve;
        let mut blocking_issues = Vec::new();
        let mut review_caveats = Vec::new();

        for check in checks {
            let eval = self.evaluate_run(check);
            bump_summary_status(&mut analysis_status, &mut merge_recommendation, &eval);
            if eval.conclusion == PolicyConclusion::Blocked {
                blocking_issues.push(format!(
                    "{} ({})",
                    check.name,
                    display_status_label(check.status.as_str())
                ));
            } else if eval.conclusion == PolicyConclusion::Advisory {
                review_caveats.push(describe_advisory_check(&eval));
            }
            evaluations.push(eval);
        }

        for skipped in skipped_checks {
            let eval = self.evaluate_skip(skipped);
            bump_summary_status(&mut analysis_status, &mut merge_recommendation, &eval);
            if eval.conclusion == PolicyConclusion::Blocked {
                blocking_issues.push(format!("{} ({})", skipped.name, skipped.reason));
            } else if eval.conclusion == PolicyConclusion::Advisory {
                review_caveats.push(format!("{} skipped: {}", skipped.name, skipped.reason));
            }
            evaluations.push(eval);
        }

        PolicyRunSummary {
            analysis_status,
            merge_recommendation,
            evaluations,
            blocking_issues,
            review_caveats,
        }
    }
}

/// Map a skip's execution-state classification to its reported tool outcome.
fn skip_outcome_for(execution_state: CheckExecutionState) -> ToolOutcome {
    match execution_state {
        CheckExecutionState::Skipped => ToolOutcome::Skipped,
        CheckExecutionState::Unavailable => ToolOutcome::Unavailable,
        CheckExecutionState::Unknown => ToolOutcome::Unknown,
        CheckExecutionState::Executed => ToolOutcome::Skipped,
    }
}

fn classify_skip_execution_state(reason: &str) -> CheckExecutionState {
    if reason.is_empty() {
        return CheckExecutionState::Unknown;
    }
    if reason.starts_with("profile")
        || reason.contains("disabled")
        || reason.contains("fast remote-only preset")
    {
        return CheckExecutionState::Skipped;
    }
    if reason.contains("not installed")
        || reason.contains("not found")
        || reason.contains("missing")
        || reason.contains("unavailable")
    {
        return CheckExecutionState::Unavailable;
    }
    CheckExecutionState::Unknown
}

fn bump_summary_status(
    analysis_status: &mut AnalysisStatus,
    merge_recommendation: &mut MergeRecommendation,
    eval: &CheckEvaluation,
) {
    if eval.confidence_impact == AnalysisStatus::Incomplete {
        *analysis_status = AnalysisStatus::Incomplete;
    } else if eval.confidence_impact == AnalysisStatus::Degraded
        && *analysis_status == AnalysisStatus::Complete
    {
        *analysis_status = AnalysisStatus::Degraded;
    }

    if eval.merge_impact == MergeRecommendation::Block {
        *merge_recommendation = MergeRecommendation::Block;
    } else if eval.merge_impact == MergeRecommendation::ReviewRequired
        && *merge_recommendation == MergeRecommendation::Approve
    {
        *merge_recommendation = MergeRecommendation::ReviewRequired;
    }
}

fn describe_advisory_check(eval: &CheckEvaluation) -> String {
    match eval.execution_state {
        CheckExecutionState::Executed => format!("{} returned {}", eval.name, eval.raw_status),
        CheckExecutionState::Skipped => format!("{} was skipped", eval.name),
        CheckExecutionState::Unavailable => format!("{} was unavailable for this run", eval.name),
        CheckExecutionState::Unknown => format!("{} needs manual review", eval.name),
    }
}

fn display_status_label(status: &str) -> String {
    let mut chars = status.chars();
    match chars.next() {
        Some(first) => format!("{}{}", first.to_ascii_uppercase(), chars.as_str()),
        None => status.to_string(),
    }
}