Class: KairosMcp::StateCommit::CommitService

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

Overview

CommitService: Orchestrates the state commit process

Responsibilities:

  • Execute explicit and auto commits

  • Check auto-commit conditions

  • Record commits to blockchain

Instance Method Summary collapse

Constructor Details

#initialize(config: nil, user_context: nil) ⇒ CommitService

Returns a new instance of CommitService.



18
19
20
21
22
23
24
25
26
27
# File 'lib/kairos_mcp/state_commit/commit_service.rb', line 18

def initialize(config: nil, user_context: nil)
  @config = config || load_config
  @user_context = user_context
  @manifest_builder = ManifestBuilder.new(user_context: user_context)
  @snapshot_manager = SnapshotManager.new(
    snapshot_dir: @config.dig('state_commit', 'snapshot_dir'),
    max_snapshots: @config.dig('state_commit', 'max_snapshots')
  )
  @diff_calculator = DiffCalculator.new
end

Instance Method Details

#auto_commit(trigger:) ⇒ Hash

Execute an auto commit (system-initiated)

Parameters:

  • trigger (String)

    What triggered the auto-commit

Returns:

  • (Hash)

    Result with success status and commit info



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kairos_mcp/state_commit/commit_service.rb', line 50

def auto_commit(trigger:)
  # Generate auto-commit reason
  summary = PendingChanges.summary
  reason = generate_auto_reason(trigger, summary)

  perform_commit(
    reason: reason,
    commit_type: 'auto',
    actor: 'system',
    force: false
  )
end

#check_and_auto_commitHash?

Check auto-commit and execute if needed

Returns:

  • (Hash, nil)

    Commit result if committed, nil otherwise



92
93
94
95
96
97
# File 'lib/kairos_mcp/state_commit/commit_service.rb', line 92

def check_and_auto_commit
  result = should_auto_commit?
  return nil unless result[:should_commit]

  auto_commit(trigger: result[:trigger])
end

#enabled?Boolean

Check if state commit is enabled

Returns:

  • (Boolean)

    True if enabled



151
152
153
# File 'lib/kairos_mcp/state_commit/commit_service.rb', line 151

def enabled?
  @config.dig('state_commit', 'enabled') != false
end

#explicit_commit(reason:, actor: 'human', force: false) ⇒ Hash

Execute an explicit commit (user-initiated)

Parameters:

  • reason (String)

    Reason for the commit (required)

  • actor (String) (defaults to: 'human')

    Who is committing (human/ai)

  • force (Boolean) (defaults to: false)

    Force commit even if no changes

Returns:

  • (Hash)

    Result with success status and commit info



35
36
37
38
39
40
41
42
43
44
# File 'lib/kairos_mcp/state_commit/commit_service.rb', line 35

def explicit_commit(reason:, actor: 'human', force: false)
  return { success: false, error: "Reason is required for explicit commit" } if reason.nil? || reason.empty?

  perform_commit(
    reason: reason,
    commit_type: 'explicit',
    actor: actor,
    force: force
  )
end

#history(limit: 20) ⇒ Array<Hash>

Get commit history

Parameters:

  • limit (Integer) (defaults to: 20)

    Maximum number of commits to return

Returns:

  • (Array<Hash>)

    List of commit metadata



144
145
146
# File 'lib/kairos_mcp/state_commit/commit_service.rb', line 144

def history(limit: 20)
  @snapshot_manager.list_snapshots(limit: limit)
end

#should_auto_commit?Hash

Check if auto-commit should be triggered

Returns:

  • (Hash)

    Result with should_commit flag and trigger



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/kairos_mcp/state_commit/commit_service.rb', line 66

def should_auto_commit?
  return { should_commit: false, reason: "State commit disabled" } unless enabled?

  auto_config = @config.dig('state_commit', 'auto_commit')
  return { should_commit: false, reason: "Auto-commit disabled" } unless auto_config&.dig('enabled')

  # Check trigger conditions (OR)
  trigger_result = PendingChanges.check_trigger_conditions(auto_config)
  return { should_commit: false, reason: "No trigger conditions met" } unless trigger_result[:should_commit]

  # Check hash comparison (AND condition)
  if auto_config.dig('skip_if_no_changes') != false
    current_manifest = @manifest_builder.build_full_manifest
    last_snapshot = @snapshot_manager.get_last_snapshot

    if last_snapshot && !@diff_calculator.has_changes?(last_snapshot, current_manifest)
      return { should_commit: false, reason: "No actual changes detected" }
    end
  end

  { should_commit: true, trigger: trigger_result[:trigger] }
end

#statusHash

Get current status

Returns:

  • (Hash)

    Current state commit status



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

def status
  last_snapshot = @snapshot_manager.get_last_snapshot
  current_manifest = @manifest_builder.build_full_manifest
  pending_summary = PendingChanges.summary
  auto_config = @config.dig('state_commit', 'auto_commit') || {}

  has_changes = if last_snapshot
                  @diff_calculator.has_changes?(last_snapshot, current_manifest)
                else
                  true
                end

  trigger_result = PendingChanges.check_trigger_conditions(auto_config)

  {
    enabled: enabled?,
    last_commit: last_snapshot ? {
      hash: last_snapshot['snapshot_hash'],
      timestamp: last_snapshot['created_at'],
      reason: last_snapshot['reason'],
      commit_type: last_snapshot['commit_type']
    } : nil,
    current_hash: current_manifest[:combined_hash],
    has_changes: has_changes,
    pending_changes: pending_summary,
    auto_commit: {
      enabled: auto_config.dig('enabled') || false,
      trigger_met: trigger_result[:should_commit],
      trigger: trigger_result[:trigger],
      thresholds: {
        l1_changes: auto_config.dig('change_threshold', 'l1_changes') || 5,
        total_changes: auto_config.dig('change_threshold', 'total_changes') || 10
      }
    },
    snapshot_count: @snapshot_manager.count
  }
end