Class: KairosMcp::StateCommit::DiffCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/kairos_mcp/state_commit/diff_calculator.rb

Overview

DiffCalculator: Calculates differences between snapshots

Detects:

  • Added/modified/deleted items in each layer

  • Promotions (L2→L1, L1→L0)

  • Demotions (L1→L2, L0→L1)

Instance Method Summary collapse

Instance Method Details

#calculate(prev_snapshot, current_manifest) ⇒ Hash

Calculate diff between previous snapshot and current manifest

Parameters:

  • prev_snapshot (Hash, nil)

    Previous snapshot (nil for first commit)

  • current_manifest (Hash)

    Current manifest from ManifestBuilder

Returns:

  • (Hash)

    Diff result with changes per layer



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kairos_mcp/state_commit/diff_calculator.rb', line 20

def calculate(prev_snapshot, current_manifest)
  if prev_snapshot.nil?
    return initial_diff(current_manifest)
  end

  prev_layers = normalize_layers(prev_snapshot['layers'] || prev_snapshot[:layers])
  curr_layers = current_manifest[:layers]

  {
    L0: calculate_layer_diff(:L0, prev_layers, curr_layers),
    L1: calculate_layer_diff(:L1, prev_layers, curr_layers),
    L2: calculate_layer_diff(:L2, prev_layers, curr_layers),
    promotions: detect_promotions(prev_layers, curr_layers),
    demotions: detect_demotions(prev_layers, curr_layers),
    has_changes: has_changes?(prev_snapshot, current_manifest)
  }
end

#describe(diff) ⇒ String

Generate human-readable change description

Parameters:

  • diff (Hash)

    Diff result from calculate

Returns:

  • (String)

    Description of changes



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
# File 'lib/kairos_mcp/state_commit/diff_calculator.rb', line 74

def describe(diff)
  parts = []

  if diff[:L0][:changed]
    parts << "L0: changed"
  end

  l1_changes = []
  l1_changes << "+#{diff[:L1][:added].size}" if diff[:L1][:added].any?
  l1_changes << "~#{diff[:L1][:modified].size}" if diff[:L1][:modified].any?
  l1_changes << "-#{diff[:L1][:deleted].size}" if diff[:L1][:deleted].any?
  parts << "L1: #{l1_changes.join(', ')}" if l1_changes.any?

  l2_changes = []
  l2_changes << "+#{diff[:L2][:added].size} sessions" if diff[:L2][:added].any?
  l2_changes << "-#{diff[:L2][:deleted].size} sessions" if diff[:L2][:deleted].any?
  parts << "L2: #{l2_changes.join(', ')}" if l2_changes.any?

  if diff[:promotions].any?
    parts << "Promotions: #{diff[:promotions].size}"
  end

  if diff[:demotions].any?
    parts << "Demotions: #{diff[:demotions].size}"
  end

  parts.empty? ? "No changes" : parts.join("; ")
end

#has_changes?(prev_snapshot, current_manifest) ⇒ Boolean

Check if there are any changes between snapshot and manifest

Parameters:

  • prev_snapshot (Hash, nil)

    Previous snapshot

  • current_manifest (Hash)

    Current manifest

Returns:

  • (Boolean)

    True if there are changes



43
44
45
46
47
48
49
50
# File 'lib/kairos_mcp/state_commit/diff_calculator.rb', line 43

def has_changes?(prev_snapshot, current_manifest)
  return true if prev_snapshot.nil?

  prev_hash = prev_snapshot['snapshot_hash'] || prev_snapshot[:snapshot_hash]
  curr_hash = current_manifest[:combined_hash]

  prev_hash != curr_hash
end

#summarize(diff) ⇒ Hash

Generate a summary of changes

Parameters:

  • diff (Hash)

    Diff result from calculate

Returns:

  • (Hash)

    Summary with counts



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kairos_mcp/state_commit/diff_calculator.rb', line 56

def summarize(diff)
  {
    L0_changed: diff[:L0][:changed],
    L1_added: diff[:L1][:added].size,
    L1_modified: diff[:L1][:modified].size,
    L1_deleted: diff[:L1][:deleted].size,
    L2_sessions_added: diff[:L2][:added].size,
    L2_sessions_deleted: diff[:L2][:deleted].size,
    promotions: diff[:promotions].size,
    demotions: diff[:demotions].size,
    total_changes: count_total_changes(diff)
  }
end