ragrig-gpui 0.5.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
//! Managed GROBID service.
//!
//! [`GrobidService`] owns the Docker lifecycle of the container created by the
//! **External services** settings page. It also probes existing servers, so a
//! host can switch between a local container and a server the user already
//! runs. The controller mirrors every state change for the settings page and
//! emits [`GrobidServiceEvent`]s for hosts that persist the URL.
//!
//! All Docker and HTTP work happens on a dedicated Tokio runtime thread. Do not
//! drive these futures from GPUI's executor.

use std::{
    collections::HashMap,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    thread,
    time::{Duration, Instant},
};

use async_channel::{Sender, TrySendError};
use bollard::{
    Docker,
    models::{ContainerCreateBody, HostConfig, PortBinding, ResourcesUlimits},
    query_parameters::{
        CreateContainerOptionsBuilder, CreateImageOptionsBuilder, RemoveContainerOptionsBuilder,
        StopContainerOptionsBuilder,
    },
};
use futures_util::StreamExt;
use gpui_kit::{Context, Entity, EventEmitter};
use reqwest::Client;

use crate::rag::{GrobidSource, RagSettings};

/// Container name used for the managed GROBID container.
pub const MANAGED_GROBID_CONTAINER: &str = "ragrig-grobid";

/// Loopback URL exposed by the managed GROBID container.
pub const MANAGED_GROBID_URL: &str = "http://127.0.0.1:8070";

/// Host port exposed by the managed GROBID container.
pub const MANAGED_GROBID_PORT: u16 = 8070;

/// Ports probed by **Discover on localhost**.
pub const GROBID_DISCOVERY_PORTS: [u16; 2] = [8070, 8080];

const CONTAINER_LABEL: &str = "io.ragrig.managed";
const SERVICE_LABEL: &str = "io.ragrig.service";
const READY_TIMEOUT: Duration = Duration::from_secs(180);
const CANCELLED: &str = "cancelled";

/// Whether Docker is reachable from this process.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum GrobidDockerStatus {
    /// Docker has not been checked yet.
    #[default]
    Unknown,
    /// The daemon is being contacted.
    Checking,
    /// The daemon answered and reported its version.
    Available {
        /// Docker Engine version.
        version: String,
    },
    /// The daemon could not be reached.
    Unavailable {
        /// Human-readable diagnosis and remediation hint.
        message: String,
    },
}

impl GrobidDockerStatus {
    /// One line for the settings page.
    pub fn summary(&self) -> String {
        match self {
            Self::Unknown => "Not checked".to_owned(),
            Self::Checking => "Checking Docker…".to_owned(),
            Self::Available { version } => format!("Docker {version} ready"),
            Self::Unavailable { message } => message.clone(),
        }
    }
}

/// Lifecycle of the managed GROBID container.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum GrobidContainerStatus {
    /// The container has not been inspected yet.
    #[default]
    Unknown,
    /// Docker is being inspected.
    Checking,
    /// No container with Scico's name exists.
    Missing,
    /// The managed container exists but is stopped.
    Stopped,
    /// An image pull is running.
    Pulling {
        /// Image reference being pulled.
        image: String,
        /// Bytes downloaded across all layers so far.
        received: u64,
        /// Total bytes across all layers, when Docker reported it.
        total: u64,
    },
    /// The container is starting and its health endpoint is polled.
    Starting,
    /// GROBID answers its health endpoint.
    Running,
    /// The container is being stopped.
    Stopping,
    /// The last operation failed.
    Failed {
        /// Human-readable failure.
        message: String,
    },
}

impl GrobidContainerStatus {
    /// One line for the settings page.
    pub fn summary(&self) -> String {
        match self {
            Self::Unknown => "Not checked".to_owned(),
            Self::Checking => "Checking the container…".to_owned(),
            Self::Missing => "Container not created".to_owned(),
            Self::Stopped => "Container stopped".to_owned(),
            Self::Pulling {
                image,
                received,
                total,
            } if *total > 0 => {
                let percent = (*received as f64 / *total as f64 * 100.0).clamp(0.0, 100.0);
                format!(
                    "Pulling {image} — {percent:.0}% ({} of {})",
                    format_bytes(*received),
                    format_bytes(*total)
                )
            }
            Self::Pulling {
                image, received, ..
            } => {
                format!("Pulling {image} — {}", format_bytes(*received))
            }
            Self::Starting => "Starting GROBID…".to_owned(),
            Self::Running => "Running".to_owned(),
            Self::Stopping => "Stopping…".to_owned(),
            Self::Failed { message } => format!("Failed: {message}"),
        }
    }
}

/// Result of probing one GROBID endpoint.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GrobidProbe {
    /// Normalized base URL that answered.
    pub url: String,
    /// Version reported by `/api/version`, when available.
    pub version: Option<String>,
}

/// State of the last endpoint probe.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum GrobidProbeStatus {
    /// No endpoint has been tested.
    #[default]
    Idle,
    /// A probe is running.
    Checking {
        /// Endpoint being tested.
        url: String,
    },
    /// The endpoint answered `/api/isalive`.
    Reachable {
        /// Normalized endpoint that answered.
        url: String,
        /// Version reported by `/api/version`, when available.
        version: Option<String>,
    },
    /// The endpoint did not answer as GROBID.
    Unreachable {
        /// Endpoint that was tested.
        url: String,
        /// Human-readable failure.
        message: String,
    },
}

impl GrobidProbeStatus {
    /// One line for the settings page.
    pub fn summary(&self) -> String {
        match self {
            Self::Idle => "Not tested".to_owned(),
            Self::Checking { url } => format!("Testing {url}…"),
            Self::Reachable { url, version } => match version {
                Some(version) => format!("GROBID {version} at {url}"),
                None => format!("GROBID reachable at {url}"),
            },
            Self::Unreachable { url, message } => format!("{url}: {message}"),
        }
    }
}

