Class: Smith::Tool::ExecutionBatchState
- Inherits:
-
Object
- Object
- Smith::Tool::ExecutionBatchState
- Extended by:
- Dry::Initializer
- Defined in:
- lib/smith/tool/execution_batch_state.rb
Instance Method Summary collapse
- #claim_dispatch!(tool_call) ⇒ Object
- #claim_failure_request(tool_call) ⇒ Object
- #claim_unsettled_request ⇒ Object
- #complete_failure_notification!(tool_call) ⇒ Object
- #dispatch_claimed?(tool_call, claim) ⇒ Boolean
-
#initialize ⇒ ExecutionBatchState
constructor
A new instance of ExecutionBatchState.
- #mark_executed!(tool_call, claim:) ⇒ Object
- #mark_started!(tool_call, claim:) ⇒ Object
- #release_failure_notification!(tool_call, state) ⇒ Object
- #started?(tool_call) ⇒ Boolean
Constructor Details
#initialize ⇒ ExecutionBatchState
Returns a new instance of ExecutionBatchState.
15 16 17 18 19 20 21 22 23 |
# File 'lib/smith/tool/execution_batch_state.rb', line 15 def initialize(...) super @states = requests.each_key.to_h { [_1, :pending] }.compare_by_identity @dispatch_claims = {}.compare_by_identity @notification_attempted = {}.compare_by_identity @unsettled_tool_calls = requests.each_key.to_a.freeze @unsettled_index = 0 @mutex = Mutex.new end |
Instance Method Details
#claim_dispatch!(tool_call) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/smith/tool/execution_batch_state.rb', line 25 def claim_dispatch!(tool_call) @mutex.synchronize do unless @states[tool_call] == :pending raise ToolDispatchRejected, "admitted tool invocation was already claimed" end claim = Object.new.freeze @dispatch_claims[tool_call] = claim @states[tool_call] = :claimed claim end end |
#claim_failure_request(tool_call) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/smith/tool/execution_batch_state.rb', line 50 def claim_failure_request(tool_call) @mutex.synchronize do state = @states[tool_call] return if state.nil? || SETTLED_STATES.include?(state) || @notification_attempted.key?(tool_call) claim_notification!(tool_call) [requests.fetch(tool_call), state] end end |
#claim_unsettled_request ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/smith/tool/execution_batch_state.rb', line 60 def claim_unsettled_request @mutex.synchronize do while @unsettled_index < @unsettled_tool_calls.length tool_call = next_unsettled_tool_call state = @states[tool_call] next if SETTLED_STATES.include?(state) || @notification_attempted.key?(tool_call) claim_notification!(tool_call) return [tool_call, requests.fetch(tool_call), state] end nil end end |
#complete_failure_notification!(tool_call) ⇒ Object
74 75 76 77 78 |
# File 'lib/smith/tool/execution_batch_state.rb', line 74 def complete_failure_notification!(tool_call) @mutex.synchronize do @states[tool_call] = :failure_notified if @states[tool_call] == :failure_notifying end end |
#dispatch_claimed?(tool_call, claim) ⇒ Boolean
38 39 40 41 42 |
# File 'lib/smith/tool/execution_batch_state.rb', line 38 def dispatch_claimed?(tool_call, claim) @mutex.synchronize do @dispatch_claims[tool_call].equal?(claim) && %i[claimed started].include?(@states[tool_call]) end end |
#mark_executed!(tool_call, claim:) ⇒ Object
46 |
# File 'lib/smith/tool/execution_batch_state.rb', line 46 def mark_executed!(tool_call, claim:) = transition_dispatch!(tool_call, claim, from: :started, to: :executed) |
#mark_started!(tool_call, claim:) ⇒ Object
44 |
# File 'lib/smith/tool/execution_batch_state.rb', line 44 def mark_started!(tool_call, claim:) = transition_dispatch!(tool_call, claim, from: :claimed, to: :started) |
#release_failure_notification!(tool_call, state) ⇒ Object
80 81 82 83 84 |
# File 'lib/smith/tool/execution_batch_state.rb', line 80 def release_failure_notification!(tool_call, state) @mutex.synchronize do @states[tool_call] = state if @states[tool_call] == :failure_notifying end end |
#started?(tool_call) ⇒ Boolean
48 |
# File 'lib/smith/tool/execution_batch_state.rb', line 48 def started?(tool_call) = @mutex.synchronize { %i[started executed].include?(@states[tool_call]) } |