Class: Lemans::Trial::Patch

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

Overview

The agent's work as one git patch, diffed against a baseline sealed before its first turn

Constant Summary collapse

REMOTE_PATCH =
"/tmp/lemans-agent.patch"
REMOTE_INDEX =
"/tmp/lemans-patch.idx"

Instance Method Summary collapse

Constructor Details

#initialize(task, environment, timeout: 300, path: "agent.patch") ⇒ Patch

Returns a new instance of Patch.



17
18
19
20
21
22
23
24
25
# File 'lib/lemans/trial/patch.rb', line 17

def initialize(task, environment, timeout: 300, path: "agent.patch")
  @task = task
  @environment = environment
  @path = path
  @timeout = timeout

  @workdir = task.environment.workdir
  @baseline = nil
end

Instance Method Details

#collect!(result, store) ⇒ Object

Must run before the verifier restores the graded surfaces: a patch taken after would not show what the agent did to them



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lemans/trial/patch.rb', line 33

def collect!(result, store)
  return unless baseline

  after = write_tree
  return unless after

  diffed = environment.exec("#{git} diff --binary #{baseline} #{after} > #{REMOTE_PATCH}", timeout:)
  return unless diffed.success?

  Tempfile.create(%w[agent .patch]) do |file|
    environment.download(REMOTE_PATCH, file.path)
    store.save_artifact(result, Pathname(file.path), path:)
  end

  environment.exec("rm -f #{REMOTE_PATCH} #{REMOTE_INDEX}", timeout:)
  path
rescue InfrastructureError => e
  warn "lemans: could not collect the agent patch for #{result.id}: #{e.message}"
  nil
end

#seal!Object



27
28
29
# File 'lib/lemans/trial/patch.rb', line 27

def seal!
  @baseline = write_tree
end