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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/aidp/auto_update/coordinator.rb', line 9

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.



7
8
9
# File 'lib/aidp/auto_update/coordinator.rb', line 7

def checkpoint_store
  @checkpoint_store
end

#failure_trackerObject (readonly)

Returns the value of attribute failure_tracker.



7
8
9
# File 'lib/aidp/auto_update/coordinator.rb', line 7

def failure_tracker
  @failure_tracker
end

#policyObject (readonly)

Returns the value of attribute policy.



7
8
9
# File 'lib/aidp/auto_update/coordinator.rb', line 7

def policy
  @policy
end

#update_loggerObject (readonly)

Returns the value of attribute update_logger.



7
8
9
# File 'lib/aidp/auto_update/coordinator.rb', line 7

def update_logger
  @update_logger
end

#version_detectorObject (readonly)

Returns the value of attribute version_detector.



7
8
9
# File 'lib/aidp/auto_update/coordinator.rb', line 7

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:



34
35
36
37
# File 'lib/aidp/auto_update/coordinator.rb', line 34

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:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aidp/auto_update/coordinator.rb', line 41

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

#hot_reload_available?Boolean

Check if hot reloading is available

Returns:

  • (Boolean)


249
250
251
# File 'lib/aidp/auto_update/coordinator.rb', line 249

def hot_reload_available?
  Aidp::Loader.setup? && Aidp::Loader.reloading?
end

#hot_reload_update(update_check = nil) ⇒ Boolean

Perform hot code reload without restarting the process

This method performs a git pull and then uses Zeitwerk to reload all Ruby classes. Unlike initiate_update (which exits with code 75), this method keeps the process running.

Parameters:

  • update_check (UpdateCheck) (defaults to: nil)

    Update check result

Returns:

  • (Boolean)

    Whether reload was successful

Raises:

  • (UpdateError)

    If updates are disabled or reload fails



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

def hot_reload_update(update_check = nil)
  raise UpdateError, "Updates disabled by configuration" unless @policy.enabled

  # Verify Zeitwerk loader is set up for reloading
  unless Aidp::Loader.setup? && Aidp::Loader.reloading?
    Aidp.log_warn("auto_update_coordinator", "hot_reload_not_available",
      reason: "Zeitwerk loader not configured for reloading")
    raise UpdateError, "Hot reloading not available. Loader must be configured with enable_reloading: true"
  end

  update_check ||= check_for_update
  return false unless update_check.should_update?

  from_version = update_check.current_version
  to_version = update_check.available_version

  Aidp.log_info("auto_update_coordinator", "hot_reload_starting",
    from_version: from_version,
    to_version: to_version)

  # Perform git pull
  unless perform_git_pull
    raise UpdateError, "Git pull failed"
  end

  # Reload all classes using Zeitwerk
  unless Aidp::Loader.reload!
    @failure_tracker.record_failure
    @update_logger.log_failure("Zeitwerk reload failed")
    raise UpdateError, "Zeitwerk reload failed"
  end

  # Log success
  @update_logger.log_success(
    from_version: from_version,
    to_version: to_version
  )
  @failure_tracker.reset_on_success

  Aidp.log_info("auto_update_coordinator", "hot_reload_complete",
    from_version: from_version,
    to_version: to_version)

  true
rescue UpdateError
  raise
rescue => e
  @failure_tracker.record_failure
  @update_logger.log_failure("Hot reload failed: #{e.message}")
  Aidp.log_error("auto_update_coordinator", "hot_reload_failed",
    error: e.message)
  raise UpdateError, "Hot reload 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:



58
59
60
61
62
63
64
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
# File 'lib/aidp/auto_update/coordinator.rb', line 58

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



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

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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/aidp/auto_update/coordinator.rb', line 167

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