Class: Lemans::Trial

Inherits:
Object
  • Object
show all
Defined in:
lib/lemans/trial.rb

Overview

One task, one agent, one reward. Only what happens inside the agent phase is a statement about the model; everything else is the harness's fault.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task:, bench:, agent_name:, runs_dir:, model: nil, backend: "daytona") ⇒ Trial

Returns a new instance of Trial.



13
14
15
16
17
18
19
20
21
# File 'lib/lemans/trial.rb', line 13

def initialize(task:, bench:, agent_name:, runs_dir:, model: nil, backend: "daytona")
  @task = task
  @bench = bench
  @agent_name = agent_name
  @model = model
  @backend = backend
  @id = "#{task.name}__#{SecureRandom.alphanumeric(7)}"
  @dir = Pathname(runs_dir).join(model_dir, @id)
end

Instance Attribute Details

#agent_nameObject (readonly)

Returns the value of attribute agent_name.



11
12
13
# File 'lib/lemans/trial.rb', line 11

def agent_name
  @agent_name
end

#backendObject (readonly)

Returns the value of attribute backend.



11
12
13
# File 'lib/lemans/trial.rb', line 11

def backend
  @backend
end

#benchObject (readonly)

Returns the value of attribute bench.



11
12
13
# File 'lib/lemans/trial.rb', line 11

def bench
  @bench
end

#dirObject (readonly)

Returns the value of attribute dir.



11
12
13
# File 'lib/lemans/trial.rb', line 11

def dir
  @dir
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/lemans/trial.rb', line 11

def id
  @id
end

#modelObject (readonly)

Returns the value of attribute model.



11
12
13
# File 'lib/lemans/trial.rb', line 11

def model
  @model
end

#taskObject (readonly)

Returns the value of attribute task.



11
12
13
# File 'lib/lemans/trial.rb', line 11

def task
  @task
end

Instance Method Details

#runObject



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
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
87
88
89
# File 'lib/lemans/trial.rb', line 23

def run
  logs_dir.mkpath
  started_at = Time.now.utc
  reward = nil
  usage = nil
  outcome = Results::Outcome.new(:completed)
  @phases = {}
  # Declared outside the phase blocks they are assigned in, or `ensure`
  # could not stop a sandbox whose setup raised.
  environment = nil
  snapshot = nil
  patch = nil

  begin
    agent = Agents.build(agent_name, profile: bench.agent, model: model)

    phase(:environment_setup) do
      environment = start_environment
      prepare(environment)

      snapshot = Snapshot.new(environment, bench: bench, task: task,
                                           timeout: bench.environment.build_timeout_sec)
      snapshot.capture!

      patch = Patch.new(environment, bench: bench, dir: dir)
      patch.seal!

      agent.install(environment, task: task)
      environment.network_policy = bench.agent.network
    end

    agent_result = phase(:agent) do
      in_agent_phase { agent.call(environment, task: task, logs_dir: logs_dir) }
    end
    usage = agent_result.usage
    outcome = over_ceiling(agent_result) || agent_result.outcome

    patch.collect!

    if outcome.scored?
      phase(:verifier) do
        # The sandbox is sealed before the tests arrive
        environment.network_policy = NetworkPolicy.none
        reward = Verifier.new(bench: bench, task: task, dir: dir, snapshot: snapshot).call(environment)
      end
    end
  rescue VerifierError => e
    outcome = Results::Outcome.new(:verifier_error, detail: e.message)
  rescue ::Miniswen::AccountingError => e
    outcome = Results::Outcome.new(:accounting_error, detail: e.message)
  rescue InfrastructureError, ::Miniswen::InfrastructureError => e
    outcome = Results::Outcome.new(@agent_phase ? :agent_error : :environment_error, detail: e.message)
  rescue ConfigError
    # A malformed bench is the author's bug to fix - raise!
    raise
  rescue StandardError => e
    # A harness bug must leave evidence.
    outcome = Results::Outcome.new(:harness_crash, detail: crash_detail(e))
  ensure
    environment&.stop
  end

  write_result(started_at: started_at, reward: reward, outcome: outcome, usage: usage)
rescue SystemCallError, JSON::GeneratorError => e
  # runs_dir unwritable, disk full
  raise ConfigError, "cannot record trial #{id}: #{e.message}"
end