ragrig-gpui 0.2.0

Reusable GPUI components for Ragrig RAG chat, citations, and reference ingestion
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
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
//! Bibliographic references extracted with GROBID.
//!
//! The heavy lifting lives in [`grobid_bibtex`]: its pre-pass fingerprints the
//! PDFs, extracts headers with GROBID, optionally completes them against
//! OpenAlex, optionally renames the PDFs, and merges fresh records into a
//! BibTeX file. This module adapts the result to the two UI needs of M4:
//!
//! - [`ReferenceEntry`] rows with authors, year, title, venue and a resolved
//!   PDF path;
//! - [`ReferenceRow`], a two-line row whose **Copy @key** and **Copy path**
//!   actions appear while the pointer is over it, plus a leading **+** that
//!   toggles multi-selection;
//! - [`ReferenceSelection`] and [`ReferenceSelectionBar`], the bulk actions for
//!   a set of selected rows;
//! - [`resolve_citations`], which turns the `@key` mentions of a question into
//!   one-shot context documents.
//!
//! Available with the `grobid` feature.

use biblatex::ChunksExt;
use std::{
    collections::{BTreeSet, HashSet},
    path::{Path, PathBuf},
    rc::Rc,
};

use gpui_kit::{
    App, ClipboardItem, ElementId, InteractiveElement as _, IntoElement, ParentElement as _,
    RenderOnce, SharedString, Styled as _, Window,
    assets::IconName,
    component::{
        ActiveTheme as _, Disableable as _, Selectable as _, Sizable as _,
        button::{Button, ButtonVariants as _},
        h_flex, v_flex,
    },
    div,
    prelude::FluentBuilder as _,
    px,
};
use grobid_bibtex::{
    Collection,
    cite::{CiteState, Located},
    files::collect_pdfs,
    prepass::{
        BibtexTarget, PrepassConfig, PrepassError, PrepassObserver, PrepassProgress, PrepassReport,
        PrepassStage, prepass_with,
    },
};
use ragrig::{AttachedDocument, DocumentParsers};

/// Server and pipeline options for one reference extraction run.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReferenceOptions {
    /// Base URL of the GROBID server.
    pub grobid_url: String,
    /// Rename PDFs to `Key - Authors - Title - Year` after parsing.
    pub rename: bool,
    /// Complete extracted headers against OpenAlex.
    pub openalex: bool,
    /// Concurrent GROBID/OpenAlex requests.
    pub workers: usize,
}

impl Default for ReferenceOptions {
    fn default() -> Self {
        Self {
            grobid_url: "http://localhost:8070".to_owned(),
            rename: false,
            openalex: false,
            workers: 4,
        }
    }
}

impl ReferenceOptions {
    /// Options with the given server URL and the safe defaults (no rename, no
    /// OpenAlex completion).
    pub fn new(grobid_url: impl Into<String>) -> Self {
        Self {
            grobid_url: grobid_url.into(),
            ..Self::default()
        }
    }
}

/// One row of the reference list.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReferenceEntry {
    /// Canonical citation key.
    pub key: String,
    /// Short author string, e.g. `Kano & Smith`.
    pub authors: String,
    /// Publication year, when known.
    pub year: String,
    /// Title, when known.
    pub title: String,
    /// Journal, book, series, or publisher, when known.
    pub source: String,
    /// Resolved PDF path, when one exists.
    pub path: Option<PathBuf>,
}

impl ReferenceEntry {
    /// The text the **Copy @key** action writes.
    pub fn copy_key_text(&self) -> String {
        format!("@{}", self.key)
    }

    /// One-line citation for tooltips, skipping unknown parts.
    pub fn citation_line(&self) -> String {
        let mut parts = Vec::new();
        if !self.authors.is_empty() {
            parts.push(self.authors.clone());
        }
        if !self.year.is_empty() {
            parts.push(format!("({})", self.year));
        }
        let mut line = parts.join(" ");
        for part in [&self.title, &self.source] {
            if !part.is_empty() {
                if !line.is_empty() {
                    line.push_str(". ");
                }
                line.push_str(part);
            }
        }
        line
    }

    /// Authors, year, and venue on one muted line.
    pub fn meta_line(&self) -> String {
        [&self.authors, &self.year, &self.source]
            .into_iter()
            .filter(|part| !part.is_empty())
            .cloned()
            .collect::<Vec<_>>()
            .join(" · ")
    }

    /// A title or a placeholder.
    pub fn display_title(&self) -> &str {
        if self.title.is_empty() {
            "Untitled"
        } else {
            &self.title
        }
    }
}

/// What a row's hover action copied.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ReferenceCopy {
    /// The bare citation key. The clipboard already holds `@key`.
    Key(String),
    /// The resolved PDF path.
    Path(PathBuf),
}

/// Progress stage of a reference extraction.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ReferenceStage {
    /// PDFs are parsed by GROBID.
    #[default]
    Parsing,
    /// Records are completed against OpenAlex.
    Completing,
    /// PDFs are renamed.
    Renaming,
    /// Records are merged into the BibTeX file.
    Merging,
}

impl ReferenceStage {
    /// Short human-readable name.
    pub fn label(self) -> &'static str {
        match self {
            Self::Parsing => "parsing",
            Self::Completing => "completing",
            Self::Renaming => "renaming",
            Self::Merging => "merging",
        }
    }
}

/// A snapshot of a running extraction.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ReferenceProgress {
    /// Current stage.
    pub stage: ReferenceStage,
    /// 1-based step within the stage.
    pub done: usize,
    /// Steps in the stage.
    pub total: usize,
    /// File the step is about.
    pub path: Option<PathBuf>,
}

impl ReferenceProgress {
    /// One line for a status bar.
    pub fn summary(&self) -> String {
        let file = self
            .path
            .as_ref()
            .and_then(|path| path.file_name())
            .and_then(|name| name.to_str());
        match file {
            Some(file) => format!(
                "GROBID · {} {}/{} · {file}",
                self.stage.label(),
                self.done.min(self.total),
                self.total
            ),
            None => format!(
                "GROBID · {} {}/{}",
                self.stage.label(),
                self.done.min(self.total),
                self.total
            ),
        }
    }
}

