difflore-core 0.1.0

Core library for the difflore CLI — rule store, retrieval, MCP server, hooks, cloud sync. Not intended for direct use; depend on `difflore-cli` instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
use super::parse::{parse_issues, severity_rank};
use super::prompts::{build_segmented_prompt, build_user_prompt};
use super::{
    HttpReviewLlm, ReviewCheckInput, ReviewCheckResult, ReviewIssueRecord, ReviewLlm,
    ReviewPerspective, ReviewStats,
};
use crate::review_trajectory::{RuleSource, TrajectoryBuilder, TrajectoryStep};
use gate4agent::CliTool;

mod chat;
mod judge;
mod resolver;
mod rules;
mod validate;

pub(super) use chat::resolve_review_engine;
#[cfg(test)]
pub(super) use validate::{run_review_summary, verify_pass};

use chat::{
    PerspectiveRun, call_review_engine, get_active_provider, make_review_llm, run_one_perspective,
};
use rules::{build_recalled_verdicts, recall_past_verdicts_for_review};
use validate::{
    run_review_summary as run_review_summary_internal, verify_pass as verify_pass_internal,
};

pub(super) fn repo_scopes_for_input(input: &ReviewCheckInput) -> Vec<String> {
    let mut scopes = Vec::new();
    if let Some(repo) = input.repo_full_name.as_deref() {
        let repo = repo.trim();
        if !repo.is_empty() {
            scopes.push(repo.to_owned());
        }
    }
    for repo in &input.repo_full_name_aliases {
        let repo = repo.trim();
        if repo.is_empty() {
            continue;
        }
        if !scopes
            .iter()
            .any(|existing| existing.eq_ignore_ascii_case(repo))
        {
            scopes.push(repo.to_owned());
        }
    }
    scopes
}

/// Candidate rule pool size requested at REVIEW time when the applicability
/// judge is enabled. Review is latency-tolerant (unlike the 800ms commit
/// hook), so it pulls a deeper pool than the production default
/// ([`crate::context::DEFAULT_TOP_K_RULES`]) and lets the judge filter it
/// down to the rules that actually apply. The assembler's `rule_token_budget`
/// still bounds what reaches the prompt, so this only deepens the judge's
/// candidate set, not the final injected-rule volume.
const JUDGE_CANDIDATE_POOL_TOP_K: usize = 18;

/// Outcome of preparing the review's matched-rule context: the rendered
/// rules text plus the parallel id/title/count bookkeeping the rest of the
/// pipeline (attribution, trajectory, result) consumes.
struct PreparedReviewRules {
    rules_text: Option<String>,
    count: i32,
    ids: Vec<String>,
    titles: Vec<String>,
}

/// Join a pool of rule items into the `rules_text` blob the review prompt
/// expects (one rule's `content` per section, blank-line separated) — the
/// same shape `intent_filter::maybe_rerank_for_review` produces. Used to
/// rebuild the text after the applicability judge drops rules from the pool.
fn rules_text_from_items(
    items: &[crate::context::types::ContextSourceItemRecord],
) -> Option<String> {
    if items.is_empty() {
        return None;
    }
    Some(
        items
            .iter()
            .map(|item| item.content.clone())
            .collect::<Vec<_>>()
            .join("\n\n"),
    )
}

/// Shared matched-rule preparation for both the single-pass and
/// multi-perspective review paths.
///
/// Steps: (1) retrieve the candidate rule pool via the context orchestrator —
/// deepened to [`JUDGE_CANDIDATE_POOL_TOP_K`] when the applicability judge is
/// enabled; (2) apply the existing intent rerank; (3) when the judge is
/// enabled, ask the review LLM which recalled rules actually apply to this
/// diff and drop the rest, rebuilding `rules_text`/ids/titles from the
/// survivors.
///
/// With the judge flag OFF this is behaviourally identical to the inline
/// logic it replaced: same `DEFAULT_TOP_K_RULES` retrieval, same rerank, and
/// the original `rules_text` is returned untouched (no rebuild), preserving
/// byte-identical prompts.
async fn prepare_review_rules(
    db: &sqlx::SqlitePool,
    input: &ReviewCheckInput,
    retrieval_query: &str,
    repo_scopes: &[String],
    judge_llm: &dyn ReviewLlm,
    review_engine: &crate::models::ReviewEngineRecord,
    log_tag: &str,
) -> PreparedReviewRules {
    if input.project_id.is_empty() {
        return PreparedReviewRules {
            rules_text: None,
            count: 0,
            ids: Vec::new(),
            titles: Vec::new(),
        };
    }

    let judge_enabled = review_engine.rule_applicability_judge;
    // Deepen the candidate pool only when the judge will filter it back down,
    // so the flag-off path keeps the exact production retrieval depth.
    let top_k_override = judge_enabled.then_some(JUDGE_CANDIDATE_POOL_TOP_K);

    let pack = match crate::context::orchestrator::prepare_with_hint_and_repo_scopes_with_top_k(
        db,
        &input.project_id,
        input.engine.as_deref().unwrap_or("claude"),
        retrieval_query,
        Some("review"),
        input.file_path.as_deref(),
        repo_scopes,
        top_k_override,
    )
    .await
    {
        Ok(pack) => pack,
        Err(e) => {
            eprintln!("[{log_tag}] context prepare failed: {e:?}, proceeding without rules");
            return PreparedReviewRules {
                rules_text: None,
                count: 0,
                ids: Vec::new(),
                titles: Vec::new(),
            };
        }
    };

    let reranked =
        crate::context::intent_filter::maybe_rerank_for_review(&pack.rule_context, retrieval_query);

    // Flag-OFF fast path: reproduce the original inline bookkeeping exactly
    // (rerank count from the reranked len, else the assembler's
    // `metadata.rule_count`; ids/titles from whichever item set), so the
    // default review path stays byte-for-byte identical.
    if !judge_enabled {
        let (rules_text, count, ids, titles) = if let Some((reranked, rules_text)) = reranked {
            let count = i32::try_from(reranked.len()).unwrap_or(i32::MAX);
            let (ids, titles) = matched_rule_ids_and_titles(&reranked);
            (rules_text, count, ids, titles)
        } else {
            let count = i32::try_from(pack.metadata.rule_count).unwrap_or(i32::MAX);
            let (ids, titles) = matched_rule_ids_and_titles(&pack.rule_context);
            (pack.sections.rules, count, ids, titles)
        };
        return PreparedReviewRules {
            rules_text,
            count,
            ids,
            titles,
        };
    }

    // Judge-ON path: resolve a concrete working pool (the reranked items when
    // rerank is active, else the full retrieved context), let the judge drop
    // non-applicable rules, then derive text/ids/titles from the FINAL pool so
    // all three stay mutually consistent — `rules_text_from_items` joins rule
    // `content` exactly as the rerank path does, so an unchanged pool yields
    // the same text the rerank would have.
    let pool: Vec<_> = match reranked {
        Some((reranked, _reranked_text)) => reranked,
        None => pack.rule_context.clone(),
    };

    let pool = judge::run_applicability_judge(judge_llm, true, &input.diff_content, pool).await;

    let rules_text = rules_text_from_items(&pool);
    let count = i32::try_from(pool.len()).unwrap_or(i32::MAX);
    let (ids, titles) = matched_rule_ids_and_titles(&pool);
    PreparedReviewRules {
        rules_text,
        count,
        ids,
        titles,
    }
}