/// Changes a host may want to observe.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GrobidServiceEvent {
    /// Docker availability changed.
    Docker(GrobidDockerStatus),
    /// Managed container lifecycle changed.
    Container(GrobidContainerStatus),
    /// Endpoint probe changed.
    Probe(GrobidProbeStatus),
    /// Localhost discovery found one or more servers.
    Discovered(Vec<GrobidProbe>),
    /// A managed container or an adopted server is ready.
    Ready {
        /// Base URL that answered.
        url: String,
        /// Version reported by `/api/version`, when available.
        version: Option<String>,
        /// Whether the app created or started the container.
        managed: bool,
    },
    /// The configured GROBID URL changed.
    UrlChanged(String),
    /// An operation failed with a user-facing message.
    Failed {
        /// Human-readable failure.
        message: String,
    },
}

enum GrobidCommand {
    Inspect { adopt: bool },
    Probe { url: String },
    Discover,
    PullAndStart { image: String },
    Stop,
    Remove,
}

/// Owns the managed GROBID container and probes existing servers.
///
/// Dropping the entity stops a container this service created or adopted in
/// managed mode. A GROBID server that is not a managed container is never
/// touched.
pub struct GrobidService {
    commands: Sender<GrobidCommand>,
    worker: Option<thread::JoinHandle<()>>,
    cancel: Arc<AtomicBool>,
    settings: Entity<RagSettings>,
    docker: GrobidDockerStatus,
    container: GrobidContainerStatus,
    probe: GrobidProbeStatus,
    discovered: Vec<GrobidProbe>,
    notice: Option<String>,
}

impl GrobidService {
    /// Spawn the service worker and start inspecting Docker and the endpoint.
    pub fn new(settings: Entity<RagSettings>, cx: &mut Context<Self>) -> Result<Self, String> {
        let initial = settings.read(cx).clone();
        let (commands, command_rx) = async_channel::bounded(16);
        let (events_tx, events) = async_channel::bounded(128);
        let cancel = Arc::new(AtomicBool::new(false));

        // GPUI's test scheduler rejects wakes that arrive from a foreign
        // thread, and starting the worker would probe Docker and localhost on
        // every test. Tests drive `on_event` directly instead.
        let worker = if cx.is_test() {
            None
        } else {
            let worker_cancel = cancel.clone();
            let worker = thread::Builder::new()
                .name("ragrig-grobid".to_owned())
                .spawn(move || {
                    let runtime = tokio::runtime::Builder::new_multi_thread()
                        .worker_threads(2)
                        .enable_all()
                        .build();
                    match runtime {
                        Ok(runtime) => runtime.block_on(run(command_rx, events_tx, worker_cancel)),
                        Err(error) => {
                            let _ = events_tx.send_blocking(GrobidServiceEvent::Failed {
                                message: format!(
                                    "cannot start the GROBID service runtime: {error}"
                                ),
                            });
                        }
                    }
                })
                .map_err(|error| format!("cannot start the GROBID service thread: {error}"))?;

            cx.spawn(async move |this, cx| {
                while let Ok(event) = events.recv().await {
                    if this
                        .update(cx, |service, cx| service.on_event(event, cx))
                        .is_err()
                    {
                        break;
                    }
                }
            })
            .detach();

            let _ = commands.try_send(GrobidCommand::Inspect {
                adopt: initial.grobid_source == GrobidSource::Managed,
            });
            if initial.grobid_source == GrobidSource::Existing {
                let _ = commands.try_send(GrobidCommand::Probe {
                    url: initial.grobid_url,
                });
            }

            Some(worker)
        };

        Ok(Self {
            commands,
            worker,
            cancel,
            settings,
            docker: GrobidDockerStatus::Unknown,
            container: GrobidContainerStatus::Unknown,
            probe: GrobidProbeStatus::Idle,
            discovered: Vec::new(),
            notice: None,
        })
    }

    /// Whether Docker is reachable.
    pub fn docker(&self) -> &GrobidDockerStatus {
        &self.docker
    }

    /// Managed container lifecycle.
    pub fn container(&self) -> &GrobidContainerStatus {
        &self.container
    }

    /// State of the last endpoint probe.
    pub fn probe(&self) -> &GrobidProbeStatus {
        &self.probe
    }

    /// Servers found by the last localhost discovery.
    pub fn discovered(&self) -> &[GrobidProbe] {
        &self.discovered
    }

    /// The last user-facing notice.
    pub fn notice(&self) -> Option<&str> {
        self.notice.as_deref()
    }

    /// Whether a container operation is in flight.
    pub fn is_busy(&self) -> bool {
        matches!(
            &self.container,
            GrobidContainerStatus::Pulling { .. }
                | GrobidContainerStatus::Starting
                | GrobidContainerStatus::Stopping
        )
    }

    /// Whether the running pull or startup can still be cancelled.
    pub fn is_cancellable(&self) -> bool {
        matches!(
            &self.container,
            GrobidContainerStatus::Pulling { .. } | GrobidContainerStatus::Starting
        ) && !self.cancel.load(Ordering::Relaxed)
    }

    /// Whether a cancellation was requested and the worker has not reacted yet.
    pub fn is_cancelling(&self) -> bool {
        self.cancel.load(Ordering::Relaxed)
            && matches!(
                &self.container,
                GrobidContainerStatus::Pulling { .. } | GrobidContainerStatus::Starting
            )
    }

    /// Ask the worker to abandon the current pull or startup.
    pub fn cancel(&mut self, cx: &mut Context<Self>) {
        if self.is_cancellable() {
            self.cancel.store(true, Ordering::Relaxed);
            self.notice = Some("cancelling…".to_owned());
            cx.notify();
        }
    }

