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.



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

#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



133
134
135
136
137
# File 'lib/aidp/execute/work_loop_state.rb', line 133

def append_output(message, type: :info)
  synchronize do
    @output_buffer << {message: 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

Returns:

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

Returns:

  • (Boolean)


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

def completed? = @current_state == :completed

#config_reload_requested?Boolean

Returns:

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



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_outputObject



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

Returns:

  • (Boolean)


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

def error? = @current_state == :error

#idle?Boolean

Check current state

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

def paused? = @current_state == :paused

#pending_guard_updatesObject



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_countObject



103
104
105
# File 'lib/aidp/execute/work_loop_state.rb', line 103

def queued_count
  synchronize { @queued_instructions.size }
end

#request_config_reloadObject



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

Returns:

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

#summaryObject

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