nexus-chat 0.1.0

A local-first terminal chat app for deep research and multi-agent work
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
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
//! Space filesets: importing files into `spaces/<name>/files/`, keeping the
//! db index in sync with the directory, and extracting searchable text.

// Casts here are on bounded values: token counts, byte sizes, and
// selection indices — never on unbounded input. JSON-derived indices in
// provider/tools go through try_from instead.
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss
)]
use std::fmt::Write as _;
use std::path::Path;

use crate::db::FileRow;

use anyhow::{Context, Result};
use sha2::{Digest, Sha256};

use super::App;

/// A message from the background OCR batch about one file.
pub enum OcrUpdate {
    /// A human-readable phase ("rendering pages…") shown while nothing is
    /// countable yet.
    Stage(String),
    /// (pages done, total pages, pages failed so far).
    Progress(usize, usize, usize),
    /// Final outcome: (extracted text, per-page errors as (index, reason)),
    /// or a whole-document error message.
    Done(std::result::Result<(String, Vec<(usize, String)>), String>),
}

/// One row of the file-picker browser.
pub struct PickerEntry {
    pub name: String,
    pub is_dir: bool,
}

/// Which service transcribes a rendered page image.
#[derive(Clone)]
pub enum OcrBackend {
    /// `OpenRouter` vision model (`ocr_model`).
    Router(crate::provider::openrouter::OpenRouter, String),
    /// Local Ollama model via its native /api/generate endpoint — the
    /// OpenAI-compatible route mishandles GLM-OCR's vision input.
    Ollama(reqwest::Client, String),
}

impl OcrBackend {
    async fn transcribe(&self, png: &[u8]) -> anyhow::Result<String> {
        self.transcribe_image(png, "image/png").await
    }

    /// Describe an image (not OCR — uses a description prompt so another model
    /// can reason about the image content). For standalone space-file images.
    async fn describe(&self, bytes: &[u8], mime: &str) -> anyhow::Result<String> {
        match self {
            Self::Router(provider, model) => {
                use base64::Engine;
                let b64 = base64::engine::general_purpose::STANDARD.encode(bytes);
                let url = format!("data:{mime};base64,{b64}");
                provider.describe_image(model, &url).await
            }
            Self::Ollama(client, model) => {
                use base64::Engine;
                let b64 = base64::engine::general_purpose::STANDARD.encode(bytes);
                let resp = client
                    .post("http://127.0.0.1:11434/api/generate")
                    .timeout(std::time::Duration::from_mins(10))
                    .json(&serde_json::json!({
                        "model": model,
                        "prompt": "Describe this image so another AI model can reason about \
                                   it without seeing it. Cover: what it is (screenshot, chart, \
                                   photo, diagram…), overall layout and structure, the key \
                                   entities and how they relate, ALL visible text verbatim, \
                                   and any notable visual details. Be thorough but do not \
                                   speculate beyond what is visible.",
                        "images": [b64],
                        "stream": false,
                        "options": { "num_ctx": 8192 },
                    }))
                    .send()
                    .await
                    .map_err(|e| {
                        if e.is_timeout() {
                            anyhow::anyhow!("timeout after 600s")
                        } else if e.is_connect() {
                            anyhow::anyhow!("cannot reach ollama — is it running?")
                        } else {
                            e.into()
                        }
                    })?;
                if resp.status().as_u16() == 404 {
                    anyhow::bail!("model '{model}' not pulled");
                }
                let v = resp.error_for_status()?.json::<serde_json::Value>().await?;
                Ok(v.get("response")
                    .and_then(|r| r.as_str())
                    .unwrap_or("")
                    .to_string())
            }
        }
    }

    /// Transcribe an image file with the given MIME type. For standalone images
    /// (not PDF pages) that may be JPEG, PNG, etc.
    async fn transcribe_image(&self, bytes: &[u8], mime: &str) -> anyhow::Result<String> {
        match self {
            Self::Router(provider, model) => {
                use base64::Engine;
                let b64 = base64::engine::general_purpose::STANDARD.encode(bytes);
                let url = format!("data:{mime};base64,{b64}");
                provider.ocr_page(model, &url).await
            }
            Self::Ollama(client, model) => {
                use base64::Engine;
                let b64 = base64::engine::general_purpose::STANDARD.encode(bytes);
                let resp = client
                    .post("http://127.0.0.1:11434/api/generate")
                    .timeout(std::time::Duration::from_mins(10))
                    .json(&ollama_ocr_body(model, &b64))
                    .send()
                    .await
                    .map_err(|e| {
                        if e.is_timeout() {
                            anyhow::anyhow!("timeout after 600s")
                        } else if e.is_connect() {
                            anyhow::anyhow!(
                                "cannot reach ollama at 127.0.0.1:11434 — is it running? (systemctl start ollama)"
                            )
                        } else {
                            e.into()
                        }
                    })?;
                if resp.status().as_u16() == 404 {
                    anyhow::bail!(
                        "model '{model}' not pulled — cycle OCR engine to 'local' in /config"
                    );
                }
                let v = resp.error_for_status()?.json::<serde_json::Value>().await?;
                Ok(v.get("response")
                    .and_then(|r| r.as_str())
                    .unwrap_or("")
                    .to_string())
            }
        }
    }
}

/// First ~90 chars of an error, so a page failure fits in the status column
/// without swallowing the reason.
/// A stem that looks like a UUID (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
fn is_uuid_like(stem: &str) -> bool {
    let hex = |s: &str| s.chars().all(|c| c.is_ascii_hexdigit());
    let parts: Vec<&str> = stem.split('-').collect();
    parts.len() == 5
        && parts[0].len() == 8
        && hex(parts[0])
        && parts[1].len() == 4
        && hex(parts[1])
        && parts[2].len() == 4
        && hex(parts[2])
        && parts[3].len() == 4
        && hex(parts[3])
        && parts[4].len() == 12
        && hex(parts[4])
}

fn clip_err(e: &str) -> String {
    let mut s: String = e.chars().take(90).collect();
    if s.len() < e.len() {
        s.push('');
    }
    s
}

/// Request body for Ollama's native generate endpoint: raw base64 in
/// `images`, not an OpenAI-style content part.
fn ollama_ocr_body(model: &str, png_b64: &str) -> serde_json::Value {
    serde_json::json!({
        "model": model,
        "prompt": crate::provider::openrouter::OCR_PROMPT,
        "images": [png_b64],
        "stream": false,
        // Ollama defaults to 4096 ctx — page image tokens plus a dense page's
        // transcription overflow that and silently clip the output.
        "options": { "num_ctx": 8192 },
    })
}

/// OCR a scanned PDF through a vision backend: render pages at 300 DPI color,
/// transcribe up to 4 pages concurrently (one retry each), and join with
/// `[page N]` markers — a page that fails twice becomes a `[page N: ocr
/// failed]` marker instead of sinking the document.
/// Rendered page PNGs are saved permanently to `<files_dir>/<pdf_stem>/` so
/// the model can fetch them later via `files(action=pdf_page)`.
async fn ocr_pdf_vlm(
    backend: &OcrBackend,
    path: &Path,
    tx: &tokio::sync::mpsc::UnboundedSender<(String, String, OcrUpdate)>,
    space_id: &str,
    name: &str,
    files_dir: &Path,
) -> std::result::Result<(String, Vec<(usize, String)>), String> {
    let stem = std::path::Path::new(name)
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or(name);
    let page_dir = files_dir.join(stem);
    if let Err(e) = std::fs::create_dir_all(&page_dir) {
        return Err(format!("error: ocr: {e}"));
    }
    ocr_pdf_vlm_in(backend, path, &page_dir, tx, space_id, name).await
}

