Class: RailsDoctor::Agent::Handoff

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_doctor/agent/handoff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_name:, project:, config:, runner:, options:) ⇒ Handoff

Returns a new instance of Handoff.



13
14
15
16
17
18
19
# File 'lib/rails_doctor/agent/handoff.rb', line 13

def initialize(agent_name:, project:, config:, runner:, options:)
  @agent_name = agent_name
  @project = project
  @config = config
  @runner = runner
  @options = options
end

Instance Attribute Details

#agent_nameObject (readonly)

Returns the value of attribute agent_name.



11
12
13
# File 'lib/rails_doctor/agent/handoff.rb', line 11

def agent_name
  @agent_name
end

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/rails_doctor/agent/handoff.rb', line 11

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/rails_doctor/agent/handoff.rb', line 11

def options
  @options
end

#projectObject (readonly)

Returns the value of attribute project.



11
12
13
# File 'lib/rails_doctor/agent/handoff.rb', line 11

def project
  @project
end

#runnerObject (readonly)

Returns the value of attribute runner.



11
12
13
# File 'lib/rails_doctor/agent/handoff.rb', line 11

def runner
  @runner
end

Instance Method Details

#run(scan_result) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rails_doctor/agent/handoff.rb', line 21

def run(scan_result)
  agent_config = config.agent(agent_name)
  raise Error, "Unknown agent #{agent_name.inspect}" unless agent_config

  findings = filtered_findings(scan_result.findings)
  brief = build_brief(scan_result, findings)
  brief_path = write_brief(brief)

  output = +"Wrote agent brief: #{brief_path}\n"
  output << "Findings included: #{findings.size}\n"

  if options[:apply]
    ensure_clean_worktree!(agent_config)
    command = "#{agent_config.fetch("command")} #{Shellwords.escape(brief_path)}"
    command_result = runner.run(command, timeout_seconds: 900)
    audit_path = write_audit(scan_result, findings, brief_path, command, command_result)
    output << "Invoked #{agent_name}: exit #{command_result.exit_status}\n"
    output << "Audit trail: #{audit_path}\n"
    output << command_result.stdout unless command_result.stdout.to_s.empty?
    output << command_result.stderr unless command_result.stderr.to_s.empty?
  else
    output << "No agent was invoked. Re-run with --apply to execute #{agent_name}.\n"
  end

  output
end