Module: RailVerdict::Check

Defined in:
lib/rail_verdict/check.rb

Defined Under Namespace

Classes: Outcome

Constant Summary collapse

REGISTRY =
{
  "rubocop" => RailVerdict::Analyzers::RuboCop,
  "minitest" => RailVerdict::Analyzers::Minitest,
  "rspec" => RailVerdict::Analyzers::RSpec,
  "simplecov" => RailVerdict::Analyzers::SimpleCov,
  "bundler_audit" => RailVerdict::Analyzers::BundlerAudit
}.freeze

Class Method Summary collapse

Class Method Details

.effective_input_paths(root:, config_path: ".railverdict.yml", baseline_path_override: nil, waiver_path_override: nil) ⇒ Object

Resolves the EFFECTIVE verification input paths (config plus baseline and waivers, honoring configuration-declared paths and overrides). Shared by the guard and by receipt freshness validation so both sides bind exactly the same files.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rail_verdict/check.rb', line 58

def self.effective_input_paths(root:, config_path: ".railverdict.yml", baseline_path_override: nil, waiver_path_override: nil)
  return nil if root.nil?

  resolved_config = resolve_config_path(root, config_path)
  configuration = begin
    Configuration.load(resolved_config)
  rescue StandardError
    nil
  end
  {
    config: resolved_config,
    baseline: Baseline.resolve_path(repository_root: root, configuration: configuration, output_override: baseline_path_override),
    waivers: WaiverStore.resolve_path(repository_root: root, configuration: configuration, waiver_override: waiver_path_override)
  }
rescue StandardError
  {
    config: File.join(root, ".railverdict.yml"),
    baseline: File.join(root, ".railverdict-baseline.json"),
    waivers: File.join(root, ".railverdict-waivers.json")
  }
end

.execute(repository_root:, config_path:, runner: ProcessRunner, rubocop_command_resolver: nil, analyzer_timeout_seconds: 30.0, interrupted: nil, baseline_path_override: nil, waiver_path_override: nil, clock: Time.now.utc, changed: false, base: nil) ⇒ Object



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
# File 'lib/rail_verdict/check.rb', line 89

