Class: Aidp::AutoUpdate::Coordinator

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/auto_update/coordinator.rb

Overview

Facade for orchestrating the complete auto-update workflow

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy:, version_detector: nil, checkpoint_store: nil, update_logger: nil, failure_tracker: nil, project_dir: Dir.pwd) ⇒ Coordinator

Returns a new instance of Coordinator.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aidp/auto_update/coordinator.rb', line 16

def initialize(
  policy:,
  version_detector: nil,
  checkpoint_store: nil,
  update_logger: nil,
  failure_tracker: nil,
  project_dir: Dir.pwd
)
  @policy = policy
  @project_dir = project_dir

  # Use provided instances or create defaults
  @version_detector = version_detector || VersionDetector.new(policy: policy)
  @checkpoint_store = checkpoint_store || CheckpointStore.new(project_dir: project_dir)
  @update_logger = update_logger || UpdateLogger.new(project_dir: project_dir)
  @failure_tracker = failure_tracker || FailureTracker.new(
    project_dir: project_dir,
    max_failures: policy.max_consecutive_failures
  )
end

Instance Attribute Details

#checkpoint_storeObject (readonly)

Returns the value of attribute checkpoint_store.



14
15
16
# File 'lib/aidp/auto_update/coordinator.rb', line 14

def checkpoint_store
  @checkpoint_store
end

#failure_trackerObject (readonly)

Returns the value of attribute failure_tracker.



14
15
16
# File 'lib/aidp/auto_update/coordinator.rb', line 14

def failure_tracker
  @failure_tracker
end

#policyObject (readonly)

Returns the value of attribute policy.



14
15
16
# File 'lib/aidp/auto_update/coordinator.rb', line 14

def policy
  @policy
end

#update_loggerObject (readonly)

Returns the value of attribute update_logger.



14
15
16
# File 'lib/aidp/auto_update/coordinator.rb', line 14

def update_logger
  @update_logger
end

#version_detectorObject (readonly)

Returns the value of attribute version_detector.



14
15
16
# File 'lib/aidp/auto_update/coordinator.rb', line 14

def version_detector
  @version_detector
end

Class Method Details

.from_config(config, project_dir: Dir.pwd) ⇒ Coordinator

Create coordinator from configuration

Parameters:

  • config (Hash)

    Auto-update configuration from aidp.yml

  • project_dir (String) (defaults to: Dir.pwd)

    Project root directory

Returns:



41
42
43
44
# File 'lib/aidp/auto_update/coordinator.rb', line 41

def self.from_config(config, project_dir: Dir.pwd)
  policy = UpdatePolicy.from_config(config)
  new(policy: policy, project_dir: project_dir)
end

Instance Method Details

#check_for_updateUpdateCheck

Check if update is available and allowed

Returns:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aidp/auto_update/coordinator.rb', line 48

def check_for_update
  return UpdateCheck.unavailable unless @policy.enabled

  update_check = @version_detector.check_for_update
  @update_logger.log_check(update_check)
  update_check
rescue => e
  Aidp.log_error("auto_update_coordinator", "check_failed",
    error: e.message)
  UpdateCheck.failed(e.message)
end

#initiate_update(current_state) ⇒ void

This method returns an undefined value.

Initiate update process (checkpoint + exit with code 75)

Parameters:

  • current_state (Hash)

    Current application state (from Watch::Runner)

Raises:



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
# File 'lib/aidp/auto_update/coordinator.rb', line 65