impl From<PrepassProgress> for ReferenceProgress {
    fn from(progress: PrepassProgress) -> Self {
        Self {
            stage: match progress.stage {
                PrepassStage::Parsing => ReferenceStage::Parsing,
                PrepassStage::Completing => ReferenceStage::Completing,
                PrepassStage::Renaming => ReferenceStage::Renaming,
                PrepassStage::Merging => ReferenceStage::Merging,
            },
            done: progress.done,
            total: progress.total,
            path: progress.path,
        }
    }
}

/// What one extraction run did.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ReferenceReport {
    /// Files parsed by GROBID in this run.
    pub parsed: usize,
    /// Files that came from the manifest cache.
    pub cached: usize,
    /// Files renamed.
    pub renamed: usize,
    /// Entries newly written to the bibliography.
    pub merged: usize,
    /// Records skipped as duplicates.
    pub duplicates: usize,
    /// Per-file failures.
    pub failed: Vec<String>,
}

impl ReferenceReport {
    /// One line for a status bar.
    pub fn summary(&self) -> String {
        let mut summary = format!(
            "{} parsed · {} cached · {} merged",
            self.parsed, self.cached, self.merged
        );
        if self.duplicates > 0 {
            summary.push_str(&format!(" · {} duplicates", self.duplicates));
        }
        if self.renamed > 0 {
            summary.push_str(&format!(" · {} renamed", self.renamed));
        }
        if !self.failed.is_empty() {
            summary.push_str(&format!(" · {} failed", self.failed.len()));
        }
        summary
    }

    fn from_prepass(report: PrepassReport) -> Self {
        Self {
            parsed: report.parsed,
            cached: report.cached,
            renamed: report.renamed,
            merged: report.merged,
            duplicates: report.duplicates,
            failed: report
                .failed
                .into_iter()
                .map(|failure| format!("{}: {}", failure.path.display(), failure.reason))
                .collect(),
        }
    }
}

/// Maximum number of `@key` papers attached to one query.
pub const MAX_CITATIONS: usize = 3;
/// Maximum characters attached per `@key` paper.
pub const MAX_CITATION_CHARS: usize = 100_000;

/// One `@key` paper that was attached to a query.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CitationAttachment {
    /// Canonical citation key.
    pub key: String,
    /// PDF or document that supplied the context.
    pub path: PathBuf,
}

/// Result of resolving the `@key` mentions of one query.
#[derive(Default)]
pub(crate) struct CitationResolution {
    /// Documents ready for Ragrig's attach strategy.
    pub attachments: Vec<AttachedDocument>,
    /// Canonical keys that were attached (aliases count).
    pub resolved: Vec<String>,
    /// One attachment per distinct file, for source chips.
    pub attached: Vec<CitationAttachment>,
    /// One readable line per key that could not be attached.
    pub missed: Vec<String>,
}

/// Resolve `@key` mentions against the bibliography and the active corpora.
///
/// Mirrors the host-side resolution in Sciscraper: the recorded `file` field
/// first, then a corpus file named after the key; up to [`MAX_CITATIONS`]
/// papers, each truncated to [`MAX_CITATION_CHARS`] characters. Misses never
/// abort the query; they are returned for the transcript note.
pub(crate) fn resolve_citations(
    state: &mut CiteState,
    bibliography: &Path,
    corpora: &[PathBuf],
    keys: &[String],
    parsers: &DocumentParsers,
) -> CitationResolution {
    let mut resolution = CitationResolution::default();
    if keys.is_empty() {
        return resolution;
    }
    if !bibliography.is_file() {
        for key in keys {
            resolution.missed.push(format!(
                "@{key} — no bibliography at {}",
                bibliography.display()
            ));
        }
        return resolution;
    }

    let corpus_paths: Vec<&Path> = corpora.iter().map(PathBuf::as_path).collect();
    let located = state.locate(keys, Some(bibliography), &corpus_paths);
    let mut attached_paths: HashSet<PathBuf> = HashSet::new();
    for item in located {
        match item {
            Located::Found { key, path, .. } => {
                if !attached_paths.insert(path.clone()) {
                    // Aliases of an already attached document count as
                    // resolved, but do not produce a second context block.
                    resolution.resolved.push(key);
                    continue;
                }
                if resolution.attachments.len() >= MAX_CITATIONS {
                    resolution.missed.push(format!(
                        "@{key} — attachment limit ({MAX_CITATIONS}) reached"
                    ));
                    continue;
                }
                match parsers.parse(&path) {
                    Ok(text) => {
                        resolution.attachments.push(AttachedDocument {
                            name: format!("@{key}"),
                            content: truncate_text(text, MAX_CITATION_CHARS),
                        });
                        resolution.resolved.push(key.clone());
                        resolution.attached.push(CitationAttachment { key, path });
                    }
                    Err(error) => resolution.missed.push(format!(
                        "@{key} — cannot read {}: {error:#}",
                        path.display()
                    )),
                }
            }
            other => resolution.missed.push(other.describe()),
        }
    }
    resolution
}

/// Build the `@key @key` text for a set of selected rows.
pub fn keys_text(entries: &[ReferenceEntry]) -> String {
    entries
        .iter()
        .map(|entry| entry.copy_key_text())
        .collect::<Vec<_>>()
        .join(" ")
}

/// Build the newline-separated absolute paths of a set of rows.
pub fn paths_text(entries: &[ReferenceEntry]) -> String {
    entries
        .iter()
        .filter_map(|entry| entry.path.as_ref())
        .map(|path| path.display().to_string())
        .collect::<Vec<_>>()
        .join("\n")
}

/// Keys selected in a reference list, keyed by citation key.
///
/// Selection survives list refreshes: [`retain_existing`](Self::retain_existing)
/// drops keys that disappeared, and rows are always returned in list order.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ReferenceSelection {
    keys: BTreeSet<String>,
}

impl ReferenceSelection {
    /// Number of selected keys.
    pub fn len(&self) -> usize {
        self.keys.len()
    }

    /// Whether nothing is selected.
    pub fn is_empty(&self) -> bool {
        self.keys.is_empty()
    }

    /// Whether `key` is selected.
    pub fn contains(&self, key: &str) -> bool {
        self.keys.contains(key)
    }

    /// Select `key`; returns whether it was newly added.
    pub fn insert(&mut self, key: impl Into<String>) -> bool {
        self.keys.insert(key.into())
    }

    /// Deselect `key`; returns whether it was selected.
    pub fn remove(&mut self, key: &str) -> bool {
        self.keys.remove(key)
    }