pub(in super::super) fn count_blocking(issues: &[ReviewIssueRecord]) -> (u32, u32) {
    let mut blocking = 0u32;
    let mut non_blocking = 0u32;
    for i in issues {
        match i.severity.as_str() {
            "error" | "critical" => blocking += 1,
            _ => non_blocking += 1,
        }
    }
    (blocking, non_blocking)
}

pub(in super::super) fn collect_diff_files(diff: &str) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for line in diff.lines() {
        if let Some(rest) = line.strip_prefix("+++ ") {
            let file = rest.strip_prefix("b/").unwrap_or(rest).trim().to_owned();
            if file.is_empty() || file == "/dev/null" {
                continue;
            }
            if !out.iter().any(|f| f == &file) {
                out.push(file);
            }
        }
    }
    out
}

/// How `run_review` talks to the LLM: remote HTTP provider when one is
/// configured, else a local agent CLI driven through `gate4agent` (Claude
/// Code, Codex, Gemini, or `OpenCode` — whichever is installed). Resolved
/// once per review by `resolve_review_engine`.
#[derive(Debug, Clone)]
pub enum ReviewEngine {
    HttpProvider {
        provider_name: String,
        base_url: String,
        api_key: String,
        model: String,
    },
    AgentCli {
        tool: CliTool,
        /// Empty string lets the CLI default kick in. Populated when the
        /// user explicitly configured a model in `providers setup`.
        model: String,
    },
}

/// Merge issues from multiple perspective passes.
///
/// Dedupe key: `(file, line, rule_id_or_rule)`. When duplicates exist, the
/// issue with the highest severity wins, and the `perspectives` vector
/// lists every perspective (in fixed canonical order) whose pass flagged it.
pub fn merge_perspective_issues(
    per_perspective: Vec<(ReviewPerspective, Vec<ReviewIssueRecord>)>,
) -> Vec<ReviewIssueRecord> {
    use std::collections::BTreeMap;

    // Preserve first-seen order while still deduping by key.
    let mut order: Vec<String> = Vec::new();
    let mut merged: BTreeMap<String, ReviewIssueRecord> = BTreeMap::new();

    for (persp, issues) in per_perspective {
        let persp_name = persp.name();
        for mut issue in issues {
            let key = format!(
                "{}|{}|{}",
                issue.file.as_deref().unwrap_or_default(),
                issue.line.map(|n| n.to_string()).unwrap_or_default(),
                issue.rule_id.as_deref().unwrap_or(issue.rule.as_str()),
            );

            if let Some(existing) = merged.get_mut(&key) {
                if severity_rank(&issue.severity) > severity_rank(&existing.severity) {
                    let mut perspectives = existing.perspectives.clone();
                    if !perspectives.iter().any(|p| p == persp_name) {
                        perspectives.push(persp_name.to_owned());
                    }
                    issue.perspectives = perspectives;
                    *existing = issue;
                } else if !existing.perspectives.iter().any(|p| p == persp_name) {
                    existing.perspectives.push(persp_name.to_owned());
                }
            } else {
                if !issue.perspectives.iter().any(|p| p == persp_name) {
                    issue.perspectives.push(persp_name.to_owned());
                }
                order.push(key.clone());
                merged.insert(key, issue);
            }
        }
    }

    // Reorder perspectives on each issue to a stable canonical order.
    let canonical = [
        ReviewPerspective::Safety.name(),
        ReviewPerspective::Performance.name(),
        ReviewPerspective::Style.name(),
        ReviewPerspective::Docs.name(),
        ReviewPerspective::ApiDesign.name(),
    ];

    order
        .into_iter()
        .filter_map(|k| merged.remove(&k))
        .map(|mut issue| {
            let mut sorted: Vec<String> = canonical
                .iter()
                .filter(|c| issue.perspectives.iter().any(|p| p == *c))
                .map(ToString::to_string)
                .collect();
            // Append any non-canonical perspectives at the end (defensive).
            for p in &issue.perspectives {
                if !sorted.iter().any(|s| s == p) {
                    sorted.push(p.clone());
                }
            }
            issue.perspectives = sorted;
            issue
        })
        .collect()
}

fn matched_rule_ids_and_titles(
    rule_context: &[crate::context::types::ContextSourceItemRecord],
) -> (Vec<String>, Vec<String>) {
    let ids = rule_context
        .iter()
        .map(|item| item.source_id.clone())
        .collect();
    let titles = rule_context
        .iter()
        .map(|item| {
            item.title
                .clone()
                .filter(|title| !title.trim().is_empty())
                .unwrap_or_else(|| item.source_id.clone())
        })
        .collect();
    (ids, titles)
}

fn issue_text_for_attribution(issue: &ReviewIssueRecord) -> String {
    format!(
        "{} {} {} {}",
        issue.rule,
        issue.message,
        issue.suggestion.as_deref().unwrap_or_default(),
        issue.file.as_deref().unwrap_or_default(),
    )
    .to_ascii_lowercase()
}

fn contains_any(text: &str, needles: &[&str]) -> bool {
    needles.iter().any(|needle| text.contains(needle))
}

fn is_workflow_pin_issue(issue: &ReviewIssueRecord) -> bool {
    let text = issue_text_for_attribution(issue);
    let workflow_context = issue
        .file
        .as_deref()
        .is_some_and(|file| file.contains(".github/workflows/"))
        || contains_any(
            &text,
            &[
                "github action",
                "actions/",
                "uses:",
                "workflow",
                "checkout@",
            ],
        );
    let pin_context = contains_any(
        &text,
        &[
            "pin",
            "sha",
            "immutable",
            "mutable",
            "floating",
            "@main",
            "@master",
        ],
    );
    workflow_context && pin_context
}

fn is_workflow_pin_rule_title(title: &str) -> bool {
    let text = title.to_ascii_lowercase();
    contains_any(&text, &["github action", "actions", "workflow"])
        && contains_any(&text, &["pin", "sha", "immutable"])
}

fn attribution_tokens(text: &str) -> std::collections::BTreeSet<String> {
    const STOPWORDS: &[&str] = &[
        "the", "and", "for", "from", "into", "with", "this", "that", "must", "should", "would",
        "could", "rule", "rules", "file", "line", "review", "code", "when", "where", "than",
        "then", "they", "them", "your", "their",
    ];
    text.split(|c: char| !c.is_ascii_alphanumeric())
        .filter_map(|raw| {
            let token = raw.trim().to_ascii_lowercase();
            if token.is_empty() || token.len() < 3 {
                return None;
            }
            let token = match token.as_str() {
                "shas" => "sha".to_owned(),
                "references" => "reference".to_owned(),
                other => other.to_owned(),
            };
            (!STOPWORDS.contains(&token.as_str())).then_some(token)
        })
        .collect()
}

