Module: Legion::Extensions::Validator::Helpers::ReviewOrchestrator

Extended by:
ReviewOrchestrator
Included in:
ReviewOrchestrator
Defined in:
lib/legion/extensions/validator/helpers/review_orchestrator.rb

Constant Summary collapse

REVIEW_PROMPT_TEMPLATE =
<<~PROMPT
  You are a senior code reviewer performing an adversarial review of a code change.
  Your job is to find bugs, security issues, logic errors, and quality problems.

  Be thorough and critical. If you see ANY issue, reject the change.

  ## Work Item
  Title: %<title>s
  Repository: %<repo>s
  Language: %<language>s

  ## Diff
  %<diff>s

  ## Instructions
  Respond with a JSON object inside a fenced code block:
  ```json
  {
    "verdict": "approve" or "reject",
    "confidence": 0.0 to 1.0,
    "issues": ["list of specific issues found"],
    "summary": "brief explanation of your verdict"
  }
  ```
PROMPT
REVIEW_SCHEMA =
{
  type:       :object,
  properties: {
    verdict:    { type: :string, enum: %w[approve reject] },
    confidence: { type: :number, minimum: 0.0, maximum: 1.0 },
    issues:     { type: :array, items: { type: :string } },
    summary:    { type: :string }
  },
  required:   %i[verdict confidence issues summary]
}.freeze
CAPABILITY_LEVELS =
%i[basic moderate reasoning].freeze

Instance Method Summary collapse

Instance Method Details

#intent_for_reviewer(index:, base_difficulty:) ⇒ Object



81
82
83
84
85
# File 'lib/legion/extensions/validator/helpers/review_orchestrator.rb', line 81

def intent_for_reviewer(index:, base_difficulty:)
  base_idx = difficulty_to_capability_index(base_difficulty)
  shifted = (base_idx + index) % CAPABILITY_LEVELS.size
  { capability: CAPABILITY_LEVELS[shifted] }
end

#parse_review_response(content:) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/legion/extensions/validator/helpers/review_orchestrator.rb', line 71

def parse_review_response(content:)
  json_str = extract_json(content)
  parsed = ::JSON.parse(json_str, symbolize_names: true)
  validate_review(parsed)
rescue ::JSON::ParserError, TypeError => e
  { verdict: 'reject', confidence: 0.0,
    issues: ["Failed to parse review response: #{e.message}"],
    summary: 'Review response was not valid JSON' }
end

#review(work_item:, diff:, k:) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/legion/extensions/validator/helpers/review_orchestrator.rb', line 52

def review(work_item:, diff:, k:)
  return { verdict: 'approved', reviews: [], merged_feedback: [] } if k.zero?

  exclude = build_upstream_exclusion(work_item: work_item)
  timeout = Legion::Settings.dig(:fleet, :llm, :validator_timeout_seconds) || 120

  futures = (0...k).map do |i|
    Concurrent::Future.execute do
      run_single_review(work_item: work_item, diff: diff, index: i, exclude: exclude)
    end
  end

  reviews = collect_reviews(futures: futures, timeout: timeout)
  verdict = reviews.all? { |r| r[:verdict] == 'approve' } ? 'approved' : 'rejected'
  merged_feedback = extract_merged_feedback(reviews: reviews)

  { verdict: verdict, reviews: reviews, merged_feedback: merged_feedback }
end