    /// Re-inspect Docker and the managed container.
    pub fn refresh(&mut self, cx: &mut Context<Self>) {
        let adopt = self.settings.read(cx).grobid_source == GrobidSource::Managed;
        self.send_quiet(GrobidCommand::Inspect { adopt }, cx);
    }

    /// Probe the given URL without changing it until the probe succeeds.
    pub fn test_url(&mut self, url: impl Into<String>, cx: &mut Context<Self>) {
        let url = url.into();
        match normalize_grobid_url(&url) {
            Ok(url) => {
                self.probe = GrobidProbeStatus::Checking { url: url.clone() };
                self.discovered.clear();
                self.notice = None;
                self.send_quiet(GrobidCommand::Probe { url }, cx);
            }
            Err(message) => {
                self.probe = GrobidProbeStatus::Unreachable { url, message };
                cx.notify();
            }
        }
    }

    /// Probe the common localhost ports and use the first server found.
    pub fn discover(&mut self, cx: &mut Context<Self>) {
        self.probe = GrobidProbeStatus::Checking {
            url: "localhost".to_owned(),
        };
        self.discovered.clear();
        self.notice = None;
        self.send_quiet(GrobidCommand::Discover, cx);
    }

    /// Pull `image` when needed, create the container, and wait for health.
    pub fn pull_and_start(&mut self, image: impl Into<String>, cx: &mut Context<Self>) {
        let image = image.into();
        if image.trim().is_empty() {
            self.notice = Some("enter a Docker image first".to_owned());
            cx.notify();
            return;
        }
        self.notice = None;
        self.cancel.store(false, Ordering::Relaxed);
        self.send_quiet(
            GrobidCommand::PullAndStart {
                image: image.trim().to_owned(),
            },
            cx,
        );
    }

    /// Stop the managed container without removing it.
    pub fn stop(&mut self, cx: &mut Context<Self>) {
        self.container = GrobidContainerStatus::Stopping;
        self.send_quiet(GrobidCommand::Stop, cx);
    }

    /// Stop and remove the managed container.
    pub fn remove(&mut self, cx: &mut Context<Self>) {
        self.container = GrobidContainerStatus::Stopping;
        self.send_quiet(GrobidCommand::Remove, cx);
    }

    /// Clear a stale endpoint probe after the URL was edited.
    pub fn clear_probe(&mut self, cx: &mut Context<Self>) {
        if !matches!(&self.probe, GrobidProbeStatus::Idle) || !self.discovered.is_empty() {
            self.probe = GrobidProbeStatus::Idle;
            self.discovered.clear();
            cx.notify();
        }
    }

    fn send_quiet(&mut self, command: GrobidCommand, cx: &mut Context<Self>) {
        if let Err(error) = self.commands.try_send(command) {
            self.notice = Some(match error {
                TrySendError::Full(_) => "the GROBID service is busy; try again".to_owned(),
                TrySendError::Closed(_) => "the GROBID service stopped".to_owned(),
            });
        }
        cx.notify();
    }

    fn on_event(&mut self, event: GrobidServiceEvent, cx: &mut Context<Self>) {
        let mut changed_url = None;
        match &event {
            GrobidServiceEvent::Docker(status) => {
                if matches!(status, GrobidDockerStatus::Available { .. }) {
                    self.notice = None;
                }
                self.docker = status.clone();
            }
            GrobidServiceEvent::Container(status) => {
                if matches!(
                    status,
                    GrobidContainerStatus::Missing
                        | GrobidContainerStatus::Stopped
                        | GrobidContainerStatus::Running
                        | GrobidContainerStatus::Failed { .. }
                ) {
                    self.cancel.store(false, Ordering::Relaxed);
                }
                self.container = status.clone();
            }
            GrobidServiceEvent::Probe(status) => {
                if matches!(status, GrobidProbeStatus::Reachable { .. }) {
                    self.notice = None;
                }
                if let GrobidProbeStatus::Reachable { url, .. } = status {
                    changed_url = Some(url.clone());
                }
                self.probe = status.clone();
            }
            GrobidServiceEvent::Discovered(probes) => {
                self.discovered = probes.clone();
                if let Some(first) = probes.first() {
                    changed_url = Some(first.url.clone());
                    self.settings.update(cx, |settings, cx| {
                        if settings.grobid_source != GrobidSource::Existing {
                            settings.grobid_source = GrobidSource::Existing;
                            cx.notify();
                        }
                    });
                }
            }
            GrobidServiceEvent::Ready {
                url,
                version,
                managed,
            } => {
                self.container = GrobidContainerStatus::Running;
                self.probe = GrobidProbeStatus::Reachable {
                    url: url.clone(),
                    version: version.clone(),
                };
                self.notice = if *managed {
                    None
                } else {
                    Some(format!(
                        "{url} already answers; using it instead of starting a container"
                    ))
                };
                changed_url = Some(url.clone());
                let source = if *managed {
                    GrobidSource::Managed
                } else {
                    GrobidSource::Existing
                };
                self.settings.update(cx, |settings, cx| {
                    if settings.grobid_source != source {
                        settings.grobid_source = source;
                        cx.notify();
                    }
                });
            }
            GrobidServiceEvent::UrlChanged(_) => {}
            GrobidServiceEvent::Failed { message } => {
                self.notice = Some(message.clone());
            }
        }

        if let Some(url) = changed_url {
            let url_for_settings = url.clone();
            self.settings.update(cx, |settings, cx| {
                if settings.grobid_url != url_for_settings {
                    settings.grobid_url = url_for_settings;
                    cx.notify();
                }
            });
            cx.emit(GrobidServiceEvent::UrlChanged(url));
        }
        cx.notify();
        cx.emit(event);
    }
}

