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
readonly
Returns the value of attribute current_state.
-
#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.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/aidp/execute/work_loop_state.rb', line 23 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 (readonly)
Returns the value of attribute current_state.
21 22 23 |
# File 'lib/aidp/execute/work_loop_state.rb', line 21 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
133 134 135 136 137 |
# File 'lib/aidp/execute/work_loop_state.rb', line 133 def append_output(, type: :info) synchronize do @output_buffer << {message: , type: type, timestamp: Time.now} end end |
#cancel! ⇒ Object
65 66 67 68 69 70 |
# File 'lib/aidp/execute/work_loop_state.rb', line 65 def cancel! synchronize do raise StateError, "Cannot cancel from #{@current_state}" if completed? || error? @current_state = :cancelled end end |
#cancelled? ⇒ Boolean
38 |
# File 'lib/aidp/execute/work_loop_state.rb', line 38 def cancelled? = @current_state == :cancelled |
#complete! ⇒ Object
72 73 74 75 76 |
# File 'lib/aidp/execute/work_loop_state.rb', line 72 def complete! synchronize do @current_state = :completed end end |
#completed? ⇒ Boolean
39 |
# File 'lib/aidp/execute/work_loop_state.rb', line 39 def completed? = @current_state == :completed |
#config_reload_requested? ⇒ Boolean
124 125 126 127 128 129 130 |
# File 'lib/aidp/execute/work_loop_state.rb', line 124 def config_reload_requested? synchronize do requested = @config_reload_requested @config_reload_requested = false requested end end |
#dequeue_instructions ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/aidp/execute/work_loop_state.rb', line 95 def dequeue_instructions synchronize do instructions = @queued_instructions.dup @queued_instructions.clear instructions end end |
#drain_output ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/aidp/execute/work_loop_state.rb', line 139 def drain_output synchronize do output = @output_buffer.dup @output_buffer.clear output end end |
#enqueue_instruction(instruction) ⇒ Object
Instruction queueing
91 92 93 |
# File 'lib/aidp/execute/work_loop_state.rb', line 91 def enqueue_instruction(instruction) synchronize { @queued_instructions << instruction } end |
#error!(error) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/aidp/execute/work_loop_state.rb', line 78 def error!(error) synchronize do @current_state = :error @last_error = error end end |
#error? ⇒ Boolean
40 |
# File 'lib/aidp/execute/work_loop_state.rb', line 40 def error? = @current_state == :error |
#idle? ⇒ Boolean
Check current state
35 |
# File 'lib/aidp/execute/work_loop_state.rb', line 35 def idle? = @current_state == :idle |
#increment_iteration! ⇒ Object
Iteration management
86 87 88 |
# File 'lib/aidp/execute/work_loop_state.rb', line 86 def increment_iteration! synchronize { @iteration += 1 } end |
#pause! ⇒ Object
51 52 53 54 55 56 |
# File 'lib/aidp/execute/work_loop_state.rb', line 51 def pause! synchronize do raise StateError, "Cannot pause from #{@current_state}" unless running? @current_state = :paused end end |
#paused? ⇒ Boolean
37 |
# File 'lib/aidp/execute/work_loop_state.rb', line 37 def paused? = @current_state == :paused |
#pending_guard_updates ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/aidp/execute/work_loop_state.rb', line 112 def pending_guard_updates synchronize do updates = @guard_updates.dup @guard_updates.clear updates end end |
#queued_count ⇒ Object
103 104 105 |
# File 'lib/aidp/execute/work_loop_state.rb', line 103 def queued_count synchronize { @queued_instructions.size } end |
#request_config_reload ⇒ Object
120 121 122 |
# File 'lib/aidp/execute/work_loop_state.rb', line 120 def request_config_reload synchronize { @config_reload_requested = true } end |
#request_guard_update(key, value) ⇒ Object
Guard/config updates
108 109 110 |
# File 'lib/aidp/execute/work_loop_state.rb', line 108 def request_guard_update(key, value) synchronize { @guard_updates[key] = value } end |
#resume! ⇒ Object
58 59 60 61 62 63 |
# File 'lib/aidp/execute/work_loop_state.rb', line 58 def resume! synchronize do raise StateError, "Cannot resume from #{@current_state}" unless paused? @current_state = :running end end |
#running? ⇒ Boolean
36 |
# File 'lib/aidp/execute/work_loop_state.rb', line 36 def running? = @current_state == :running |
#start! ⇒ Object
State transitions (thread-safe)
43 44 45 46 47 48 49 |
# File 'lib/aidp/execute/work_loop_state.rb', line 43 def start! synchronize do raise StateError, "Cannot start from #{@current_state}" unless idle? @current_state = :running @iteration = 0 end end |
#summary ⇒ Object
Status summary
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/aidp/execute/work_loop_state.rb', line 148 def summary synchronize do { state: STATES[@current_state], iteration: @iteration, queued_instructions: @queued_instructions.size, has_error: !@last_error.nil? } end end |