    /// Toggle `key`; returns the new selection state.
    pub fn toggle(&mut self, key: impl Into<String>) -> bool {
        let key = key.into();
        if self.keys.remove(&key) {
            false
        } else {
            self.keys.insert(key);
            true
        }
    }

    /// Clear every selection.
    pub fn clear(&mut self) {
        self.keys.clear();
    }

    /// Drop selected keys that no longer exist in `entries`.
    pub fn retain_existing(&mut self, entries: &[ReferenceEntry]) {
        let available: HashSet<&str> = entries.iter().map(|entry| entry.key.as_str()).collect();
        self.keys.retain(|key| available.contains(key.as_str()));
    }

    /// The selected entries, in the order they appear in `entries`.
    pub fn entries<'a>(&self, entries: &'a [ReferenceEntry]) -> Vec<&'a ReferenceEntry> {
        entries
            .iter()
            .filter(|entry| self.keys.contains(&entry.key))
            .collect()
    }
}

fn truncate_text(text: String, max_chars: usize) -> String {
    if text.chars().count() <= max_chars {
        text
    } else {
        text.chars().take(max_chars).collect()
    }
}

pub(crate) enum ExtractionError {
    Cancelled,
    Failed(String),
}

#[derive(Default)]
pub(crate) struct ExtractionObserver<'a> {
    pub progress: Option<&'a mut (dyn FnMut(ReferenceProgress) + Send)>,
    pub cancel: Option<&'a (dyn Fn() -> bool + Send + Sync)>,
}

impl PrepassObserver for ExtractionObserver<'_> {
    fn progress(&mut self, progress: PrepassProgress) {
        if let Some(callback) = self.progress.as_mut() {
            callback(progress.into());
        }
    }

    fn cancelled(&self) -> bool {
        self.cancel.is_some_and(|cancel| cancel())
    }
}

/// Run the GROBID pre-pass and return the merged bibliography as UI rows.
pub(crate) async fn extract(
    folder: &Path,
    bibliography: &Path,
    options: &ReferenceOptions,
    observer: &mut ExtractionObserver<'_>,
) -> Result<(Vec<ReferenceEntry>, ReferenceReport), ExtractionError> {
    if let Some(parent) = bibliography.parent() {
        std::fs::create_dir_all(parent).map_err(|error| {
            ExtractionError::Failed(format!("cannot create '{}': {error}", parent.display()))
        })?;
    }

    let mut config = PrepassConfig::new(folder);
    config.bibtex = Some(BibtexTarget {
        path: bibliography.to_path_buf(),
        link: true,
    });
    config.grobid_url.clone_from(&options.grobid_url);
    config.openalex = options.openalex;
    config.rename = options.rename;
    config.workers = options.workers.max(1);

    let report = match prepass_with(&config, observer).await {
        Ok(report) => report,
        Err(PrepassError::Cancelled) => return Err(ExtractionError::Cancelled),
        Err(error) => return Err(ExtractionError::Failed(format!("{error:#}"))),
    };
    let entries = load_entries(bibliography, folder).map_err(ExtractionError::Failed)?;
    Ok((entries, ReferenceReport::from_prepass(report)))
}

/// Load reference rows from a merged bibliography.
pub fn load_entries(bibliography: &Path, folder: &Path) -> Result<Vec<ReferenceEntry>, String> {
    let collection = Collection::load_or_new(bibliography)
        .map_err(|error| format!("cannot read '{}': {error}", bibliography.display()))?;
    let pdfs = collect_pdfs(folder).unwrap_or_default();
    let mut entries: Vec<ReferenceEntry> = collection
        .iter()
        .map(|entry| {
            let field = |names: &[&str]| -> String {
                for name in names {
                    if let Some(chunks) = entry.get(name) {
                        let text = normalize_text(&chunks.format_verbatim());
                        if !text.is_empty() {
                            return text;
                        }
                    }
                }
                String::new()
            };
            let key = entry.key.clone();
            let raw_file = field(&["file", "pdf"]);
            let file = (!raw_file.is_empty()).then(|| PathBuf::from(&raw_file));
            let year = {
                let year = field(&["year"]);
                if year.is_empty() {
                    first_year(&field(&["date"]))
                } else {
                    year
                }
            };
            ReferenceEntry {
                key: key.clone(),
                authors: short_authors(&field(&["author", "editor"])),
                year,
                title: field(&["title"]),
                source: field(&[
                    "journaltitle",
                    "journal",
                    "booktitle",
                    "series",
                    "publisher",
                    "institution",
                ]),
                path: resolve_document(&key, file.as_deref(), folder, &pdfs),
            }
        })
        .collect();

    entries.sort_by_cached_key(|entry| {
        (
            entry.authors.is_empty(),
            entry.authors.to_lowercase(),
            entry.year.clone(),
            entry.title.to_lowercase(),
        )
    });
    Ok(entries)
}

fn resolve_document(
    key: &str,
    file: Option<&Path>,
    folder: &Path,
    pdfs: &[PathBuf],
) -> Option<PathBuf> {
    if let Some(file) = file {
        let candidate = if file.is_absolute() {
            file.to_path_buf()
        } else {
            folder.join(file)
        };
        if candidate.is_file() {
            return Some(candidate);
        }
        // The bibliography may have been written from a different working
        // directory; try the recorded path as-is before falling back.
        if file.is_file() {
            return Some(file.to_path_buf());
        }
    }
    let key = key.to_ascii_lowercase();
    let prefix = format!("{key} - ");
    pdfs.iter()
        .find(|path| {
            path.file_stem()
                .and_then(|stem| stem.to_str())
                .map(|stem| {
                    let stem = stem.to_ascii_lowercase();
                    stem == key || stem.starts_with(&prefix)
                })
                .unwrap_or(false)
        })
        .cloned()
}

fn short_authors(raw: &str) -> String {
    let names: Vec<String> = raw
        .split(" and ")
        .map(|part| normalize_text(part.split(',').next().unwrap_or(part)))
        .filter(|name| !name.is_empty())
        .collect();
    match names.as_slice() {
        [] => String::new(),
        [one] => one.clone(),
        [first, second] => format!("{first} & {second}"),
        [first, ..] => format!("{first} et al."),
    }
}