impl EventEmitter<GrobidServiceEvent> for GrobidService {}

impl Drop for GrobidService {
    fn drop(&mut self) {
        self.commands.close();
        if let Some(worker) = self.worker.take() {
            let _ = worker.join();
        }
    }
}

async fn run(
    commands: async_channel::Receiver<GrobidCommand>,
    events: Sender<GrobidServiceEvent>,
    cancel: Arc<AtomicBool>,
) {
    let client = match Client::builder().timeout(Duration::from_secs(3)).build() {
        Ok(client) => client,
        Err(error) => {
            let _ = events
                .send(GrobidServiceEvent::Failed {
                    message: format!("cannot build the GROBID HTTP client: {error}"),
                })
                .await;
            return;
        }
    };
    let mut owns_container = false;

    while let Ok(command) = commands.recv().await {
        match command {
            GrobidCommand::Inspect { adopt } => {
                let present = inspect(&client, &events, adopt).await;
                if adopt {
                    owns_container = present;
                }
            }
            GrobidCommand::Probe { url } => {
                probe_command(&client, &url, &events).await;
            }
            GrobidCommand::Discover => {
                discover(&client, &events, &commands).await;
            }
            GrobidCommand::PullAndStart { image } => {
                if pull_and_start(&client, &image, &events, &commands, &cancel).await {
                    owns_container = true;
                }
            }
            GrobidCommand::Stop => {
                stop(&events).await;
            }
            GrobidCommand::Remove => {
                if !remove(&events).await {
                    owns_container = false;
                }
            }
        }
    }

    if owns_container && let Ok(docker) = connect_docker_quiet().await {
        let options = StopContainerOptionsBuilder::default().t(5).build();
        let _ = tokio::time::timeout(
            Duration::from_secs(8),
            docker.stop_container(MANAGED_GROBID_CONTAINER, Some(options)),
        )
        .await;
    }
}

async fn inspect(client: &Client, events: &Sender<GrobidServiceEvent>, adopt: bool) -> bool {
    events
        .send(GrobidServiceEvent::Container(
            GrobidContainerStatus::Checking,
        ))
        .await
        .ok();
    let Some(docker) = connect_docker(events).await else {
        return false;
    };
    match docker
        .inspect_container(MANAGED_GROBID_CONTAINER, None)
        .await
    {
        Ok(info) => {
            let running = info
                .state
                .as_ref()
                .and_then(|state| state.running)
                .unwrap_or(false);
            if running {
                events
                    .send(GrobidServiceEvent::Container(
                        GrobidContainerStatus::Running,
                    ))
                    .await
                    .ok();
                if adopt
                    && let Ok(probe) =
                        probe_grobid(client, MANAGED_GROBID_URL, Duration::from_secs(2)).await
                {
                    events
                        .send(GrobidServiceEvent::Ready {
                            url: probe.url,
                            version: probe.version,
                            managed: true,
                        })
                        .await
                        .ok();
                }
            } else {
                events
                    .send(GrobidServiceEvent::Container(
                        GrobidContainerStatus::Stopped,
                    ))
                    .await
                    .ok();
            }
            true
        }
        Err(_) => {
            events
                .send(GrobidServiceEvent::Container(
                    GrobidContainerStatus::Missing,
                ))
                .await
                .ok();
            false
        }
    }
}

async fn probe_command(client: &Client, url: &str, events: &Sender<GrobidServiceEvent>) {
    let normalized = match normalize_grobid_url(url) {
        Ok(url) => url,
        Err(message) => {
            events
                .send(GrobidServiceEvent::Probe(GrobidProbeStatus::Unreachable {
                    url: url.to_owned(),
                    message,
                }))
                .await
                .ok();
            return;
        }
    };
    events
        .send(GrobidServiceEvent::Probe(GrobidProbeStatus::Checking {
            url: normalized.clone(),
        }))
        .await
        .ok();
    let status = match probe_grobid(client, &normalized, Duration::from_secs(3)).await {
        Ok(probe) => GrobidProbeStatus::Reachable {
            url: probe.url,
            version: probe.version,
        },
        Err(message) => GrobidProbeStatus::Unreachable {
            url: normalized,
            message,
        },
    };
    events.send(GrobidServiceEvent::Probe(status)).await.ok();
}

async fn discover(
    client: &Client,
    events: &Sender<GrobidServiceEvent>,
    commands: &async_channel::Receiver<GrobidCommand>,
) {
    events
        .send(GrobidServiceEvent::Probe(GrobidProbeStatus::Checking {
            url: "localhost".to_owned(),
        }))
        .await
        .ok();

    let mut found = Vec::new();
    for port in GROBID_DISCOVERY_PORTS {
        for host in ["127.0.0.1", "localhost"] {
            if commands.is_closed() {
                return;
            }
            let url = format!("http://{host}:{port}");
            if let Ok(probe) = probe_grobid(client, &url, Duration::from_millis(900)).await {
                found.push(probe);
                break;
            }
        }
        if !found.is_empty() {
            break;
        }
    }

    if found.is_empty() {
        events
            .send(GrobidServiceEvent::Probe(GrobidProbeStatus::Unreachable {
                url: "localhost".to_owned(),
                message: "No GROBID server answered on ports 8070 or 8080".to_owned(),
            }))
            .await
            .ok();
        return;
    }

    events
        .send(GrobidServiceEvent::Probe(GrobidProbeStatus::Reachable {
            url: found[0].url.clone(),
            version: found[0].version.clone(),
        }))
        .await
        .ok();
    events
        .send(GrobidServiceEvent::Discovered(found))
        .await
        .ok();
}

