Module: McpDiff::Core::Classify

Defined in:
lib/mcp_diff/core/classify.rb

Overview

Apply the ruleset to a set of changes, then compute the exit code. Port of core/src/classify.ts.

Constant Summary collapse

CLASS_RANK =
{ "breaking" => 0, "behavior" => 1, "compat" => 2, "info" => 3 }.freeze

Class Method Summary collapse

Class Method Details

.classify(changes, rules = Rules::ALL) ⇒ Object

Sorted worst-first, then by rule, target, message (deterministic order).



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mcp_diff/core/classify.rb', line 15

def classify(changes, rules = Rules::ALL)
  findings = []
  changes.each do |change|
    rules.each do |rule|
      matched = rule.match(change)
      next if matched.nil?

      findings.concat(matched.is_a?(Array) ? matched : [matched])
    end
  end
  findings.sort_by { |f| [CLASS_RANK[f["class"]], f["ruleId"], f["target"], f["message"]] }
end

.compute_exit_code(findings, strict = "breaking") ⇒ Object

Exit-code contract (D4): 0 clean/compat/info, 1 breaking, 2 behavior-only. strict: "behavior" promotes behavior findings to exit 1.



30
31
32
33
34
35
36
37
# File 'lib/mcp_diff/core/classify.rb', line 30

def compute_exit_code(findings, strict = "breaking")
  has_breaking = findings.any? { |f| f["class"] == "breaking" }
  has_behavior = findings.any? { |f| f["class"] == "behavior" }
  return 1 if has_breaking || (strict == "behavior" && has_behavior)
  return 2 if has_behavior

  0
end