async fn ocr_pdf_vlm_in(
    backend: &OcrBackend,
    path: &Path,
    page_dir: &Path,
    tx: &tokio::sync::mpsc::UnboundedSender<(String, String, OcrUpdate)>,
    space_id: &str,
    name: &str,
) -> std::result::Result<(String, Vec<(usize, String)>), String> {
    let _ = tx.send((
        space_id.to_string(),
        name.to_string(),
        OcrUpdate::Stage("rendering pages (300 dpi)…".to_string()),
    ));
    let (pdf, dir) = (path.to_path_buf(), page_dir.to_path_buf());
    let pages = tokio::task::spawn_blocking(move || {
        crate::extract::render_pdf_pages("pdftoppm", &pdf, &dir, 300, false)
    })
    .await
    .map_err(|e| format!("error: ocr: {e}"))?
    .map_err(|e| match e {
        crate::extract::OcrError::MissingTools => {
            "scanned pdf — install poppler (pdftoppm) for ocr".to_string()
        }
        crate::extract::OcrError::Failed(m) => format!("error: ocr: {m}"),
    })?;

    let total = pages.len();
    // Show "0/N pages" immediately — on CPU the first page can take minutes,
    // and a frozen "ocr…" reads as stuck.
    let _ = tx.send((
        space_id.to_string(),
        name.to_string(),
        OcrUpdate::Progress(0, total, 0),
    ));
    let mut results: Vec<std::result::Result<String, String>> =
        vec![Err("not transcribed".to_string()); total];
    let mut set = tokio::task::JoinSet::new();
    let spawn_page =
        |set: &mut tokio::task::JoinSet<(usize, std::result::Result<String, String>)>, i: usize| {
            let (backend, png) = (backend.clone(), pages[i].clone());
            set.spawn(async move {
                let Ok(bytes) = std::fs::read(&png) else {
                    return (i, Err("page image unreadable".to_string()));
                };
                let mut last = String::new();
                for _ in 0..2 {
                    match backend.transcribe(&bytes).await {
                        Ok(text) => return (i, Ok(text)),
                        Err(e) => last = e.to_string(),
                    }
                }
                (i, Err(last))
            });
        };

    // ponytail: 16 concurrent pages — the bottleneck is API latency, not
    // local CPU, so a wider window reduces wall-clock time significantly.
    // Tune this down if the backend rate-limits you.
    let window = (16_usize).min(total);
    let mut next = 0;
    while next < window {
        spawn_page(&mut set, next);
        next += 1;
    }
    let mut done = 0;
    let mut failed = 0;
    while let Some(joined) = set.join_next().await {
        let (i, r) = joined.unwrap_or_else(|_| (usize::MAX, Err("page task panicked".to_string())));
        if r.is_err() {
            failed += 1;
        }
        if let Some(slot) = results.get_mut(i) {
            *slot = r;
        }
        done += 1;
        let _ = tx.send((
            space_id.to_string(),
            name.to_string(),
            OcrUpdate::Progress(done, total, failed),
        ));
        if next < total {
            spawn_page(&mut set, next);
            next += 1;
        }
    }
    // Rename pdftoppm output to stable page-<N>.png names
    let _ = std::fs::create_dir_all(page_dir);
    for (i, p) in pages.iter().enumerate() {
        let stable = page_dir.join(format!("page-{}.png", i + 1));
        let _ = std::fs::rename(p, &stable);
    }
    let errors: Vec<(usize, String)> = results
        .iter()
        .enumerate()
        .filter_map(|(i, r)| r.as_ref().err().map(|e| (i, e.clone())))
        .collect();
    Ok((crate::extract::join_pages(&results), errors))
}

/// OCR a standalone image file through a vision backend: read the file,
/// transcribe it directly (no page rendering), return OCR text. Reuses the
/// same `OcrUpdate` channel as `pdf_vlm` for status/progress.
async fn ocr_image_vlm(
    backend: &OcrBackend,
    path: &Path,
    tx: &tokio::sync::mpsc::UnboundedSender<(String, String, OcrUpdate)>,
    space_id: &str,
    name: &str,
) -> std::result::Result<(String, Vec<(usize, String)>), String> {
    let _ = tx.send((
        space_id.to_string(),
        name.to_string(),
        OcrUpdate::Stage("transcribing image…".to_string()),
    ));
    let Ok(bytes) = std::fs::read(path) else {
        return Err(format!("cannot read {name}"));
    };
    let ext = path
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("")
        .to_lowercase();
    let mime = match ext.as_str() {
        "jpg" | "jpeg" => "image/jpeg",
        "gif" => "image/gif",
        "webp" => "image/webp",
        "bmp" => "image/bmp",
        _ => "image/png",
    };
    let _ = tx.send((
        space_id.to_string(),
        name.to_string(),
        OcrUpdate::Progress(0, 1, 0),
    ));
    match backend.describe(&bytes, mime).await {
        Ok(text) => {
            let _ = tx.send((
                space_id.to_string(),
                name.to_string(),
                OcrUpdate::Progress(1, 1, 0),
            ));
            Ok((text, Vec::new()))
        }
        Err(e) => {
            let err = e.to_string();
            Err(format!("error: ocr: {err}"))
        }
    }
}

impl App {
    /// Enter the picker at `picker_dir` (home on first open, remembered after).
    pub(crate) fn open_file_picker(&mut self) {
        self.picker_filter.clear();
        self.picker_selected = 0;
        self.reload_picker_entries();
        self.files_mode = super::FilesMode::Pick;
    }

    /// Re-read the current directory: dirs first, then files, both alphabetical.
    /// Unreadable dirs just yield an empty list (status explains).
    fn reload_picker_entries(&mut self) {
        let mut entries: Vec<PickerEntry> = match std::fs::read_dir(&self.picker_dir) {
            Ok(rd) => rd
                .flatten()
                .filter_map(|e| {
                    let name = e.file_name().to_string_lossy().to_string();
                    let is_dir = e.file_type().ok()?.is_dir();
                    Some(PickerEntry { name, is_dir })
                })
                .collect(),
            Err(e) => {
                self.status = format!("cannot read {}: {e}", self.picker_dir.display());
                Vec::new()
            }
        };
        entries.sort_by(|a, b| b.is_dir.cmp(&a.is_dir).then_with(|| a.name.cmp(&b.name)));
        self.picker_entries = entries;
    }

    /// Entries matching the fuzzy filter (all of them, dirs first, when empty).
    pub fn filtered_picker_entries(&self) -> Vec<&PickerEntry> {
        use crate::input::fuzzy_score;
        let needle = self.picker_filter.trim();
        if needle.is_empty() {
            return self.picker_entries.iter().collect();
        }
        super::fuzzy_filter_sorted(&self.picker_entries, |e| fuzzy_score(&e.name, needle))
    }

    pub fn move_picker_selection(&mut self, delta: i32) {
        self.picker_selected = super::clamp_cursor(
            self.picker_selected,
            self.filtered_picker_entries().len(),
            delta,
        );
    }

    pub fn picker_filter_push(&mut self, c: char) {
        self.picker_filter.push(c);
        self.picker_selected = 0;
    }

    /// Backspace erases the filter first; on an empty filter it goes up a level.
    pub fn picker_backspace(&mut self) {
        if !self.picker_filter.is_empty() {
            self.picker_filter.pop();
            self.picker_selected = 0;
            return;
        }
        if let Some(parent) = self.picker_dir.parent().map(std::path::Path::to_path_buf) {
            self.picker_dir = parent;
            self.picker_selected = 0;
            self.reload_picker_entries();
        }
    }

    /// Enter descends into a directory, or imports the selected file.
    pub fn picker_enter(&mut self) {
        let filtered = self.filtered_picker_entries();
        let Some(entry) = filtered.get(self.picker_selected) else {
            return;
        };
        let name = entry.name.clone();
        let is_dir = entry.is_dir;
        let path = self.picker_dir.join(&name);
        if is_dir {
            self.picker_dir = path;
            self.picker_filter.clear();
            self.picker_selected = 0;
            self.reload_picker_entries();
            return;
        }
        match self.import_file(&path) {
            Ok(n) => self.status = format!("imported {n}"),
            Err(e) => self.status = format!("import failed: {e}"),
        }
        self.files_mode = super::FilesMode::Browse;
    }