async fn pull_and_start(
    client: &Client,
    image: &str,
    events: &Sender<GrobidServiceEvent>,
    commands: &async_channel::Receiver<GrobidCommand>,
    cancel: &AtomicBool,
) -> bool {
    let Some(docker) = connect_docker(events).await else {
        return false;
    };

    let existing = docker
        .inspect_container(MANAGED_GROBID_CONTAINER, None)
        .await
        .ok();
    if let Some(info) = existing {
        let running = info
            .state
            .as_ref()
            .and_then(|state| state.running)
            .unwrap_or(false);
        if !running {
            events
                .send(GrobidServiceEvent::Container(
                    GrobidContainerStatus::Starting,
                ))
                .await
                .ok();
            if let Err(error) = docker.start_container(MANAGED_GROBID_CONTAINER, None).await {
                fail(
                    &format!("cannot start the GROBID container: {error}"),
                    events,
                )
                .await;
                return true;
            }
        }
    } else {
        // Never fight a server that already owns the port. It is not ours, so
        // the service adopts its URL without owning its lifecycle.
        if let Ok(probe) =
            probe_grobid(client, MANAGED_GROBID_URL, Duration::from_millis(900)).await
        {
            events
                .send(GrobidServiceEvent::Ready {
                    url: probe.url,
                    version: probe.version,
                    managed: false,
                })
                .await
                .ok();
            return false;
        }
        if let Err(error) = pull_image(&docker, image, events, commands, cancel).await {
            if error == CANCELLED {
                if !commands.is_closed() {
                    events
                        .send(GrobidServiceEvent::Container(
                            GrobidContainerStatus::Missing,
                        ))
                        .await
                        .ok();
                    events
                        .send(GrobidServiceEvent::Failed {
                            message: "pull cancelled".to_owned(),
                        })
                        .await
                        .ok();
                }
                return false;
            }
            fail(&format!("cannot pull '{image}': {error}"), events).await;
            return false;
        }
        if cancelled(commands, cancel) {
            if !commands.is_closed() {
                events
                    .send(GrobidServiceEvent::Failed {
                        message: "pull cancelled".to_owned(),
                    })
                    .await
                    .ok();
            }
            return false;
        }
        if let Err(error) = create_container(&docker, image).await {
            fail(
                &format!("cannot create the GROBID container: {error}"),
                events,
            )
            .await;
            return false;
        }
        if let Err(error) = docker.start_container(MANAGED_GROBID_CONTAINER, None).await {
            fail(
                &format!("cannot start the GROBID container: {error}"),
                events,
            )
            .await;
            return true;
        }
    }

    match wait_until_ready(client, events, commands, cancel).await {
        StartOutcome::Ready | StartOutcome::TimedOut => true,
        StartOutcome::Cancelled => {
            if commands.is_closed() {
                return true;
            }
            let options = StopContainerOptionsBuilder::default().t(5).build();
            let _ = docker
                .stop_container(MANAGED_GROBID_CONTAINER, Some(options))
                .await;
            events
                .send(GrobidServiceEvent::Container(
                    GrobidContainerStatus::Stopped,
                ))
                .await
                .ok();
            events
                .send(GrobidServiceEvent::Failed {
                    message: "start cancelled".to_owned(),
                })
                .await
                .ok();
            true
        }
    }
}

enum StartOutcome {
    Ready,
    TimedOut,
    Cancelled,
}

fn cancelled(commands: &async_channel::Receiver<GrobidCommand>, cancel: &AtomicBool) -> bool {
    commands.is_closed() || cancel.load(Ordering::Relaxed)
}

async fn pull_image(
    docker: &Docker,
    image: &str,
    events: &Sender<GrobidServiceEvent>,
    commands: &async_channel::Receiver<GrobidCommand>,
    cancel: &AtomicBool,
) -> Result<(), String> {
    let mut options = CreateImageOptionsBuilder::default().from_image(image);
    if let Some(platform) = managed_platform(image) {
        options = options.platform(platform);
    }
    let options = options.build();
    let mut stream = docker.create_image(Some(options), None, None);
    let mut layers: HashMap<String, u64> = HashMap::new();
    let mut totals: HashMap<String, u64> = HashMap::new();
    let mut last_emit = Instant::now() - Duration::from_secs(1);

    while let Some(item) = stream.next().await {
        if cancelled(commands, cancel) {
            return Err(CANCELLED.to_owned());
        }
        let info = item.map_err(|error| error.to_string())?;
        if let Some(error) = info
            .error_detail
            .as_ref()
            .and_then(|detail| detail.message.clone())
        {
            return Err(error);
        }
        if let Some(detail) = info.progress_detail.as_ref() {
            let id = info.id.clone().unwrap_or_default();
            let current = detail.current.unwrap_or_default().max(0) as u64;
            let total = detail.total.unwrap_or_default().max(0) as u64;
            layers.insert(id.clone(), current);
            if total > 0 {
                totals.insert(id, total);
            }
        }

        if last_emit.elapsed() >= Duration::from_millis(120) {
            let received = layers.values().sum();
            let total = totals.values().sum();
            send_progress(
                events,
                GrobidContainerStatus::Pulling {
                    image: image.to_owned(),
                    received,
                    total,
                },
            );
            last_emit = Instant::now();
        }
    }

    let received = layers.values().sum();
    let total = totals.values().sum();
    send_progress(
        events,
        GrobidContainerStatus::Pulling {
            image: image.to_owned(),
            received,
            total,
        },
    );
    Ok(())
}

/// Progress is droppable: a stalled UI must never block the Docker worker while
/// the app is shutting down and no longer drains the event channel.
fn send_progress(events: &Sender<GrobidServiceEvent>, status: GrobidContainerStatus) {
    let _ = events.try_send(GrobidServiceEvent::Container(status));
}

