Class: Aidp::Harness::UI::WorkflowController

Inherits:
Base
  • Object
show all
Defined in:
lib/aidp/harness/ui/workflow_controller.rb

Overview

Workflow control interface for pause/resume/cancel/stop

Defined Under Namespace

Classes: ControlError, InvalidStateError, WorkflowError

Constant Summary collapse

WORKFLOW_STATES =
{
  running: "Running",
  paused: "Paused",
  cancelled: "Cancelled",
  stopped: "Stopped",
  completed: "Completed"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(ui_components = {}) ⇒ WorkflowController

Returns a new instance of WorkflowController.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 27

def initialize(ui_components = {})
  super()
  @status_manager = ui_components[:status_manager] || StatusManager.new
  @frame_manager = ui_components[:frame_manager] || FrameManager.new
  @formatter = ui_components[:formatter] || WorkflowControllerFormatter.new
  @output = ui_components[:output]

  @current_state = :running
  @state_history = []
  @pause_time = nil
  @control_thread = nil
  @control_mutex = Mutex.new
end

Instance Method Details

#can_cancel?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 145

def can_cancel?
  running? || paused?
end

#can_complete?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 153

def can_complete?
  running?
end

#can_pause?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 137

def can_pause?
  running?
end

#can_resume?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 141

def can_resume?
  paused?
end

#can_stop?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 149

def can_stop?
  running? || paused?
end

#cancel_workflow(reason = "User requested cancellation") ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 71

def cancel_workflow(reason = "User requested cancellation")
  validate_state_transition(:cancel)

  @control_mutex.synchronize do
    @current_state = :cancelled
    record_state_change(:cancelled, reason)
    @status_manager.show_warning_status("Workflow cancelled: #{reason}")
    cleanup_workflow_resources
  end
rescue InvalidStateError => e
  raise e
rescue => e
  raise ControlError, "Failed to cancel workflow: #{e.message}"
end

#cancelled?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 125

def cancelled?
  @current_state == :cancelled
end

#complete_workflow(reason = "Workflow completed successfully") ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 99

def complete_workflow(reason = "Workflow completed successfully")
  validate_state_transition(:complete)

  @control_mutex.synchronize do
    @current_state = :completed
    record_state_change(:completed, reason)
    @status_manager.show_success_status("Workflow completed: #{reason}")
  end
rescue InvalidStateError => e
  raise e
rescue => e
  raise ControlError, "Failed to complete workflow: #{e.message}"
end

#completed?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 133

def completed?
  @current_state == :completed
end

#current_stateObject



113
114
115
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 113

def current_state
  @control_mutex.synchronize { @current_state }
end

#display_workflow_statusObject



173
174
175
176
177
178
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 173

def display_workflow_status
  @frame_manager.section("Workflow Status") do
    status = get_workflow_status
    display_status_info(status)
  end
end

#get_workflow_statusObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 157

def get_workflow_status
  @control_mutex.synchronize do
    {
      state: @current_state,
      state_name: WORKFLOW_STATES[@current_state],
      pause_time: @pause_time,
      state_history: @state_history.dup,
      can_pause: can_pause?,
      can_resume: can_resume?,
      can_cancel: can_cancel?,
      can_stop: can_stop?,
      can_complete: can_complete?
    }
  end
end

#pause_workflow(reason = "User requested pause") ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 41

def pause_workflow(reason = "User requested pause")
  validate_state_transition(:pause)

  @control_mutex.synchronize do
    @current_state = :paused
    @pause_time = Time.now
    record_state_change(:paused, reason)
    @status_manager.show_warning_status("Workflow paused: #{reason}")
  end
rescue InvalidStateError => e
  raise e
rescue => e
  raise ControlError, "Failed to pause workflow: #{e.message}"
end

#paused?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 121

def paused?
  @current_state == :paused
end

#resume_workflow(reason = "User requested resume") ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 56

def resume_workflow(reason = "User requested resume")
  validate_state_transition(:resume)

  @control_mutex.synchronize do
    @current_state = :running
    pause_duration = calculate_pause_duration
    record_state_change(:running, reason, pause_duration)
    @status_manager.show_success_status("Workflow resumed: #{reason}")
  end
rescue InvalidStateError => e
  raise e
rescue => e
  raise ControlError, "Failed to resume workflow: #{e.message}"
end

#running?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 117

def running?
  @current_state == :running
end

#set_state_for_testing(state) ⇒ Object

Testing helper method to set state



194
195
196
197
198
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 194

def set_state_for_testing(state)
  @control_mutex.synchronize do
    @current_state = state
  end
end

#start_control_interfaceObject



180
181
182
183
184
185
186
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 180

def start_control_interface
  return if @control_thread&.alive?

  @control_thread = Thread.new do
    control_interface_loop
  end
end

#stop_control_interfaceObject



188
189
190
191
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 188

def stop_control_interface
  @control_thread&.kill
  @control_thread = nil
end

#stop_workflow(reason = "User requested stop") ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 86

def stop_workflow(reason = "User requested stop")
  validate_state_transition(:stop)

  @control_mutex.synchronize do
    @current_state = :stopped
    record_state_change(:stopped, reason)
    @status_manager.show_error_status("Workflow stopped: #{reason}")
    cleanup_workflow_resources
  end
rescue => e
  raise ControlError, "Failed to stop workflow: #{e.message}"
end

#stopped?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 129

def stopped?
  @current_state == :stopped
end