Class: RobotLab::To::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/robot_lab/to/run.rb

Overview

Holds mutable state for a single autonomous run.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(run_id:, objective:, branch:, base_commit:, notes_path:, log_path:, run_dir: nil, decisions_path: nil) ⇒ Run

Returns a new instance of Run.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/robot_lab/to/run.rb', line 16

def initialize(run_id:, objective:, branch:, base_commit:, notes_path:, log_path:,
               run_dir: nil, decisions_path: nil)
  @run_id              = run_id
  @objective           = objective
  @branch              = branch
  @base_commit         = base_commit
  @notes_path          = Pathname.new(notes_path)
  @log_path            = Pathname.new(log_path)
  @run_dir             = Pathname.new(run_dir || @notes_path.parent)
  @decisions_path      = Pathname.new(decisions_path || @run_dir.join("decisions"))
  @started_at          = Time.now
  @iteration           = 0
  @consecutive_failures = 0
  @consecutive_errors  = 0
  @commits             = 0
  @input_tokens        = 0
  @output_tokens       = 0
  @last_score_value    = nil
  @iterations_since_improvement = 0
end

Instance Attribute Details

#base_commitObject (readonly)

Returns the value of attribute base_commit.



10
11
12
# File 'lib/robot_lab/to/run.rb', line 10

def base_commit
  @base_commit
end

#branchObject (readonly)

Returns the value of attribute branch.



10
11
12
# File 'lib/robot_lab/to/run.rb', line 10

def branch
  @branch
end

#commitsObject

Returns the value of attribute commits.



12
13
14
# File 'lib/robot_lab/to/run.rb', line 12

def commits
  @commits
end

#consecutive_errorsObject

Returns the value of attribute consecutive_errors.



12
13
14
# File 'lib/robot_lab/to/run.rb', line 12

def consecutive_errors
  @consecutive_errors
end

#consecutive_failuresObject

Returns the value of attribute consecutive_failures.



12
13
14
# File 'lib/robot_lab/to/run.rb', line 12

def consecutive_failures
  @consecutive_failures
end

#decisions_pathObject (readonly)

Returns the value of attribute decisions_path.



10
11
12
# File 'lib/robot_lab/to/run.rb', line 10

def decisions_path
  @decisions_path
end

#input_tokensObject

Returns the value of attribute input_tokens.



12
13
14
# File 'lib/robot_lab/to/run.rb', line 12

def input_tokens
  @input_tokens
end

#iterationObject

Returns the value of attribute iteration.



12
13
14
# File 'lib/robot_lab/to/run.rb', line 12

def iteration
  @iteration
end

#iterations_since_improvementObject

Returns the value of attribute iterations_since_improvement.



12
13
14
# File 'lib/robot_lab/to/run.rb', line 12

def iterations_since_improvement
  @iterations_since_improvement
end

#last_score_valueObject

Returns the value of attribute last_score_value.



12
13
14
# File 'lib/robot_lab/to/run.rb', line 12

def last_score_value
  @last_score_value
end

#log_pathObject (readonly)

Returns the value of attribute log_path.



10
11
12
# File 'lib/robot_lab/to/run.rb', line 10

def log_path
  @log_path
end

#notes_pathObject (readonly)

Returns the value of attribute notes_path.



10
11
12
# File 'lib/robot_lab/to/run.rb', line 10

def notes_path
  @notes_path
end

#objectiveObject (readonly)

Returns the value of attribute objective.



10
11
12
# File 'lib/robot_lab/to/run.rb', line 10

def objective
  @objective
end

#output_tokensObject

Returns the value of attribute output_tokens.



12
13
14
# File 'lib/robot_lab/to/run.rb', line 12

def output_tokens
  @output_tokens
end

#run_dirObject (readonly)

Returns the value of attribute run_dir.



10
11
12
# File 'lib/robot_lab/to/run.rb', line 10

def run_dir
  @run_dir
end

#run_idObject (readonly)

Returns the value of attribute run_id.



