Class: PetriDish::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/petri_dish/runner.rb

Constant Summary collapse

POLL_INTERVAL =

seconds

2
STARTUP_DEADLINE =

seconds, must see a SessionStart event by then or fail loud

60
AUTH_ERROR_PATTERN =
%r{API Error: 40[13]|Please run /login|Invalid authentication credentials|authentication_error}

Instance Method Summary collapse

Constructor Details

#initialize(test_name, cultures_dir:, results_dir:, deny: false, debug: false, keep: false) ⇒ Runner

Returns a new instance of Runner.



22
23
24
25
26
27
28
29
30
31
# File 'lib/petri_dish/runner.rb', line 22

def initialize(test_name, cultures_dir:, results_dir:, deny: false, debug: false, keep: false)
  @cultures_dir = cultures_dir
  @results_dir = results_dir
  @test_dir = File.join(cultures_dir, test_name)
  @config = Config.new(@test_dir)
  @deny = deny
  @debug = debug
  @keep = keep
  @tmux_session = "test-#{@config.name}"
end

Instance Method Details

#run!Object



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
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/petri_dish/runner.rb', line 33

def run!
  preflight!
  ensure_env!
  run_prepare!

  env = Environment.new(@config.environment[:name])

  # Re-trust after prepare. The setup-time trust call runs before the
  # work_dir exists, so File.realpath falls back to expand_path and stores
  # the trust key under /tmp/... — but Claude resolves symlinks and looks
  # under /private/tmp/... on macOS. Re-trusting here, with the path now
  # existing, lets realpath canonicalize properly.
  env.trust!(@config.runtime[:work_dir])

  results_dir = create_results_dir
  signal_file = File.join(results_dir, "signal")
  transcript_path = File.join(results_dir, "transcript.log")

  env.clear_hook_log!

  prompt = build_prompt(signal_file)
  launch_tmux!(prompt)

  failure = nil
  begin
    poll_for_completion(signal_file, env)
  rescue Interrupt
    log "Interrupted by user"
  rescue StartupFailure, AuthFailure => e
    failure = e
  end

  # Capture transcript first. If we failed, tmux may be dying and we
  # want whatever output Claude printed (auth error, version mismatch, etc.).
  transcript = Transcript.new(@tmux_session)
  transcript.save!(transcript_path) if tmux_alive?

  # Collect artifacts
  hook_log_dest = File.join(results_dir, "hook-events.jsonl")
  FileUtils.cp(env.hook_log_path, hook_log_dest) if File.exist?(env.hook_log_path)

  if failure
    # Tear down tmux now so the error message isn't competing with a live session.
    system("tmux kill-session -t #{@tmux_session} 2>/dev/null")
    report_failure!(failure, transcript_path, results_dir)
    exit 1
  end

  # Build results
  if File.exist?(hook_log_dest)
    builder = ResultsBuilder.new(hook_log_dest, transcript_path, results_dir)
    builder.build!
  end

  # Teardown
  unless @keep
    system("tmux kill-session -t #{@tmux_session} 2>/dev/null")
  end

  # Summary
  results_file = File.join(results_dir, "results.md")
  log ""
  log "Run complete: #{@config.name}"
  log "  Results:    #{File.exist?(results_file) ? results_file : '(not written)'}"
  log "  Hook log:   #{File.exist?(hook_log_dest) ? hook_log_dest : '(not captured)'}"
  log "  Transcript: #{File.exist?(transcript_path) ? transcript_path : '(not captured)'}"
  if @keep
    log "  tmux:       tmux attach -t #{@tmux_session}"
  end
end