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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ui_components = {}) ⇒ WorkflowController



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 30

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 Attribute Details

#control_threadObject

Expose for testability



20
21
22
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 20

def control_thread
  @control_thread
end

#pause_timeObject

Expose for testability



20
21
22
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 20

def pause_time
  @pause_time
end

Instance Method Details

#can_cancel?Boolean



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

def can_cancel?
  running? || paused?
end

#can_complete?Boolean



156
157
158
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 156

def can_complete?
  running?
end

#can_pause?Boolean



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

def can_pause?
  running?
end

#can_resume?Boolean



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

def can_resume?
  paused?
end

#can_stop?Boolean



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

def can_stop?
  running? || paused?
end

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



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

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



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

def cancelled?
  @current_state == :cancelled
end

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



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 102

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



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

def completed?
  @current_state == :completed
end

#current_stateObject



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

def current_state
  @control_mutex.synchronize { @current_state }
end

#display_workflow_statusObject



176
177
178
179
180
181
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 176

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

#get_workflow_statusObject



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

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



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

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



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

def paused?
  @current_state == :paused
end

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



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

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



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

def running?
  @current_state == :running
end

#set_state_for_testing(state) ⇒ Object

Testing helper method to set state



197
198
199
200
201
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 197

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

#start_control_interfaceObject



183
184
185
186
187
188
189
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 183

def start_control_interface
  return if @control_thread&.alive?

  @control_thread = Thread.new do
    control_interface_loop
  end
end

#stop_control_interfaceObject



191
192
193
194
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 191

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

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



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/aidp/harness/ui/workflow_controller.rb', line 89

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



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

def stopped?
  @current_state == :stopped
end