10
11
12
# File 'lib/robot_lab/to/run.rb', line 10

def run_id
  @run_id
end

#started_atObject (readonly)

Returns the value of attribute started_at.



10
11
12
# File 'lib/robot_lab/to/run.rb', line 10

def started_at
  @started_at
end

Class Method Details

.branch_name(objective) ⇒ Object



102
103
104
105
106
# File 'lib/robot_lab/to/run.rb', line 102

def self.branch_name(objective)
  slug = slugify(objective)
  ts   = Time.now.strftime("%Y%m%d-%H%M%S")
  "robot-to/#{slug}-#{ts}"
end

.generate_idObject

Derive run_id from current timestamp + random hex suffix.



87
88
89
90
91
# File 'lib/robot_lab/to/run.rb', line 87

def self.generate_id
  t = Time.now
  hex = SecureRandom.hex(3)
  "#{t.strftime("%Y%m%d-%H%M%S")}-#{hex}"
end

.load(path) ⇒ Object

Rebuild a Run from a run.json written by #to_h.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/robot_lab/to/run.rb', line 68

def self.load(path)
  data = JSON.parse(File.read(path), symbolize_names: true)
  run  = new(run_id: data[:run_id], objective: data[:objective], branch: data[:branch],
             base_commit: data[:base_commit], notes_path: data[:notes_path],
             log_path: data[:log_path], run_dir: data[:run_dir],
             decisions_path: data[:decisions_path])
  run.iteration            = data[:iteration].to_i
  run.consecutive_failures = data[:consecutive_failures].to_i
  run.consecutive_errors   = data[:consecutive_errors].to_i
  run.commits              = data[:commits].to_i
  run.input_tokens         = data[:input_tokens].to_i
  run.output_tokens        = data[:output_tokens].to_i
  run.last_score_value     = data[:last_score_value]
  run.iterations_since_improvement = data[:iterations_since_improvement].to_i
  run.instance_variable_set(:@started_at, Time.parse(data[:started_at])) if data[:started_at]
  run
end

.slugify(text) ⇒ Object

Slugify objective into a branch-name fragment (max 40 chars).



94
95
96
97
98
99
100
# File 'lib/robot_lab/to/run.rb', line 94

def self.slugify(text)
  text.downcase
      .gsub(/[^a-z0-9]+/, "-")
      .gsub(/^-+|-+$/, "")
      .slice(0, 40)
      .then { |s| s.empty? ? "takeover" : s }
end

Instance Method Details

#elapsed_humanObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/robot_lab/to/run.rb', line 41

def elapsed_human
  secs = elapsed_seconds.to_i
  h = secs / 3600
  m = (secs % 3600) / 60
  s = secs % 60
  if h.positive?
    "#{h}h #{m}m #{s}s"
  else
    m.positive? ? "#{m}m #{s}s" : "#{s}s"
  end
end

#elapsed_secondsObject



38
# File 'lib/robot_lab/to/run.rb', line 38

def elapsed_seconds  = Time.now - started_at

#state_pathObject



39
# File 'lib/robot_lab/to/run.rb', line 39

def state_path       = @run_dir.join("run.json")

#to_hObject

Serializable snapshot for run.json (enables --resume across processes).



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/robot_lab/to/run.rb', line 54

def to_h
  {
    run_id: run_id, objective: objective, branch: branch, base_commit: base_commit,
    notes_path: notes_path.to_s, log_path: log_path.to_s, run_dir: run_dir.to_s,
    decisions_path: decisions_path.to_s, started_at: started_at.iso8601,
    iteration: iteration, consecutive_failures: consecutive_failures,
    consecutive_errors: consecutive_errors, commits: commits,
    input_tokens: input_tokens, output_tokens: output_tokens,
    last_score_value: last_score_value,
    iterations_since_improvement: iterations_since_improvement
  }
end

#total_tokensObject



37
# File 'lib/robot_lab/to/run.rb', line 37

def total_tokens     = input_tokens + output_tokens