async fn create_container(docker: &Docker, image: &str) -> Result<(), String> {
    let mut options = CreateContainerOptionsBuilder::default().name(MANAGED_GROBID_CONTAINER);
    if let Some(platform) = managed_platform(image) {
        options = options.platform(platform);
    }
    docker
        .create_container(Some(options.build()), managed_container_config(image))
        .await
        .map(|_| ())
        .map_err(|error| error.to_string())
}

async fn wait_until_ready(
    client: &Client,
    events: &Sender<GrobidServiceEvent>,
    commands: &async_channel::Receiver<GrobidCommand>,
    cancel: &AtomicBool,
) -> StartOutcome {
    events
        .send(GrobidServiceEvent::Container(
            GrobidContainerStatus::Starting,
        ))
        .await
        .ok();
    let deadline = Instant::now() + READY_TIMEOUT;

    loop {
        if cancelled(commands, cancel) {
            return StartOutcome::Cancelled;
        }
        if let Ok(probe) = probe_grobid(client, MANAGED_GROBID_URL, Duration::from_secs(2)).await {
            events
                .send(GrobidServiceEvent::Probe(GrobidProbeStatus::Reachable {
                    url: probe.url.clone(),
                    version: probe.version.clone(),
                }))
                .await
                .ok();
            events
                .send(GrobidServiceEvent::Ready {
                    url: probe.url,
                    version: probe.version,
                    managed: true,
                })
                .await
                .ok();
            return StartOutcome::Ready;
        }
        if Instant::now() >= deadline {
            let message = format!(
                "GROBID did not answer {MANAGED_GROBID_URL}/api/isalive within {} seconds",
                READY_TIMEOUT.as_secs()
            );
            fail(&message, events).await;
            return StartOutcome::TimedOut;
        }
        tokio::time::sleep(Duration::from_millis(750)).await;
    }
}

async fn stop(events: &Sender<GrobidServiceEvent>) {
    let Some(docker) = connect_docker(events).await else {
        return;
    };
    if docker
        .inspect_container(MANAGED_GROBID_CONTAINER, None)
        .await
        .is_err()
    {
        events
            .send(GrobidServiceEvent::Container(
                GrobidContainerStatus::Missing,
            ))
            .await
            .ok();
        return;
    }
    let options = StopContainerOptionsBuilder::default().t(10).build();
    match docker
        .stop_container(MANAGED_GROBID_CONTAINER, Some(options))
        .await
    {
        Ok(()) => {
            events
                .send(GrobidServiceEvent::Container(
                    GrobidContainerStatus::Stopped,
                ))
                .await
                .ok();
        }
        Err(error) => {
            fail(
                &format!("cannot stop the GROBID container: {error}"),
                events,
            )
            .await;
        }
    }
}

async fn remove(events: &Sender<GrobidServiceEvent>) -> bool {
    let Some(docker) = connect_docker(events).await else {
        return true;
    };
    let exists = docker
        .inspect_container(MANAGED_GROBID_CONTAINER, None)
        .await
        .is_ok();
    if !exists {
        events
            .send(GrobidServiceEvent::Container(
                GrobidContainerStatus::Missing,
            ))
            .await
            .ok();
        return false;
    }
    let stop_options = StopContainerOptionsBuilder::default().t(5).build();
    let _ = docker
        .stop_container(MANAGED_GROBID_CONTAINER, Some(stop_options))
        .await;
    let options = RemoveContainerOptionsBuilder::default().force(true).build();
    match docker
        .remove_container(MANAGED_GROBID_CONTAINER, Some(options))
        .await
    {
        Ok(()) => {
            events
                .send(GrobidServiceEvent::Container(
                    GrobidContainerStatus::Missing,
                ))
                .await
                .ok();
            false
        }
        Err(error) => {
            fail(
                &format!("cannot remove the GROBID container: {error}"),
                events,
            )
            .await;
            true
        }
    }
}

async fn connect_docker(events: &Sender<GrobidServiceEvent>) -> Option<Docker> {
    events
        .send(GrobidServiceEvent::Docker(GrobidDockerStatus::Checking))
        .await
        .ok();
    match connect_docker_quiet().await {
        Ok(docker) => {
            let version = docker
                .version()
                .await
                .ok()
                .and_then(|version| version.version)
                .unwrap_or_else(|| "unknown".to_owned());
            events
                .send(GrobidServiceEvent::Docker(GrobidDockerStatus::Available {
                    version,
                }))
                .await
                .ok();
            Some(docker)
        }
        Err(error) => {
            let message = docker_message(&error);
            events
                .send(GrobidServiceEvent::Docker(
                    GrobidDockerStatus::Unavailable {
                        message: message.clone(),
                    },
                ))
                .await
                .ok();
            events
                .send(GrobidServiceEvent::Container(
                    GrobidContainerStatus::Failed { message },
                ))
                .await
                .ok();
            None
        }
    }
}

async fn connect_docker_quiet() -> Result<Docker, String> {
    let connection = if std::env::var_os("DOCKER_HOST").is_some() {
        Docker::connect_with_defaults()
    } else {
        Docker::connect_with_local_defaults()
    };
    let docker = connection
        .map_err(|error| error.to_string())?
        .with_timeout(Duration::from_secs(3));
    docker.ping().await.map_err(|error| error.to_string())?;
    Ok(docker)
}

fn docker_message(error: &str) -> String {
    format!(
        "Docker is not reachable ({error}) — start Docker Desktop or the Docker daemon, then retry"
    )
}