fn first_year(date: &str) -> String {
    let digits: String = date
        .chars()
        .skip_while(|character| !character.is_ascii_digit())
        .take(4)
        .collect();
    if digits.len() == 4 {
        digits
    } else {
        String::new()
    }
}

fn normalize_text(text: &str) -> String {
    let mut without_braces = String::with_capacity(text.len());
    let mut depth = 0usize;
    for character in text.chars() {
        match character {
            '{' => depth += 1,
            '}' => depth = depth.saturating_sub(1),
            _ if depth == 0 => without_braces.push(character),
            _ => {}
        }
    }
    without_braces
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
}

type CopyHandler = Rc<dyn Fn(ReferenceCopy, &mut Window, &mut App)>;
type SelectHandler = Rc<dyn Fn(&mut Window, &mut App)>;

/// A two-line reference row with hover-revealed copy and selection actions.
///
/// While the pointer is over the row, a **+** appears at its leading edge and
/// **Copy @key** / **Copy path** at its trailing edge. Every control keeps its
/// slot in every frame, so the text never jumps when the actions become
/// visible. A row without a resolved PDF keeps the path button disabled and
/// explains why in its tooltip.
#[derive(IntoElement)]
pub struct ReferenceRow {
    index: usize,
    entry: ReferenceEntry,
    on_copy: Option<CopyHandler>,
    on_select: Option<SelectHandler>,
    selectable: bool,
    selected: bool,
}

impl ReferenceRow {
    /// A row for the entry at `index` in the current list.
    pub fn new(index: usize, entry: ReferenceEntry) -> Self {
        Self {
            index,
            entry,
            on_copy: None,
            on_select: None,
            selectable: false,
            selected: false,
        }
    }

    /// Receive the copied value, e.g. to update a status line.
    ///
    /// The row itself writes the clipboard text: `@key` for the key action and
    /// the absolute PDF path for the path action.
    pub fn on_copy(
        mut self,
        handler: impl Fn(ReferenceCopy, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_copy = Some(Rc::new(handler));
        self
    }

    /// Show the leading **+** toggle and report clicks.
    ///
    /// Implies [`selectable`](Self::selectable).
    pub fn on_select(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_select = Some(Rc::new(handler));
        self.selectable = true;
        self
    }

    /// Reserve the leading selection slot without a click handler.
    ///
    /// Use [`on_select`](Self::on_select) for an interactive toggle.
    pub fn selectable(mut self, selectable: bool) -> Self {
        self.selectable = selectable;
        self
    }

    /// Paint the row as selected and keep the leading toggle visible.
    pub fn selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }
}

impl RenderOnce for ReferenceRow {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.theme();
        let group: SharedString = format!("reference-row-{}", self.index).into();
        let key = self.entry.key.clone();
        let key_label = self.entry.copy_key_text();
        let meta = self.entry.meta_line();
        let path = self.entry.path.clone();
        let handler = self.on_copy;
        let selected = self.selected;
        let hover_bg = if selected {
            theme.primary.opacity(0.16)
        } else {
            theme.muted.opacity(0.28)
        };

        let toggle = (self.selectable || selected).then(|| {
            let on_select = self.on_select;
            let button = Button::new(ElementId::Name(
                format!("reference-{}-select", self.index).into(),
            ))
            .icon(if selected {
                IconName::Check
            } else {
                IconName::Plus
            })
            .small()
            .ghost()
            .selected(selected)
            .toggled(selected)
            .tooltip(if selected {
                "Deselect reference"
            } else {
                "Select reference"
            })
            .when_some(on_select, |button, on_select| {
                button.on_click(move |_, window, cx| on_select(window, cx))
            });
            div()
                .debug_selector(|| format!("reference-select-{}", self.index))
                .flex_none()
                .w(px(24.))
                .h(px(24.))
                .flex()
                .items_center()
                .justify_center()
                .when(!selected, |slot| {
                    slot.invisible()
                        .group_hover(group.clone(), |slot| slot.visible())
                })
                .child(button)
        });

        let copy_key = {
            let key = key.clone();
            let handler = handler.clone();
            Button::new(ElementId::Name(
                format!("reference-{}-copy-key", self.index).into(),
            ))
            .icon(IconName::AtSign)
            .xsmall()
            .ghost()
            .tooltip("Copy @key")
            .on_click(move |_, window, cx| {
                cx.write_to_clipboard(ClipboardItem::new_string(format!("@{key}")));
                if let Some(handler) = &handler {
                    handler(ReferenceCopy::Key(key.clone()), window, cx);
                }
            })
        };

        let copy_path = match path {
            Some(path) => {
                let handler = handler.clone();
                Button::new(ElementId::Name(
                    format!("reference-{}-copy-path", self.index).into(),
                ))
                .icon(IconName::FileText)
                .xsmall()
                .ghost()
                .tooltip("Copy path")
                .on_click(move |_, window, cx| {
                    cx.write_to_clipboard(ClipboardItem::new_string(path.display().to_string()));
                    if let Some(handler) = &handler {
                        handler(ReferenceCopy::Path(path.clone()), window, cx);
                    }
                })
            }
            None => Button::new(ElementId::Name(
                format!("reference-{}-copy-path", self.index).into(),
            ))
            .icon(IconName::FileText)
            .xsmall()
            .ghost()
            .disabled(true)
            .tooltip("No PDF path"),
        };

        h_flex()
            .id(ElementId::Name(
                format!("reference-row-{}", self.index).into(),
            ))
            .group(group.clone())
            .items_center()
            .gap_2()
            .p_2()
            .rounded(theme.radius)
            .border_1()
            .border_color(if selected {
                theme.primary.opacity(0.55)
            } else {
                theme.border
            })
            .when(selected, |this| this.bg(theme.primary.opacity(0.10)))
            .hover(move |this| this.bg(hover_bg))
            .when_some(toggle, |this, toggle| this.child(toggle))
            .child(
                div()
                    .w(px(3.))
                    .h(px(28.))
                    .flex_none()
                    .rounded_full()
                    .bg(theme.primary.opacity(if selected { 1.0 } else { 0.65 })),
            )
            .child(
                v_flex()
                    .flex_1()
                    .min_w_0()
                    .gap_1()
                    .child(
                        h_flex()
                            .min_w_0()
                            .gap_2()
                            .child(
                                div()
                                    .flex_none()
                                    .text_xs()
                                    .text_color(theme.primary)
                                    .child(key_label),
                            )
                            .when(!meta.is_empty(), |this| {
                                this.child(
                                    div()
                                        .min_w_0()
                                        .truncate()
                                        .text_xs()
                                        .text_color(theme.muted_foreground)
                                        .child(meta),
                                )
                            }),
                    )
                    .child(
                        div()
                            .min_w_0()
                            .truncate()
                            .text_sm()
                            .child(self.entry.display_title().to_owned()),
                    ),
            )
            .child(
                h_flex()
                    .flex_none()
                    .gap_1()
                    .invisible()
                    .group_hover(group, |this| this.visible())
                    .child(
                        div()
                            .debug_selector(|| "reference-copy-key".to_owned())
                            .child(copy_key),
                    )
                    .child(
                        div()
                            .debug_selector(|| "reference-copy-path".to_owned())
                            .child(copy_path),
                    ),
            )
    }
}