fn infer_rule_id_for_issue(
    issue: &ReviewIssueRecord,
    matched_rule_ids: &[String],
    matched_rule_titles: &[String],
) -> Option<String> {
    if matched_rule_ids.is_empty() {
        return None;
    }

    if is_workflow_pin_issue(issue)
        && let Some((idx, _)) = matched_rule_titles
            .iter()
            .enumerate()
            .find(|(_, title)| is_workflow_pin_rule_title(title))
    {
        return matched_rule_ids.get(idx).cloned();
    }

    let issue_tokens = attribution_tokens(&issue_text_for_attribution(issue));
    if issue_tokens.is_empty() {
        return None;
    }

    let mut best: Option<(usize, f32, usize)> = None;
    let mut second_best = 0.0_f32;
    for (idx, title) in matched_rule_titles.iter().enumerate() {
        let title_tokens = attribution_tokens(title);
        if title_tokens.is_empty() {
            continue;
        }
        let overlap = title_tokens
            .iter()
            .filter(|token| issue_tokens.contains(*token))
            .count();
        if overlap < 2 {
            continue;
        }
        let score = overlap as f32 / title_tokens.len() as f32;
        match best {
            Some((_, best_score, _)) if score > best_score => {
                second_best = best_score;
                best = Some((idx, score, overlap));
            }
            Some(_) => {
                second_best = second_best.max(score);
            }
            None => best = Some((idx, score, overlap)),
        }
    }

    let (idx, score, overlap) = best?;
    if overlap >= 2 && score >= 0.60 && score >= second_best + 0.15 {
        matched_rule_ids.get(idx).cloned()
    } else {
        None
    }
}

fn apply_missing_rule_attributions(
    issues: &mut [ReviewIssueRecord],
    matched_rule_ids: &[String],
    matched_rule_titles: &[String],
) {
    for issue in issues {
        if issue
            .rule_id
            .as_deref()
            .is_some_and(|rule_id| !rule_id.trim().is_empty())
        {
            continue;
        }
        if let Some(rule_id) = infer_rule_id_for_issue(issue, matched_rule_ids, matched_rule_titles)
        {
            issue.rule_id = Some(rule_id);
        }
    }
}

/// Apply hunk-aware line resolution to a batch of issues.
///
/// For each issue, parses the hunks of the file it touches out of the raw
/// unified `diff`, then snaps `issue.line` to the exact new-file line via
/// [`resolver::resolve_issue_lines`] using the model-supplied `snippets`
/// (parallel to `issues`) and the issue's claimed line. Issues whose file
/// can't be found in the diff, or that don't confidently match, are left
/// untouched — this only ever sharpens a line number, never regresses it.
///
/// `snippets[i]` is the optional verbatim source for `issues[i]` (from
/// `parse::extract_issue_snippets`); a shorter/empty slice is tolerated.
fn apply_hunk_line_resolution(
    issues: &mut [ReviewIssueRecord],
    snippets: &[Option<String>],
    diff: &str,
) {
    use std::collections::HashMap;

    // Parse hunks once per file path, lazily, and cache.
    let sections = split_diff_by_file(diff);
    let mut cache: HashMap<String, Vec<resolver::DiffHunk>> = HashMap::new();

    for (idx, issue) in issues.iter_mut().enumerate() {
        let Some(file) = issue.file.as_deref() else {
            continue;
        };
        let hunks = cache.entry(file.to_owned()).or_insert_with(|| {
            sections
                .get(file)
                .map(|section| resolver::parse_hunks(section))
                .unwrap_or_default()
        });
        if hunks.is_empty() {
            continue;
        }
        let target = resolver::ResolveTarget {
            snippet: snippets.get(idx).and_then(Clone::clone),
            claimed_line: issue.line,
        };
        if let Some((start, _end)) = resolver::resolve_issue_lines(&target, hunks) {
            issue.line = Some(start);
        }
    }
}

/// Split a multi-file unified diff into `path -> section` so each file's
/// hunks can be parsed independently. The section is the slice starting at
/// the file's `@@` hunks (file headers are tolerated by the hunk parser).
/// Keyed by the new-side (`+++ b/…`) path so it matches `issue.file`.
fn split_diff_by_file(diff: &str) -> std::collections::HashMap<String, String> {
    let mut out = std::collections::HashMap::new();
    let mut current_path: Option<String> = None;
    let mut current_body = String::new();

    let flush = |path: &mut Option<String>,
                 body: &mut String,
                 out: &mut std::collections::HashMap<String, String>| {
        if let Some(p) = path.take() {
            if body.trim().is_empty() {
                body.clear();
            } else {
                out.insert(p, std::mem::take(body));
            }
        }
    };

    for line in diff.lines() {
        if line.starts_with("diff --git ") {
            flush(&mut current_path, &mut current_body, &mut out);
            current_path = None;
            current_body.clear();
        } else if let Some(rest) = line.strip_prefix("+++ ") {
            let path = rest.strip_prefix("b/").unwrap_or(rest).trim();
            if !path.is_empty() && path != "/dev/null" {
                current_path = Some(path.to_owned());
            }
        }
        if current_path.is_some() {
            current_body.push_str(line);
            current_body.push('\n');
        }
    }
    flush(&mut current_path, &mut current_body, &mut out);
    out
}

/// Multi-perspective review.
pub async fn run_review_multi(
    db: &sqlx::SqlitePool,
    input: ReviewCheckInput,
) -> crate::Result<ReviewCheckResult> {
    run_review_multi_with_trajectory(db, input, None).await
}