async fn fail(message: &str, events: &Sender<GrobidServiceEvent>) {
    events
        .send(GrobidServiceEvent::Container(
            GrobidContainerStatus::Failed {
                message: message.to_owned(),
            },
        ))
        .await
        .ok();
    events
        .send(GrobidServiceEvent::Failed {
            message: message.to_owned(),
        })
        .await
        .ok();
}

async fn probe_grobid(
    client: &Client,
    url: &str,
    timeout: Duration,
) -> Result<GrobidProbe, String> {
    let url = normalize_grobid_url(url)?;
    let response = client
        .get(format!("{url}/api/isalive"))
        .timeout(timeout)
        .send()
        .await
        .map_err(probe_error)?;
    if !response.status().is_success() {
        return Err(format!("HTTP {}", response.status()));
    }
    let body = response.text().await.map_err(probe_error)?;
    if !body.trim().eq_ignore_ascii_case("true") {
        return Err("the endpoint did not answer /api/isalive".to_owned());
    }

    let version = match client
        .get(format!("{url}/api/version"))
        .timeout(timeout)
        .send()
        .await
    {
        Ok(response) => match response.error_for_status() {
            Ok(response) => response
                .json::<serde_json::Value>()
                .await
                .ok()
                .and_then(|body| {
                    body.get("version")
                        .and_then(|value| value.as_str())
                        .map(str::to_owned)
                }),
            Err(_) => None,
        },
        Err(_) => None,
    };

    Ok(GrobidProbe { url, version })
}

fn probe_error(error: reqwest::Error) -> String {
    if error.is_connect() {
        "connection refused".to_owned()
    } else if error.is_timeout() {
        "timed out".to_owned()
    } else {
        error.to_string()
    }
}

/// Normalize user input into a GROBID base URL.
///
/// A missing scheme becomes `http://`; whitespace and trailing slashes are
/// removed. Other schemes are rejected.
pub fn normalize_grobid_url(input: &str) -> Result<String, String> {
    let trimmed = input.trim().trim_end_matches('/');
    if trimmed.is_empty() {
        return Err("enter the GROBID server URL".to_owned());
    }
    if trimmed.split_whitespace().count() != 1 {
        return Err("the GROBID URL must not contain spaces".to_owned());
    }
    let candidate = if trimmed.contains("://") {
        trimmed.to_owned()
    } else {
        format!("http://{trimmed}")
    };
    let lower = candidate.to_ascii_lowercase();
    if !lower.starts_with("http://") && !lower.starts_with("https://") {
        return Err("the GROBID URL must start with http:// or https://".to_owned());
    }
    Ok(candidate.trim_end_matches('/').to_owned())
}

/// The full GROBID images are built for `amd64` only. Asking Docker for that
/// platform lets Apple silicon and `linux/arm64` hosts use emulation instead of
/// failing the pull. The `-crf` images are multi-architecture and stay native.
fn managed_platform(image: &str) -> Option<&'static str> {
    image.trim().ends_with("-full").then_some("linux/amd64")
}

fn managed_container_config(image: &str) -> ContainerCreateBody {
    let mut port_bindings = HashMap::new();
    port_bindings.insert(
        format!("{MANAGED_GROBID_PORT}/tcp"),
        Some(vec![PortBinding {
            host_ip: Some("127.0.0.1".to_owned()),
            host_port: Some(MANAGED_GROBID_PORT.to_string()),
        }]),
    );
    let mut labels = HashMap::new();
    labels.insert(CONTAINER_LABEL.to_owned(), "true".to_owned());
    labels.insert(SERVICE_LABEL.to_owned(), "grobid".to_owned());

    ContainerCreateBody {
        image: Some(image.to_owned()),
        exposed_ports: Some(vec![format!("{MANAGED_GROBID_PORT}/tcp")]),
        labels: Some(labels),
        host_config: Some(HostConfig {
            init: Some(true),
            ulimits: Some(vec![ResourcesUlimits {
                name: Some("core".to_owned()),
                soft: Some(0),
                hard: Some(0),
            }]),
            port_bindings: Some(port_bindings),
            auto_remove: Some(false),
            ..HostConfig::default()
        }),
        ..ContainerCreateBody::default()
    }
}

