Class: SkillBench::Agent::ReactAgent::LoopRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/skill_bench/agent/react_agent/loop_runner.rb

Overview

Executes the ReAct loop iterations until completion or max iterations.

Class Method Summary collapse

Class Method Details

.add_usage(total, usage) ⇒ Hash

Adds a single step's usage onto a running total.

Parameters:

  • total (Hash)

    The running usage total.

  • usage (Hash, nil)

    A step's usage hash (may be nil or empty).

Returns:

  • (Hash)

    A new summed usage hash.



84
85
86
87
88
89
90
91
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 84

def self.add_usage(total, usage)
  usage ||= {}
  {
    prompt_tokens: total[:prompt_tokens] + token_count(usage, :prompt_tokens),
    completion_tokens: total[:completion_tokens] + token_count(usage, :completion_tokens),
    total_tokens: total[:total_tokens] + token_count(usage, :total_tokens)
  }
end

.attach_step_number(iteration, step_count) ⇒ Hash

Attaches the step number to an iteration hash.

Parameters:

  • iteration (Hash)

    The iteration metadata from a Step.

  • step_count (Integer)

    The current step number.

Returns:

  • (Hash)

    The iteration with :step_number added.



57
58
59
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 57

def self.attach_step_number(iteration, step_count)
  iteration.merge(step_number: step_count)
end

.call(initial_prompt, max_iterations, config) ⇒ Hash

Executes the loop.

Parameters:

  • initial_prompt (String)

    The user task the agent must complete.

  • max_iterations (Integer)

    The maximum allowed steps before aborting.

  • config (Hash)

    The configuration for the Step execution.

Returns:

  • (Hash)

    A result hash indicating success or failure.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 16

def self.call(initial_prompt, max_iterations, config)
  messages = [{ role: 'user', content: initial_prompt }]
  iterations_log = []
  total_usage = empty_usage
  step_count = 0

  while step_count < max_iterations
    step_count += 1

    step_result = Step.call(messages, config)
    iteration = step_result[:iteration]
    iterations_log << attach_step_number(iteration, step_count) if iteration
    total_usage = add_usage(total_usage, step_result[:usage])

    unless step_result[:continue]
      final_result = step_result[:result] || { success: false, response: { error: { message: 'Step returned no result' } } }
      return finalize(final_result, iterations_log, total_usage)
    end

    messages = step_result[:messages]
  end

  finalize(
    { success: false, response: { error: { message: Agent::ReactAgent::MAX_ITERATIONS_REACHED } } },
    iterations_log,
    total_usage
  )
rescue StandardError => e
  SkillBench::ErrorLogger.log_error(e, 'ReactAgent Error')
  finalize(
    { success: false, response: { error: { message: e.message } } },
    iterations_log,
    total_usage
  )
end

.empty_usageHash

A zeroed token-usage accumulator.

Returns:

  • (Hash)

    Usage hash with prompt/completion/total token counts set to zero.



75
76
77
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 75

def self.empty_usage
  { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 }
end

.finalize(result, iterations_log, total_usage) ⇒ Hash

Merges the collected iterations and accumulated usage into the response.

Parameters:

  • result (Hash)

    The final result hash from the loop.

  • iterations_log (Array<Hash>)

    Collected iteration metadata.

  • total_usage (Hash)

    Summed token usage across all iterations.

Returns:

  • (Hash)

    The result with :iterations and :usage injected into :response.



67
68
69
70
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 67

def self.finalize(result, iterations_log, total_usage)
  response = result[:response] || {}
  result.merge(response: response.merge(iterations: iterations_log, usage: total_usage))
end

.token_count(usage, key) ⇒ Integer

Reads a token count from a usage hash, tolerating string keys.

Parameters:

  • usage (Hash)

    The usage hash.

  • key (Symbol)

    The usage key (e.g. :prompt_tokens).

Returns:

  • (Integer)

    The token count, or zero when absent.



98
99
100
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 98

def self.token_count(usage, key)
  (usage[key] || usage[key.to_s] || 0).to_i
end