def initiate_update(current_state)
  raise UpdateError, "Updates disabled by configuration" unless @policy.enabled

  # Check for restart loops
  if @failure_tracker.too_many_failures?
    @update_logger.log_restart_loop(@failure_tracker.failure_count)
    raise UpdateLoopError, "Too many consecutive update failures (#{@failure_tracker.failure_count}/#{@policy.max_consecutive_failures})"
  end

  # Verify supervisor is configured
  unless @policy.supervised?
    raise UpdateError, "No supervisor configured. Set auto_update.supervisor in aidp.yml"
  end

  # Get latest version to record in checkpoint
  update_check = check_for_update

  unless update_check.should_update?
    Aidp.log_info("auto_update_coordinator", "no_update_needed",
      reason: update_check.policy_reason)
    return
  end

  # Create checkpoint from current state
  checkpoint = build_checkpoint(current_state, update_check.available_version)

  # Save checkpoint
  unless @checkpoint_store.save_checkpoint(checkpoint)
    raise UpdateError, "Failed to save checkpoint"
  end

  # Log update initiation
  @update_logger.log_update_initiated(checkpoint, target_version: update_check.available_version)

  Aidp.log_info("auto_update_coordinator", "exiting_for_update",
    from_version: update_check.current_version,
    to_version: update_check.available_version,
    checkpoint_id: checkpoint.checkpoint_id)

  # Exit with special code 75 to signal supervisor to update
  exit(75)
rescue UpdateError, UpdateLoopError
  # Re-raise domain errors
  raise
rescue => e
  @failure_tracker.record_failure
  @update_logger.log_failure(e.message)
  Aidp.log_error("auto_update_coordinator", "initiate_failed",
    error: e.message)
  raise UpdateError, "Update initiation failed: #{e.message}"
end

#restore_from_checkpointCheckpoint?

Restore from checkpoint after update

Returns:

  • (Checkpoint, nil)

    Restored checkpoint or nil



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
# File 'lib/aidp/auto_update/coordinator.rb', line 119

def restore_from_checkpoint
  checkpoint = @checkpoint_store.latest_checkpoint
  return nil unless checkpoint

  Aidp.log_info("auto_update_coordinator", "restoring_checkpoint",
    id: checkpoint.checkpoint_id,
    created_at: checkpoint.created_at.iso8601)

  # Validate checkpoint
  unless checkpoint.valid?
    Aidp.log_error("auto_update_coordinator", "invalid_checkpoint",
      id: checkpoint.checkpoint_id,
      reason: "Checksum validation failed")
    @failure_tracker.record_failure
    @update_logger.log_failure("Invalid checkpoint checksum", checkpoint_id: checkpoint.checkpoint_id)
    return nil
  end

  # Check version compatibility
  unless checkpoint.compatible_version?
    Aidp.log_warn("auto_update_coordinator", "incompatible_version",
      checkpoint_version: checkpoint.aidp_version,
      current_version: Aidp::VERSION)
    @failure_tracker.record_failure
    @update_logger.log_failure(
      "Incompatible version: checkpoint from #{checkpoint.aidp_version}, current #{Aidp::VERSION}",
      checkpoint_id: checkpoint.checkpoint_id
    )
    return nil
  end

  # Log successful restoration
  @update_logger.log_restore(checkpoint)
  @update_logger.log_success(
    from_version: checkpoint.aidp_version,
    to_version: Aidp::VERSION
  )

  # Reset failure tracker on success
  @failure_tracker.reset_on_success

  # Delete checkpoint after successful restore
  @checkpoint_store.delete_checkpoint(checkpoint.checkpoint_id)

  checkpoint
rescue => e
  @failure_tracker.record_failure
  @update_logger.log_failure("Checkpoint restore failed: #{e.message}")
  Aidp.log_error("auto_update_coordinator", "restore_failed",
    error: e.message)
  nil
end

#statusHash

Get status summary for CLI display

Returns:

  • (Hash)

    Status information



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/aidp/auto_update/coordinator.rb', line 174

def status
  update_check = check_for_update

  {
    enabled: @policy.enabled,
    policy: @policy.policy,
    supervisor: @policy.supervisor,
    current_version: Aidp::VERSION,
    available_version: update_check.available_version,
    update_available: update_check.update_available,
    update_allowed: update_check.update_allowed,
    policy_reason: update_check.policy_reason,
    failure_tracker: @failure_tracker.status,
    recent_updates: @update_logger.recent_entries(limit: 5)
  }
end