Module: RailVerdict::Verification::Policy

Defined in:
lib/rail_verdict/verification/policy.rb

Class Method Summary collapse

Class Method Details

.evaluate(configuration:, analyzer_results:, findings:, comparison: nil, baseline_meta: nil, git_context: nil) ⇒ Object



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
# File 'lib/rail_verdict/verification/policy.rb', line 8

def evaluate(configuration:, analyzer_results:, findings:, comparison: nil, baseline_meta: nil, git_context: nil)
  analyzer_results = analyzer_results.sort_by(&:analyzer)
  findings = findings.sort_by(&:sort_key)
  operational_failures = []
  required_incomplete = []

  configuration.analyzers.each do |analyzer, selection|
    next unless selection.fetch("enabled")

    result = analyzer_results.find { |candidate| candidate.analyzer == analyzer }
    if result.nil?
      failure = { "code" => "unavailable", "analyzer" => analyzer, "message" => "required analyzer did not produce a result" }
      operational_failures << failure
      required_incomplete << failure if selection.fetch("required")
    elsif result.evidence_status == "incomplete"
      failure = {
        "code" => result.failure.fetch("code"),
        "analyzer" => analyzer,
        "message" => result.failure.fetch("message")
      }
      operational_failures << failure
      required_incomplete << failure if selection.fetch("required")
    elsif selection.fetch("required") && (failure = incomplete_evidence_failure(analyzer, result))
      operational_failures << failure
      required_incomplete << failure
    end
  end

  unless required_incomplete.empty?
    return GateResult.new(
      completion_status: "incomplete",
      gate: "INCOMPLETE",
      policy_status: "not_evaluated",
      findings: summaries(findings, blocking: false),
      analyzer_results: analyzer_results,
      operational_failures: required_incomplete,
      decision_reasons: [{
        "code" => "required_evidence_incomplete",
        "message" => "Required analyzer evidence is incomplete; policy was not evaluated."
      }]
    )
  end

  optional_failures = operational_failures.reject { |failure| required_incomplete.include?(failure) }

  changed_findings = if git_context
                       filter_to_changed_scope(findings, git_context)
                     else
                       findings
                     end

  mode = configuration.mode

  if mode == "advisory" && git_context
    scoped = changed_findings
    gate = scoped.empty? ? "PASS" : "WARN"
    policy_status = gate.downcase
    reasons = []
    reasons << if scoped.empty?
                 if findings.empty?
                   { "code" => "no_findings_detected", "message" => "All enabled analyzer evidence contains no findings." }
                 else
                   { "code" => "advisory_no_changed_findings", "message" => "No findings in changed scope; #{findings.length} findings outside changed scope are advisory." }
                 end
               else
                 { "code" => "advisory_findings_in_changed_scope", "message" => "#{scoped.length} findings in changed scope are advisory and do not block." }
               end
    reasons << { "code" => "changed_scope", "message" => "Evaluated #{scoped.length} findings in changed scope (#{findings.length} total)." }
    reasons << { "code" => "optional_evidence_unavailable", "message" => "Optional analyzer evidence was unavailable and was retained as an operational failure." } unless optional_failures.empty?
    blocking_map = Set.new(scoped.map(&:fingerprint))
    summaries_scoped = findings.sort_by(&:sort_key).map do |finding|
      blocking = false
      { "id" => finding.id, "fingerprint" => finding.fingerprint, "severity" => finding.severity, "state" => finding.state, "blocking" => blocking }
    end
    return GateResult.new(
      completion_status: "complete",
      gate: gate,
      policy_status: policy_status,
      findings: summaries_scoped,
      analyzer_results: analyzer_results,
      operational_failures: optional_failures,
      decision_reasons: reasons
    )
  end

  if mode == "advisory"
    gate = findings.empty? ? "PASS" : "WARN"
    policy_status = gate.downcase
    reasons = []
    reasons << if findings.empty?
                 { "code" => "no_findings_detected", "message" => "All enabled analyzer evidence contains no findings." }
               else
                 { "code" => "advisory_findings_present", "message" => "#{findings.length} findings are advisory and do not block." }
               end
    reasons << { "code" => "optional_evidence_unavailable", "message" => "Optional analyzer evidence was unavailable and was retained as an operational failure." } unless optional_failures.empty?
    return GateResult.new(
      completion_status: "complete",
      gate: gate,
      policy_status: policy_status,
      findings: summaries(findings, blocking: false),
      analyzer_results: analyzer_results,
      operational_failures: optional_failures,
      decision_reasons: reasons
    )
  end

  if mode == "no_new_debt" && comparison.nil? && !findings.empty?
    return GateResult.new(
      completion_status: "incomplete",
      gate: "INCOMPLETE",
      policy_status: "not_evaluated",
      findings: summaries(findings, blocking: false),
      analyzer_results: analyzer_results,
      operational_failures: [{ "code" => "failed", "message" => "baseline required for no_new_debt policy; run `railverdict baseline create`" }],
      decision_reasons: [{ "code" => "baseline_required", "message" => "no_new_debt requires a baseline; create one with `railverdict baseline create`" }]
    )
  end

  if mode == "no_new_debt" && comparison.nil? && findings.empty?
    return GateResult.new(
      completion_status: "complete",
      gate: "PASS",
      policy_status: "pass",
      findings: summaries(findings, blocking: false),
      analyzer_results: analyzer_results,
      operational_failures: optional_failures,
      decision_reasons: [{ "code" => "no_findings_detected", "message" => "All enabled analyzer evidence contains no findings." }]
    )
  end

  if mode == "no_new_debt" && comparison
    counts = comparison.fetch("counts") rescue comparison["counts"] if comparison.is_a?(Hash)
    counts ||= {}
    waived_fps = Set.new((comparison.is_a?(Hash) ? (comparison["waived"] || []) : []))
    introduced_fps = Set.new((comparison.is_a?(Hash) ? (comparison["introduced"] || []) : []))
    changed_fps = Set.new((comparison.is_a?(Hash) ? (comparison["changed"] || []) : []))
    moved_fps = Set.new((comparison.is_a?(Hash) ? (comparison["moved"] || []) : []))
    regression_fps = introduced_fps | changed_fps | moved_fps
    if git_context
      regression_findings = changed_findings.select { |finding| regression_fps.include?(finding.fingerprint) && !waived_fps.include?(finding.fingerprint) }
      existing_count = counts["existing"] || 0
      gate = regression_findings.empty? ? "PASS" : "FAIL"
      policy_status = gate.downcase
      reasons = []
      reasons << { "code" => "existing_debt_retained", "message" => "#{existing_count} existing findings retained from baseline." } if existing_count > 0
      reasons << { "code" => "changed_scope", "message" => "Evaluated #{changed_findings.length} findings in changed scope (#{findings.length} total)." }
      reasons << if regression_findings.empty?
                   if findings.empty?
                     { "code" => "no_findings_detected", "message" => "All enabled analyzer evidence contains no findings." }
                   elsif changed_findings.empty?
                     { "code" => "no_new_debt_pass_changed_scope", "message" => "No introduced regressions in changed scope." }
                   else
                     { "code" => "no_new_debt_pass", "message" => "No introduced regressions detected." }
                   end
                 else
                   { "code" => "introduced_findings_blocking", "message" => "#{regression_findings.length} introduced findings in changed scope violate no_new_debt policy." }
                 end
      reasons << { "code" => "optional_evidence_unavailable", "message" => "Optional analyzer evidence was unavailable and was retained as an operational failure." } unless optional_failures.empty?
      summaries_with_state = findings.sort_by(&:sort_key).map do |finding|
        in_scope = changed_findings.any? { |scope_finding| scope_finding.fingerprint == finding.fingerprint }
        blocking = in_scope && regression_fps.include?(finding.fingerprint) && !waived_fps.include?(finding.fingerprint)
        blocking = false if finding.state == "existing"
        blocking = false if finding.state == "waived"
        { "id" => finding.id, "fingerprint" => finding.fingerprint, "severity" => finding.severity, "state" => finding.state, "blocking" => blocking }
      end
      return GateResult.new(
        completion_status: "complete",
        gate: gate,
        policy_status: policy_status,
        findings: summaries_with_state,
        analyzer_results: analyzer_results,
        operational_failures: optional_failures,
        decision_reasons: reasons
      )
    end
    blocking_findings = findings.select { |finding| regression_fps.include?(finding.fingerprint) && !waived_fps.include?(finding.fingerprint) }
    existing_count = counts["existing"] || 0
    gate = blocking_findings.empty? ? "PASS" : "FAIL"
    policy_status = gate.downcase
    reasons = []
    reasons << { "code" => "existing_debt_retained", "message" => "#{existing_count} existing findings retained from baseline." } if existing_count > 0
    reasons << if blocking_findings.empty?
                 if findings.empty?
                   { "code" => "no_findings_detected", "message" => "All enabled analyzer evidence contains no findings." }
                 else
                   { "code" => "no_new_debt_pass", "message" => "No introduced regressions detected." }
                 end
               else
                 { "code" => "introduced_findings_blocking", "message" => "#{blocking_findings.length} introduced findings violate no_new_debt policy." }
               end
    reasons << { "code" => "optional_evidence_unavailable", "message" => "Optional analyzer evidence was unavailable and was retained as an operational failure." } unless optional_failures.empty?
    summaries_with_state = findings.sort_by(&:sort_key).map do |finding|
      blocking = regression_fps.include?(finding.fingerprint) && !waived_fps.include?(finding.fingerprint)
      blocking = false if finding.state == "existing"
      blocking = false if finding.state == "waived"
      { "id" => finding.id, "fingerprint" => finding.fingerprint, "severity" => finding.severity, "state" => finding.state, "blocking" => blocking }
    end
    return GateResult.new(
      completion_status: "complete",
      gate: gate,
      policy_status: policy_status,
      findings: summaries_with_state,
      analyzer_results: analyzer_results,
      operational_failures: optional_failures,
      decision_reasons: reasons
    )
  end

  if git_context
    scoped = changed_findings
    gate = scoped.empty? ? "PASS" : "FAIL"
    policy_status = gate.downcase
    reasons = []
    reasons << { "code" => "changed_scope", "message" => "Evaluated #{scoped.length} findings in changed scope (#{findings.length} total)." }
    reasons << if scoped.empty?
                 if findings.empty?
                   { "code" => "no_findings_detected", "message" => "All enabled analyzer evidence contains no findings." }
                 else
                   { "code" => "no_findings_in_changed_scope", "message" => "No findings in changed scope." }
                 end
               else
                 { "code" => "blocking_findings_in_changed_scope", "message" => "#{scoped.length} findings in changed scope require policy FAIL." }
               end
    reasons << { "code" => "optional_evidence_unavailable", "message" => "Optional analyzer evidence was unavailable and was retained as an operational failure." } unless optional_failures.empty?
    scoped_fps = Set.new(scoped.map(&:fingerprint))
    summaries_scoped = findings.sort_by(&:sort_key).map do |finding|
      blocking = scoped_fps.include?(finding.fingerprint)
      { "id" => finding.id, "fingerprint" => finding.fingerprint, "severity" => finding.severity, "state" => finding.state, "blocking" => blocking }
    end
    return GateResult.new(
      completion_status: "complete",
      gate: gate,
      policy_status: policy_status,
      findings: summaries_scoped,
      analyzer_results: analyzer_results,
      operational_failures: optional_failures,
      decision_reasons: reasons
    )
  end

  strict = true
  blocking = true
  gate = findings.empty? ? "PASS" : "FAIL"
  policy_status = gate.downcase
  reasons = []
  reasons << if findings.empty?
               { "code" => "no_findings_detected", "message" => "All enabled analyzer evidence contains no findings." }
             else
               { "code" => "blocking_findings_present", "message" => "#{findings.length} findings require policy FAIL." }
             end
  reasons << { "code" => "optional_evidence_unavailable", "message" => "Optional analyzer evidence was unavailable and was retained as an operational failure." } unless optional_failures.empty?

  GateResult.new(
    completion_status: "complete",
    gate: gate,
    policy_status: policy_status,
    findings: summaries(findings, blocking: blocking),
    analyzer_results: analyzer_results,
    operational_failures: optional_failures,
    decision_reasons: reasons
  )
end

.incomplete_result(operational_failures:, analyzer_results: [], findings: [], code: "required_evidence_incomplete", message: "Required evidence is incomplete; policy was not evaluated.") ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/rail_verdict/verification/policy.rb', line 271

def incomplete_result(operational_failures:, analyzer_results: [], findings: [], code: "required_evidence_incomplete", message: "Required evidence is incomplete; policy was not evaluated.")
  GateResult.new(
    completion_status: "incomplete",
    gate: "INCOMPLETE",
    policy_status: "not_evaluated",
    findings: summaries(findings, blocking: false),
    analyzer_results: analyzer_results,
    operational_failures: operational_failures,
    decision_reasons: [{ "code" => code, "message" => message }]
  )
end

.interrupted_result(analyzer_results: [], findings: []) ⇒ Object



283
284
285
286
287
288
289
290
291
# File 'lib/rail_verdict/verification/policy.rb', line 283

def interrupted_result(analyzer_results: [], findings: [])
  incomplete_result(
    analyzer_results: analyzer_results,
    findings: findings,
    operational_failures: [{ "code" => "interrupted", "message" => "Execution was interrupted after child-process cleanup." }],
    code: "interrupted",
    message: "Execution was interrupted; no trustworthy gate was produced."
  )
end