Class: Aidp::Execute::WorkLoopState
- Inherits:
-
Object
- Object
- Aidp::Execute::WorkLoopState
- Includes:
- MonitorMixin
- Defined in:
- lib/aidp/execute/work_loop_state.rb
Overview
Thread-safe state container for async work loop execution Manages execution state, control signals, and queued modifications
Defined Under Namespace
Classes: StateError
Constant Summary collapse
- STATES =
{ idle: "IDLE", running: "RUNNING", paused: "PAUSED", cancelled: "CANCELLED", completed: "COMPLETED", error: "ERROR" }.freeze
Instance Attribute Summary collapse
-
#current_state ⇒ Object
Expose current_state for testability (use state transition methods in production).
-
#iteration ⇒ Object
readonly
Returns the value of attribute iteration.
-
#last_error ⇒ Object
readonly
Returns the value of attribute last_error.
-
#queued_instructions ⇒ Object
readonly
Returns the value of attribute queued_instructions.
Instance Method Summary collapse
-
#append_output(message, type: :info) ⇒ Object
Output buffering for streaming display.
- #cancel! ⇒ Object
- #cancelled? ⇒ Boolean
- #complete! ⇒ Object
- #completed? ⇒ Boolean
- #config_reload_requested? ⇒ Boolean
- #dequeue_instructions ⇒ Object
- #drain_output ⇒ Object
-
#enqueue_instruction(instruction) ⇒ Object
Instruction queueing.
- #error!(error) ⇒ Object
- #error? ⇒ Boolean
-
#idle? ⇒ Boolean
Check current state.
-
#increment_iteration! ⇒ Object
Iteration management.
-
#initialize ⇒ WorkLoopState
constructor
A new instance of WorkLoopState.
- #pause! ⇒ Object
- #paused? ⇒ Boolean
- #pending_guard_updates ⇒ Object
- #queued_count ⇒ Object
- #request_config_reload ⇒ Object
-
#request_guard_update(key, value) ⇒ Object
Guard/config updates.
- #resume! ⇒ Object
- #running? ⇒ Boolean
-
#start! ⇒ Object
State transitions (thread-safe).
-
#summary ⇒ Object
Status summary.
Constructor Details
#initialize ⇒ WorkLoopState
Returns a new instance of WorkLoopState.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/aidp/execute/work_loop_state.rb', line 26 def initialize super # Initialize MonitorMixin @current_state = :idle @iteration = 0 @queued_instructions = [] @guard_updates = {} @config_reload_requested = false @last_error = nil @output_buffer = [] end |
Instance Attribute Details
#current_state ⇒ Object
Expose current_state for testability (use state transition methods in production)
24 25 26 |
# File 'lib/aidp/execute/work_loop_state.rb', line 24 def current_state @current_state end |
#iteration ⇒ Object (readonly)
Returns the value of attribute iteration.
21 22 23 |
# File 'lib/aidp/execute/work_loop_state.rb', line 21 def iteration @iteration end |
#last_error ⇒ Object (readonly)
Returns the value of attribute last_error.
21 22 23 |
# File 'lib/aidp/execute/work_loop_state.rb', line 21 def last_error @last_error end |
#queued_instructions ⇒ Object (readonly)
Returns the value of attribute queued_instructions.
21 22 23 |
# File 'lib/aidp/execute/work_loop_state.rb', line 21 def queued_instructions @queued_instructions end |
Instance Method Details
#append_output(message, type: :info) ⇒ Object
Output buffering for streaming display
136 137 138 139 140 |
# File 'lib/aidp/execute/work_loop_state.rb', line 136 def append_output(, type: :info) synchronize do @output_buffer << {message: , type: type, timestamp: Time.now} end end |
#cancel! ⇒ Object
68 69 70 71 72 73 |
# File 'lib/aidp/execute/work_loop_state.rb', line 68 def cancel! synchronize do raise StateError, "Cannot cancel from #{@current_state}" if completed? || error? @current_state = :cancelled end end |
#cancelled? ⇒ Boolean
41 |
# File 'lib/aidp/execute/work_loop_state.rb', line 41 def cancelled? = @current_state == :cancelled |
#complete! ⇒ Object
75 76 77 78 79 |
# File 'lib/aidp/execute/work_loop_state.rb', line 75 def complete! synchronize do @current_state = :completed end end |
#completed? ⇒ Boolean
42 |
# File 'lib/aidp/execute/work_loop_state.rb', line 42 def completed? = @current_state == :completed |
#config_reload_requested? ⇒ Boolean
127 128 129 130 131 132 133 |
# File 'lib/aidp/execute/work_loop_state.rb', line 127 def config_reload_requested? synchronize do requested = @config_reload_requested @config_reload_requested = false requested end end |
#dequeue_instructions ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/aidp/execute/work_loop_state.rb', line 98 def dequeue_instructions synchronize do instructions = @queued_instructions.dup @queued_instructions.clear instructions end end |
#drain_output ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/aidp/execute/work_loop_state.rb', line 142 def drain_output synchronize do output = @output_buffer.dup @output_buffer.clear output end end |
#enqueue_instruction(instruction) ⇒ Object
Instruction queueing
94 95 96 |
# File 'lib/aidp/execute/work_loop_state.rb', line 94 def enqueue_instruction(instruction) synchronize { @queued_instructions << instruction } end |
#error!(error) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/aidp/execute/work_loop_state.rb', line 81 def error!(error) synchronize do @current_state = :error @last_error = error end end |
#error? ⇒ Boolean
43 |
# File 'lib/aidp/execute/work_loop_state.rb', line 43 def error? = @current_state == :error |
#idle? ⇒ Boolean
Check current state
38 |
# File 'lib/aidp/execute/work_loop_state.rb', line 38 def idle? = @current_state == :idle |
#increment_iteration! ⇒ Object
Iteration management
89 90 91 |
# File 'lib/aidp/execute/work_loop_state.rb', line 89 def increment_iteration! synchronize { @iteration += 1 } end |
#pause! ⇒ Object
54 55 56 57 58 59 |
# File 'lib/aidp/execute/work_loop_state.rb', line 54 def pause! synchronize do raise StateError, "Cannot pause from #{@current_state}" unless running? @current_state = :paused end end |
#paused? ⇒ Boolean
40 |
# File 'lib/aidp/execute/work_loop_state.rb', line 40 def paused? = @current_state == :paused |
#pending_guard_updates ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/aidp/execute/work_loop_state.rb', line 115 def pending_guard_updates synchronize do updates = @guard_updates.dup @guard_updates.clear updates end end |
#queued_count ⇒ Object
106 107 108 |
# File 'lib/aidp/execute/work_loop_state.rb', line 106 def queued_count synchronize { @queued_instructions.size } end |
#request_config_reload ⇒ Object
123 124 125 |
# File 'lib/aidp/execute/work_loop_state.rb', line 123 def request_config_reload synchronize { @config_reload_requested = true } end |
#request_guard_update(key, value) ⇒ Object
Guard/config updates
111 112 113 |
# File 'lib/aidp/execute/work_loop_state.rb', line 111 def request_guard_update(key, value) synchronize { @guard_updates[key] = value } end |
#resume! ⇒ Object
61 62 63 64 65 66 |
# File 'lib/aidp/execute/work_loop_state.rb', line 61 def resume! synchronize do raise StateError, "Cannot resume from #{@current_state}" unless paused? @current_state = :running end end |
#running? ⇒ Boolean
39 |
# File 'lib/aidp/execute/work_loop_state.rb', line 39 def running? = @current_state == :running |
#start! ⇒ Object
State transitions (thread-safe)
46 47 48 49 50 51 52 |
# File 'lib/aidp/execute/work_loop_state.rb', line 46 def start! synchronize do raise StateError, "Cannot start from #{@current_state}" unless idle? @current_state = :running @iteration = 0 end end |
#summary ⇒ Object
Status summary
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/aidp/execute/work_loop_state.rb', line 151 def summary synchronize do { state: STATES[@current_state], iteration: @iteration, queued_instructions: @queued_instructions.size, has_error: !@last_error.nil? } end end |