fn format_bytes(bytes: u64) -> String {
    const UNITS: [&str; 4] = ["B", "KB", "MB", "GB"];
    let mut value = bytes as f64;
    let mut unit = 0;
    while value >= 1024.0 && unit + 1 < UNITS.len() {
        value /= 1024.0;
        unit += 1;
    }
    if unit == 0 {
        format!("{bytes} B")
    } else {
        format!("{value:.1} {}", UNITS[unit])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rag::DEFAULT_GROBID_IMAGE;

    #[test]
    fn urls_gain_a_scheme_and_lose_trailing_slashes() {
        assert_eq!(
            normalize_grobid_url(" localhost:8070/ ").expect("url"),
            "http://localhost:8070"
        );
        assert_eq!(
            normalize_grobid_url("https://example.org/grobid/").expect("url"),
            "https://example.org/grobid"
        );
    }

    #[test]
    fn empty_and_unsupported_urls_are_rejected() {
        assert!(normalize_grobid_url("   ").is_err());
        assert!(normalize_grobid_url("ftp://localhost:8070").is_err());
        assert!(normalize_grobid_url("not a url").is_err());
    }

    #[test]
    fn the_managed_container_uses_the_full_image_and_a_loopback_port() {
        let config = managed_container_config(DEFAULT_GROBID_IMAGE);
        assert_eq!(config.image.as_deref(), Some(DEFAULT_GROBID_IMAGE));
        assert_eq!(config.exposed_ports, Some(vec!["8070/tcp".to_owned()]));
        assert_eq!(
            config
                .labels
                .as_ref()
                .and_then(|labels| labels.get(CONTAINER_LABEL))
                .map(String::as_str),
            Some("true")
        );

        let host = config.host_config.expect("host config");
        assert_eq!(host.init, Some(true));
        assert_eq!(host.auto_remove, Some(false));
        let binding = host
            .port_bindings
            .as_ref()
            .and_then(|bindings| bindings.get("8070/tcp"))
            .and_then(|binding| binding.as_ref())
            .and_then(|binding| binding.first())
            .expect("port binding");
        assert_eq!(binding.host_ip.as_deref(), Some("127.0.0.1"));
        assert_eq!(binding.host_port.as_deref(), Some("8070"));

        let ulimit = host
            .ulimits
            .as_ref()
            .and_then(|limits| limits.first())
            .expect("ulimit");
        assert_eq!(ulimit.name.as_deref(), Some("core"));
        assert_eq!((ulimit.soft, ulimit.hard), (Some(0), Some(0)));
    }

    #[test]
    fn the_full_image_is_requested_for_amd64() {
        assert_eq!(managed_platform(DEFAULT_GROBID_IMAGE), Some("linux/amd64"));
        assert_eq!(managed_platform("grobid/grobid:0.9.1-crf"), None);
        assert_eq!(
            managed_platform("example.org/grobid:custom-full"),
            Some("linux/amd64")
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    #[ignore = "requires a Docker daemon; run with --ignored"]
    async fn a_cancelled_pull_stops_before_touching_the_container() {
        let (commands_tx, commands) = async_channel::bounded(1);
        let (events, events_rx) = async_channel::bounded(8);
        let docker = connect_docker_quiet().await.expect("docker daemon");
        assert!(
            !remove(&events).await,
            "the managed container could not be removed"
        );

        let error = pull_image(
            &docker,
            "hello-world:latest",
            &events,
            &commands,
            &AtomicBool::new(true),
        )
        .await
        .expect_err("the pull should have been cancelled");

        assert_eq!(error, CANCELLED);
        drop(commands_tx);
        drop(events_rx);
    }

    /// Serve one HTTP response per accepted connection and return the base URL.
    fn serve(responses: Vec<String>) -> String {
        use std::io::{Read as _, Write as _};

        let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind");
        let address = listener.local_addr().expect("address");
        std::thread::spawn(move || {
            for response in responses {
                let Ok((mut socket, _)) = listener.accept() else {
                    return;
                };
                let mut request = [0u8; 2048];
                let _ = socket.read(&mut request);
                let _ = socket.write_all(response.as_bytes());
            }
        });
        format!("http://{address}")
    }

    fn response(body: &str) -> String {
        format!(
            "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
            body.len()
        )
    }

    #[tokio::test]
    async fn a_live_server_reports_its_version() {
        let base = serve(vec![
            response("true"),
            response(r#"{"version":"0.9.1","revision":"abc"}"#),
        ]);
        let client = Client::builder()
            .timeout(Duration::from_secs(2))
            .build()
            .expect("client");

        let probe = probe_grobid(&client, &base, Duration::from_secs(2))
            .await
            .expect("probe");

        assert_eq!(probe.url, base);
        assert_eq!(probe.version.as_deref(), Some("0.9.1"));
    }

    #[tokio::test]
    async fn a_server_that_is_not_alive_is_rejected() {
        let base = serve(vec![response("false")]);
        let client = Client::builder()
            .timeout(Duration::from_secs(2))
            .build()
            .expect("client");

        let error = probe_grobid(&client, &base, Duration::from_secs(2))
            .await
            .expect_err("not alive");

        assert!(error.contains("isalive"), "{error}");
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    #[ignore = "requires a Docker daemon; run with --ignored"]
    async fn the_managed_container_starts_answers_and_stops() {
        let image = std::env::var("RAGRIG_GROBID_TEST_IMAGE")
            .unwrap_or_else(|_| "grobid/grobid:0.9.1-crf".to_owned());
        let (commands_tx, commands) = async_channel::bounded(16);
        let (events_tx, events) = async_channel::bounded(256);
        let client = Client::builder()
            .timeout(Duration::from_secs(3))
            .build()
            .expect("client");
        let docker = connect_docker_quiet().await.expect("docker daemon");

        let remove_options = RemoveContainerOptionsBuilder::default().force(true).build();
        let _ = docker
            .remove_container(MANAGED_GROBID_CONTAINER, Some(remove_options))
            .await;

        pull_image(
            &docker,
            &image,
            &events_tx,
            &commands,
            &AtomicBool::new(false),
        )
        .await
        .expect("pull");
        create_container(&docker, &image).await.expect("create");
        docker
            .start_container(MANAGED_GROBID_CONTAINER, None)
            .await
            .expect("start");
        wait_until_ready(&client, &events_tx, &commands, &AtomicBool::new(false)).await;

        let ready = std::iter::from_fn(|| events.try_recv().ok())
            .any(|event| matches!(event, GrobidServiceEvent::Ready { managed: true, .. }));
        assert!(ready, "the managed container never reported ready");

        stop(&events_tx).await;
        let stopped = std::iter::from_fn(|| events.try_recv().ok()).any(|event| {
            matches!(
                event,
                GrobidServiceEvent::Container(GrobidContainerStatus::Stopped)
            )
        });
        assert!(stopped, "the managed container did not stop cleanly");

        assert!(!remove(&events_tx).await, "the container still exists");
        drop(commands_tx);
    }

    #[test]
    fn container_summaries_include_progress() {
        let status = GrobidContainerStatus::Pulling {
            image: "grobid/grobid:0.9.1-full".to_owned(),
            received: 512 * 1024 * 1024,
            total: 1024 * 1024 * 1024,
        };
        assert!(status.summary().contains("50%"));
        assert!(status.summary().contains("512.0 MB"));
    }
}