Class: Ralph::Loop

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/ralph/loop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#check_completion, #escape_regex, #format_duration, #format_duration_long, #format_tool_summary, #now_ms, #strip_ansi, #which

Constructor Details

#initialize(config, state, history, context, tasks) ⇒ Loop

Returns a new instance of Loop.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ralph/loop.rb', line 16

def initialize(config, state, history, context, tasks)
  @config = config
  @agent = Agents.resolve(@config.chosen_agent)
  @state = state
  @history = history
  @context = context
  @tasks = tasks

  @struggle_indicators = {
    repeated_errors: {},
    no_progress_iterations: 0,
    short_iterations: 0
  }

  @prompt = @config.prompt
  @existing_state = Storage::State.load
  @state.save
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



14
15
16
# File 'lib/ralph/loop.rb', line 14

def agent
  @agent
end

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/ralph/loop.rb', line 14

def config
  @config
end

#contextObject (readonly)

Returns the value of attribute context.



14
15
16
# File 'lib/ralph/loop.rb', line 14

def context
  @context
end

#historyObject (readonly)

Returns the value of attribute history.



14
15
16
# File 'lib/ralph/loop.rb', line 14

def history
  @history
end

#promptObject (readonly)

Returns the value of attribute prompt.



14
15
16
# File 'lib/ralph/loop.rb', line 14

def prompt
  @prompt
end

#stateObject (readonly)

Returns the value of attribute state.



14
15
16
# File 'lib/ralph/loop.rb', line 14

def state
  @state
end

#struggle_indicatorsObject (readonly)

Returns the value of attribute struggle_indicators.



14
15
16
# File 'lib/ralph/loop.rb', line 14

def struggle_indicators
  @struggle_indicators
end

#tasksObject (readonly)

Returns the value of attribute tasks.



14
15
16
# File 'lib/ralph/loop.rb', line 14

def tasks
  @tasks
end

Instance Method Details

#existing_stateObject



35
# File 'lib/ralph/loop.rb', line 35

def existing_state = @existing_state

#runObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ralph/loop.rb', line 37

def run
  if existing_state&.active
    Output::ActiveLoopError.call(existing_state, path: Storage::State.path)
    exit 1
  end

  Output::Banner.call(self)

  # |..................................................|
  # |--------------------------------------------------|
  # |==================================================|
  # |**************************************************|
  # |##################################################|
  # | Main loop                                        |
  # |##################################################|
  # |**************************************************|
  # |==================================================|
  # |--------------------------------------------------|
  # |..................................................|
  #
  # © 2026 Nathan K.
  # Honestly, I've no idea where this graphic
  # wonder came from. It's MIT lisenced now, thought.

  setup_signal_handler

  loop do
    if @config.stopping
      break
    elsif max_iterations_reached?
      Output::MaxIterationsReached.call(self)
      @state.clear
      break
    else
      Output::Iteration::Header.call(self)

      iteration = Iteration.new(self)
      result = iteration.run
      should_continue = process_result(result, iteration)

      if should_continue
        @state.iteration += 1
        @state.save
        sleep 1
      else
        break
      end
    end
  end
end