/// Trajectory-aware variant of `run_review_multi`.
pub async fn run_review_multi_with_trajectory(
    db: &sqlx::SqlitePool,
    input: ReviewCheckInput,
    mut trajectory: Option<&mut TrajectoryBuilder>,
) -> crate::Result<ReviewCheckResult> {
    let trace_id = uuid::Uuid::new_v4().to_string();

    // 1. Get active provider (once — shared by all perspectives)
    let (provider_name, base_url, api_key, model) = get_active_provider(db).await?;

    let retrieval_intent = crate::context::intent_filter::build_review_intent_text(
        input.file_path.as_deref(),
        &input.diff_content,
    );
    let retrieval_query = if retrieval_intent.trim().is_empty() {
        input.diff_content.as_str()
    } else {
        retrieval_intent.as_str()
    };
    let repo_scopes = repo_scopes_for_input(&input);

    // Settings drive the (opt-in) applicability judge below and the
    // past-verdict recall / self-check / summary gating further down.
    let settings_for_recall = crate::settings::get().await.unwrap_or_default();

    // 2. Get matched rules via context engine (once — shared by all
    // perspectives). The applicability judge, when enabled, reuses the active
    // provider through a dedicated `HttpReviewLlm` built from the same tuple.
    let judge_llm = HttpReviewLlm {
        provider_name: provider_name.clone(),
        base_url: base_url.clone(),
        api_key: api_key.clone(),
        model: model.clone(),
    };
    let prepared = prepare_review_rules(
        db,
        &input,
        retrieval_query,
        &repo_scopes,
        &judge_llm,
        &settings_for_recall.review_engine,
        "review_check_multi",
    )
    .await;
    let PreparedReviewRules {
        rules_text,
        count: matched_rules,
        ids: matched_rule_ids,
        titles: matched_rule_titles,
    } = prepared;

    if let Some(tb) = trajectory.as_deref_mut() {
        tb.push(TrajectoryStep::ChunksRetrieved {
            count: matched_rules.try_into().unwrap_or(usize::MAX),
            symbols: matched_rule_titles.clone(),
            similarity_scores: Vec::new(),
        });
        tb.push(TrajectoryStep::RulesApplied {
            rule_ids: matched_rule_ids.clone(),
            source: RuleSource::Team,
        });
    }

    // 3. Shared user prompt (once — identical across perspectives)
    let user_prompt = build_user_prompt(
        &input.diff_content,
        rules_text.as_deref(),
        input.file_path.as_deref(),
    );
    let prompt_tokens_estimate = (i32::try_from(user_prompt.len())
        .unwrap_or(i32::MAX)
        .saturating_add(3))
        / 4;

    // 4. Past-verdict recall. Preview callers need a bounded first answer;
    // they can inspect memory separately with `difflore recall --diff`.
    let past_verdicts = if input.fast_preview {
        Vec::new()
    } else {
        recall_past_verdicts_for_review(
            &settings_for_recall,
            &input.diff_content,
            if input.project_id.is_empty() {
                None
            } else {
                Some(&input.project_id)
            },
            &repo_scopes,
        )
        .await
    };

    if let Some(tb) = trajectory.as_deref_mut() {
        let recalled_items = build_recalled_verdicts(&past_verdicts);
        let top_similarities: Vec<f32> =
            recalled_items.iter().map(|item| item.similarity).collect();
        tb.push(TrajectoryStep::PastVerdictsRecalled {
            count: past_verdicts.len(),
            top_similarities,
            recalled_items,
        });
    }

    let (safety_issues, perf_issues, style_issues, docs_issues, api_design_issues) = tokio::join!(
        run_one_perspective(PerspectiveRun {
            provider_name: &provider_name,
            base_url: &base_url,
            api_key: &api_key,
            model: &model,
            user_prompt: &user_prompt,
            perspective: ReviewPerspective::Safety,
            diff_content: &input.diff_content,
            past_verdicts: &past_verdicts,
        }),
        run_one_perspective(PerspectiveRun {
            provider_name: &provider_name,
            base_url: &base_url,
            api_key: &api_key,
            model: &model,
            user_prompt: &user_prompt,
            perspective: ReviewPerspective::Performance,
            diff_content: &input.diff_content,
            past_verdicts: &past_verdicts,
        }),
        run_one_perspective(PerspectiveRun {
            provider_name: &provider_name,
            base_url: &base_url,
            api_key: &api_key,
            model: &model,
            user_prompt: &user_prompt,
            perspective: ReviewPerspective::Style,
            diff_content: &input.diff_content,
            past_verdicts: &past_verdicts,
        }),
        run_one_perspective(PerspectiveRun {
            provider_name: &provider_name,
            base_url: &base_url,
            api_key: &api_key,
            model: &model,
            user_prompt: &user_prompt,
            perspective: ReviewPerspective::Docs,
            diff_content: &input.diff_content,
            past_verdicts: &past_verdicts,
        }),
        run_one_perspective(PerspectiveRun {
            provider_name: &provider_name,
            base_url: &base_url,
            api_key: &api_key,
            model: &model,
            user_prompt: &user_prompt,
            perspective: ReviewPerspective::ApiDesign,
            diff_content: &input.diff_content,
            past_verdicts: &past_verdicts,
        }),
    );

    if let Some(tb) = trajectory.as_deref_mut() {
        let per_call_input = u32::try_from(prompt_tokens_estimate).unwrap_or(u32::MAX);
        for perspective in ReviewPerspective::all() {
            tb.push(TrajectoryStep::LlmCall {
                perspective: perspective.name().to_owned(),
                input_tokens: per_call_input,
                output_tokens: 0,
                raw_output: None,
            });
        }
    }

    let issues = merge_perspective_issues(vec![
        (ReviewPerspective::Safety, safety_issues),
        (ReviewPerspective::Performance, perf_issues),
        (ReviewPerspective::Style, style_issues),
        (ReviewPerspective::Docs, docs_issues),
        (ReviewPerspective::ApiDesign, api_design_issues),
    ]);

    // Provider tuple is no longer needed after the per-perspective fan-out,
    // so move (don't clone) into the verify-pass LLM box.
    let llm: Box<dyn ReviewLlm> = Box::new(HttpReviewLlm {
        provider_name,
        base_url,
        api_key,
        model,
    });
    let pre_verify_count = issues.len();
    let issues = verify_pass_internal(
        llm.as_ref(),
        settings_for_recall.review_engine.self_check_enabled && !input.fast_preview,
        &input.diff_content,
        issues,
    )
    .await;

    if let Some(tb) = trajectory.as_deref_mut() {
        let keep_count = u32::try_from(issues.len()).unwrap_or(u32::MAX);
        let drop_count =
            u32::try_from(pre_verify_count.saturating_sub(issues.len())).unwrap_or(u32::MAX);
        let avg_confidence = if issues.is_empty() {
            0.0
        } else {
            issues.iter().map(|i| i.confidence).sum::<f32>() / (issues.len() as f32)
        };
        tb.push(TrajectoryStep::SelfCheck {
            keep_count,
            drop_count,
            avg_confidence,
        });
    }

    let mut issues = issues;
    apply_missing_rule_attributions(&mut issues, &matched_rule_ids, &matched_rule_titles);
    // Hunk-aware line snap (gated; default off). The multi-pass merge does not
    // retain snippets, so this path snaps using the claimed line only.
    if settings_for_recall.review_engine.hunk_line_resolution {
        apply_hunk_line_resolution(&mut issues, &[], &input.diff_content);
    }
    issues.sort_by(|a, b| {
        b.confidence
            .partial_cmp(&a.confidence)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    let summary = run_review_summary_internal(
        llm.as_ref(),
        settings_for_recall.review_engine.review_summary_enabled && !input.fast_preview,
        &input.diff_content,
        &issues,
    )
    .await;

    if let Some(tb) = trajectory.as_deref_mut() {
        let ids = issues
            .iter()
            .map(|i| i.rule_id.clone().unwrap_or_else(|| i.rule.clone()))
            .collect();
        tb.push(TrajectoryStep::FinalDecision {
            issue_ids_emitted: ids,
        });
    }

    let stats = ReviewStats {
        input_tokens: u32::try_from(prompt_tokens_estimate.max(0)).unwrap_or(u32::MAX),
        duration_ms: None,
        perspective_count: 5,
        past_verdicts_used: u32::try_from(past_verdicts.len()).unwrap_or(u32::MAX),
        trajectory_step_count: trajectory
            .as_deref()
            .map(|tb| u32::try_from(tb.len()).unwrap_or(u32::MAX)),
    };

    Ok(ReviewCheckResult {
        issues,
        matched_rules,
        matched_rule_ids,
        matched_rule_titles,
        prompt_tokens_estimate,
        trace_id,
        summary,
        stats: Some(stats),
    })
}

/// Stable label for the code path selected by `run_review_smart`.
pub const fn select_review_mode(multi_perspective: bool) -> &'static str {
    if multi_perspective { "multi" } else { "single" }
}

#[allow(clippy::items_after_test_module)]
#[cfg(test)]
mod tests {
    use super::*;

    fn review_input(repo: Option<&str>, aliases: Vec<&str>) -> ReviewCheckInput {
        ReviewCheckInput {
            project_id: "project-1".to_owned(),
            diff_content: String::new(),
            file_path: None,
            engine: None,
            review_id: None,
            repo_full_name: repo.map(str::to_owned),
            repo_full_name_aliases: aliases.into_iter().map(str::to_owned).collect(),
            fast_preview: false,
        }
    }

    #[test]
    fn repo_scopes_include_origin_and_upstream_aliases() {
        let input = review_input(
            Some("difflore-fixtures/router"),
            vec!["difflore-fixtures/router", "tanstack/router"],
        );

        assert_eq!(
            repo_scopes_for_input(&input),
            vec![
                "difflore-fixtures/router".to_owned(),
                "tanstack/router".to_owned(),
            ],
        );
    }

    #[test]
    fn repo_scopes_dedupe_aliases_case_insensitively() {
        let input = review_input(
            Some("TanStack/router"),
            vec!["tanstack/router", "  ", "difflore-fixtures/router"],
        );

        assert_eq!(
            repo_scopes_for_input(&input),
            vec![
                "TanStack/router".to_owned(),
                "difflore-fixtures/router".to_owned(),
            ],
        );
    }

    #[test]
    fn fast_preview_input_marks_secondary_review_passes_skippable() {
        let mut input = review_input(Some("owner/repo"), vec![]);
        assert!(!input.fast_preview);

        input.fast_preview = true;

        assert!(input.fast_preview);
    }

    #[test]
    fn workflow_pin_issue_gets_recalled_rule_id_when_model_omits_it() {
        let issue = ReviewIssueRecord {
            severity: "warning".to_owned(),
            rule: "Pin GitHub Actions to immutable references".to_owned(),
            rule_id: None,
            message: "actions/checkout@main is a floating ref".to_owned(),
            file: Some(".github/workflows/pr.yml".to_owned()),
            line: Some(26),
            suggestion: Some("Use a commit SHA instead of main.".to_owned()),
            source_badge: None,
            perspectives: Vec::new(),
            confidence: 0.98,
        };

        let rule_id = infer_rule_id_for_issue(
            &issue,
            &[
                "pin-actions-rule".to_owned(),
                "version-update-rule".to_owned(),
            ],
            &[
                "Pin Actions to commit SHAs".to_owned(),
                "Update GitHub Actions versions atomically".to_owned(),
            ],
        );

        assert_eq!(rule_id.as_deref(), Some("pin-actions-rule"));
    }

    #[test]
    fn missing_rule_attribution_stays_empty_for_ambiguous_text() {
        let mut issues = vec![ReviewIssueRecord {
            severity: "warning".to_owned(),
            rule: "Improve code".to_owned(),
            rule_id: None,
            message: "This should be cleaner.".to_owned(),
            file: Some("src/lib.rs".to_owned()),
            line: Some(1),
            suggestion: Some("Refactor it.".to_owned()),
            source_badge: None,
            perspectives: Vec::new(),
            confidence: 0.8,
        }];

        apply_missing_rule_attributions(
            &mut issues,
            &["pin-actions-rule".to_owned()],
            &["Pin Actions to commit SHAs".to_owned()],
        );

        assert!(issues[0].rule_id.is_none());
    }

    const MULTI_FILE_DIFF: &str = "\
diff --git a/src/a.rs b/src/a.rs
index 1111111..2222222 100644
--- a/src/a.rs
+++ b/src/a.rs
@@ -5,4 +5,5 @@ fn a() {
     let x = 1;
     let y = 2;
+    let z = dangerous(x, y);
     done();
 }
diff --git a/src/b.rs b/src/b.rs
index 3333333..4444444 100644
--- a/src/b.rs
+++ b/src/b.rs
@@ -20,3 +20,4 @@ fn b() {
     setup();
+    let secret = read_env();
     teardown();
";

    fn issue_at(file: &str, line: i32) -> ReviewIssueRecord {
        ReviewIssueRecord {
            severity: "warning".to_owned(),
            rule: "r".to_owned(),
            rule_id: None,
            message: "m".to_owned(),
            file: Some(file.to_owned()),
            line: Some(line),
            suggestion: None,
            source_badge: None,
            perspectives: Vec::new(),
            confidence: 0.9,
        }
    }

    #[test]
    fn split_diff_by_file_keys_on_new_side_path() {
        let map = split_diff_by_file(MULTI_FILE_DIFF);
        assert_eq!(map.len(), 2);
        assert!(map.contains_key("src/a.rs"));
        assert!(map.contains_key("src/b.rs"));
        assert!(map["src/a.rs"].contains("dangerous(x, y)"));
        assert!(map["src/b.rs"].contains("read_env()"));
    }

    #[test]
    fn hunk_resolution_snaps_issue_to_exact_line_via_snippet() {
        let mut issues = vec![issue_at("src/a.rs", 999), issue_at("src/b.rs", 1)];
        let snippets = vec![
            Some("let z = dangerous(x, y);".to_owned()),
            Some("let secret = read_env();".to_owned()),
        ];
        apply_hunk_line_resolution(&mut issues, &snippets, MULTI_FILE_DIFF);
        // a.rs new-side: 5,6 context, 7 added `z`, ... → line 7.
        assert_eq!(issues[0].line, Some(7));
        // b.rs new-side: 20 context, 21 added `secret` → line 21.
        assert_eq!(issues[1].line, Some(21));
    }

    #[test]
    fn hunk_resolution_leaves_line_when_file_not_in_diff() {
        let mut issues = vec![issue_at("src/unknown.rs", 42)];
        let snippets = vec![Some("whatever".to_owned())];
        apply_hunk_line_resolution(&mut issues, &snippets, MULTI_FILE_DIFF);
        assert_eq!(issues[0].line, Some(42), "untouched when no diff section");
    }

    #[test]
    fn hunk_resolution_snaps_via_claimed_line_without_snippet() {
        // No snippets at all (mirrors the multi-perspective path). The model
        // claimed line 6 in a.rs, which is a real new-side context line.
        let mut issues = vec![issue_at("src/a.rs", 6)];
        apply_hunk_line_resolution(&mut issues, &[], MULTI_FILE_DIFF);
        assert_eq!(issues[0].line, Some(6));
    }

    #[test]
    fn hunk_resolution_tolerates_shorter_snippet_slice() {
        // snippets slice shorter than issues — extra issues fall back to
        // claimed-line snap, no panic.
        let mut issues = vec![issue_at("src/a.rs", 7), issue_at("src/b.rs", 21)];
        let snippets = vec![Some("let z = dangerous(x, y);".to_owned())];
        apply_hunk_line_resolution(&mut issues, &snippets, MULTI_FILE_DIFF);
        assert_eq!(issues[0].line, Some(7));
        assert_eq!(issues[1].line, Some(21));
    }

    #[test]
    fn hunk_resolution_falls_back_when_nothing_matches() {
        // Backward-safety guarantee: the file IS in the diff, but neither the
        // snippet nor the claimed line corresponds to any hunk line. Hunk
        // attribution must decline (resolver → None) and leave the model's
        // claimed line exactly as-is, so we degrade to the prior token-overlap
        // behaviour instead of inventing a wrong line.
        let mut issues = vec![issue_at("src/a.rs", 900)];
        let snippets = vec![Some("text that appears nowhere in the diff".to_owned())];
        apply_hunk_line_resolution(&mut issues, &snippets, MULTI_FILE_DIFF);
        assert_eq!(
            issues[0].line,
            Some(900),
            "no confident hunk match → claimed line preserved (no regression)"
        );
    }

    #[test]
    fn hunk_resolution_maps_multiline_finding_to_range_start() {
        // A finding whose snippet spans two consecutive new-side lines must be
        // attributed to the START of that exact range. In a.rs the added
        // `dangerous` line is 7 and the following context `done();` is 8, so a
        // two-line snippet resolves to the range 7..=8; `issue.line` carries
        // the range start (7). This is the "exact changed line RANGE" mapping
        // the hunk resolver provides over token overlap.
        let mut issues = vec![issue_at("src/a.rs", 1)];
        let snippets = vec![Some("let z = dangerous(x, y);\ndone();".to_owned())];
        apply_hunk_line_resolution(&mut issues, &snippets, MULTI_FILE_DIFF);
        assert_eq!(
            issues[0].line,
            Some(7),
            "multi-line finding anchors on the first line of the changed range"
        );
    }

    // === hunk_line_resolution end-to-end coverage (real captured diff) ===
    //
    // Real diff + real LLM response captured from a live `difflore fix
    // --preview` run against difflore-test-e2e/hono/src/compose.ts
    // (Sonnet via claude-cli). Ground-truth new-file lines: 42, 43, 52.
    //
    // This drives the EXACT production gate path: split_diff_by_file →
    // parse_hunks → resolve_issue_lines, via apply_hunk_line_resolution.
    // OFF = claimed line untouched; ON = apply_hunk_line_resolution.

    const HONO_DIFF: &str = "\
--- a/src/compose.ts
+++ b/src/compose.ts
@@ -39,6 +39,9 @@ export const compose = <E extends Env = Env>(
       let isError = false
       let handler

+      const apiKey = \"sk-live-1234567890abcdef\"
+      console.log(\"dispatching middleware at index \" + i + \" key=\" + apiKey)
+
       if (middleware[i]) {
         handler = middleware[i][0][0]
         context.req.routeIndex = i
@@ -46,6 +49,10 @@ export const compose = <E extends Env = Env>(
         handler = (i === middleware.length && next) || undefined
       }

+      if (handler == null) {
+        handler = middleware[i][0][0]
+      }
+
       if (handler) {
         try {
           res = await handler(context, () => dispatch(i + 1))
";

    // (real_snippet, ground_truth_new_file_line) for each issue the model
    // actually returned.
    fn hono_cases() -> Vec<(String, i32)> {
        vec![
            (
                "      const apiKey = \"sk-live-1234567890abcdef\"".to_owned(),
                42,
            ),
            (
                "      console.log(\"dispatching middleware at index \" + i + \" key=\" + apiKey)"
                    .to_owned(),
                43,
            ),
            (
                "      if (handler == null) {\n        handler = middleware[i][0][0]\n      }"
                    .to_owned(),
                52,
            ),
        ]
    }

    /// Build the issue set + snippet set for a scenario.
    /// `claimed[i]` is the line the model "claimed"; `with_snippet` chooses
    /// whether the real snippet is supplied (snippet path) or not (claimed-
    /// line-only path).
    fn build(claimed: &[i32], with_snippet: bool) -> (Vec<ReviewIssueRecord>, Vec<Option<String>>) {
        let cases = hono_cases();
        let issues = claimed
            .iter()
            .map(|&l| issue_at("src/compose.ts", l))
            .collect();
        let snippets = cases
            .iter()
            .map(|(s, _)| if with_snippet { Some(s.clone()) } else { None })
            .collect();
        (issues, snippets)
    }

    fn ground_truth() -> Vec<i32> {
        hono_cases().into_iter().map(|(_, gt)| gt).collect()
    }

    /// Count how many issues land exactly on ground truth.
    fn precise_count(issues: &[ReviewIssueRecord], gt: &[i32]) -> usize {
        issues
            .iter()
            .zip(gt.iter())
            .filter(|(iss, g)| iss.line == Some(**g))
            .count()
    }

    #[test]
    fn measure_real_response_off_equals_on_no_change() {
        // Scenario A: the REAL claimed lines the model returned (already
        // correct). OFF should equal ON — feature is a no-op here.
        let gt = ground_truth();
        let claimed_real = gt.clone(); // model claimed 42,43,52 == GT
        let (off_issues, snippets) = build(&claimed_real, true);
        let off_precise = precise_count(&off_issues, &gt);

        let (mut on_issues, _) = build(&claimed_real, true);
        apply_hunk_line_resolution(&mut on_issues, &snippets, HONO_DIFF);
        let on_precise = precise_count(&on_issues, &gt);

        let on_lines: Vec<_> = on_issues.iter().map(|i| i.line).collect();
        eprintln!(
            "[MEASURE A real-response] OFF precise={off_precise}/3 ON precise={on_precise}/3 ON_lines={on_lines:?}"
        );
        assert_eq!(off_precise, 3, "model already correct on this diff");
        assert_eq!(on_precise, 3, "ON keeps all correct (no regression)");
    }

    #[test]
    fn measure_corrupted_lines_with_real_snippets() {
        // Scenario B: simulate the documented model failure modes the
        // resolver exists to fix (resolver.rs docstring: "diff-relative or
        // off-by-N numbers, or count from the hunk header"), with the REAL
        // snippets preserved.
        //
        //   issue0 (GT 42): diff-relative  -> 4  (counts within hunk body)
        //   issue1 (GT 43): off-by-2 high  -> 45
        //   issue2 (GT 52): hunk-header rel-> 49 (the @@ new_start, off by 3)
        let gt = ground_truth();
        let corrupted = vec![4, 45, 49];

        let (off_issues, _) = build(&corrupted, true);
        let off_precise = precise_count(&off_issues, &gt);

        let (mut on_issues, snippets) = build(&corrupted, true);
        apply_hunk_line_resolution(&mut on_issues, &snippets, HONO_DIFF);
        let on_precise = precise_count(&on_issues, &gt);

        let off_lines: Vec<_> = off_issues.iter().map(|i| i.line).collect();
        let on_lines: Vec<_> = on_issues.iter().map(|i| i.line).collect();
        eprintln!(
            "[MEASURE B corrupted+snippet] GT={gt:?} corrupted={corrupted:?} \
             OFF_lines={off_lines:?} (precise {off_precise}/3) \
             ON_lines={on_lines:?} (precise {on_precise}/3)"
        );
        // Honest assertions: ON must recover all 3 via snippet; OFF gets 0.
        assert_eq!(off_precise, 0, "all corrupted lines are wrong");
        assert_eq!(on_precise, 3, "snippet match recovers exact line for all");
    }

    #[test]
    fn measure_corrupted_lines_without_snippets_claimed_only() {
        // Scenario C: same corruption but NO snippets (mirrors the multi-
        // perspective merge path, which drops per-issue snippets). Tests the
        // weaker claimed-line snap + checks for any REGRESSION (snapping a
        // line further from GT than where it started).
        let gt = ground_truth();
        let corrupted = vec![4, 45, 49];

        let (off_issues, _) = build(&corrupted, false);
        let off_precise = precise_count(&off_issues, &gt);

        let (mut on_issues, _) = build(&corrupted, false);
        apply_hunk_line_resolution(&mut on_issues, &[], HONO_DIFF);
        let on_precise = precise_count(&on_issues, &gt);

        // Regression detector: for each issue, did ON move it strictly
        // farther from GT than OFF was?
        let mut regressions = 0;
        for ((off, on), &g) in off_issues.iter().zip(on_issues.iter()).zip(gt.iter()) {
            let off_d = (off.line.unwrap_or(g) - g).abs();
            let on_d = (on.line.unwrap_or(g) - g).abs();
            if on_d > off_d {
                regressions += 1;
            }
        }

        let off_lines: Vec<_> = off_issues.iter().map(|i| i.line).collect();
        let on_lines: Vec<_> = on_issues.iter().map(|i| i.line).collect();
        eprintln!(
            "[MEASURE C corrupted no-snippet] GT={gt:?} corrupted={corrupted:?} \
             OFF_lines={off_lines:?} (precise {off_precise}/3) \
             ON_lines={on_lines:?} (precise {on_precise}/3) regressions={regressions}"
        );
        // No assertion on precise count here (claimed-only is weaker); the
        // eprintln carries the numbers. Guard only against regressions.
        assert_eq!(
            regressions, 0,
            "claimed-line snap must not move AWAY from GT"
        );
    }

    #[test]
    fn measure_claimed_only_boundary_offbyone() {
        // Scenario C': the one case the claimed-line snap CAN help —
        // claimed lands just OUTSIDE the hunk (within the +/-2 tolerance) on
        // a non-existent new-side position. GT 43; claim 48 = one past
        // hunk1's last new line (47) -> snaps back to 47 (closer to GT, not
        // exact). GT 52; claim 59 = one past hunk2 last line (58) -> 58.
        let gt = vec![43, 52];
        let corrupted = vec![48, 59];
        let issues_off: Vec<_> = corrupted
            .iter()
            .map(|&l| issue_at("src/compose.ts", l))
            .collect();
        let mut issues_on = issues_off.clone();
        apply_hunk_line_resolution(&mut issues_on, &[], HONO_DIFF);
        let on_lines: Vec<_> = issues_on.iter().map(|i| i.line).collect();
        // Did ON move each line CLOSER to GT than OFF?
        let mut improved = 0;
        let mut regressed = 0;
        for ((off, on), &g) in issues_off.iter().zip(issues_on.iter()).zip(gt.iter()) {
            let off_d = (off.line.unwrap_or(g) - g).abs();
            let on_d = (on.line.unwrap_or(g) - g).abs();
            if on_d < off_d {
                improved += 1;
            }
            if on_d > off_d {
                regressed += 1;
            }
        }
        eprintln!(
            "[MEASURE C' claimed-only boundary] GT={gt:?} corrupted={corrupted:?} \
             ON_lines={on_lines:?} improved(closer)={improved} regressed={regressed}"
        );
        // Snap clamps an out-of-range claim back to the nearest real line:
        // strictly closer, never farther. (Closer, not exact — snippetless.)
        assert_eq!(regressed, 0);
    }

    #[test]
    fn ambiguous_duplicate_snippet_prefers_claimed_occurrence() {
        // The snippet `handler = middleware[i][0][0]` occurs on the new side at
        // BOTH line 46 (pre-existing context) and line 53 (the newly-added
        // dup). For an issue whose claimed line is 53, the claimed-line
        // tie-break must keep the nearer occurrence (53) rather than snapping
        // to the first match (46) — the end-to-end check of the resolver guard
        // (without it, ON would move a correctly-claimed line onto line 46).
        let snippet = "      handler = middleware[i][0][0]".to_owned();
        let mut issues = vec![issue_at("src/compose.ts", 53)];
        let snippets = vec![Some(snippet)];
        apply_hunk_line_resolution(&mut issues, &snippets, HONO_DIFF);
        assert_eq!(
            issues[0].line,
            Some(53),
            "must keep the claimed duplicate (53), not snap to the far one (46)"
        );
    }
}

pub async fn run_review_smart(
    db: &sqlx::SqlitePool,
    input: ReviewCheckInput,
) -> crate::Result<ReviewCheckResult> {
    let settings = crate::settings::get().await.unwrap_or_default();
    let review_id = input.review_id.clone();
    let multi_perspective = settings.review_engine.multi_perspective;

    if review_id.is_none() {
        let started = std::time::Instant::now();
        let mut result = match select_review_mode(multi_perspective) {
            "multi" => run_review_multi(db, input).await?,
            _ => run_review(db, input).await?,
        };
        let duration_ms = u64::try_from(started.elapsed().as_millis()).unwrap_or(u64::MAX);
        if let Some(stats) = result.stats.as_mut() {
            stats.duration_ms = Some(duration_ms);
        }
        return Ok(result);
    }

    let started = std::time::Instant::now();
    let mut trajectory = TrajectoryBuilder::new();
    let mut result = match select_review_mode(multi_perspective) {
        "multi" => run_review_multi_with_trajectory(db, input, Some(&mut trajectory)).await?,
        _ => run_review_with_trajectory(db, input, Some(&mut trajectory)).await?,
    };
    let duration_ms = u64::try_from(started.elapsed().as_millis()).unwrap_or(u64::MAX);
    if let Some(stats) = result.stats.as_mut() {
        stats.duration_ms = Some(duration_ms);
    }

    if let Some(id) = review_id {
        upload_review_telemetry(id, duration_ms, multi_perspective, &result, trajectory).await;
    }

    Ok(result)
}

/// Fire-and-forget telemetry writeback.
async fn upload_review_telemetry(
    review_id: String,
    duration_ms: u64,
    multi_perspective: bool,
    result: &ReviewCheckResult,
    trajectory: TrajectoryBuilder,
) {
    let cloud = crate::cloud::client::CloudClient::create().await;
    if !cloud.is_logged_in() {
        return;
    }

    let past_verdicts_used = trajectory.steps().iter().find_map(|step| match step {
        TrajectoryStep::PastVerdictsRecalled { count, .. } => {
            Some(u32::try_from(*count).unwrap_or(u32::MAX))
        }
        _ => None,
    });

    let metrics_req = crate::cloud::api_types::RecordReviewMetricsRequest {
        input_tokens: Some(u32::try_from(result.prompt_tokens_estimate.max(0)).unwrap_or(u32::MAX)),
        output_tokens: None,
        estimated_cost_usd: None,
        duration_ms: Some(duration_ms),
        perspective_count: Some(if multi_perspective { 5 } else { 1 }),
        past_verdicts_used,
    };

    let pool = crate::db::init_db().await.ok();
    if let Some(pool) = pool {
        let q = crate::cloud::outbox::OutboxQueue::new(pool);
        let metrics_payload = serde_json::json!({
            "review_id": review_id,
            "req": metrics_req,
        });
        if let Ok(s) = serde_json::to_string(&metrics_payload) {
            let _ = q
                .enqueue(crate::cloud::outbox::kind::REVIEW_METRICS, &s)
                .await;
        }
        if !trajectory.is_empty() {
            let trajectory_payload = serde_json::json!({
                "pr_review_id": review_id,
                "steps": trajectory.into_json(),
            });
            if let Ok(s) = serde_json::to_string(&trajectory_payload) {
                let _ = q.enqueue(crate::cloud::outbox::kind::TRAJECTORY, &s).await;
            }
        }
        let _ = crate::cloud::outbox::drain_outbox(&q, &cloud, 8).await;
    } else {
        let _ = cloud.record_review_metrics(&review_id, metrics_req).await;
        if !trajectory.is_empty() {
            let _ = cloud
                .save_trajectory(&review_id, trajectory.into_json())
                .await;
        }
    }
}

pub async fn run_review(
    db: &sqlx::SqlitePool,
    input: ReviewCheckInput,
) -> crate::Result<ReviewCheckResult> {
    run_review_with_trajectory(db, input, None).await
}

/// Trajectory-aware variant of `run_review`.
pub async fn run_review_with_trajectory(
    db: &sqlx::SqlitePool,
    input: ReviewCheckInput,
    mut trajectory: Option<&mut TrajectoryBuilder>,
) -> crate::Result<ReviewCheckResult> {
    let trace_id = uuid::Uuid::new_v4().to_string();

    let engine = resolve_review_engine(db).await?;

    let retrieval_intent = crate::context::intent_filter::build_review_intent_text(
        input.file_path.as_deref(),
        &input.diff_content,
    );
    let retrieval_query = if retrieval_intent.trim().is_empty() {
        input.diff_content.as_str()
    } else {
        retrieval_intent.as_str()
    };
    let repo_scopes = repo_scopes_for_input(&input);

    // Loaded before rule prep so the (opt-in) applicability judge can gate on
    // it; reused for the recall / self-check / summary steps below.
    let settings = crate::settings::get().await.unwrap_or_default();

    // The applicability judge, when enabled, reuses the resolved review engine
    // through its own `ReviewLlm` (the engine is consumed later by the main
    // review call, so clone it here for the judge's separate round-trip).
    let judge_llm = make_review_llm(engine.clone());
    let prepared = prepare_review_rules(
        db,
        &input,
        retrieval_query,
        &repo_scopes,
        judge_llm.as_ref(),
        &settings.review_engine,
        "review_check",
    )
    .await;
    let PreparedReviewRules {
        rules_text,
        count: matched_rules,
        ids: matched_rule_ids,
        titles: matched_rule_titles,
    } = prepared;

    if let Some(tb) = trajectory.as_deref_mut() {
        tb.push(TrajectoryStep::ChunksRetrieved {
            count: matched_rules.try_into().unwrap_or(usize::MAX),
            symbols: matched_rule_titles.clone(),
            similarity_scores: Vec::new(),
        });
        tb.push(TrajectoryStep::RulesApplied {
            rule_ids: matched_rule_ids.clone(),
            source: RuleSource::Team,
        });
    }

    let past_verdicts = if input.fast_preview {
        Vec::new()
    } else {
        recall_past_verdicts_for_review(
            &settings,
            &input.diff_content,
            if input.project_id.is_empty() {
                None
            } else {
                Some(&input.project_id)
            },
            &repo_scopes,
        )
        .await
    };

    if let Some(tb) = trajectory.as_deref_mut() {
        let recalled_items = build_recalled_verdicts(&past_verdicts);
        let top_similarities: Vec<f32> =
            recalled_items.iter().map(|item| item.similarity).collect();
        tb.push(TrajectoryStep::PastVerdictsRecalled {
            count: past_verdicts.len(),
            top_similarities,
            recalled_items,
        });
    }

    let seg = build_segmented_prompt(
        None,
        &[],
        &input.diff_content,
        "",
        None,
        if past_verdicts.is_empty() {
            None
        } else {
            Some(&past_verdicts)
        },
    );
    let user_prompt = build_user_prompt(
        &input.diff_content,
        rules_text.as_deref(),
        input.file_path.as_deref(),
    );

    let prompt_tokens_estimate = (i32::try_from(user_prompt.len())
        .unwrap_or(i32::MAX)
        .saturating_add(3))
        / 4;

    if let Some(path) = crate::env::fix_dump_dir() {
        let _ = std::fs::create_dir_all(&path);
        let _ = std::fs::write(format!("{path}/last_user.txt"), &user_prompt);
        let _ = std::fs::write(
            format!("{path}/last_system.txt"),
            format!("{}{}", seg.stable_prefix, seg.dynamic_suffix),
        );
    }

    let ai_response = call_review_engine(&engine, &seg, &user_prompt).await?;
    if let Some(path) = crate::env::fix_dump_dir() {
        let _ = std::fs::write(format!("{path}/last_response.txt"), &ai_response);
    }

    if let Some(tb) = trajectory.as_deref_mut() {
        tb.push(TrajectoryStep::LlmCall {
            perspective: "single".to_owned(),
            input_tokens: u32::try_from(prompt_tokens_estimate.max(0)).unwrap_or(u32::MAX),
            output_tokens: 0,
            raw_output: None,
        });
    }

    let mut issues = parse_issues(&ai_response);
    // Snap each issue to its exact diff line before verification so the verify
    // pass and `difflore fix` see the precise location. Gated by
    // `review_engine.hunk_line_resolution` (default off -> no-op).
    if settings.review_engine.hunk_line_resolution {
        let snippets = super::parse::extract_issue_snippets(&ai_response);
        apply_hunk_line_resolution(&mut issues, &snippets, &input.diff_content);
    }
    let issues = issues;
    if crate::env::fix_debug() {
        eprintln!(
            "[fix-debug] single-pass raw_response_len={} parsed_issues={}",
            ai_response.len(),
            issues.len(),
        );
        if issues.is_empty() && ai_response.len() < 4000 {
            eprintln!("[fix-debug] response body: {ai_response}");
        }
    }

    let llm: Box<dyn ReviewLlm> = make_review_llm(engine);
    let pre_verify_count = issues.len();
    let issues = verify_pass_internal(
        llm.as_ref(),
        settings.review_engine.self_check_enabled && !input.fast_preview,
        &input.diff_content,
        issues,
    )
    .await;
    if crate::env::fix_debug() {
        eprintln!(
            "[fix-debug] verify: pre={} post={} self_check_enabled={}",
            pre_verify_count,
            issues.len(),
            settings.review_engine.self_check_enabled && !input.fast_preview,
        );
    }

    if let Some(tb) = trajectory.as_deref_mut() {
        let keep_count = u32::try_from(issues.len()).unwrap_or(u32::MAX);
        let drop_count =
            u32::try_from(pre_verify_count.saturating_sub(issues.len())).unwrap_or(u32::MAX);
        let avg_confidence = if issues.is_empty() {
            0.0
        } else {
            issues.iter().map(|i| i.confidence).sum::<f32>() / (issues.len() as f32)
        };
        tb.push(TrajectoryStep::SelfCheck {
            keep_count,
            drop_count,
            avg_confidence,
        });
    }

    let mut issues = issues;
    apply_missing_rule_attributions(&mut issues, &matched_rule_ids, &matched_rule_titles);
    issues.sort_by(|a, b| {
        b.confidence
            .partial_cmp(&a.confidence)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    let summary = run_review_summary_internal(
        llm.as_ref(),
        settings.review_engine.review_summary_enabled && !input.fast_preview,
        &input.diff_content,
        &issues,
    )
    .await;

    if let Some(tb) = trajectory.as_deref_mut() {
        let ids = issues
            .iter()
            .map(|i| i.rule_id.clone().unwrap_or_else(|| i.rule.clone()))
            .collect();
        tb.push(TrajectoryStep::FinalDecision {
            issue_ids_emitted: ids,
        });
    }

    let stats = ReviewStats {
        input_tokens: u32::try_from(prompt_tokens_estimate.max(0)).unwrap_or(u32::MAX),
        duration_ms: None,
        perspective_count: 1,
        past_verdicts_used: u32::try_from(past_verdicts.len()).unwrap_or(u32::MAX),
        trajectory_step_count: trajectory
            .as_deref()
            .map(|tb| u32::try_from(tb.len()).unwrap_or(u32::MAX)),
    };

    Ok(ReviewCheckResult {
        issues,
        matched_rules,
        matched_rule_ids,
        matched_rule_titles,
        prompt_tokens_estimate,
        trace_id,
        summary,
        stats: Some(stats),
    })
}