/// A bulk action offered by [`ReferenceSelectionBar`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ReferenceAction {
    /// Write the selected `@key` mentions to the clipboard.
    CopyKeys,
    /// Write the selected PDF paths to the clipboard.
    CopyPaths,
    /// Attach the selected papers to the next question. The host does this.
    Attach,
    /// Drop the selection. The host does this.
    Clear,
}

type ActionHandler = Rc<dyn Fn(ReferenceAction, &[ReferenceEntry], &mut Window, &mut App)>;

/// One floating bar for a multi-selection of references.
///
/// The bar writes **Copy @keys** and **Copy paths** itself. **Attach** and
/// **Clear** are shown only when a host action handler is installed; the host
/// owns the selection and the chat, so it performs those two actions. Build the
/// bar from the selected entries in list order and give it a stable parent.
#[derive(IntoElement)]
pub struct ReferenceSelectionBar {
    entries: Vec<ReferenceEntry>,
    on_action: Option<ActionHandler>,
}

impl ReferenceSelectionBar {
    /// A bar for `entries`, which should be the selected rows in list order.
    pub fn new(entries: impl IntoIterator<Item = ReferenceEntry>) -> Self {
        Self {
            entries: entries.into_iter().collect(),
            on_action: None,
        }
    }

    /// Receive every action after its built-in effect.
    ///
    /// Copy actions have already updated the clipboard when the handler runs.
    pub fn on_action(
        mut self,
        handler: impl Fn(ReferenceAction, &[ReferenceEntry], &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_action = Some(Rc::new(handler));
        self
    }
}

impl RenderOnce for ReferenceSelectionBar {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.theme();
        let count = self.entries.len();
        let path_text = paths_text(&self.entries);
        let paths_available = !path_text.is_empty();
        let handler = self.on_action;

        let copy_keys = {
            let handler = handler.clone();
            let entries = self.entries.clone();
            Button::new("reference-bulk-copy-keys")
                .icon(IconName::AtSign)
                .xsmall()
                .ghost()
                .tooltip("Copy @keys")
                .on_click(move |_, window, cx| {
                    cx.write_to_clipboard(ClipboardItem::new_string(keys_text(&entries)));
                    if let Some(handler) = &handler {
                        handler(ReferenceAction::CopyKeys, &entries, window, cx);
                    }
                })
        };
        let copy_paths = {
            let captured = path_text;
            let handler = handler.clone();
            let entries = self.entries.clone();
            Button::new("reference-bulk-copy-paths")
                .icon(IconName::FileText)
                .xsmall()
                .ghost()
                .disabled(!paths_available)
                .tooltip("Copy paths")
                .on_click(move |_, window, cx| {
                    cx.write_to_clipboard(ClipboardItem::new_string(captured.clone()));
                    if let Some(handler) = &handler {
                        handler(ReferenceAction::CopyPaths, &entries, window, cx);
                    }
                })
        };

        let mut bar = h_flex()
            .debug_selector(|| "reference-selection-bar".to_owned())
            .items_center()
            .gap_1()
            .px_2()
            .py_1()
            .rounded_full()
            .border_1()
            .border_color(theme.primary.opacity(0.55))
            .bg(theme.background)
            .shadow_md()
            .child(
                div()
                    .flex_none()
                    .px_1()
                    .text_xs()
                    .text_color(theme.primary)
                    .child(format!("{count} selected")),
            )
            .child(
                div()
                    .debug_selector(|| "reference-bulk-copy-keys-button".to_owned())
                    .child(copy_keys),
            )
            .child(
                div()
                    .debug_selector(|| "reference-bulk-copy-paths-button".to_owned())
                    .child(copy_paths),
            );

        if handler.is_some() {
            let attach_handler = handler.clone();
            let attach_entries = self.entries.clone();
            bar = bar.child(
                Button::new("reference-bulk-attach")
                    .icon(IconName::Paperclip)
                    .xsmall()
                    .ghost()
                    .disabled(!paths_available)
                    .tooltip("Attach to next question")
                    .on_click(move |_, window, cx| {
                        if let Some(handler) = &attach_handler {
                            handler(ReferenceAction::Attach, &attach_entries, window, cx);
                        }
                    }),
            );
            let clear_handler = handler.clone();
            let clear_entries = self.entries.clone();
            bar = bar.child(
                Button::new("reference-bulk-clear")
                    .icon(IconName::X)
                    .xsmall()
                    .ghost()
                    .tooltip("Clear selection")
                    .on_click(move |_, window, cx| {
                        if let Some(handler) = &clear_handler {
                            handler(ReferenceAction::Clear, &clear_entries, window, cx);
                        }
                    }),
            );
        }

        bar
    }
}

