aireview 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +46 -0
- data/README.md +215 -79
- data/config/defaults.yml +6 -7
- data/lib/aireview/candidate_checker.rb +0 -1
- data/lib/aireview/cli.rb +91 -63
- data/lib/aireview/config.rb +42 -4
- data/lib/aireview/config_fallbacks.rb +8 -11
- data/lib/aireview/config_layers.rb +6 -4
- data/lib/aireview/config_loader.rb +31 -23
- data/lib/aireview/context_budget.rb +6 -2
- data/lib/aireview/context_builder.rb +1 -0
- data/lib/aireview/dry_run_report.rb +3 -0
- data/lib/aireview/github_client.rb +284 -0
- data/lib/aireview/jev_stage.rb +19 -2
- data/lib/aireview/llm_client.rb +40 -14
- data/lib/aireview/llm_router.rb +69 -90
- data/lib/aireview/model_candidate.rb +6 -3
- data/lib/aireview/model_pool.rb +6 -0
- data/lib/aireview/model_state.rb +5 -17
- data/lib/aireview/mr_parser.rb +44 -6
- data/lib/aireview/platform.rb +99 -0
- data/lib/aireview/publisher.rb +8 -7
- data/lib/aireview/review_comment_formatter.rb +158 -0
- data/lib/aireview/review_marker.rb +5 -1
- data/lib/aireview/review_pipeline.rb +24 -13
- data/lib/aireview/review_renderer.rb +33 -12
- data/lib/aireview/reviewer.rb +4 -5
- data/lib/aireview/stage_chains.rb +4 -0
- data/lib/aireview/version.rb +1 -1
- data/lib/aireview.rb +2 -1
- metadata +13 -14
data/lib/aireview/cli.rb
CHANGED
|
@@ -4,6 +4,8 @@ require_relative 'model_checker'
|
|
|
4
4
|
|
|
5
5
|
module Aireview
|
|
6
6
|
class CLI # rubocop:disable Metrics/ClassLength
|
|
7
|
+
DIFF_FIELDS = %w[sha target_branch diff_refs].freeze
|
|
8
|
+
|
|
7
9
|
def self.start(argv, out: $stdout, err: $stderr, env: ENV)
|
|
8
10
|
new(argv, out: out, err: err, env: env).start
|
|
9
11
|
end
|
|
@@ -76,6 +78,7 @@ module Aireview
|
|
|
76
78
|
parser_result = MrParser.parse(mr_url)
|
|
77
79
|
config = load_review_config(options)
|
|
78
80
|
context = load_review_context(parser_result, config, options)
|
|
81
|
+
return 0 unless context
|
|
79
82
|
|
|
80
83
|
execute_review(config, context, options)
|
|
81
84
|
end
|
|
@@ -109,31 +112,37 @@ module Aireview
|
|
|
109
112
|
end
|
|
110
113
|
|
|
111
114
|
def load_review_context(parser_result, config, options)
|
|
112
|
-
|
|
113
|
-
|
|
115
|
+
client = Platform.client(config: config, target: parser_result, logger: @logger)
|
|
116
|
+
@logger.info("Loading #{Platform.label(parser_result)}")
|
|
117
|
+
merge_request = client.fetch_merge_request(parser_result.project_id, parser_result.iid)
|
|
118
|
+
if options[:post] && skip_review_title?(merge_request['title'])
|
|
119
|
+
@out.puts("Review skipped: #{Platform.noun(parser_result)} title contains [skip review]")
|
|
120
|
+
return nil
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
changes = client.fetch_merge_request_changes(parser_result.project_id, parser_result.iid)
|
|
124
|
+
merge_request = note_files_not_returned(merge_request, changes)
|
|
114
125
|
|
|
115
126
|
{
|
|
116
127
|
parser_result: parser_result,
|
|
117
|
-
|
|
128
|
+
client: client,
|
|
118
129
|
merge_request: merge_request,
|
|
119
130
|
changes: prepare_changes(changes, config),
|
|
120
131
|
jira_issue: maybe_load_jira_issue(config, merge_request, options)
|
|
121
132
|
}
|
|
122
133
|
end
|
|
123
134
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
@logger.
|
|
134
|
-
merge_request
|
|
135
|
-
changes = gitlab_client.fetch_merge_request_changes(parser_result.project_id, parser_result.iid)
|
|
136
|
-
[merge_request, changes]
|
|
135
|
+
# A platform may return fewer files than the merge request changes
|
|
136
|
+
# (GitHub lists at most 3000) and report the total as changed_files. The
|
|
137
|
+
# rest goes into the report as not reviewed: a log line alone would
|
|
138
|
+
# publish a report that looks complete. Counted before ignore_paths.
|
|
139
|
+
def note_files_not_returned(merge_request, changes)
|
|
140
|
+
total = merge_request['changed_files']
|
|
141
|
+
missing = total.is_a?(Integer) ? total - changes.size : 0
|
|
142
|
+
return merge_request unless missing.positive?
|
|
143
|
+
|
|
144
|
+
@logger.warn("Only #{changes.size} of #{total} changed files were returned; the rest is not reviewed")
|
|
145
|
+
merge_request.merge('files_not_returned' => missing)
|
|
137
146
|
end
|
|
138
147
|
|
|
139
148
|
# The diff travels on as files, not as one string: the context budget
|
|
@@ -178,7 +187,7 @@ module Aireview
|
|
|
178
187
|
# least stays in the job log.
|
|
179
188
|
@out.puts(review)
|
|
180
189
|
|
|
181
|
-
publish_review(review, context, publication) if publication
|
|
190
|
+
publish_review(review, context, publication, language: config.review_language) if publication
|
|
182
191
|
|
|
183
192
|
0
|
|
184
193
|
end
|
|
@@ -188,7 +197,11 @@ module Aireview
|
|
|
188
197
|
def prepare_publication(pipeline, config, context, options)
|
|
189
198
|
return nil unless options[:post]
|
|
190
199
|
|
|
191
|
-
publisher = Publisher.new(
|
|
200
|
+
publisher = Publisher.new(
|
|
201
|
+
client: context[:client],
|
|
202
|
+
platform_name: Platform.name(context[:parser_result]),
|
|
203
|
+
logger: @logger
|
|
204
|
+
)
|
|
192
205
|
prompts = pipeline.dry_run_prompts(
|
|
193
206
|
merge_request: context[:merge_request],
|
|
194
207
|
changes: context[:changes],
|
|
@@ -202,60 +215,49 @@ module Aireview
|
|
|
202
215
|
)
|
|
203
216
|
|
|
204
217
|
mode = options[:review_mode] || config.review_mode
|
|
205
|
-
return :skip if skip_review?(existing, key: key, mode: mode, force: options[:force],
|
|
206
|
-
gitlab_client: context[:gitlab_client])
|
|
218
|
+
return :skip if skip_review?(existing, key: key, mode: mode, force: options[:force], context: context)
|
|
207
219
|
|
|
208
220
|
{publisher: publisher, existing: existing, key: key}
|
|
209
221
|
end
|
|
210
222
|
|
|
211
|
-
def skip_review?(existing, key:, mode:, force:,
|
|
223
|
+
def skip_review?(existing, key:, mode:, force:, context:)
|
|
212
224
|
return false if existing.nil? || force
|
|
213
225
|
|
|
214
226
|
up_to_date = existing[:key] == key
|
|
215
|
-
return false unless up_to_date || (mode == 'once' && !
|
|
227
|
+
return false unless up_to_date || (mode == 'once' && !retried_run?(context))
|
|
216
228
|
|
|
217
|
-
|
|
229
|
+
reason = skip_reason(existing, up_to_date: up_to_date, mode: mode, target: context[:parser_result])
|
|
230
|
+
@out.puts("Review skipped: #{reason}")
|
|
218
231
|
true
|
|
219
232
|
end
|
|
220
233
|
|
|
221
234
|
# In once mode the review is not repeated on new pushes, even when the MR
|
|
222
235
|
# has changed. So besides the mode the message says whether the review is
|
|
223
|
-
# up to date: if it is not,
|
|
224
|
-
def skip_reason(existing, up_to_date:, mode:)
|
|
236
|
+
# up to date: if it is not, a CI retry updates it (see Platform.update_hint).
|
|
237
|
+
def skip_reason(existing, up_to_date:, mode:, target:)
|
|
225
238
|
return 'existing review is up to date' unless mode == 'once'
|
|
226
239
|
|
|
240
|
+
hint = Platform.update_hint(target: target, env: @env)
|
|
227
241
|
state = if up_to_date then 'the review is up to date'
|
|
228
|
-
elsif existing[:key].nil? then "review freshness is unknown: #{
|
|
229
|
-
else "review inputs changed: #{
|
|
242
|
+
elsif existing[:key].nil? then "review freshness is unknown: #{hint}"
|
|
243
|
+
else "review inputs changed: #{hint}"
|
|
230
244
|
end
|
|
231
|
-
"
|
|
232
|
-
end
|
|
233
|
-
|
|
234
|
-
def update_hint
|
|
235
|
-
ci_job_context ? 'retry the job to update' : 'use --force to review again'
|
|
236
|
-
end
|
|
237
|
-
|
|
238
|
-
def retried_ci_job?(gitlab_client)
|
|
239
|
-
project_id, job_id = ci_job_context
|
|
240
|
-
return false unless project_id
|
|
241
|
-
|
|
242
|
-
gitlab_client.retried_job?(project_id, job_id)
|
|
245
|
+
"#{Platform.noun(target)} already reviewed (review_mode=once), #{state}"
|
|
243
246
|
end
|
|
244
247
|
|
|
245
|
-
def
|
|
246
|
-
|
|
247
|
-
return if Aireview::Utils.blank?(project_id) || Aireview::Utils.blank?(job_id)
|
|
248
|
-
|
|
249
|
-
[project_id, job_id]
|
|
248
|
+
def retried_run?(context)
|
|
249
|
+
Platform.retried_run?(client: context[:client], target: context[:parser_result], env: @env)
|
|
250
250
|
end
|
|
251
251
|
|
|
252
|
-
def publish_review(review, context, publication)
|
|
253
|
-
|
|
252
|
+
def publish_review(review, context, publication, language:)
|
|
253
|
+
status, changed = merge_request_status(context)
|
|
254
|
+
return if status == :skip
|
|
254
255
|
|
|
256
|
+
body = changed.any? ? "#{review}#{edited_note(language)}" : review
|
|
255
257
|
publication[:publisher].publish(
|
|
256
258
|
project_id: context[:parser_result].project_id,
|
|
257
259
|
iid: context[:parser_result].iid,
|
|
258
|
-
review_body:
|
|
260
|
+
review_body: body,
|
|
259
261
|
key: publication[:key],
|
|
260
262
|
existing: publication[:existing]
|
|
261
263
|
)
|
|
@@ -264,20 +266,46 @@ module Aireview
|
|
|
264
266
|
# While the LLM was working the MR may have moved on: a new commit, a
|
|
265
267
|
# rebase or a target branch change. Publishing a review of a stale diff
|
|
266
268
|
# is worse than publishing nothing, and a failed check cannot be read as
|
|
267
|
-
# "all in place", so it is not swallowed.
|
|
268
|
-
|
|
269
|
-
|
|
269
|
+
# "all in place", so it is not swallowed. Edits to title or description
|
|
270
|
+
# alone do not invalidate the diff, so the formed report is still posted
|
|
271
|
+
# with a note, unless [skip review] was added to the title.
|
|
272
|
+
def merge_request_status(context)
|
|
273
|
+
current = context[:client].fetch_merge_request(
|
|
270
274
|
context[:parser_result].project_id,
|
|
271
275
|
context[:parser_result].iid
|
|
272
276
|
)
|
|
277
|
+
noun = Platform.noun(context[:parser_result]).capitalize
|
|
278
|
+
if skip_review_title?(current['title'])
|
|
279
|
+
@logger.warn("#{noun} title contains [skip review]; skipping publication")
|
|
280
|
+
return [:skip, []]
|
|
281
|
+
end
|
|
282
|
+
|
|
273
283
|
before = ReviewMarker.state(context[:merge_request])
|
|
274
284
|
after = ReviewMarker.state(current)
|
|
275
285
|
changed = after.keys.reject { |field| after[field] == before[field] }
|
|
276
|
-
return
|
|
286
|
+
return [:ok, []] if changed.empty?
|
|
277
287
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
288
|
+
unless changed.intersect?(DIFF_FIELDS)
|
|
289
|
+
@logger.warn("#{noun} edited while review was running (#{changed.join(', ')}); publishing anyway")
|
|
290
|
+
return [:ok, changed]
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
@logger.warn("#{noun} changed while review was running (#{changed.join(', ')}); skipping publication")
|
|
294
|
+
[:skip, changed]
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def skip_review_title?(title)
|
|
298
|
+
title.to_s.match?(/\[skip review\]/i)
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def edited_note(language)
|
|
302
|
+
if language == 'ru'
|
|
303
|
+
"\n\nПримечание: заголовок или описание были изменены во время выполнения ревью; " \
|
|
304
|
+
'отчёт сформирован по предыдущей версии.'
|
|
305
|
+
else
|
|
306
|
+
"\n\nNote: the title or description was edited while the review was running; " \
|
|
307
|
+
'this report reflects the previous version.'
|
|
308
|
+
end
|
|
281
309
|
end
|
|
282
310
|
|
|
283
311
|
def maybe_load_jira_issue(config, merge_request, options)
|
|
@@ -310,7 +338,7 @@ module Aireview
|
|
|
310
338
|
}
|
|
311
339
|
|
|
312
340
|
OptionParser.new do |parser|
|
|
313
|
-
parser.banner = 'Usage: aireview review <
|
|
341
|
+
parser.banner = 'Usage: aireview review <merge_or_pull_request_url> [options]'
|
|
314
342
|
|
|
315
343
|
add_llm_options(parser, options)
|
|
316
344
|
add_publication_options(parser, options)
|
|
@@ -346,13 +374,13 @@ module Aireview
|
|
|
346
374
|
options[:no_critique] = true
|
|
347
375
|
end
|
|
348
376
|
|
|
349
|
-
parser.on('--no-fallbacks', 'Use only the primary model
|
|
377
|
+
parser.on('--no-fallbacks', 'Use only the primary model of each stage') do
|
|
350
378
|
options[:no_fallbacks] = true
|
|
351
379
|
end
|
|
352
380
|
end
|
|
353
381
|
|
|
354
382
|
def add_publication_options(parser, options)
|
|
355
|
-
parser.on('--post', 'Post review back to
|
|
383
|
+
parser.on('--post', 'Post review back to the merge or pull request') do
|
|
356
384
|
options[:post] = true
|
|
357
385
|
end
|
|
358
386
|
|
|
@@ -361,7 +389,7 @@ module Aireview
|
|
|
361
389
|
options[:review_mode] = value
|
|
362
390
|
end
|
|
363
391
|
|
|
364
|
-
parser.on('--force', 'Review again even if the merge request was already reviewed') do
|
|
392
|
+
parser.on('--force', 'Review again even if the merge or pull request was already reviewed') do
|
|
365
393
|
options[:force] = true
|
|
366
394
|
end
|
|
367
395
|
end
|
|
@@ -396,16 +424,16 @@ module Aireview
|
|
|
396
424
|
def help
|
|
397
425
|
<<~HELP
|
|
398
426
|
Usage:
|
|
399
|
-
aireview review <
|
|
427
|
+
aireview review <merge_or_pull_request_url> [options]
|
|
400
428
|
aireview models check [--config PATH] [--strict] [--verbose]
|
|
401
429
|
|
|
402
430
|
Commands:
|
|
403
|
-
review Run review for a GitLab merge request URL
|
|
431
|
+
review Run review for a GitLab merge request or GitHub pull request URL
|
|
404
432
|
models check Send a probe request with the generate and critique schemas
|
|
405
433
|
to every model of both stages; exit 1 if any fails
|
|
406
434
|
|
|
407
435
|
Options:
|
|
408
|
-
--post Post review as a merge request
|
|
436
|
+
--post Post review as a merge or pull request comment
|
|
409
437
|
--generate-model MODEL
|
|
410
438
|
Override Generate pass model
|
|
411
439
|
--critique-model MODEL
|
|
@@ -419,11 +447,11 @@ module Aireview
|
|
|
419
447
|
--config PATH Path to .aireview.yml
|
|
420
448
|
--review-mode MODE
|
|
421
449
|
How to treat an existing review: update (default) or once
|
|
422
|
-
--force Review again even if the merge request was already reviewed
|
|
450
|
+
--force Review again even if the merge or pull request was already reviewed
|
|
423
451
|
--no-jira Disable Jira enrichment
|
|
424
452
|
--dry-run Print prompts without LLM calls
|
|
425
453
|
--no-critique Skip second LLM critique pass
|
|
426
|
-
--no-fallbacks Use only the primary model
|
|
454
|
+
--no-fallbacks Use only the primary model of each stage
|
|
427
455
|
--verbose Enable verbose logging
|
|
428
456
|
-h, --help Show help
|
|
429
457
|
HELP
|
data/lib/aireview/config.rb
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
require 'logger'
|
|
3
|
-
require 'pathname'
|
|
4
3
|
require 'yaml'
|
|
5
4
|
require_relative 'errors'
|
|
6
5
|
require_relative 'utils'
|
|
@@ -57,8 +56,8 @@ module Aireview
|
|
|
57
56
|
|
|
58
57
|
attr_reader :config_path, :layers
|
|
59
58
|
|
|
60
|
-
def self.load(**
|
|
61
|
-
ConfigLoader.load(**
|
|
59
|
+
def self.load(**)
|
|
60
|
+
ConfigLoader.load(**)
|
|
62
61
|
end
|
|
63
62
|
|
|
64
63
|
def self.env_names
|
|
@@ -77,7 +76,7 @@ module Aireview
|
|
|
77
76
|
end
|
|
78
77
|
|
|
79
78
|
# CLI overrides change only the primary model of a stage, the reserves
|
|
80
|
-
# from the config stay; no_fallbacks leaves
|
|
79
|
+
# from the config stay; no_fallbacks leaves only the primary model.
|
|
81
80
|
# no_critique — --no-critique: the run has no Critique, so its settings
|
|
82
81
|
# are neither routed nor validated (see llm_stages).
|
|
83
82
|
# Seven named flags of the CLI read better than a struct for its own sake.
|
|
@@ -90,6 +89,18 @@ module Aireview
|
|
|
90
89
|
no_fallbacks: false,
|
|
91
90
|
no_critique: false
|
|
92
91
|
)
|
|
92
|
+
# A model from the CLI names a model too, so the bundled defaults step
|
|
93
|
+
# aside as they do for .aireview.yml and env (see ConfigLoader): with
|
|
94
|
+
# their pool loaded, a CLI model outside it would drop the stage's
|
|
95
|
+
# configured fallbacks.
|
|
96
|
+
if bundled_defaults_under?(generate_model, critique_model)
|
|
97
|
+
return without_bundled_defaults.with_overrides(
|
|
98
|
+
generate_model: generate_model, critique_model: critique_model,
|
|
99
|
+
generate_temperature: generate_temperature, critique_temperature: critique_temperature,
|
|
100
|
+
critique_engine: critique_engine, no_fallbacks: no_fallbacks, no_critique: no_critique
|
|
101
|
+
)
|
|
102
|
+
end
|
|
103
|
+
|
|
93
104
|
llm_config = {
|
|
94
105
|
'generate' => stage_overrides(model: generate_model, temperature: generate_temperature),
|
|
95
106
|
'critique' => stage_overrides(model: critique_model, temperature: critique_temperature)
|
|
@@ -116,6 +127,18 @@ module Aireview
|
|
|
116
127
|
@data['gitlab_token']
|
|
117
128
|
end
|
|
118
129
|
|
|
130
|
+
def github_token
|
|
131
|
+
@data['github_token']
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def github_api_url
|
|
135
|
+
@data['github_api_url']
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def github_review_author
|
|
139
|
+
@data['github_review_author']
|
|
140
|
+
end
|
|
141
|
+
|
|
119
142
|
def jira_url
|
|
120
143
|
@data['jira_url']
|
|
121
144
|
end
|
|
@@ -249,6 +272,12 @@ module Aireview
|
|
|
249
272
|
raise ConfigError, 'GITLAB_TOKEN is required'
|
|
250
273
|
end
|
|
251
274
|
|
|
275
|
+
def require_github_token!
|
|
276
|
+
return github_token if Aireview::Utils.present?(github_token)
|
|
277
|
+
|
|
278
|
+
raise ConfigError, 'GITHUB_TOKEN is required'
|
|
279
|
+
end
|
|
280
|
+
|
|
252
281
|
private
|
|
253
282
|
|
|
254
283
|
# Without a pool an override changes only the primary model of the stage,
|
|
@@ -256,6 +285,15 @@ module Aireview
|
|
|
256
285
|
# model from the pool becomes the start and the stage's own model and
|
|
257
286
|
# fallbacks are reset; a model outside the pool is a single chain, start
|
|
258
287
|
# and fallbacks are reset.
|
|
288
|
+
def bundled_defaults_under?(*cli_models)
|
|
289
|
+
cli_models.any? && @layers.any? { |layer| layer.name == ConfigLayers::BUNDLED_LAYER }
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def without_bundled_defaults
|
|
293
|
+
layers = @layers.reject { |layer| layer.name == ConfigLayers::BUNDLED_LAYER }
|
|
294
|
+
self.class.new(config_path: config_path, logger: @logger, layers: layers)
|
|
295
|
+
end
|
|
296
|
+
|
|
259
297
|
def stage_overrides(model:, temperature:)
|
|
260
298
|
overrides = {'temperature' => temperature}.compact
|
|
261
299
|
return overrides unless model
|
|
@@ -7,10 +7,9 @@ require_relative 'stage_chains'
|
|
|
7
7
|
require_relative 'model_pool'
|
|
8
8
|
|
|
9
9
|
module Aireview
|
|
10
|
-
# Reserves for when the primary model is down
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
# only in the environment.
|
|
10
|
+
# Reserves for when the primary model is down: the routing plan (see
|
|
11
|
+
# StageChains and ModelPool) and the key per provider. Models are set in
|
|
12
|
+
# .aireview.yml or the environment, keys only in the environment.
|
|
14
13
|
module ConfigFallbacks
|
|
15
14
|
KEYLESS_PROVIDERS = %w[ollama].freeze
|
|
16
15
|
DEFAULT_TIME_BUDGET = 1_800
|
|
@@ -27,7 +26,7 @@ module Aireview
|
|
|
27
26
|
routing.chain(stage)
|
|
28
27
|
end
|
|
29
28
|
|
|
30
|
-
# The
|
|
29
|
+
# The key a model is called with. A server of your own (a model with
|
|
31
30
|
# api_base) never gets the provider's key — OPENAI_API_KEY must not leave
|
|
32
31
|
# for a self-hosted server — but LLM_API_KEY, or a placeholder for a
|
|
33
32
|
# server without auth, since the client wants some key.
|
|
@@ -40,15 +39,13 @@ module Aireview
|
|
|
40
39
|
[Aireview::Utils.presence(llm_api_key) || SELF_HOSTED_PLACEHOLDER_KEY]
|
|
41
40
|
end
|
|
42
41
|
|
|
43
|
-
#
|
|
44
|
-
#
|
|
42
|
+
# The provider's key; a single nil for a keyless provider, so that
|
|
43
|
+
# walking the chain does not depend on the provider.
|
|
45
44
|
def provider_api_keys(provider)
|
|
46
45
|
provider = provider.to_s
|
|
47
46
|
return [nil] if KEYLESS_PROVIDERS.include?(provider)
|
|
48
47
|
|
|
49
|
-
|
|
50
|
-
keys = [provider_api_key(provider)].compact if keys.empty?
|
|
51
|
-
fallbacks_disabled? ? keys.first(1) : keys
|
|
48
|
+
[provider_api_key(provider)].compact
|
|
52
49
|
end
|
|
53
50
|
|
|
54
51
|
def fallbacks_disabled?
|
|
@@ -59,7 +56,7 @@ module Aireview
|
|
|
59
56
|
stage_chain(stage).drop(1).map(&:to_s)
|
|
60
57
|
end
|
|
61
58
|
|
|
62
|
-
#
|
|
59
|
+
# Whether a key is present per provider, for --dry-run; the values never
|
|
63
60
|
# leave. Servers of your own are not counted: they take LLM_API_KEY.
|
|
64
61
|
def api_key_counts(stages)
|
|
65
62
|
providers = stages.flat_map { |stage| stage_chain(stage).reject(&:api_base).map(&:provider) }.uniq
|
|
@@ -4,14 +4,16 @@ require_relative 'utils'
|
|
|
4
4
|
|
|
5
5
|
module Aireview
|
|
6
6
|
# Configuration layers in ascending priority: built-in values, image
|
|
7
|
-
# defaults (AIREVIEW_DEFAULTS), the project's
|
|
8
|
-
# layer name is shown by --dry-run, so that it
|
|
7
|
+
# defaults (AIREVIEW_DEFAULTS) or bundled defaults, the project's
|
|
8
|
+
# .aireview.yml, env, CLI. The layer name is shown by --dry-run, so that it
|
|
9
|
+
# is clear where a model came from.
|
|
9
10
|
module ConfigLayers
|
|
10
11
|
Layer = Struct.new(:name, :path, :data, keyword_init: true)
|
|
11
12
|
|
|
12
13
|
BUILT_IN_LAYER = 'built-in'
|
|
13
14
|
DATA_LAYER = 'config'
|
|
14
15
|
IMAGE_LAYER = 'image defaults'
|
|
16
|
+
BUNDLED_LAYER = 'bundled defaults'
|
|
15
17
|
FILE_LAYER = '.aireview.yml'
|
|
16
18
|
ENV_LAYER = 'env'
|
|
17
19
|
CLI_LAYER = 'cli'
|
|
@@ -32,7 +34,7 @@ module Aireview
|
|
|
32
34
|
|
|
33
35
|
def stage_setting_layer(stage, key)
|
|
34
36
|
stage = stage.to_s
|
|
35
|
-
@layers.
|
|
37
|
+
@layers.rfind do |layer|
|
|
36
38
|
!Utils.dig(layer.data, 'llm', stage, key).nil? || !Utils.dig(layer.data, 'llm', key).nil?
|
|
37
39
|
end
|
|
38
40
|
end
|
|
@@ -95,7 +97,7 @@ module Aireview
|
|
|
95
97
|
private
|
|
96
98
|
|
|
97
99
|
def layer_of(*keys)
|
|
98
|
-
@layers.
|
|
100
|
+
@layers.rfind { |layer| !Utils.dig(layer.data, *keys).nil? }
|
|
99
101
|
end
|
|
100
102
|
|
|
101
103
|
def inherited_fallback_names(stage)
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
require_relative 'stages'
|
|
3
3
|
require 'logger'
|
|
4
|
-
require 'pathname'
|
|
5
4
|
require 'yaml'
|
|
6
5
|
require_relative 'errors'
|
|
7
6
|
require_relative 'utils'
|
|
@@ -12,13 +11,17 @@ require_relative 'config'
|
|
|
12
11
|
|
|
13
12
|
module Aireview
|
|
14
13
|
# Builds a Config from layers: built-in values, image defaults
|
|
15
|
-
# (AIREVIEW_DEFAULTS
|
|
16
|
-
#
|
|
17
|
-
#
|
|
14
|
+
# (AIREVIEW_DEFAULTS, or the bundled config/defaults.yml when no upper layer
|
|
15
|
+
# names a model), the project's .aireview.yml (searched upwards from cwd),
|
|
16
|
+
# env. Everything that knows environment variable names and file formats
|
|
17
|
+
# lives here; Config only answers questions about merged data.
|
|
18
18
|
module ConfigLoader
|
|
19
19
|
ENV_MAPPING = {
|
|
20
20
|
'gitlab_url' => 'GITLAB_URL',
|
|
21
21
|
'gitlab_token' => 'GITLAB_TOKEN',
|
|
22
|
+
'github_token' => 'GITHUB_TOKEN',
|
|
23
|
+
'github_api_url' => 'GITHUB_API_URL',
|
|
24
|
+
'github_review_author' => 'GITHUB_REVIEW_AUTHOR',
|
|
22
25
|
'jira_url' => 'JIRA_URL',
|
|
23
26
|
'jira_login' => 'JIRA_LOGIN',
|
|
24
27
|
'jira_password' => 'JIRA_PASSWORD',
|
|
@@ -35,12 +38,6 @@ module Aireview
|
|
|
35
38
|
'anthropic' => 'ANTHROPIC_API_KEY',
|
|
36
39
|
'openrouter' => 'OPENROUTER_API_KEY'
|
|
37
40
|
}.freeze
|
|
38
|
-
PROVIDER_KEYS_MAPPING = {
|
|
39
|
-
'gemini' => 'GEMINI_API_KEYS',
|
|
40
|
-
'openai' => 'OPENAI_API_KEYS',
|
|
41
|
-
'anthropic' => 'ANTHROPIC_API_KEYS',
|
|
42
|
-
'openrouter' => 'OPENROUTER_API_KEYS'
|
|
43
|
-
}.freeze
|
|
44
41
|
CONTEXT_ENV = {
|
|
45
42
|
'max_diff_chars' => 'MAX_DIFF_CHARS',
|
|
46
43
|
'max_mr_description_chars' => 'MAX_MR_DESCRIPTION_CHARS',
|
|
@@ -54,6 +51,7 @@ module Aireview
|
|
|
54
51
|
].freeze
|
|
55
52
|
LLM_STAGE_ENV_SUFFIXES = %w[PROVIDER MODEL TEMPERATURE MAX_PROMPT_CHARS FALLBACK_MODEL START].freeze
|
|
56
53
|
IMAGE_DEFAULTS_ENV = 'AIREVIEW_DEFAULTS'
|
|
54
|
+
BUNDLED_DEFAULTS = File.expand_path('../../config/defaults.yml', __dir__)
|
|
57
55
|
|
|
58
56
|
module_function
|
|
59
57
|
|
|
@@ -61,11 +59,11 @@ module Aireview
|
|
|
61
59
|
load_dotenv(cwd)
|
|
62
60
|
|
|
63
61
|
file_path = config_path ? File.expand_path(config_path, cwd) : discover_file(cwd, '.aireview.yml')
|
|
62
|
+
upper = [file_layer(file_path), ConfigLayers::Layer.new(name: ConfigLayers::ENV_LAYER, data: env_config(env))]
|
|
64
63
|
layers = [
|
|
65
64
|
ConfigLayers::Layer.new(name: ConfigLayers::BUILT_IN_LAYER, data: Config::DEFAULTS),
|
|
66
|
-
image_defaults_layer(env),
|
|
67
|
-
|
|
68
|
-
ConfigLayers::Layer.new(name: ConfigLayers::ENV_LAYER, data: env_config(env))
|
|
65
|
+
image_defaults_layer(env) || bundled_defaults_layer(upper.compact),
|
|
66
|
+
*upper
|
|
69
67
|
].compact
|
|
70
68
|
|
|
71
69
|
Config.new(config_path: File.file?(file_path) ? file_path : nil, logger: logger, layers: layers)
|
|
@@ -79,7 +77,7 @@ module Aireview
|
|
|
79
77
|
LLM_STAGE_ENV_SUFFIXES.map { |suffix| "LLM_#{stage.upcase}_#{suffix}" }
|
|
80
78
|
end
|
|
81
79
|
[
|
|
82
|
-
*ENV_MAPPING.values, *PROVIDER_KEY_MAPPING.values,
|
|
80
|
+
*ENV_MAPPING.values, *PROVIDER_KEY_MAPPING.values, 'LLM_API_KEY',
|
|
83
81
|
*LLM_ENV, *stage_env, *CONTEXT_ENV.values
|
|
84
82
|
].uniq
|
|
85
83
|
end
|
|
@@ -118,6 +116,24 @@ module Aireview
|
|
|
118
116
|
ConfigLayers::Layer.new(name: ConfigLayers::IMAGE_LAYER, path: path, data: read_yaml(path))
|
|
119
117
|
end
|
|
120
118
|
|
|
119
|
+
# Outside the image (gem install, a checkout) the same defaults.yml stands
|
|
120
|
+
# in for the image layer, but only when no upper layer names a model (CLI
|
|
121
|
+
# models are checked in Config#with_overrides): a setup with models of its
|
|
122
|
+
# own keeps working exactly as before, and one without models would fail
|
|
123
|
+
# with "LLM models are required" anyway.
|
|
124
|
+
def bundled_defaults_layer(upper)
|
|
125
|
+
return nil if upper.any? { |layer| models_configured?(layer.data) }
|
|
126
|
+
return nil unless File.file?(BUNDLED_DEFAULTS)
|
|
127
|
+
|
|
128
|
+
ConfigLayers::Layer.new(name: ConfigLayers::BUNDLED_LAYER, path: BUNDLED_DEFAULTS,
|
|
129
|
+
data: read_yaml(BUNDLED_DEFAULTS))
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def models_configured?(data)
|
|
133
|
+
Aireview::Utils.present?(Aireview::Utils.dig(data, 'llm', 'models')) ||
|
|
134
|
+
STAGES.any? { |stage| Aireview::Utils.present?(Aireview::Utils.dig(data, 'llm', stage, 'model')) }
|
|
135
|
+
end
|
|
136
|
+
|
|
121
137
|
def file_layer(file_path)
|
|
122
138
|
return nil unless File.file?(file_path)
|
|
123
139
|
|
|
@@ -133,7 +149,6 @@ module Aireview
|
|
|
133
149
|
.merge('llm' => Aireview::Utils.deep_merge(llm_env_config(env), pool_env_config(env)))
|
|
134
150
|
.merge(context_env_config(env))
|
|
135
151
|
.merge(provider_key_env_config(env))
|
|
136
|
-
.merge(provider_keys_env_config(env))
|
|
137
152
|
.merge(generic_api_key_env_config(env))
|
|
138
153
|
end
|
|
139
154
|
|
|
@@ -189,7 +204,7 @@ module Aireview
|
|
|
189
204
|
value.split(',').map(&:strip).reject(&:empty?).map { |item| ModelCandidate.parse_item(item) }
|
|
190
205
|
end
|
|
191
206
|
|
|
192
|
-
# LLM_MODELS=gemini/gemini-
|
|
207
|
+
# LLM_MODELS=gemini/gemini-4-hydrogen,gemini/gemini-3.8-flash;
|
|
193
208
|
# LLM_GENERATE_START / LLM_CRITIQUE_START — a model from the pool;
|
|
194
209
|
# LLM_CRITIQUE_RANK, LLM_CRITIQUE_ALLOW_WEAKER=true|false.
|
|
195
210
|
def pool_env_config(env)
|
|
@@ -223,13 +238,6 @@ module Aireview
|
|
|
223
238
|
end
|
|
224
239
|
end
|
|
225
240
|
|
|
226
|
-
def provider_keys_env_config(env)
|
|
227
|
-
PROVIDER_KEYS_MAPPING.each_with_object({}) do |(provider, env_key), config|
|
|
228
|
-
keys = env[env_key].to_s.split(',').map(&:strip).reject(&:empty?)
|
|
229
|
-
config["#{provider}_api_keys"] = keys unless keys.empty?
|
|
230
|
-
end
|
|
231
|
-
end
|
|
232
|
-
|
|
233
241
|
def generic_api_key_env_config(env)
|
|
234
242
|
api_key = env['LLM_API_KEY']
|
|
235
243
|
return {} if Aireview::Utils.blank?(api_key)
|
|
@@ -12,16 +12,20 @@ module Aireview
|
|
|
12
12
|
NOT_SHOWN_LIST_LIMIT = 20
|
|
13
13
|
TRAILER_RESERVE_CHARS = 400
|
|
14
14
|
|
|
15
|
+
# files_not_returned is a count, not a list: the platform returned fewer
|
|
16
|
+
# files than the merge request changes and did not say which ones.
|
|
15
17
|
Coverage = Struct.new(
|
|
16
18
|
:truncated_sections, :files_not_shown, :files_partial, :files_unavailable, :hunks_skipped,
|
|
19
|
+
:files_not_returned,
|
|
17
20
|
keyword_init: true
|
|
18
21
|
) do
|
|
19
22
|
def self.empty
|
|
20
|
-
new(truncated_sections: [], files_not_shown: [], files_partial: [], files_unavailable: [], hunks_skipped: []
|
|
23
|
+
new(truncated_sections: [], files_not_shown: [], files_partial: [], files_unavailable: [], hunks_skipped: [],
|
|
24
|
+
files_not_returned: 0)
|
|
21
25
|
end
|
|
22
26
|
|
|
23
27
|
def complete?
|
|
24
|
-
to_h.values.all?(&:empty?)
|
|
28
|
+
to_h.except(:files_not_returned).values.all?(&:empty?) && files_not_returned.zero?
|
|
25
29
|
end
|
|
26
30
|
end
|
|
27
31
|
|
|
@@ -39,6 +39,7 @@ module Aireview
|
|
|
39
39
|
|
|
40
40
|
def prepare(merge_request:, changes:, jira_issue: nil, critique: true)
|
|
41
41
|
coverage = ContextBudget::Coverage.empty
|
|
42
|
+
coverage.files_not_returned = merge_request['files_not_returned'].to_i
|
|
42
43
|
budget = context_budget(critique: critique)
|
|
43
44
|
sections = merge_request_sections(merge_request, coverage: coverage)
|
|
44
45
|
sections << jira_section(jira_issue, coverage: coverage) if jira_issue
|
|
@@ -116,6 +116,9 @@ module Aireview
|
|
|
116
116
|
list('files not shown', coverage.files_not_shown)
|
|
117
117
|
coverage.files_partial.each { |file| @out.puts(" #{file[:path]}: #{file[:shown]} of #{file[:total]} hunks") }
|
|
118
118
|
list('diff not available', coverage.files_unavailable)
|
|
119
|
+
return if coverage.files_not_returned.zero?
|
|
120
|
+
|
|
121
|
+
@out.puts(" files not returned by the platform: #{coverage.files_not_returned}")
|
|
119
122
|
end
|
|
120
123
|
|
|
121
124
|
def render_reserves(dry_run)
|