def execute(repository_root:, config_path:, runner: ProcessRunner, rubocop_command_resolver: nil, analyzer_timeout_seconds: 30.0, interrupted: nil, baseline_path_override: nil, waiver_path_override: nil, clock: Time.now.utc, changed: false, base: nil)
  root = File.realpath(repository_root)
  resolved_config = resolve_config_path(root, config_path)
  configuration = Configuration.load(resolved_config)
  return interrupted_outcome if interrupted&.call

  probes = probe_enabled_analyzers(root, configuration, runner: runner, rubocop_command_resolver: rubocop_command_resolver, default_timeout_seconds: analyzer_timeout_seconds)

  analyzer_versions = probes.transform_values(&:version)

  git_context = nil
  git_error = nil
  if changed
    begin
      git_context = Git::Context.build(repository_root: root, base_override: base, configuration: configuration, runner: runner)
    rescue Git::Error => error
      git_error = error
    end
    if git_error
      git_incomplete = Verification::Policy.incomplete_result(
        analyzer_results: [],
        findings: [],
        operational_failures: [{ "code" => "failed", "message" => git_error.message }],
        code: "git_scope_failed",
        message: git_error.message
      )
      context_for_error = begin
        RunContext.build(repository_root: root, configuration: configuration, analyzer_versions: analyzer_versions, revision_resolver: revision_resolver(root, runner))
      rescue StandardError
        nil
      end
      git_payload = git_error_payload(git_error, base, configuration)
      enriched = GateResult.new(
        completion_status: git_incomplete.completion_status,
        gate: git_incomplete.gate,
        policy_status: git_incomplete.policy_status,
        findings: git_incomplete.findings,
        analyzer_results: git_incomplete.analyzer_results,
        operational_failures: git_incomplete.operational_failures,
        decision_reasons: git_incomplete.decision_reasons,
        git: git_payload
      )
      return Outcome.new(result: enriched, context: context_for_error, configuration: configuration, findings: [].freeze)
    end
  end

  context = RunContext.build(
    repository_root: root,
    configuration: configuration,
    analyzer_versions: analyzer_versions,
    revision_resolver: revision_resolver(root, runner),
    git_context: git_context
  )
  return interrupted_outcome(context: context, configuration: configuration) if interrupted&.call

  analyzer_results = []
  findings = []
  configuration.analyzers.each do |name, selection|
    next unless selection.fetch("enabled")

    adapter_class = REGISTRY[name]
    unless adapter_class
      analyzer_results << AnalyzerResult.new(
        analyzer: name,
        invocation: { "executable" => name, "argv" => [] },
        execution_status: "unavailable",
        finding_ids: [],
        failure: { "code" => "unavailable", "message" => "analyzer #{name} is not implemented in this build" }
      )
      next
    end

    adapter = build_adapter(name, rubocop_command_resolver)
    probe = probes[name]
    timeout_seconds = resolve_timeout_seconds(configuration, name, analyzer_timeout_seconds)
    begin
      analyzer_result, analyzer_findings = adapter.run(
        root,
        runner: runner,
        probe_result: probe,
        timeout_seconds: timeout_seconds,
        configuration: configuration
      )
    rescue StandardError => error
      # Guarded boundary: analyzer normalization must never bypass GateResult
      message = RailVerdict::Analyzers::Shared.bounded_message("#{error.class}: #{error.message}")
      analyzer_result = RailVerdict::Analyzers::Shared.failure_result(
        analyzer_id: name,
        invocation: { "executable" => name, "argv" => [] },
        status: "malformed",
        message: message,
        tool_version: probe&.version
      )
      analyzer_findings = []
    end
    analyzer_results << analyzer_result
    findings.concat(analyzer_findings)
  end
  return interrupted_outcome(context: context, configuration: configuration, analyzer_results: analyzer_results, findings: findings) if interrupted&.call

  baseline = nil
  baseline_meta = nil
  comparison = nil
  classified_findings = findings
  waivers = []
  waiver_meta = nil
  resolved_baseline_path = Baseline.resolve_path(repository_root: root, configuration: configuration, output_override: baseline_path_override)
  resolved_waiver_path = WaiverStore.resolve_path(repository_root: root, configuration: configuration, waiver_override: waiver_path_override)
  begin
    waivers = WaiverStore.read_optional(resolved_waiver_path)
    waiver_meta = { "loaded" => !waivers.empty?, "path" => resolved_waiver_path, "count" => waivers.length }
  rescue Waiver::IncompatibleError => error
    incomplete = Verification::Policy.incomplete_result(
      analyzer_results: analyzer_results,
      findings: findings,
      operational_failures: [{ "code" => "failed", "message" => error.message }],
      code: "waiver_incompatible",
      message: error.message
    )
    enriched = GateResult.new(completion_status: incomplete.completion_status, gate: incomplete.gate, policy_status: incomplete.policy_status, findings: incomplete.findings, analyzer_results: incomplete.analyzer_results, operational_failures: incomplete.operational_failures, decision_reasons: incomplete.decision_reasons, baseline: nil, comparison: { "counts" => {}, "waiver_error" => error.message })
    return Outcome.new(result: enriched, context: context, configuration: configuration, findings: findings.freeze)
  end
  rename_map = git_context ? git_context.rename_map : nil
  begin
    if File.file?(resolved_baseline_path)
      loaded = Baseline.read(resolved_baseline_path)
      baseline = loaded
      baseline_meta = { "loaded" => true, "path" => resolved_baseline_path, "schema_version" => Baseline::SCHEMA_VERSION, "fingerprint_version" => Fingerprint::VERSION, "compatible" => true }
      cmp = Comparison.classify(findings: findings, baseline: loaded, waivers: waivers.map(&:to_h), clock: clock, rename_map: rename_map)
      comparison = { "counts" => cmp.counts, "introduced" => cmp.introduced.map(&:fingerprint).sort, "existing" => cmp.existing.map(&:fingerprint).sort, "resolved" => cmp.resolved.map { |entry| entry.fetch("fingerprint") }.sort, "changed" => cmp.changed.map(&:fingerprint).sort, "moved" => cmp.moved.map(&:fingerprint).sort, "waived" => cmp.counts.fetch("waived", 0) == 0 ? [] : cmp.classified_findings.select { |item| item.state == "waived" }.map(&:fingerprint).sort, "orphaned_waivers" => cmp.orphaned_waivers.map { |w| w["fingerprint"] || w[:fingerprint] }.compact.sort }
      if waiver_meta
        comparison["waivers"] = waiver_meta
      end
      classified_findings = cmp.classified_findings
    elsif waiver_meta && waiver_meta["count"] > 0
      cmp = Comparison.classify(findings: findings, baseline: nil, waivers: waivers.map(&:to_h), clock: clock, rename_map: rename_map)
      comparison = { "counts" => cmp.counts, "introduced" => cmp.introduced.map(&:fingerprint).sort, "existing" => cmp.existing.map(&:fingerprint).sort, "resolved" => cmp.resolved.map { |entry| entry.fetch("fingerprint") }.sort, "changed" => cmp.changed.map(&:fingerprint).sort, "moved" => cmp.moved.map(&:fingerprint).sort, "waived" => cmp.counts.fetch("waived", 0) == 0 ? [] : cmp.classified_findings.select { |item| item.state == "waived" }.map(&:fingerprint).sort, "orphaned_waivers" => cmp.orphaned_waivers.map { |w| w["fingerprint"] || w[:fingerprint] }.compact.sort, "waivers" => waiver_meta }
      classified_findings = cmp.classified_findings
    elsif waiver_meta
      comparison = nil
    end
  rescue Baseline::IncompatibleError => error
    incomplete = Verification::Policy.incomplete_result(
      analyzer_results: analyzer_results,
      findings: findings,
      operational_failures: [{ "code" => "failed", "message" => error.message }],
      code: "baseline_incompatible",
      message: error.message
    )
    enriched = GateResult.new(completion_status: incomplete.completion_status, gate: incomplete.gate, policy_status: incomplete.policy_status, findings: incomplete.findings, analyzer_results: incomplete.analyzer_results, operational_failures: incomplete.operational_failures, decision_reasons: incomplete.decision_reasons, baseline: { "loaded" => false, "path" => resolved_baseline_path, "compatible" => false }, comparison: nil)
    return Outcome.new(result: enriched, context: context, configuration: configuration, findings: findings.freeze)
  end

  unless git_context.nil?
    analyzer_results = attach_changed_coverage(analyzer_results: analyzer_results, git_context: git_context)
  end

  filtered_findings_for_policy = classified_findings
  changed_filter_active = !git_context.nil?
  if changed_filter_active
    filtered_findings_for_policy = classified_findings
  end

  result = Verification::Policy.evaluate(
    configuration: configuration,
    analyzer_results: analyzer_results,
    findings: filtered_findings_for_policy,
    comparison: comparison,
    baseline_meta: baseline_meta,
    git_context: git_context
  )

  git_payload = nil
  if git_context
    git_payload = build_git_payload(git_context, root)
  end

  rails_context_h = nil
  begin
    rails_ctx = RailsContext::Context.build(repository_root: root, git_context: git_context)
    rails_context_h = rails_ctx.to_h
  rescue StandardError
    rails_context_h = nil
  end

  if baseline_meta || comparison || git_payload || rails_context_h
    result = GateResult.new(
      completion_status: result.completion_status,
      gate: result.gate,
      policy_status: result.policy_status,
      findings: result.findings,
      analyzer_results: result.analyzer_results,
      operational_failures: result.operational_failures,
      decision_reasons: result.decision_reasons,
      baseline: baseline_meta,
      comparison: comparison,
      git: git_payload,
      rails_context: rails_context_h
    )
  elsif git_payload && result.complete?
    result = GateResult.new(
      completion_status: result.completion_status,
      gate: result.gate,
      policy_status: result.policy_status,
      findings: result.findings,
      analyzer_results: result.analyzer_results,
      operational_failures: result.operational_failures,
      decision_reasons: result.decision_reasons,
      git: git_payload,
      rails_context: rails_context_h
    )
  elsif git_payload
    result = GateResult.new(
      completion_status: result.completion_status,
      gate: result.gate,
      policy_status: result.policy_status,
      findings: result.findings,
      analyzer_results: result.analyzer_results,
      operational_failures: result.operational_failures,
      decision_reasons: result.decision_reasons,
      git: git_payload,
      rails_context: rails_context_h
    )
  elsif rails_context_h
    result = GateResult.new(
      completion_status: result.completion_status,
      gate: result.gate,
      policy_status: result.policy_status,
      findings: result.findings,
      analyzer_results: result.analyzer_results,
      operational_failures: result.operational_failures,
      decision_reasons: result.decision_reasons,
      rails_context: rails_context_h
    )
  end
  Outcome.new(result: result, context: context, configuration: configuration, findings: classified_findings.freeze)
