Class: Aidp::Execute::WorkLoopState

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeWorkLoopState

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_stateObject

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

#iterationObject (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_errorObject (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_instructionsObject (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(message, type: :info)
  synchronize do
    @output_buffer << {message: 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

Returns:

  • (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

Returns:

  • (Boolean)


42
# File 'lib/aidp/execute/work_loop_state.rb', line 42

def completed? = @current_state == :completed

#config_reload_requested?Boolean

Returns:

  • (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_instructionsObject



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_outputObject



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

Returns:

  • (Boolean)


43
# File 'lib/aidp/execute/work_loop_state.rb', line 43

def error? = @current_state == :error

#idle?Boolean

Check current state

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


40
# File 'lib/aidp/execute/work_loop_state.rb', line 40

def paused? = @current_state == :paused

#pending_guard_updatesObject



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_countObject



106
107
108
# File 'lib/aidp/execute/work_loop_state.rb', line 106

def queued_count
  synchronize { @queued_instructions.size }
end

#request_config_reloadObject



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

Returns:

  • (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

#summaryObject

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