    /// Sync the active space's files directory with the db: new or changed
    /// files (by sha256) are re-extracted and re-indexed, rows for deleted
    /// files are dropped, and `files_cache` is refreshed. Best-effort: a
    /// single bad file gets an "error: …" status instead of failing the scan.
    /// ponytail: runs synchronously on the UI task — extraction of a huge PDF
    /// blocks a beat; move to a blocking task if that ever hurts.
    pub fn rescan_files(&mut self) {
        let dir = self.space.files_dir(&self.active_space.name);
        let known = self
            .db
            .list_files(&self.active_space.id)
            .unwrap_or_default();
        let mut seen: Vec<String> = Vec::new();
        let mut ocr_jobs: Vec<(String, String, std::path::PathBuf)> = Vec::new();

        let entries = std::fs::read_dir(&dir)
            .map(|rd| rd.flatten().collect::<Vec<_>>())
            .unwrap_or_default();
        for entry in entries {
            let path = entry.path();
            if !path.is_file() {
                continue;
            }
            let name = entry.file_name().to_string_lossy().to_string();
            seen.push(name.clone());
            let mtime = entry
                .metadata()
                .ok()
                .and_then(|m| m.modified().ok())
                .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                .map_or(0, |d| d.as_secs() as i64);
            let disk_size = entry.metadata().map_or(0, |m| m.len() as i64);
            let existing = known.iter().find(|f| f.name == name);
            // Unchanged by stat: skip entirely — no read, no hash. This is what
            // keeps /files and space switches snappy with big filesets.
            if let Some(f) = existing
                && f.size == disk_size
                && f.mtime == mtime
                && mtime != 0
            {
                // Stale "ocr…"/"ocr N/M" (app quit mid-OCR) re-queues once no
                // batch is in flight.
                if f.status.starts_with("ocr") && self.ocr_rx.is_none() {
                    ocr_jobs.push((self.active_space.id.clone(), name.clone(), path.clone()));
                }
                continue;
            }
            let Ok(bytes) = std::fs::read(&path) else {
                continue;
            };
            let hash = Sha256::digest(&bytes)
                .iter()
                .fold(String::new(), |mut h, b| {
                    let _ = write!(h, "{b:02x}");
                    h
                });
            if let Some(f) = existing.filter(|f| f.hash == hash) {
                // Content unchanged (touched, or indexed before mtimes were
                // tracked): just record the stat for next time.
                let _ = self.db.set_file_mtime(&f.id, mtime);
                if f.status.starts_with("ocr") && self.ocr_rx.is_none() {
                    ocr_jobs.push((self.active_space.id.clone(), name.clone(), path.clone()));
                }
                continue;
            }
            let size = bytes.len() as i64;
            let (status, chunks) = match crate::extract::extract_text(&path) {
                Ok(text) if text.trim().is_empty() => {
                    let ext = std::path::Path::new(&name)
                        .extension()
                        .and_then(|e| e.to_str())
                        .unwrap_or("")
                        .to_lowercase();
                    if ext == "pdf" || crate::extract::is_image_ext(&ext) {
                        ocr_jobs.push((self.active_space.id.clone(), name.clone(), path.clone()));
                        ("ocr…".to_string(), Vec::new())
                    } else {
                        ("no text (scanned?)".to_string(), Vec::new())
                    }
                }
                Ok(text) => ("ok".to_string(), crate::extract::chunk_lines(&text)),
                Err(e) => (format!("error: {e}"), Vec::new()),
            };
            if let Ok(id) = self
                .db
                .upsert_file(&self.active_space.id, &name, &hash, size, &status)
            {
                let _ = self.db.set_file_chunks(&id, &chunks);
                let _ = self.db.set_file_mtime(&id, mtime);
            }
        }
        for gone in known.iter().filter(|f| !seen.contains(&f.name)) {
            let _ = self.db.delete_file(&gone.id);
        }
        self.start_ocr(ocr_jobs);
        // Backfill vectors for anything whose chunks changed (or that predates
        // semantic search entirely).
        self.start_embedding();
        self.files_cache = self
            .db
            .list_files(&self.active_space.id)
            .unwrap_or_default();
        self.files_selected = self
            .files_selected
            .min(self.files_cache.len().saturating_sub(1));
    }

    /// OCR queued scanned PDFs sequentially off the UI thread. One batch at a
    /// time: jobs arriving while a batch runs stay at "ocr…" and re-queue on a
    /// later rescan.
    pub(crate) fn start_ocr(&mut self, jobs: Vec<(String, String, std::path::PathBuf)>) {
        if jobs.is_empty() || self.ocr_rx.is_some() {
            return;
        }
        let backend = self.ocr_backend();
        let files_dir = self.space.files_dir(&self.active_space.name);
        let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
        self.ocr_rx = Some(rx);
        if let Some(backend) = backend {
            tokio::spawn(async move {
                for (space_id, name, path) in jobs {
                    let is_image = path
                        .extension()
                        .and_then(|e| e.to_str())
                        .is_some_and(crate::extract::is_image_ext);
                    let result = if is_image {
                        ocr_image_vlm(&backend, &path, &tx, &space_id, &name).await
                    } else {
                        ocr_pdf_vlm(&backend, &path, &tx, &space_id, &name, &files_dir).await
                    };
                    if tx.send((space_id, name, OcrUpdate::Done(result))).is_err() {
                        return;
                    }
                }
            });
            return;
        }
        tokio::task::spawn_blocking(move || {
            for (space_id, name, path) in jobs {
                let is_image = path
                    .extension()
                    .and_then(|e| e.to_str())
                    .is_some_and(crate::extract::is_image_ext);
                if is_image {
                    // Images can't be OCR'd via tesseract — skip, it'll re-queue
                    // on next rescan if a VLM backend is configured.
                    let _ = tx.send((
                        space_id,
                        name,
                        OcrUpdate::Done(Err("no vlm backend for image ocr".to_string())),
                    ));
                    continue;
                }
                let progress_tx = tx.clone();
                let (sid, fname) = (space_id.clone(), name.clone());
                let progress = move |done: usize, total: usize| {
                    let _ = progress_tx.send((
                        sid.clone(),
                        fname.clone(),
                        OcrUpdate::Progress(done, total, 0),
                    ));
                };
                let result = match crate::extract::ocr_pdf(&path, &progress) {
                    Ok(text) => Ok((text, Vec::new())),
                    Err(crate::extract::OcrError::MissingTools) => {
                        Err("scanned pdf — install tesseract + poppler for ocr".to_string())
                    }
                    Err(crate::extract::OcrError::Failed(e)) => Err(format!("error: ocr: {e}")),
                };
                if tx.send((space_id, name, OcrUpdate::Done(result))).is_err() {
                    return;
                }
            }
        });
    }

    /// The vision backend scanned PDFs OCR through, or None for tesseract:
    /// "local" → Ollama; "vlm"/"auto" with an OCR model + provider → `OpenRouter`.
    pub(crate) fn ocr_backend(&self) -> Option<OcrBackend> {
        if self.ocr_engine == "local" {
            let model = self.local_ocr_model.trim();
            let model = if model.is_empty() { "glm-ocr" } else { model };
            return Some(OcrBackend::Ollama(
                reqwest::Client::new(),
                model.to_string(),
            ));
        }
        if self.vlm_ocr_enabled() {
            let model = self.ocr_model.trim().to_string();
            return self
                .resolve_model_backend(&model)
                .map(|(p, raw_model)| OcrBackend::Router(p, raw_model));
        }
        None
    }