rescue ConfigurationError => error
  Outcome.new(
    result: Verification::Policy.incomplete_result(
      operational_failures: [{ "code" => "configuration", "message" => canonical_error_message(error, root) }],
      code: "configuration",
      message: "Configuration is invalid; no analyzer evidence was evaluated."
    ),
    context: nil,
    configuration: nil,
    findings: [].freeze
  )
rescue ProcessRunner::DirectoryError, RailVerdict::Error => error
  Outcome.new(
    result: Verification::Policy.incomplete_result(
      operational_failures: [{ "code" => "failed", "message" => error.message }],
      code: "execution_failed",
      message: "Verification could not complete."
    ),
    context: nil,
    configuration: nil,
    findings: [].freeze
  )
rescue StandardError => error
  # Fail-closed for any unexpected error that escaped analyzer guard
  Outcome.new(
    result: Verification::Policy.incomplete_result(
      operational_failures: [{ "code" => "failed", "message" => RailVerdict::Analyzers::Shared.bounded_message("#{error.class}: #{error.message}") }],
      code: "execution_failed",
      message: "Verification could not complete due to an unexpected error."
    ),
    context: nil,
    configuration: nil,
    findings: [].freeze
  )
end

.execute_with_state_guard(repository_root:, **options) ⇒ Object

Wraps a canonical verification with a pre/post repository state guard. The canonical GateResult is unchanged; the guard only attaches the two observed repository states so receipt issuance can fail closed when the repository mutated while analyzers were running. The guard binds the EFFECTIVE verification inputs (resolved config plus effective baseline and waiver paths), not merely their default locations.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rail_verdict/check.rb', line 29

def self.execute_with_state_guard(repository_root:, **options)
  root = begin
    File.realpath(repository_root)
  rescue StandardError
    nil
  end
  input_paths = effective_input_paths(
    root: root,
    config_path: options.fetch(:config_path, ".railverdict.yml"),
    baseline_path_override: options[:baseline_path_override],
    waiver_path_override: options[:waiver_path_override]
  )
  pre = capture_guard_state(root, input_paths)
  outcome = execute(repository_root: repository_root, **options)
  post = capture_guard_state(root, input_paths)
  Outcome.new(
    result: outcome.result,
    context: outcome.context,
    configuration: outcome.configuration,
    findings: outcome.findings,
    repository_state_pre: pre,
    repository_state_post: post
  )
end

.registryObject



19
20
21
# File 'lib/rail_verdict/check.rb', line 19

def self.registry
  REGISTRY
end