impl crate::rag::RagSettings {
    /// The reference options described by these settings.
    pub fn reference_options(&self) -> ReferenceOptions {
        ReferenceOptions {
            grobid_url: self.grobid_url.clone(),
            rename: self.rename_pdfs,
            openalex: self.openalex,
            workers: 4,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{cell::RefCell, rc::Rc};

    use gpui_kit::{Context, Modifiers, Render, TestAppContext, point};

    use super::*;

    fn temp_dir(label: &str) -> PathBuf {
        static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let counter = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let dir = std::env::temp_dir().join(format!(
            "ragrig-gpui-references-{label}-{}-{counter}",
            std::process::id()
        ));
        std::fs::create_dir_all(&dir).expect("temp dir");
        dir
    }

    const SAMPLE: &str = r#"
@article{Kano2025,
  author = {Kano, Fumihiro and Smith, John},
  year = {2025},
  title = {Holding a steady look at the human eye},
  journaltitle = {Journal of Vision},
  file = {PDF_PATH}
}
"#;

    #[test]
    fn entries_carry_metadata_and_resolve_the_linked_pdf() {
        let dir = temp_dir("read");
        let pdf = dir.join("paper.pdf");
        std::fs::write(&pdf, b"%PDF-1.4").expect("pdf");
        let bib = dir.join("references.bib");
        std::fs::write(&bib, SAMPLE.replace("PDF_PATH", &pdf.display().to_string())).expect("bib");

        let entries = load_entries(&bib, &dir).expect("entries");
        assert_eq!(entries.len(), 1);
        let entry = &entries[0];
        assert_eq!(entry.key, "Kano2025");
        assert_eq!(entry.authors, "Kano & Smith");
        assert_eq!(entry.year, "2025");
        assert_eq!(entry.title, "Holding a steady look at the human eye");
        assert_eq!(entry.source, "Journal of Vision");
        assert_eq!(entry.path.as_deref(), Some(pdf.as_path()));
        assert_eq!(entry.copy_key_text(), "@Kano2025");

        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn a_keyed_file_name_resolves_without_a_file_field() {
        let dir = temp_dir("keyed");
        let pdf = dir.join("Kano2025 - Holding a steady look.pdf");
        std::fs::write(&pdf, b"%PDF-1.4").expect("pdf");
        let bib = dir.join("references.bib");
        std::fs::write(
            &bib,
            r#"@article{Kano2025,
  author = {Kano, Fumihiro},
  date = {2024-03},
  title = {Gaze},
}"#,
        )
        .expect("bib");

        let entries = load_entries(&bib, &dir).expect("entries");
        assert_eq!(entries[0].path.as_deref(), Some(pdf.as_path()));
        assert_eq!(entries[0].year, "2024");

        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn a_missing_bibliography_is_empty() {
        let dir = temp_dir("missing");
        let entries = load_entries(&dir.join("missing.bib"), &dir).expect("entries");
        assert!(entries.is_empty());
        let _ = std::fs::remove_dir_all(dir);
    }

    fn cite_parsers() -> DocumentParsers {
        DocumentParsers::new(ragrig::build_parsers())
    }

    fn entry(key: &str, path: Option<PathBuf>) -> ReferenceEntry {
        ReferenceEntry {
            key: key.to_owned(),
            authors: "Kano".to_owned(),
            year: "2025".to_owned(),
            title: format!("Title of {key}"),
            source: String::new(),
            path,
        }
    }

    #[test]
    fn citations_resolve_recorded_files_and_explain_misses() {
        let dir = temp_dir("cite");
        let corpus = dir.join("corpus");
        std::fs::create_dir_all(&corpus).expect("corpus");
        let pdf = corpus.join("paper.md");
        std::fs::write(&pdf, "RECORDED PAPER BODY").expect("paper");
        let bib = dir.join("references.bib");
        std::fs::write(
            &bib,
            format!(
                "@article{{Kano2025,\n  author = {{Kano, Fumihiro}},\n  year = {{2025}},\n  title = {{Gaze}},\n  file = {{{}}}\n}}\n",
                pdf.display()
            ),
        )
        .expect("bib");

        let keys = vec!["kano2025".to_owned(), "Missing2020".to_owned()];
        let mut state = CiteState::default();
        let resolution = resolve_citations(
            &mut state,
            &bib,
            std::slice::from_ref(&corpus),
            &keys,
            &cite_parsers(),
        );

        assert_eq!(resolution.resolved, ["Kano2025"]);
        assert_eq!(resolution.attachments.len(), 1);
        assert_eq!(resolution.attachments[0].name, "@Kano2025");
        assert_eq!(resolution.attachments[0].content, "RECORDED PAPER BODY");
        assert_eq!(resolution.attached[0].path, pdf);
        assert_eq!(resolution.missed.len(), 1);
        assert!(resolution.missed[0].contains("Missing2020"));

        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn citation_aliases_share_one_attachment() {
        let dir = temp_dir("cite-alias");
        let corpus = dir.join("corpus");
        std::fs::create_dir_all(&corpus).expect("corpus");
        let paper = corpus.join("paper.md");
        std::fs::write(&paper, "SHARED BODY").expect("paper");
        let bib = dir.join("references.bib");
        std::fs::write(
            &bib,
            format!(
                "@article{{KeyA, title = {{A}}, file = {{{path}}}}}\n\
                 @article{{KeyB, title = {{B}}, file = {{{path}}}}}\n",
                path = paper.display()
            ),
        )
        .expect("bib");

        let keys = vec!["KeyA".to_owned(), "KeyB".to_owned()];
        let mut state = CiteState::default();
        let resolution = resolve_citations(
            &mut state,
            &bib,
            std::slice::from_ref(&corpus),
            &keys,
            &cite_parsers(),
        );

        assert_eq!(resolution.resolved, ["KeyA", "KeyB"]);
        assert_eq!(resolution.attachments.len(), 1);
        assert_eq!(resolution.attached.len(), 1);
        assert!(resolution.missed.is_empty());

        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn citations_cap_at_three_and_truncate_one_document() {
        let dir = temp_dir("cite-limit");
        let corpus = dir.join("corpus");
        std::fs::create_dir_all(&corpus).expect("corpus");
        let mut bib = String::new();
        for index in 1..=4 {
            let path = corpus.join(format!("paper{index}.md"));
            let body = if index == 1 {
                "x".repeat(MAX_CITATION_CHARS + 5)
            } else {
                format!("body {index}")
            };
            std::fs::write(&path, body).expect("paper");
            bib.push_str(&format!(
                "@article{{Key{index}, title = {{T{index}}}, file = {{{}}}}}\n",
                path.display()
            ));
        }
        let bib_path = dir.join("references.bib");
        std::fs::write(&bib_path, bib).expect("bib");

        let keys = (1..=4)
            .map(|index| format!("Key{index}"))
            .collect::<Vec<_>>();
        let mut state = CiteState::default();
        let resolution = resolve_citations(
            &mut state,
            &bib_path,
            std::slice::from_ref(&corpus),
            &keys,
            &cite_parsers(),
        );

        assert_eq!(resolution.attachments.len(), MAX_CITATIONS);
        assert_eq!(
            resolution.attachments[0].content.chars().count(),
            MAX_CITATION_CHARS
        );
        assert_eq!(resolution.resolved.len(), MAX_CITATIONS);
        assert_eq!(resolution.missed.len(), 1);
        assert!(resolution.missed[0].contains("limit"));

        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn a_keyed_corpus_file_is_citable_without_a_file_field() {
        let dir = temp_dir("cite-keyed");
        let corpus = dir.join("corpus");
        std::fs::create_dir_all(&corpus).expect("corpus");
        let paper = corpus.join("Kano2025 - Gaze.md");
        std::fs::write(&paper, "KEYED BODY").expect("paper");
        let bib = dir.join("references.bib");
        std::fs::write(&bib, "@article{Kano2025, title = {Gaze}}\n").expect("bib");

        let keys = vec!["kano2025".to_owned()];
        let mut state = CiteState::default();
        let resolution = resolve_citations(
            &mut state,
            &bib,
            std::slice::from_ref(&corpus),
            &keys,
            &cite_parsers(),
        );

        assert_eq!(resolution.resolved, ["Kano2025"]);
        assert_eq!(resolution.attached[0].path, paper);
        assert_eq!(resolution.attachments[0].content, "KEYED BODY");

        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn a_missing_bibliography_reports_every_key() {
        let dir = temp_dir("cite-missing-bib");
        let keys = vec!["One".to_owned(), "Two".to_owned()];
        let mut state = CiteState::default();
        let resolution = resolve_citations(
            &mut state,
            &dir.join("missing.bib"),
            std::slice::from_ref(&dir),
            &keys,
            &cite_parsers(),
        );

        assert!(resolution.attachments.is_empty());
        assert_eq!(resolution.missed.len(), 2);
        assert!(
            resolution
                .missed
                .iter()
                .all(|line| line.contains("bibliography"))
        );

        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn selection_is_keyed_and_follows_the_list_order() {
        let entries = vec![entry("B", None), entry("A", None)];
        let mut selection = ReferenceSelection::default();
        assert!(selection.insert("A"));
        assert!(selection.insert("B"));
        assert_eq!(selection.len(), 2);
        assert_eq!(
            selection
                .entries(&entries)
                .iter()
                .map(|entry| entry.key.as_str())
                .collect::<Vec<_>>(),
            ["B", "A"]
        );

        assert!(!selection.toggle("A"));
        assert_eq!(selection.len(), 1);
        assert!(selection.toggle("A"));

        selection.retain_existing(&entries[..1]);
        assert!(selection.contains("B"));
        assert!(!selection.contains("A"));
        selection.clear();
        assert!(selection.is_empty());
    }

    #[test]
    fn bulk_text_uses_citation_keys_and_skips_pathless_rows() {
        let entries = vec![
            entry("B", Some(PathBuf::from("/tmp/b.pdf"))),
            entry("A", None),
        ];
        assert_eq!(keys_text(&entries), "@B @A");
        assert_eq!(paths_text(&entries), "/tmp/b.pdf");
    }

    #[test]
    fn authors_and_citations_skip_unknown_parts() {
        assert_eq!(short_authors("Kano, Fumihiro"), "Kano");
        assert_eq!(
            short_authors("Kano, Fumihiro and Smith, John"),
            "Kano & Smith"
        );
        assert_eq!(
            short_authors("Kano, F. and Smith, J. and Doe, A."),
            "Kano et al."
        );
        assert_eq!(short_authors(""), "");

        let entry = ReferenceEntry {
            key: "Key".to_owned(),
            authors: "Kano".to_owned(),
            year: String::new(),
            title: "Title".to_owned(),
            source: String::new(),
            path: None,
        };
        assert_eq!(entry.citation_line(), "Kano. Title");
        assert_eq!(entry.meta_line(), "Kano");
        assert_eq!(
            ReferenceEntry {
                title: String::new(),
                ..entry
            }
            .display_title(),
            "Untitled"
        );
    }

    #[test]
    fn the_report_summarises_counts_and_failures() {
        let report = ReferenceReport {
            parsed: 2,
            cached: 1,
            renamed: 1,
            merged: 2,
            duplicates: 3,
            failed: vec!["a.pdf: unreadable".to_owned()],
        };
        assert_eq!(
            report.summary(),
            "2 parsed · 1 cached · 2 merged · 3 duplicates · 1 renamed · 1 failed"
        );
    }

    struct RowHost {
        entry: ReferenceEntry,
        copies: Rc<RefCell<Vec<ReferenceCopy>>>,
    }

    impl Render for RowHost {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            let copies = Rc::clone(&self.copies);
            div().w(px(420.)).h(px(80.)).child(
                ReferenceRow::new(0, self.entry.clone())
                    .on_copy(move |copy, _, _| copies.borrow_mut().push(copy)),
            )
        }
    }

    #[gpui_kit::test]
    fn hovering_reveals_copy_key_and_copy_path(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let dir = temp_dir("hover");
        let pdf = dir.join("paper.pdf");
        std::fs::write(&pdf, b"%PDF-1.4").expect("pdf");
        let entry = ReferenceEntry {
            key: "Kano2025".to_owned(),
            authors: "Kano".to_owned(),
            year: "2025".to_owned(),
            title: "Gaze".to_owned(),
            source: String::new(),
            path: Some(pdf.clone()),
        };
        let copies = Rc::new(RefCell::new(Vec::new()));
        let copies_for_host = Rc::clone(&copies);
        let (_, cx) = cx.add_window_view(move |_, _| RowHost {
            entry: entry.clone(),
            copies: Rc::clone(&copies_for_host),
        });
        cx.update(|window, cx| window.draw(cx).clear(cx));

        // The actions are hidden until the pointer enters the row.
        assert!(cx.debug_bounds("reference-copy-key").is_some());
        cx.simulate_mouse_move(point(px(200.), px(30.)), None, Modifiers::default());
        cx.update(|window, cx| window.draw(cx).clear(cx));

        let key = cx
            .debug_bounds("reference-copy-key")
            .expect("key button bounds");
        cx.simulate_click(key.center(), Modifiers::default());
        assert_eq!(
            cx.read_from_clipboard()
                .expect("clipboard")
                .text()
                .as_deref(),
            Some("@Kano2025")
        );

        let path = cx
            .debug_bounds("reference-copy-path")
            .expect("path button bounds");
        cx.simulate_click(path.center(), Modifiers::default());
        assert_eq!(
            cx.read_from_clipboard()
                .expect("clipboard")
                .text()
                .as_deref(),
            Some(pdf.display().to_string().as_str())
        );
        assert_eq!(
            copies.borrow().as_slice(),
            &[
                ReferenceCopy::Key("Kano2025".to_owned()),
                ReferenceCopy::Path(pdf),
            ]
        );

        let _ = std::fs::remove_dir_all(dir);
    }

    struct SelectHost {
        entry: ReferenceEntry,
        state: Rc<RefCell<(bool, usize)>>,
    }

    impl Render for SelectHost {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            let state = Rc::clone(&self.state);
            let selected = state.borrow().0;
            div().w(px(420.)).h(px(80.)).child(
                ReferenceRow::new(0, self.entry.clone())
                    .selected(selected)
                    .on_select(move |_, _| {
                        let mut state = state.borrow_mut();
                        state.0 = !state.0;
                        state.1 += 1;
                    }),
            )
        }
    }

    #[gpui_kit::test]
    fn hovering_reveals_the_plus_and_clicking_toggles_selection(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let state = Rc::new(RefCell::new((false, 0)));
        let host_state = Rc::clone(&state);
        let (_, cx) = cx.add_window_view(move |_, _| SelectHost {
            entry: entry("Kano2025", None),
            state: host_state,
        });
        cx.update(|window, cx| window.draw(cx).clear(cx));

        // The slot is laid out in every frame; the pointer makes it visible.
        assert!(cx.debug_bounds("reference-select-0").is_some());
        cx.simulate_mouse_move(point(px(200.), px(30.)), None, Modifiers::default());
        cx.update(|window, cx| window.draw(cx).clear(cx));

        let plus = cx
            .debug_bounds("reference-select-0")
            .expect("selection toggle bounds");
        cx.simulate_click(plus.center(), Modifiers::default());
        assert_eq!(*state.borrow(), (true, 1));

        cx.simulate_click(plus.center(), Modifiers::default());
        assert_eq!(*state.borrow(), (false, 2));
    }

    struct BarHost {
        entries: Vec<ReferenceEntry>,
        actions: Rc<RefCell<Vec<ReferenceAction>>>,
    }

    impl Render for BarHost {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            let actions = Rc::clone(&self.actions);
            div().w(px(420.)).h(px(60.)).child(
                ReferenceSelectionBar::new(self.entries.clone()).on_action(
                    move |action, entries, _, _| {
                        actions.borrow_mut().push(action);
                        assert!(!entries.is_empty());
                    },
                ),
            )
        }
    }

    #[gpui_kit::test]
    fn the_selection_bar_copies_keys_and_paths(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let dir = temp_dir("bar");
        let pdf = dir.join("paper.pdf");
        std::fs::write(&pdf, b"%PDF-1.4").expect("pdf");
        let expected_path = pdf.display().to_string();
        let actions = Rc::new(RefCell::new(Vec::new()));
        let host_actions = Rc::clone(&actions);
        let (_, cx) = cx.add_window_view(move |_, _| BarHost {
            entries: vec![entry("B", Some(pdf.clone())), entry("A", None)],
            actions: host_actions,
        });
        cx.update(|window, cx| window.draw(cx).clear(cx));

        let keys = cx
            .debug_bounds("reference-bulk-copy-keys-button")
            .expect("copy keys bounds");
        cx.simulate_click(keys.center(), Modifiers::default());
        assert_eq!(
            cx.read_from_clipboard()
                .expect("clipboard")
                .text()
                .as_deref(),
            Some("@B @A")
        );

        let paths = cx
            .debug_bounds("reference-bulk-copy-paths-button")
            .expect("copy paths bounds");
        cx.simulate_click(paths.center(), Modifiers::default());
        assert_eq!(
            cx.read_from_clipboard()
                .expect("clipboard")
                .text()
                .as_deref(),
            Some(expected_path.as_str())
        );
        assert_eq!(
            actions.borrow().as_slice(),
            [ReferenceAction::CopyKeys, ReferenceAction::CopyPaths]
        );

        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    #[ignore = "requires a running GROBID server and test_library PDFs"]
    async fn grobid_extracts_the_test_library_end_to_end() {
        let source = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_library");
        let dir = temp_dir("grobid-e2e");
        let corpus = dir.join("corpus");
        std::fs::create_dir_all(&corpus).expect("corpus");
        let mut copied = 0;
        for entry in std::fs::read_dir(&source).expect("test library") {
            let path = entry.expect("entry").path();
            if path
                .extension()
                .is_some_and(|extension| extension.eq_ignore_ascii_case("pdf"))
            {
                let target = corpus.join(path.file_name().expect("file name"));
                std::fs::copy(&path, &target).expect("copy pdf");
                copied += 1;
            }
        }
        assert!(
            copied >= 3,
            "expected the supplied PDFs in {}",
            source.display()
        );

        let bibliography = dir.join("references.bib");
        let options = ReferenceOptions {
            grobid_url: "http://localhost:8070".to_owned(),
            rename: true,
            openalex: false,
            workers: 4,
        };
        let mut observer = ExtractionObserver::default();
        let (entries, report) = extract(&corpus, &bibliography, &options, &mut observer)
            .await
            .ok()
            .expect("GROBID extraction");

        assert_eq!(entries.len(), copied);
        assert_eq!(report.merged, copied);
        assert_eq!(report.renamed, copied);
        assert!(entries.iter().all(|entry| !entry.key.is_empty()));
        assert!(
            entries
                .iter()
                .all(|entry| entry.path.as_ref().is_some_and(|path| path.is_file()))
        );

        // The rows must also be citable: the `file` links written by the
        // pre-pass resolve, and the real PDF parser returns text.
        let keys = vec![entries[0].key.clone()];
        let mut state = CiteState::default();
        let parsers = DocumentParsers::new(ragrig::build_parsers());
        let resolution = resolve_citations(
            &mut state,
            &bibliography,
            std::slice::from_ref(&corpus),
            &keys,
            &parsers,
        );
        assert_eq!(resolution.resolved, keys);
        assert_eq!(resolution.attachments.len(), 1);
        assert!(!resolution.attachments[0].content.trim().is_empty());

        let _ = std::fs::remove_dir_all(dir);
    }
}