    /// Cycling the OCR engine to "local" (in /config) pulls a local OCR model
    /// through Ollama in the background and switches the engine to it when
    /// the pull succeeds. Defaults to glm-ocr (0.9B — the current open OCR
    /// benchmark leader).
    pub(crate) fn ocr_local_install(&mut self, arg: &str) {
        if self.ocr_pull_rx.is_some() {
            self.status = "an OCR model pull is already running".to_string();
            return;
        }
        let model = if arg.is_empty() {
            "glm-ocr".to_string()
        } else {
            arg.to_string()
        };
        self.local_ocr_model.clone_from(&model);
        let _ = self.db.set_setting("local_ocr_model", &model);
        // Under `cargo test` there's no reactor to spawn onto and no real
        // ollama to pull from — just take the switch synchronously so the
        // settings-cycle test doesn't need a Tokio runtime.
        #[cfg(test)]
        {
            self.ocr_engine = "local".to_string();
            let _ = self.db.set_setting("ocr_engine", "local");
            self.status = format!("(test) local OCR: {model}");
        }
        #[cfg(not(test))]
        {
            let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
            self.ocr_pull_rx = Some(rx);
            self.status = format!("pulling {model} via ollama… (keeps running in background)");
            tokio::spawn(async move {
                let result = match tokio::process::Command::new("ollama")
                .args(["pull", &model])
                .output()
                .await
            {
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                    Err("ollama not installed — get it from https://ollama.com (pacman -S ollama), then retry".to_string())
                }
                Err(e) => Err(format!("ollama pull failed: {e}")),
                Ok(out) if !out.status.success() => {
                    let err = String::from_utf8_lossy(&out.stderr);
                    let hint = if err.contains("could not connect") || err.contains("connection refused") {
                        " — is the ollama server running? (systemctl start ollama, or `ollama serve`)"
                    } else {
                        ""
                    };
                    Err(format!("ollama pull failed: {}{hint}", err.trim()))
                }
                Ok(_) => Ok(model),
            };
                let _ = tx.send(result);
            });
        }
    }

    /// The local-OCR-model pull finished: point the OCR engine at the local model.
    pub fn on_ocr_pull(&mut self, r: Option<Result<String, String>>) {
        let Some(result) = r else {
            self.ocr_pull_rx = None;
            return;
        };
        self.ocr_pull_rx = None;
        match result {
            Ok(model) => {
                self.ocr_engine = "local".to_string();
                let _ = self.db.set_setting("ocr_engine", "local");
                self.status = format!(
                    "local OCR ready: {model} via ollama — Ctrl+O a file in /files to re-run it"
                );
            }
            Err(e) => self.status = e,
        }
    }

    /// Ctrl+O in /files: throw away the selected file's extracted text (and
    /// vectors, via `set_file_chunks`) and re-index it from disk with the
    /// current OCR engine — how a tesseract-mangled book gets redone after
    /// configuring a VLM, without re-importing.
    pub(crate) fn reextract_selected_file(&mut self) {
        let Some(f) = self.files_cache.get(self.files_selected).cloned() else {
            return;
        };
        let _ = self.db.set_file_chunks(&f.id, &[]);
        // Zeroing hash + size guarantees the rescan takes the re-extract path
        // (a real file is never 0 bytes with an empty hash).
        let _ = self
            .db
            .upsert_file(&self.active_space.id, &f.name, "", 0, "re-extracting");
        self.status = format!("re-extracting: {}", f.name);
        self.rescan_files();
    }

    /// Force OCR on the selected file, bypassing text extraction entirely.
    /// Useful when `pdf_extract` gives unreliable text and you want VLM OCR
    /// output instead.
    pub(crate) fn reocr_selected_file(&mut self) {
        let Some(f) = self.files_cache.get(self.files_selected).cloned() else {
            return;
        };
        let ext = std::path::Path::new(&f.name)
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("")
            .to_lowercase();
        if ext != "pdf" && !crate::extract::is_image_ext(&ext) {
            self.status = format!("only PDFs and images support OCR: {}", f.name);
            return;
        }
        let path = self.space.files_dir(&self.active_space.name).join(&f.name);
        // Force-cancel any in-progress OCR batch so our job isn't silently dropped
        self.ocr_rx = None;
        let _ = self.db.set_file_status(&f.id, "ocr…");
        self.start_ocr(vec![(self.active_space.id.clone(), f.name.clone(), path)]);
        self.files_cache = self
            .db
            .list_files(&self.active_space.id)
            .unwrap_or_default();
        self.status = format!("ocr queued: {}", f.name);
    }

    /// Embed the next file whose chunks lack vectors, one file per job (the
    /// done-handler chains the next). No-op without a provider, without an
    /// embedding model, or while a job is already in flight.
    pub(crate) fn start_embedding(&mut self) {
        if self.embed_rx.is_some() {
            return;
        }
        let model = self.embedding_model.trim().to_string();
        if model.is_empty() {
            return;
        }
        let Some((provider, raw_model)) = self.resolve_model_backend(&model) else {
            return;
        };
        let space_id = self.active_space.id.clone();
        let Ok(missing) = self.db.files_missing_embeddings(&space_id) else {
            return;
        };
        let Some(file_id) = missing.first().cloned() else {
            return;
        };
        let chunks = self.db.file_chunk_texts(&file_id).unwrap_or_default();
        if chunks.is_empty() {
            return;
        }
        // Embedding is best-effort background work; outside a runtime (sync
        // unit tests) there's nowhere to run it, so just skip.
        let Ok(handle) = tokio::runtime::Handle::try_current() else {
            return;
        };
        let _ = self.db.set_file_status(&file_id, "embedding…");
        if space_id == self.active_space.id {
            self.files_cache = self.db.list_files(&space_id).unwrap_or_default();
        }
        let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
        self.embed_rx = Some(rx);
        handle.spawn(async move {
            let mut out: Vec<(i64, Vec<f32>)> = Vec::with_capacity(chunks.len());
            let mut err = None;
            for batch in chunks.chunks(64) {
                let inputs: Vec<String> = batch.iter().map(|(_, t)| t.clone()).collect();
                match provider.embed(&raw_model, inputs).await {
                    Ok(vecs) => out.extend(batch.iter().zip(vecs).map(|((seq, _), v)| (*seq, v))),
                    Err(e) => {
                        err = Some(e.to_string());
                        break;
                    }
                }
            }
            let result = match err {
                Some(e) => Err(e),
                None => Ok(out),
            };
            let _ = tx.send((space_id, file_id, result));
        });
    }

    /// One embedding job finished: store vectors and chain the next file, or
    /// surface the error and stop (a dead endpoint shouldn't be hammered —
    /// the next rescan retries). Either way the file's status returns to "ok";
    /// search falls back to keywords while vectors are missing.
    pub fn on_embed_done(&mut self, r: Option<crate::app::EmbedMsg>) {
        let Some((space_id, file_id, result)) = r else {
            self.embed_rx = None;
            return;
        };
        self.embed_rx = None;
        match result {
            Ok(vecs) => {
                let _ = self.db.set_chunk_embeddings(&file_id, &vecs);
                let _ = self.db.set_file_status(&file_id, "ok");
                self.start_embedding();
            }
            Err(e) => {
                let _ = self.db.set_file_status(&file_id, "ok");
                self.status = format!("embedding failed: {e}");
            }
        }
        if space_id == self.active_space.id {
            self.files_cache = self.db.list_files(&space_id).unwrap_or_default();
        }
    }

    /// A finished OCR job: persist chunks/status, refresh the cache only if the
    /// file's space is still active. `None` = batch done (channel closed).
    pub fn on_ocr_done(&mut self, r: Option<(String, String, OcrUpdate)>) {
        let Some((space_id, name, update)) = r else {
            self.ocr_rx = None;
            // PDFs imported mid-batch sat at "ocr…" unqueued; this rescan
            // chains them into a fresh batch instead of stalling until the
            // user reopens /files.
            self.rescan_files();
            return;
        };
        let Ok(files) = self.db.list_files(&space_id) else {
            return;
        };
        let Some(f) = files.iter().find(|f| f.name == name) else {
            return; // deleted mid-OCR
        };
        if !f.status.starts_with("ocr") {
            return; // re-imported mid-OCR — this result is for stale content
        }
        match update {
            OcrUpdate::Stage(s) => {
                // Keep the "ocr" prefix — the stale-check above depends on it.
                let _ = self.db.set_file_status(&f.id, &format!("ocr: {s}"));
                if space_id == self.active_space.id {
                    self.status = format!("ocr {name}: {s}");
                }
            }
            OcrUpdate::Progress(done, total, failed) => {
                let tail = if failed > 0 {
                    format!(" ({failed} failed)")
                } else {
                    String::new()
                };
                let _ = self
                    .db
                    .set_file_status(&f.id, &format!("ocr {done}/{total}{tail}"));
                if space_id == self.active_space.id {
                    self.status = format!("ocr {name}: {done}/{total} pages{tail}");
                }
            }
            OcrUpdate::Done(Ok((text, errors))) if text.trim().is_empty() => {
                // Nothing usable came back; say exactly why if we know.
                let status = match errors.first() {
                    Some((i, e)) => {
                        format!("all pages failed (p{}: {})", i + 1, clip_err(e))
                    }
                    None => "no text (ocr found nothing)".to_string(),
                };
                let _ = self.db.set_file_status(&f.id, &status);
                if space_id == self.active_space.id {
                    self.status = format!("ocr {name}: {status}");
                }
            }
            OcrUpdate::Done(Ok((text, errors))) => {
                let _ = self
                    .db
                    .set_file_chunks(&f.id, &crate::extract::chunk_lines(&text));
                let status = match errors.first() {
                    None => "ok".to_string(),
                    Some((i, e)) => format!(
                        "ok — {} page{} failed (p{}: {})",
                        errors.len(),
                        if errors.len() == 1 { "" } else { "s" },
                        i + 1,
                        clip_err(e),
                    ),
                };
                let _ = self.db.set_file_status(&f.id, &status);

                // Rename pasted images (uuid.ext) to uuid-<slug>.ext for @-completion.
                if let Some(new_name) = Self::descriptive_paste_name(f, &text) {
                    let dir = self.space.files_dir(&self.active_space.name);
                    let old_path = dir.join(&f.name);
                    let new_path = dir.join(&new_name);
                    if old_path.exists() && std::fs::rename(&old_path, &new_path).is_ok() {
                        let _ = self.db.rename_file(&f.id, &new_name);
                        let _ = self
                            .db
                            .replace_file_ref_in_messages(&space_id, &f.name, &new_name);
                        if space_id == self.active_space.id {
                            self.status = format!("ocr done: {new_name}");
                        }
                        // f.name needs the updated name for the message below.
                    } else if space_id == self.active_space.id {
                        self.status = format!("ocr done: {name}");
                    }
                } else if space_id == self.active_space.id {
                    self.status = format!("ocr done: {name}");
                }
            }
            OcrUpdate::Done(Err(msg)) => {
                let _ = self.db.set_file_status(&f.id, &msg);
                if space_id == self.active_space.id {
                    self.status = format!("ocr {name}: {msg}");
                }
            }
        }
        if space_id == self.active_space.id {
            self.files_cache = self.db.list_files(&space_id).unwrap_or_default();
            self.files_selected = self
                .files_selected
                .min(self.files_cache.len().saturating_sub(1));
        }
    }

    /// Copy `path` into the active space's files dir and index it. Returns
    /// the imported file's name. An existing file with the same name is
    /// overwritten (the rescan re-extracts it).
    pub fn import_file(&mut self, path: &Path) -> Result<String> {
        let name = path
            .file_name()
            .map(|n| n.to_string_lossy().to_string())
            .filter(|n| !n.is_empty())
            .context("path has no file name")?;
        let dir = self.space.files_dir(&self.active_space.name);
        std::fs::create_dir_all(&dir).with_context(|| format!("creating {}", dir.display()))?;
        std::fs::copy(path, dir.join(&name))
            .with_context(|| format!("copying {} into the space", path.display()))?;
        self.rescan_files();
        Ok(name)
    }

    /// Delete the highlighted file: disk copy and index rows both go.
    pub fn confirm_files_delete(&mut self) -> Result<()> {
        if let Some(f) = self.files_cache.get(self.files_selected).cloned() {
            let disk = self.space.files_dir(&self.active_space.name).join(&f.name);
            if disk.exists() {
                std::fs::remove_file(&disk)
                    .with_context(|| format!("removing {}", disk.display()))?;
            }
            self.db.delete_file(&f.id)?;
            self.status = format!("removed {}", f.name);
            self.rescan_files();
        }
        self.files_mode = super::FilesMode::Browse;
        Ok(())
    }

    pub(crate) fn open_files_popup(&mut self, tab: super::FilesTab) {
        self.files_tab = tab;
        match tab {
            super::FilesTab::Images => {
                self.refresh_images();
            }
            super::FilesTab::Scripts => {
                self.refresh_scripts();
            }
            super::FilesTab::Files => {
                self.rescan_files();
            }
        }
        self.files_mode = super::FilesMode::Browse;
        self.popup = super::Popup::Files;
    }

    pub fn move_files_selection(&mut self, delta: i32) {
        self.files_selected =
            super::clamp_cursor(self.files_selected, self.files_cache.len(), delta);
    }

    pub fn start_files_add(&mut self) {
        self.files_edit.clear();
        self.files_mode = super::FilesMode::Add;
    }

    /// Import the path typed/pasted in Add mode. Bad paths report in the status
    /// line and return to Browse (nothing to roll back).
    pub fn confirm_files_add(&mut self) {
        let raw = self.files_edit.trim().to_string();
        self.files_mode = super::FilesMode::Browse;
        if raw.is_empty() {
            return;
        }
        let path = std::path::PathBuf::from(&raw);
        if !path.is_file() {
            self.status = format!("not a file: {raw}");
            return;
        }
        match self.import_file(&path) {
            Ok(name) => self.status = format!("imported {name}"),
            Err(e) => self.status = format!("import failed: {e}"),
        }
    }

    /// Ctrl+R in Browse: pre-fill the edit line with the current name.
    pub fn start_files_rename(&mut self) {
        if let Some(f) = self.files_cache.get(self.files_selected) {
            self.files_edit = f.name.clone();
            self.files_mode = super::FilesMode::Rename;
        }
    }

    /// Rename the highlighted file on disk; the rescan swaps the index rows
    /// (old name dropped, new name re-extracted).
    pub fn confirm_files_rename(&mut self) -> Result<()> {
        let new = self.files_edit.trim().to_string();
        self.files_mode = super::FilesMode::Browse;
        let Some(f) = self.files_cache.get(self.files_selected).cloned() else {
            return Ok(());
        };
        if new.is_empty() || new == f.name {
            return Ok(());
        }
        if new.contains(['/', '\\']) || new == "." || new == ".." {
            self.status = format!("invalid name: {new}");
            return Ok(());
        }
        let dir = self.space.files_dir(&self.active_space.name);
        if dir.join(&new).exists() {
            self.status = format!("{new} already exists");
            return Ok(());
        }
        std::fs::rename(dir.join(&f.name), dir.join(&new))
            .with_context(|| format!("renaming {} to {new}", f.name))?;
        self.rescan_files();
        self.files_selected = self
            .files_cache
            .iter()
            .position(|f| f.name == new)
            .unwrap_or(self.files_selected);
        self.status = format!("renamed {} to {new}", f.name);
        Ok(())
    }

    /// Open the highlighted file in the system viewer (Enter in Browse).
    pub fn open_selected_file(&mut self) {
        if let Some(f) = self.files_cache.get(self.files_selected) {
            let path = self.space.files_dir(&self.active_space.name).join(&f.name);
            let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
            if ext == "md" {
                self.pending_editor = Some(super::PendingEditor::ScriptFile(path));
            } else {
                let _ = open::that_detached(&path);
            }
            self.status = format!("opened {}", f.name);
        }
    }

    /// If `f` is a pasted image (UUID.ext), generate a descriptive name
    /// `uuid-<slug>.ext` from OCR text. Returns None for non-pasted files.
    fn descriptive_paste_name(f: &FileRow, ocr_text: &str) -> Option<String> {
        let stem = std::path::Path::new(&f.name).file_stem()?.to_str()?;
        let ext = std::path::Path::new(&f.name).extension()?.to_str()?;
        // Only rename files whose stem is a UUID (pasted images).
        if !is_uuid_like(stem) {
            return None;
        }
        let slug = Self::slug_from_ocr(ocr_text)?;
        Some(format!("{stem}-{slug}.{ext}"))
    }

    /// Generate a `snake_case` name from OCR text. Takes first N meaningful words
    /// and slugifies them. Returns None if text is empty or has no words.
    fn slug_from_ocr(text: &str) -> Option<String> {
        let words: Vec<&str> = text
            .split_whitespace()
            .filter(|w| {
                let w = w.trim_matches(|c: char| !c.is_alphanumeric());
                w.len() > 2 && w.chars().any(char::is_alphanumeric)
            })
            .collect();
        if words.is_empty() {
            return None;
        }
        let slug: String = words
            .iter()
            .take(5)
            .map(|w| {
                w.trim_matches(|c: char| !c.is_alphanumeric())
                    .to_lowercase()
            })
            .collect::<Vec<_>>()
            .join("_");
        if slug.is_empty() { None } else { Some(slug) }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::Db;
    use crate::space::Space;

    fn test_app() -> App {
        let db = Db::open_in_memory().unwrap();
        let root = std::env::temp_dir().join(format!("nexus-files-test-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(root.join("spaces")).unwrap();
        let space = Space { root };
        App::new(db, Some("k"), space)
    }

    #[tokio::test]
    async fn embedder_queue_backfills_chains_and_stops_on_error() {
        let mut a = test_app();
        let space = a.active_space.id.clone();
        let id = a.db.upsert_file(&space, "b.txt", "h", 1, "ok").unwrap();
        a.db.set_file_chunks(&id, &[("l".into(), "text".into())])
            .unwrap();

        // No provider → no-op.
        let saved = a.backends.clone();
        a.backends = crate::app::Backends::default();
        a.start_embedding();
        assert!(a.embed_rx.is_none());
        a.backends = saved;

        // Blank embedding model → no-op.
        let m = std::mem::take(&mut a.embedding_model);
        a.start_embedding();
        assert!(a.embed_rx.is_none());
        a.embedding_model = m;

        // Missing vectors + provider → queued, status flips.
        a.start_embedding();
        assert!(a.embed_rx.is_some());
        let files = a.db.list_files(&space).unwrap();
        assert!(
            files[0].status.starts_with("embedding"),
            "{}",
            files[0].status
        );

        // Success: vectors stored, status ok, file leaves the missing list.
        a.on_embed_done(Some((
            space.clone(),
            id.clone(),
            Ok(vec![(0, vec![1.0f32, 0.0])]),
        )));
        assert!(a.db.files_missing_embeddings(&space).unwrap().is_empty());
        assert_eq!(a.db.list_files(&space).unwrap()[0].status, "ok");

        // Error: status restored, no re-queue (don't hammer a dead endpoint).
        a.db.set_file_chunks(&id, &[("l".into(), "new".into())])
            .unwrap();
        a.on_embed_done(Some((space.clone(), id.clone(), Err("offline".into()))));
        assert!(a.embed_rx.is_none());
        assert!(a.status.contains("embedding failed"));
        assert_eq!(a.db.list_files(&space).unwrap()[0].status, "ok");
    }

    #[test]
    fn import_copies_extracts_and_indexes() {
        let mut a = test_app();
        let src = std::env::temp_dir().join(format!("nexus-src-{}.md", uuid::Uuid::new_v4()));
        std::fs::write(&src, "# quarterly report\nrevenue up").unwrap();

        let name = a.import_file(&src).unwrap();
        assert_eq!(name, src.file_name().unwrap().to_string_lossy());
        assert_eq!(a.files_cache.len(), 1);
        assert_eq!(a.files_cache[0].status, "ok");
        // Copied into the space's files dir.
        assert!(a.space.files_dir(&a.active_space.name).join(&name).exists());
        // Indexed: searchable.
        let hits = crate::db::search_chunks(a.db.conn_for_test(), &a.active_space.id, "revenue", 8)
            .unwrap();
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn ollama_ocr_body_uses_native_generate_shape() {
        let body = ollama_ocr_body("glm-ocr", "QUFB");
        assert_eq!(body["model"], "glm-ocr");
        assert_eq!(body["stream"], false);
        assert_eq!(body["images"][0], "QUFB"); // raw base64, not a data URL
        assert!(body["prompt"].as_str().unwrap().contains("furigana"));
        assert!(
            body.get("messages").is_none(),
            "must not be OpenAI chat shape"
        );
    }

    #[test]
    fn ocr_backend_routes_by_engine() {
        let mut a = test_app();
        // auto + model + provider → OpenRouter.
        assert!(matches!(a.ocr_backend(), Some(OcrBackend::Router(..))));
        // local → Ollama regardless of provider/ocr_model.
        a.ocr_engine = "local".to_string();
        a.local_ocr_model = String::new(); // blank falls back to glm-ocr
        match a.ocr_backend() {
            Some(OcrBackend::Ollama(_, model)) => assert_eq!(model, "glm-ocr"),
            other => panic!("expected ollama backend, got {}", other.is_some()),
        }
        // tesseract → none.
        a.ocr_engine = "tesseract".to_string();
        assert!(a.ocr_backend().is_none());
        // auto without provider → none (tesseract fallback).
        a.ocr_engine = "auto".to_string();
        a.backends = crate::app::Backends::default();
        assert!(a.ocr_backend().is_none());
    }

    #[tokio::test]
    async fn ocr_pull_success_switches_engine_to_local() {
        let mut a = test_app();
        a.on_ocr_pull(Some(Ok("glm-ocr".to_string())));
        assert_eq!(a.ocr_engine, "local");
        assert!(a.status.contains("local OCR ready"));
        a.on_ocr_pull(Some(Err("ollama not installed — get it".to_string())));
        assert_eq!(a.ocr_engine, "local"); // engine untouched on failure
        assert!(a.status.contains("ollama not installed"));
    }

    #[test]
    fn ocr_statuses_surface_stages_failures_and_reasons() {
        let mut a = test_app();
        let space = a.active_space.id.clone();
        let id =
            a.db.upsert_file(&space, "scan.pdf", "h", 1, "ocr…")
                .unwrap();

        // Stage → visible phase, still "ocr"-prefixed (stale-check depends on it).
        a.on_ocr_done(Some((
            space.clone(),
            "scan.pdf".into(),
            OcrUpdate::Stage("rendering pages (300 dpi)…".into()),
        )));
        let status = a.db.list_files(&space).unwrap()[0].status.clone();
        assert_eq!(status, "ocr: rendering pages (300 dpi)…");

        // Progress with failures shows the count.
        a.on_ocr_done(Some((
            space.clone(),
            "scan.pdf".into(),
            OcrUpdate::Progress(5, 10, 2),
        )));
        assert_eq!(
            a.db.list_files(&space).unwrap()[0].status,
            "ocr 5/10 (2 failed)"
        );
        assert!(a.status.contains("5/10 pages (2 failed)"));

        // Partial success keeps the first failure's reason in the status.
        a.on_ocr_done(Some((
            space.clone(),
            "scan.pdf".into(),
            OcrUpdate::Done(Ok((
                "[page 1]\ntext".to_string(),
                vec![
                    (2, "timeout after 600s".to_string()),
                    (4, "boom".to_string()),
                ],
            ))),
        )));
        let status = a.db.list_files(&space).unwrap()[0].status.clone();
        assert_eq!(status, "ok — 2 pages failed (p3: timeout after 600s)");

        // All pages failed → the reason, not a bland "no text".
        let _ = a.db.set_file_status(&id, "ocr…");
        a.on_ocr_done(Some((
            space.clone(),
            "scan.pdf".into(),
            OcrUpdate::Done(Ok((
                String::new(),
                vec![(0, "cannot reach ollama at 127.0.0.1:11434 — is it running? (systemctl start ollama)".to_string())],
            ))),
        )));
        let status = a.db.list_files(&space).unwrap()[0].status.clone();
        assert!(
            status.starts_with("all pages failed (p1: cannot reach ollama"),
            "{status}"
        );
        // Must NOT start with "ocr" — that prefix means "queued" to the rescan.
        assert!(!status.starts_with("ocr"), "{status}");
    }

    #[test]
    fn reextract_clears_stale_chunks_and_reindexes_from_disk() {
        let mut a = test_app();
        let dir = a.space.files_dir(&a.active_space.name);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("doc.txt"), "real content on disk").unwrap();
        a.rescan_files();
        let id = a.files_cache[0].id.clone();

        // Simulate a bad old extraction (e.g. tesseract-mangled OCR).
        a.db.set_file_chunks(&id, &[("p1".into(), "garbage".into())])
            .unwrap();

        a.files_selected = 0;
        a.reextract_selected_file();
        assert!(a.status.contains("re-extracting"), "{}", a.status);
        let texts = a.db.file_chunk_texts(&id).unwrap();
        assert_eq!(texts.len(), 1);
        assert!(texts[0].1.contains("real content"), "{texts:?}");
        assert_eq!(a.files_cache[0].status, "ok");
    }

    #[test]
    fn rescan_picks_up_dropped_and_deleted_files() {
        let mut a = test_app();
        let dir = a.space.files_dir(&a.active_space.name);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("dropped.txt"), "hello dropped").unwrap();

        a.rescan_files();
        assert_eq!(a.files_cache.len(), 1);
        assert_eq!(a.files_cache[0].name, "dropped.txt");

        // Changing content re-extracts (hash change), deleting drops the row.
        std::fs::write(dir.join("dropped.txt"), "hello again").unwrap();
        a.rescan_files();
        assert_eq!(a.files_cache.len(), 1);
        std::fs::remove_file(dir.join("dropped.txt")).unwrap();
        a.rescan_files();
        assert!(a.files_cache.is_empty());
    }

    #[test]
    fn empty_extraction_gets_no_text_status() {
        let mut a = test_app();
        let dir = a.space.files_dir(&a.active_space.name);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("empty.txt"), "   ").unwrap();
        a.rescan_files();
        assert_eq!(a.files_cache[0].status, "no text (scanned?)");
    }

    #[test]
    fn rescan_skips_stat_unchanged_files_without_rehashing() {
        let mut a = test_app();
        let dir = a.space.files_dir(&a.active_space.name);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("book.txt"), "big content").unwrap();
        a.rescan_files();
        let f = a.files_cache[0].clone();
        assert!(f.mtime > 0, "mtime recorded on index");

        // Plant a wrong hash; a stat-unchanged rescan must not correct it —
        // proof the file wasn't re-read/re-hashed.
        a.db.upsert_file(&a.active_space.id, "book.txt", "sentinel", f.size, "ok")
            .unwrap();
        a.rescan_files();
        assert_eq!(a.files_cache[0].hash, "sentinel");

        // A size change busts the stat check and re-hashes for real.
        std::fs::write(dir.join("book.txt"), "big content grew").unwrap();
        a.rescan_files();
        assert_ne!(a.files_cache[0].hash, "sentinel");
        assert_eq!(a.files_cache[0].status, "ok");
    }

    #[test]
    fn rename_moves_disk_file_and_reindexes() {
        let mut a = test_app();
        let dir = a.space.files_dir(&a.active_space.name);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("old.txt"), "searchable content").unwrap();
        std::fs::write(dir.join("taken.txt"), "x").unwrap();
        a.rescan_files();
        a.files_selected = a
            .files_cache
            .iter()
            .position(|f| f.name == "old.txt")
            .unwrap();

        // Collides with an existing name: rejected, nothing moves.
        a.start_files_rename();
        assert_eq!(a.files_edit, "old.txt");
        a.files_edit = "taken.txt".to_string();
        a.confirm_files_rename().unwrap();
        assert!(a.status.contains("already exists"));
        assert!(dir.join("old.txt").exists());

        // Bad name rejected.
        a.files_selected = a
            .files_cache
            .iter()
            .position(|f| f.name == "old.txt")
            .unwrap();
        a.start_files_rename();
        a.files_edit = "../evil.txt".to_string();
        a.confirm_files_rename().unwrap();
        assert!(a.status.contains("invalid name"));

        // Valid rename: disk moves, index follows, cursor tracks the file.
        a.files_selected = a
            .files_cache
            .iter()
            .position(|f| f.name == "old.txt")
            .unwrap();
        a.start_files_rename();
        a.files_edit = "new.txt".to_string();
        a.confirm_files_rename().unwrap();
        assert!(!dir.join("old.txt").exists());
        assert!(dir.join("new.txt").exists());
        assert!(a.files_cache.iter().any(|f| f.name == "new.txt"));
        assert_eq!(a.files_cache[a.files_selected].name, "new.txt");
    }

    #[test]
    fn delete_removes_disk_file_and_row() {
        let mut a = test_app();
        let dir = a.space.files_dir(&a.active_space.name);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("gone.txt"), "bye").unwrap();
        a.rescan_files();
        a.files_selected = 0;
        a.confirm_files_delete().unwrap();
        assert!(a.files_cache.is_empty());
        assert!(!dir.join("gone.txt").exists());
    }

    #[test]
    fn files_command_opens_popup_and_rescans() {
        let mut a = test_app();
        let dir = a.space.files_dir(&a.active_space.name);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("seen.txt"), "content").unwrap();
        a.run_command("files").unwrap();
        assert!(a.popup == crate::app::Popup::Files);
        assert_eq!(a.files_cache.len(), 1);
        assert!(a.files_mode == crate::app::FilesMode::Browse);
    }

    #[test]
    fn confirm_files_add_imports_typed_path() {
        let mut a = test_app();
        let src = std::env::temp_dir().join(format!("nexus-add-{}.txt", uuid::Uuid::new_v4()));
        std::fs::write(&src, "typed in").unwrap();
        a.start_files_add();
        assert!(a.files_mode == crate::app::FilesMode::Add);
        a.files_edit = src.to_string_lossy().to_string();
        a.confirm_files_add();
        assert!(a.files_mode == crate::app::FilesMode::Browse);
        assert_eq!(a.files_cache.len(), 1);

        // A bad path reports in status and stays recoverable.
        a.start_files_add();
        a.files_edit = "/definitely/not/a/file".to_string();
        a.confirm_files_add();
        assert!(a.status.contains("not a file"));
        assert_eq!(a.files_cache.len(), 1);
    }

    #[test]
    fn picker_lists_dirs_first_descends_and_imports() {
        let mut a = test_app();
        let root = std::env::temp_dir().join(format!("nexus-pick-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(root.join("subdir")).unwrap();
        std::fs::write(root.join("bbb.txt"), "file b").unwrap();
        std::fs::write(root.join("aaa.txt"), "file a").unwrap();

        a.picker_dir = root.clone();
        a.open_file_picker();
        assert!(a.files_mode == crate::app::FilesMode::Pick);
        let names: Vec<&str> = a
            .filtered_picker_entries()
            .iter()
            .map(|e| e.name.as_str())
            .collect();
        assert_eq!(names, vec!["subdir", "aaa.txt", "bbb.txt"]); // dirs first, then alpha

        // Enter on a dir descends and reloads.
        a.picker_selected = 0;
        a.picker_enter();
        assert_eq!(a.picker_dir, root.join("subdir"));
        assert!(a.filtered_picker_entries().is_empty());

        // Backspace with empty filter ascends.
        a.picker_backspace();
        assert_eq!(a.picker_dir, root);

        // Enter on a file imports it and returns to Browse.
        let idx = a
            .filtered_picker_entries()
            .iter()
            .position(|e| e.name == "aaa.txt")
            .unwrap();
        a.picker_selected = idx;
        a.picker_enter();
        assert!(a.files_mode == crate::app::FilesMode::Browse);
        assert!(a.files_cache.iter().any(|f| f.name == "aaa.txt"));
    }

    #[test]
    fn picker_filter_fuzzy_matches_and_backspace_edits_filter_first() {
        let mut a = test_app();
        let root = std::env::temp_dir().join(format!("nexus-pick-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&root).unwrap();
        std::fs::write(root.join("report-2026.pdf"), "x").unwrap();
        std::fs::write(root.join("notes.md"), "y").unwrap();
        a.picker_dir = root.clone();
        a.open_file_picker();

        a.picker_filter_push('r');
        a.picker_filter_push('p');
        a.picker_filter_push('t');
        let names: Vec<&str> = a
            .filtered_picker_entries()
            .iter()
            .map(|e| e.name.as_str())
            .collect();
        assert_eq!(names, vec!["report-2026.pdf"]); // fuzzy subsequence "rpt"

        // Backspace edits the filter (does NOT ascend while filter non-empty).
        a.picker_backspace();
        assert_eq!(a.picker_filter, "rp");
        assert_eq!(a.picker_dir, root);
    }

    #[tokio::test]
    async fn rescan_marks_empty_pdf_ocr_and_spawns_batch() {
        let mut a = test_app();
        let dir = a.space.files_dir(&a.active_space.name);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("scan.pdf"), crate::extract::minimal_pdf(None)).unwrap();

        a.rescan_files();
        assert_eq!(a.files_cache[0].status, "ocr…");
        assert!(a.ocr_rx.is_some(), "an ocr batch should be in flight");

        // A second rescan while the batch is in flight does not re-queue.
        a.rescan_files();
        assert_eq!(a.files_cache[0].status, "ocr…");
    }

    #[tokio::test]
    async fn rescan_requeues_stale_ocr_status_when_idle() {
        let mut a = test_app();
        let dir = a.space.files_dir(&a.active_space.name);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("scan.pdf"), crate::extract::minimal_pdf(None)).unwrap();
        a.rescan_files();

        // Simulate an app restart mid-OCR: status stuck at "ocr…", no batch in flight.
        a.ocr_rx = None;
        a.rescan_files();
        assert!(a.ocr_rx.is_some(), "stale ocr… should re-queue");
    }

    #[test]
    fn on_ocr_done_ok_indexes_and_marks_ok() {
        let mut a = test_app();
        let id =
            a.db.upsert_file(&a.active_space.id, "scan.pdf", "h", 9, "ocr…")
                .unwrap();
        let _ = id;
        a.on_ocr_done(Some((
            a.active_space.id.clone(),
            "scan.pdf".to_string(),
            OcrUpdate::Done(Ok((
                "[page 1]\nquarterly revenue table".to_string(),
                Vec::new(),
            ))),
        )));
        assert_eq!(a.files_cache[0].status, "ok");
        let hits = crate::db::search_chunks(a.db.conn_for_test(), &a.active_space.id, "revenue", 8)
            .unwrap();
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn on_ocr_progress_updates_status_and_status_line() {
        let mut a = test_app();
        a.db.upsert_file(&a.active_space.id, "scan.pdf", "h", 9, "ocr…")
            .unwrap();
        a.on_ocr_done(Some((
            a.active_space.id.clone(),
            "scan.pdf".to_string(),
            OcrUpdate::Progress(3, 10, 0),
        )));
        assert_eq!(a.files_cache[0].status, "ocr 3/10");
        assert!(a.status.contains("3/10"), "{}", a.status);

        // Progress for a file mid-way is still non-terminal: a Done after it applies.
        a.on_ocr_done(Some((
            a.active_space.id.clone(),
            "scan.pdf".to_string(),
            OcrUpdate::Done(Ok(("[page 1]\nfound".to_string(), Vec::new()))),
        )));
        assert_eq!(a.files_cache[0].status, "ok");
    }

    #[test]
    fn on_ocr_done_empty_and_err_statuses() {
        let mut a = test_app();
        a.db.upsert_file(&a.active_space.id, "blank.pdf", "h1", 9, "ocr…")
            .unwrap();
        a.db.upsert_file(&a.active_space.id, "bad.pdf", "h2", 9, "ocr…")
            .unwrap();

        a.on_ocr_done(Some((
            a.active_space.id.clone(),
            "blank.pdf".to_string(),
            OcrUpdate::Done(Ok((String::new(), Vec::new()))),
        )));
        a.on_ocr_done(Some((
            a.active_space.id.clone(),
            "bad.pdf".to_string(),
            OcrUpdate::Done(Err(
                "scanned pdf — install tesseract + poppler for ocr".to_string()
            )),
        )));

        let by_name = |a: &App, n: &str| {
            a.files_cache
                .iter()
                .find(|f| f.name == n)
                .unwrap()
                .status
                .clone()
        };
        assert_eq!(by_name(&a, "blank.pdf"), "no text (ocr found nothing)");
        assert_eq!(
            by_name(&a, "bad.pdf"),
            "scanned pdf — install tesseract + poppler for ocr"
        );
    }

    #[test]
    fn on_ocr_done_for_inactive_space_writes_db_but_not_cache() {
        let mut a = test_app();
        let other = a.db.create_space("other").unwrap();
        a.db.upsert_file(&other.id, "scan.pdf", "h", 9, "ocr…")
            .unwrap();

        a.on_ocr_done(Some((
            other.id.clone(),
            "scan.pdf".to_string(),
            OcrUpdate::Done(Ok(("found text".to_string(), Vec::new()))),
        )));

        assert!(
            a.files_cache.is_empty(),
            "active-space cache must not show other space's file"
        );
        let rows = a.db.list_files(&other.id).unwrap();
        assert_eq!(rows[0].status, "ok");

        // Deleted-mid-OCR: result for a row that no longer exists is a no-op.
        a.on_ocr_done(Some((
            other.id.clone(),
            "gone.pdf".to_string(),
            OcrUpdate::Done(Ok(("x".to_string(), Vec::new()))),
        )));
    }

    #[tokio::test]
    async fn on_ocr_done_none_clears_channel_and_requeues_stragglers() {
        let mut a = test_app();
        let dir = a.space.files_dir(&a.active_space.name);
        std::fs::create_dir_all(&dir).unwrap();
        // A scanned PDF stuck at "ocr…" (imported while a batch was running).
        std::fs::write(dir.join("scan.pdf"), crate::extract::minimal_pdf(None)).unwrap();
        a.rescan_files();
        assert!(a.ocr_rx.is_some());
        // Batch finishes: channel clears, and the straggler chains into a new batch.
        a.on_ocr_done(None);
        assert!(a.ocr_rx.is_some(), "straggler should re-queue on batch end");
    }
}