switchyard-libsy 0.4.0

Provider-neutral multi-LLM routing and orchestration for Switchyard
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Routing algorithms request external work through [`Driver`] and receive typed replies.
//! Hosts execute that work, keeping routing policy independent of transport.

use std::{
    collections::HashMap, future::Future, panic::AssertUnwindSafe, pin::Pin, sync::Arc,
    time::Instant,
};

use async_trait::async_trait;
use futures::{FutureExt, Stream, StreamExt};
use parking_lot::Mutex;
use serde_json::Value;
use tokio::sync::{mpsc, oneshot};
use tokio_stream::wrappers::ReceiverStream;
use tracing::Instrument;

/// The request/response protocol types come from [`switchyard_protocol`].
/// [`switchyard_protocol::LlmRequest`] is the normalized request;
/// [`switchyard_protocol::AggLlmResponse`] is the buffered response;
/// [`switchyard_protocol::LlmResponseChunk`] is normalized streaming content;
/// [`switchyard_protocol::LlmResponseStreamEvent`] is its host/algorithm envelope; and
/// [`switchyard_protocol::LlmResponse`] carries either a live
/// [`switchyard_protocol::LlmResponseStream`] or the terminal aggregate.
use switchyard_protocol::{
    AggLlmResponse, Category, ContentBlock, DecisionRequest, DecisionResponse, ModelId, Request,
    Response, StopReason,
};

use crate::{DriverError, LibsyError, Result, State, observability};

/// A boxed, `Send` stream of [`Step`]s — the output of
/// [`Algorithm::run_stream`]. Boxed so the trait method that produces it keeps
/// `Arc<dyn Algorithm>` object-safe.
pub type StepStream = Pin<Box<dyn Stream<Item = Result<Step>> + Send>>;

/// The models one algorithm run may use, grouped by [`Category`]. Within a
/// category they are ordered best-first.
///
/// Delegated sub-agent work gets its own groups, reachable only through
/// [`Driver::for_subagent`]. Keeping them separate is what stops a sub-agent's
/// `capable` from resolving to the parent's, and stops the parent falling back
/// onto a model only its sub-agents were given.
///
/// One run's driver clones all read the same value, so it is passed as
/// `Arc<RuntimeModels>` rather than cloned per driver.
#[derive(Clone, Debug, Default)]
pub struct RuntimeModels {
    /// When using subagents this is the parent agent category.
    by_category: HashMap<Category, Vec<ModelId>>,
    subagent: Option<HashMap<Category, Vec<ModelId>>>,
    has_target_restriction: bool,
    provider_models: HashMap<ModelId, ModelId>,
}

impl RuntimeModels {
    /// The models available to the algorithm itself.
    pub fn new(by_category: HashMap<Category, Vec<ModelId>>) -> Self {
        Self {
            by_category,
            subagent: None,
            has_target_restriction: false,
            provider_models: HashMap::new(),
        }
    }

    /// Supplies provider identities for telemetry while routing keeps using target keys.
    pub fn with_provider_models(mut self, models: HashMap<ModelId, ModelId>) -> Self {
        self.provider_models = models;
        self
    }

    /// Returns the provider model for a routing key, or the key for direct library callers.
    pub fn provider_model<'a>(&'a self, target: &'a ModelId) -> &'a ModelId {
        self.provider_models.get(target).unwrap_or(target)
    }

    /// Adds the groups used for delegated sub-agent work.
    pub fn with_subagent(mut self, models: HashMap<Category, Vec<ModelId>>) -> Self {
        self.subagent = Some(models);
        self
    }

    /// Rejects routing calls and completion candidates outside the configured model groups.
    /// Enforced by the driver and [`drive`]; hosts bind IDs to permitted deployments.
    pub fn with_target_restriction(mut self) -> Self {
        self.has_target_restriction = true;
        self
    }

    fn check_targets(&self, targets: &[ModelId]) -> Result<()> {
        if !self.has_target_restriction {
            return Ok(());
        }
        // Include delegated groups: the parent driver publishes subagent outcomes too.
        for target in targets {
            if !self
                .by_category
                .values()
                .chain(self.subagent.iter().flat_map(HashMap::values))
                .any(|models| models.contains(target))
            {
                return Err(LibsyError::TargetNotFound {
                    target: target.clone(),
                });
            }
        }
        Ok(())
    }

    fn check_request_targets(&self, request: &Request, targets: &[ModelId]) -> Result<()> {
        self.check_targets(targets)?;
        if self.has_target_restriction {
            let selected = targets.first().ok_or(LibsyError::NoTargets)?;
            if request.llm_request.model.as_deref() != Some(selected.as_str()) {
                return Err(LibsyError::AlgorithmError {
                    message: "request model does not match selected target".into(),
                });
            }
        }
        Ok(())
    }

    /// The models in `category`, ordered best-first.
    pub fn models_for(&self, category: &Category) -> &[ModelId] {
        self.by_category.get(category).map_or(&[], Vec::as_slice)
    }

    /// The models delegated sub-agent work uses for `category`, ordered best-first.
    pub fn subagent_models_for(&self, category: &Category) -> &[ModelId] {
        self.subagent
            .as_ref()
            .and_then(|models| models.get(category))
            .map_or(&[], Vec::as_slice)
    }
}

impl From<HashMap<Category, Vec<ModelId>>> for RuntimeModels {
    fn from(by_category: HashMap<Category, Vec<ModelId>>) -> Self {
        Self::new(by_category)
    }
}

/// Which of a [`RuntimeModels`]' groups a driver reads.
#[derive(Clone, Copy)]
enum Scope {
    Parent,
    Subagent,
}

/// An offloaded model call, surfaced inside [`Step::CallModel`].
///
/// The host reads the public fields and passes its model-call future to
/// [`respond`](Self::respond), unblocking the algorithm's [`Driver::call_model`] on the
/// other side. `switchyard-llm-client`'s `run` is the ready-made consumer that does this for
/// you.
///
/// [`Driver::call_model`] stamps the first candidate model onto the request before publishing
/// the call. A consumer that falls through to a later candidate must re-stamp it.
pub struct CallModel {
    /// The name of the algorithm that produced this call, so a host instrumenting the
    /// calls it serves can attribute its own spans to the algorithm behind them.
    pub algorithm: String,
    /// The request to serve; its `model` is stamped with the first candidate.
    pub request: Request,
    /// Candidate models in fallback order. Never empty.
    ///
    /// The host owns candidate execution and fallback. The native
    /// `switchyard-llm-client` consumer calls only the first candidate during routing.
    /// A failure stops its run unless [`Self::recover_errors`] is enabled, which
    /// returns the error to the algorithm without trying later candidates.
    /// Terminal completion fallback is handled separately.
    pub models: Vec<ModelId>,
    /// Return client errors to the algorithm so it can apply its fallback policy.
    pub recover_errors: bool,
    telemetry_model: ModelId,
    /// How to send the response back to the algorithm. `None` once the call is recorded.
    reply: Option<oneshot::Sender<Result<Response>>>,
    started: Instant,
    // Retain the originating span even if the waiting algorithm is cancelled first.
    span: tracing::Span,
}

impl CallModel {
    /// Run host work until it completes or the algorithm stops waiting for this call.
    /// Dropping the waiting [`Driver::call_model`] future drops `work` and returns `Ok(())`.
    /// Errors stop the host's run unless [`Self::recover_errors`] is enabled.
    /// Use `std::future::ready(result)` for an already available response.
    /// A delivered response stream owns its subsequent lifetime.
    pub async fn respond(mut self, work: impl Future<Output = Result<Response>>) -> Result<()> {
        let reply = self.reply.as_mut().ok_or(DriverError::ResponseDropped)?;
        let result = tokio::select! {
            biased;
            _ = reply.closed() => return Ok(()),
            result = work => result,
        };
        if self.reply.as_ref().is_some_and(oneshot::Sender::is_closed) {
            return Ok(());
        }
        if !self.recover_errors
            && let Err(error) = result
        {
            return self.fail(error);
        }
        self.record(result.is_ok());
        if let Ok(response) = &result {
            observability::record_llm_response(response, &self.span);
        }
        self.reply
            .take()
            .ok_or(DriverError::ResponseDropped)?
            .send(result)
            .ok();
        Ok(())
    }

    /// Record a failed call and return its error to stop [`drive`].
    /// Leaves the promise unfulfilled so the driver can cancel the algorithm.
    pub fn fail(mut self, error: LibsyError) -> Result<()> {
        self.reply = None;
        self.record(false);
        Err(error)
    }

    fn record(&self, is_ok: bool) {
        observability::record_llm_call(
            &self.algorithm,
            self.telemetry_model.as_str(),
            self.started.elapsed(),
            is_ok,
        );
        self.span
            .record("outcome", if is_ok { "ok" } else { "error" });
    }
}

impl Drop for CallModel {
    fn drop(&mut self) {
        if self.reply.is_some() {
            self.record(false);
        }
    }
}

/// A host-owned decision call. Dropping it without replying yields [`DriverError::ResponseDropped`].
/// Completion or drop records one call and its duration, including time waiting for the host.
pub struct CallDecision {
    /// Algorithm name for attributing host telemetry.
    pub algorithm: String,
    /// Its model is set to [`Self::model`] before dispatch.
    pub request: DecisionRequest,
    /// Target ID for client lookup.
    pub model: ModelId,
    reply: Option<oneshot::Sender<Result<DecisionResponse>>>,
    started: Instant,
    // Retain the originating span even if the waiting algorithm is cancelled first.
    span: tracing::Span,
}

impl CallDecision {
    /// Run host work and return its response or error to the algorithm.
    /// Dropping the waiting [`Driver::call_decision`] future drops `work` and returns `Ok(())`.
    /// Use `std::future::ready(result)` for an already available response.
    pub async fn respond(
        mut self,
        work: impl Future<Output = Result<DecisionResponse>>,
    ) -> Result<()> {
        let reply = self.reply.as_mut().ok_or(DriverError::ResponseDropped)?;
        let result = tokio::select! {
            biased;
            _ = reply.closed() => return Ok(()),
            result = work => result,
        };
        self.record(result.is_ok());
        if let Ok(response) = &result {
            observability::record_decision_response(response, &self.span);
        }
        self.reply
            .take()
            .ok_or(DriverError::ResponseDropped)?
            .send(result)
            .ok();
        Ok(())
    }

    /// Returning this error from the host handler aborts [`drive`].
    pub fn fail(mut self, error: LibsyError) -> Result<()> {
        self.reply = None;
        self.record(false);
        Err(error)
    }

    fn record(&self, is_ok: bool) {
        observability::record_decision_call(
            &self.algorithm,
            &self.model,
            self.started.elapsed(),
            is_ok,
        );
        self.span
            .record("outcome", if is_ok { "ok" } else { "error" });
    }
}

impl Drop for CallDecision {
    fn drop(&mut self) {
        if self.reply.is_some() {
            self.record(false);
        }
    }
}

/// The terminal result of routing.
pub struct RoutingOutcome {
    /// Models selected by the algorithm, ordered best model first.
    pub selected_model_ids: Vec<ModelId>,
    /// The request after all routing-time rewrites, stamped with the selected model.
    pub request: Request,
    /// The request the fallback candidates receive instead of `request`, when routing
    /// rewrote `request` for the selected model only.
    /// Content-policy denials do not advance to these fallback candidates.
    pub fallback_request: Option<Request>,
    /// Candidate-specific requests, including model-specific instructions.
    pub candidate_requests: HashMap<ModelId, Request>,
    /// A response produced while routing, or `None` when the client must make the answer call.
    pub response: Option<Response>,
    /// Outcome identity and optional algorithm evidence.
    ///
    /// Constructors leave this empty; [`Algorithm::run_stream`] fills it before publishing a
    /// successful outcome.
    pub metadata: Option<crate::OutcomeMetadata>,
    /// Routing state after the run, for hosts that store state outside the algorithm.
    pub state: State,
}

impl RoutingOutcome {
    /// The model the algorithm recommends, the best model for this request.
    /// `LibsyError::NoTargets` if the algorithm selected no models, which should be impossible.
    pub fn selected_model_id(&self) -> Result<&ModelId> {
        self.selected_model_ids.first().ok_or(LibsyError::NoTargets)
    }

    /// The decision is that client should send this `request`. The `selected_model_id`
    /// will be written into it by this function.
    /// If that fails client should try the `fallback_models` in order.
    pub fn route_to(
        selected_model_id: ModelId,
        fallback_models: Vec<ModelId>,
        mut request: Request,
    ) -> Self {
        request.llm_request.model = Some(selected_model_id.to_string());
        let mut selected_model_ids = Vec::with_capacity(1 + fallback_models.len());
        selected_model_ids.push(selected_model_id);
        selected_model_ids.extend(fallback_models);
        Self {
            selected_model_ids,
            request,
            fallback_request: None,
            candidate_requests: HashMap::new(),
            response: None,
            metadata: None,
            state: State::default(),
        }
    }

    /// Algorithm generated the response as part of the routing decision. Here it is.
    /// The `request` will have the `selected_model_id` written into it by this function.
    pub fn answered(selected_model_id: ModelId, mut request: Request, response: Response) -> Self {
        request.llm_request.model = Some(selected_model_id.to_string());
        Self {
            selected_model_ids: vec![selected_model_id],
            request,
            fallback_request: None,
            candidate_requests: HashMap::new(),
            response: Some(response),
            metadata: None,
            state: State::default(),
        }
    }
}

/// What the host saw of the answer it served for a routed request.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AnswerSummary {
    /// The answer ended its turn with visible text and requested no tool call.
    pub final_reply: bool,
}

impl AnswerSummary {
    /// Summarizes a completed answer.
    pub fn of(response: &AggLlmResponse) -> Self {
        let final_reply = response.outputs.iter().all(|output| {
            matches!(output.stop_reason, None | Some(StopReason::EndTurn))
                && !output
                    .content
                    .iter()
                    .any(|block| matches!(block, ContentBlock::ToolCall(_)))
        }) && response.outputs.iter().any(|output| {
            output.role == switchyard_protocol::Role::Assistant
                && output.content.iter().any(
                    |block| matches!(block, ContentBlock::Text { text } if !text.trim().is_empty()),
                )
        });
        Self { final_reply }
    }
}

#[derive(Default)]
struct RunEvidence {
    failed_models: Vec<ModelId>,
    outcome: Option<Value>,
    judgments: Vec<crate::JudgeDecision>,
}

/// An algorithm's handle for requesting external work and awaiting the host's typed replies.
#[derive(Clone)]
pub struct Driver {
    step_tx: mpsc::Sender<Result<Step>>,

    /// The owning algorithm's telemetry label, stamped onto every call this driver publishes.
    algorithm: String,

    /// Run-scoped evidence shared by driver clones and attached only to a successful outcome.
    evidence: Arc<Mutex<RunEvidence>>,

    /// Caller-supplied routing state, read and updated when a request carries no session ID.
    state: Arc<Mutex<State>>,

    /// Every group this run may route over, shared by all driver clones.
    models: Arc<RuntimeModels>,

    /// Which of those groups this driver reads.
    scope: Scope,
    judge: Option<ModelId>,
}

impl Driver {
    /// Build an empty driver with its step channel ready. Created per call by
    /// [`run_stream`](Algorithm::run_stream). Also returns the Step receiver.
    pub(crate) fn new(
        algorithm: &str,
        models: Arc<RuntimeModels>,
    ) -> (Self, mpsc::Receiver<Result<Step>>) {
        // Capacity one keeps the algorithm paced by the stream consumer. It limits queued steps,
        // not model calls already pulled from the stream, which can still run at the same time.
        // A larger buffer would use more memory and let the algorithm run farther ahead with
        // little benefit because reading a step is cheap compared with serving a model call.
        let (step_tx, step_rx) = mpsc::channel(1);
        (
            Self {
                step_tx,
                algorithm: algorithm.to_string(),
                evidence: Arc::new(Mutex::new(RunEvidence::default())),
                state: Arc::new(Mutex::new(State::default())),
                models,
                scope: Scope::Parent,
                judge: None,
            },
            step_rx,
        )
    }

    pub(crate) fn take_state(&self) -> State {
        std::mem::take(&mut *self.state.lock())
    }

    pub(crate) fn set_state(&self, state: State) {
        *self.state.lock() = state;
    }

    /// Record a generation failure for this run, independently of session or backend health.
    pub(crate) fn record_model_failure(&self, model: ModelId) {
        self.evidence.lock().failed_models.push(model);
    }

    pub(crate) fn model_failed(&self, model: &ModelId) -> bool {
        self.evidence.lock().failed_models.contains(model)
    }

    /// Replace the current run's evidence when a component makes the final decision.
    pub(crate) fn set_evidence(&self, evidence: Value) {
        self.evidence.lock().outcome = Some(evidence);
    }

    /// Supply fallback evidence without replacing a decision made earlier in the cascade.
    pub(crate) fn set_evidence_if_empty(&self, evidence: Value) {
        let mut current = self.evidence.lock();
        if current.outcome.is_none() {
            current.outcome = Some(evidence);
        }
    }

    pub(crate) fn record_judgment(&self, selected: Option<crate::Score>, evidence: Option<Value>) {
        let (target, category) = selected
            .map(|score| (Some(score.target), score.category))
            .unwrap_or_default();
        self.evidence.lock().judgments.push(crate::JudgeDecision {
            target,
            category: category.map(|category| category.as_str().to_string()),
            evidence,
        });
    }

    /// Publish a model call and await the consumer's response.
    ///
    /// Errors if the stream is closed or the call failed.
    /// The await is wrapped in a `libsy.llm_call` span measuring *fulfillment* as
    /// the algorithm observes it (host queueing/serving included; a streamed
    /// response resolves when its stream handle arrives). The host records call metrics,
    /// outcome, and buffered token usage when it completes or drops the call, even if the
    /// waiting algorithm has been cancelled. The provider call itself is the
    /// host's, and is instrumented by whoever makes it.
    pub async fn call_model(&self, request: Request, models: Vec<ModelId>) -> Result<Response> {
        self.call_model_with_error_recovery(request, models, false)
            .await
    }

    /// Allows a routing policy to handle a failed call when recovery is enabled.
    #[tracing::instrument(
        target = "libsy",
        name = "libsy.llm_call",
        skip_all,
        fields(
            algorithm = self.algorithm,
            selected_model = %models.first().map(|model| self.provider_model(model).as_str()).unwrap_or("NoTargets"),
            openinference.span.kind = "CHAIN",
            outcome = tracing::field::Empty,
            input_tokens = tracing::field::Empty,
            output_tokens = tracing::field::Empty,
            total_tokens = tracing::field::Empty,
            reasoning_tokens = tracing::field::Empty,
        )
    )]
    pub(crate) async fn call_model_with_error_recovery(
        &self,
        mut request: Request,
        models: Vec<ModelId>,
        recover_errors: bool,
    ) -> Result<Response> {
        let Some(selected_model_id) = models.first() else {
            return Err(LibsyError::NoTargets);
        };
        self.models.check_targets(&models)?;
        request.llm_request.model = Some(selected_model_id.to_string());
        let started = Instant::now();
        let (reply, response) = oneshot::channel::<Result<Response>>();
        let call = CallModel {
            algorithm: self.algorithm.clone(),
            request,
            telemetry_model: self.provider_model(selected_model_id).clone(),
            models,
            recover_errors,
            reply: Some(reply),
            started,
            span: tracing::Span::current(),
        };
        self.step_tx
            .send(Ok(Step::CallModel(Box::new(call))))
            .await
            .map_err(|_| DriverError::StreamClosed)?;
        response
            .await
            .map_err(|_| LibsyError::from(DriverError::ResponseDropped))?
    }

    /// Override the request's model with `model` and wait for the host to reply.
    #[tracing::instrument(
        target = "libsy",
        name = "libsy.decision_call",
        skip_all,
        fields(
            algorithm = self.algorithm,
            selected_model = %model,
            openinference.span.kind = "CHAIN",
            outcome = tracing::field::Empty,
            input_tokens = tracing::field::Empty,
            output_tokens = tracing::field::Empty,
            total_tokens = tracing::field::Empty,
            reasoning_tokens = tracing::field::Empty,
            gen_ai.response.id = tracing::field::Empty,
            gen_ai.response.model = tracing::field::Empty,
        ),
    )]
    pub async fn call_decision(
        &self,
        mut request: DecisionRequest,
        model: ModelId,
    ) -> Result<DecisionResponse> {
        self.models.check_targets(std::slice::from_ref(&model))?;
        request.model = Some(model.clone());
        let started = Instant::now();
        let (reply, response) = oneshot::channel();
        let call = CallDecision {
            algorithm: self.algorithm.clone(),
            request,
            model,
            reply: Some(reply),
            started,
            span: tracing::Span::current(),
        };
        self.step_tx
            .send(Ok(Step::CallDecision(Box::new(call))))
            .await
            .map_err(|_| DriverError::StreamClosed)?;
        response
            .await
            .map_err(|_| LibsyError::from(DriverError::ResponseDropped))?
    }

    /// Provider identity for telemetry; algorithms still route on the original target key.
    pub fn provider_model<'a>(&'a self, target: &'a ModelId) -> &'a ModelId {
        self.models.provider_model(target)
    }

    /// The available models for this category, typically ordered best-first.
    pub fn models_for(&self, category: &Category) -> &[ModelId] {
        if *category == Category::Judge
            && let Some(judge) = &self.judge
        {
            return std::slice::from_ref(judge);
        }
        match self.scope {
            Scope::Parent => self.models.models_for(category),
            Scope::Subagent => self.models.subagent_models_for(category),
        }
    }

    pub(crate) fn for_judge(&self, judge: ModelId) -> Self {
        Self {
            judge: Some(judge),
            ..self.clone()
        }
    }

    /// The first available model for `category`.
    pub fn first_model_for(&self, category: &Category) -> Result<&ModelId> {
        self.models_for(category)
            .first()
            .ok_or_else(|| LibsyError::AlgorithmError {
                message: format!("no models available for category {}", category.as_str()),
            })
    }

    /// A driver scoped to delegated sub-agent work: its categories are the
    /// sub-agent's own, and the parent's are no longer reachable through it.
    pub fn for_subagent(&self) -> Result<Self> {
        if self.models.subagent.is_none() {
            return Err(LibsyError::AlgorithmError {
                message: "delegated work has no sub-agent models".to_string(),
            });
        }
        Ok(Self {
            scope: Scope::Subagent,
            ..self.clone()
        })
    }

    /// Emit the terminal step: [`Step::Done`] on `Ok`, or an `Err` stream
    /// item on failure. Internal: called once by [`run_stream`](Algorithm::run_stream)
    /// when the algorithm finishes.
    pub(crate) async fn finish(&self, result: Result<RoutingOutcome>) -> Result<()> {
        let result = result.map(|mut outcome| {
            outcome.state = self.take_state();
            let evidence = std::mem::take(&mut *self.evidence.lock());
            let metadata = outcome.metadata.get_or_insert_with(|| {
                crate::OutcomeMetadata::new(self.algorithm.clone(), evidence.outcome)
            });
            metadata.judgments.extend(evidence.judgments);
            let provider_models = outcome
                .selected_model_ids
                .iter()
                .map(|target| self.provider_model(target).clone())
                .collect::<Vec<_>>();
            observability::record_outcome(metadata, &provider_models);
            outcome
        });
        let selected_model = result
            .as_ref()
            .ok()
            .and_then(|outcome| outcome.selected_model_id().ok().cloned());
        let step = result.map(|outcome| Step::Done(Box::new(outcome)));
        self.step_tx
            .send(step)
            .await
            .map_err(|_| DriverError::StreamClosed)?;
        if let Some(selected_model) = selected_model {
            observability::record_decision(&self.algorithm, self.provider_model(&selected_model));
        }
        Ok(())
    }
}

/// Work requests and the terminal outcome of [`Algorithm::run_stream`].
/// Each call carries its own reply channel, so the host can execute calls concurrently.
pub enum Step {
    /// The algorithm needs this model call performed. The host serves it and fulfills
    /// it with [`CallModel::respond`]. Boxed: it is by far the largest variant.
    CallModel(Box<CallModel>),
    /// The host fulfills this decision call with [`CallDecision::respond`].
    CallDecision(Box<CallDecision>),
    /// The algorithm finished with its routing outcome — the last step of a run.
    Done(Box<RoutingOutcome>),
}

/// Work dispatched through one host handler, with a typed reply channel per variant.
/// [`drive`] handles terminal outcomes separately; the handler only executes calls.
pub enum Call {
    /// Fulfilled with an LLM [`Response`].
    Model(Box<CallModel>),
    /// Fulfilled with a [`DecisionResponse`].
    Decision(Box<CallDecision>),
}

/// Drive [`Algorithm::run_stream`] to its routing outcome, serving calls concurrently.
///
/// The shared execution loop lets hosts supply clients or test doubles through `serve`
/// without reimplementing stream handling and cancellation.
///
/// `serve` owns each [`Call`] and performs its I/O. Passing an error to `respond`
/// lets the algorithm fall back; returning an error from `serve` aborts the run.
///
/// Dropping this future cancels the algorithm and pending handler futures.
pub async fn drive<F, Fut>(
    algorithm: Arc<dyn Algorithm>,
    request: Request,
    models: Arc<RuntimeModels>,
    serve: F,
) -> Result<RoutingOutcome>
where
    F: Fn(Call) -> Fut,
    Fut: Future<Output = Result<()>>,
{
    drive_with_state(algorithm, request, models, State::default(), serve).await
}

/// [`drive`] starting from caller-supplied `state`.
pub(crate) async fn drive_with_state<F, Fut>(
    algorithm: Arc<dyn Algorithm>,
    request: Request,
    models: Arc<RuntimeModels>,
    state: State,
    serve: F,
) -> Result<RoutingOutcome>
where
    F: Fn(Call) -> Fut,
    Fut: Future<Output = Result<()>>,
{
    let stream = algorithm.run_stream(request, Arc::clone(&models), state);
    tokio::pin!(stream);

    let mut in_flight = futures::stream::FuturesUnordered::new();
    let mut final_outcome: Option<RoutingOutcome> = None;

    loop {
        tokio::select! {
            Some(result) = in_flight.next() => match result {
                Ok(()) => {}, // Call completed successfully
                Err(err) => return Err(err), // Call failed, propagate the error
            },
            step = stream.next() => {
                match step {
                    None => break, // stream has ended, no more steps
                    Some(item) => match item? {
                        Step::CallModel(call) => {
                            models.check_request_targets(&call.request, &call.models)?;
                            in_flight.push(serve(Call::Model(call)));
                        }
                        Step::CallDecision(call) => {
                            models.check_targets(std::slice::from_ref(&call.model))?;
                            if models.has_target_restriction
                                && call.request.model.as_ref() != Some(&call.model)
                            {
                                return Err(LibsyError::AlgorithmError {
                                    message: "decision payload model does not match selected target".into(),
                                });
                            }
                            in_flight.push(serve(Call::Decision(call)));
                        }
                        Step::Done(outcome) => {
                            if outcome.response.is_none() {
                                models.check_request_targets(&outcome.request, &outcome.selected_model_ids)?;
                            }
                            final_outcome = Some(*outcome);
                            break;
                        }
                    }
                }
            },
        }
    }
    final_outcome.ok_or(LibsyError::MissingFinalResponse)
}

/// Recover the message from an algorithm's panic.
fn panic_message(payload: &(dyn std::any::Any + Send)) -> String {
    payload
        .downcast_ref::<&'static str>()
        .map(|message| (*message).to_string())
        .or_else(|| payload.downcast_ref::<String>().cloned())
        .unwrap_or_else(|| "unknown panic payload".to_string())
}

/// Abort guard
struct AbortOnDrop(tokio::task::AbortHandle);

impl Drop for AbortOnDrop {
    fn drop(&mut self) {
        self.0.abort();
    }
}

/// Errors unless `targets` contains `name`.
///
/// Config target names must be resolved before an algorithm is built. This list contains
/// model IDs, not target names.
pub(crate) fn ensure_model_is_target(targets: &[ModelId], name: &ModelId) -> Result<()> {
    targets
        .iter()
        .any(|target| target == name)
        .then_some(())
        .ok_or_else(|| LibsyError::TargetNotFound {
            target: name.clone(),
        })
}

/// Key for routing affinity: a root request by its session, a child request by its session
/// and agent.
#[derive(Clone, Hash, PartialEq, Eq)]
pub(crate) enum RoutingIdentity {
    /// Root request, keyed by session ID.
    Session(String),
    /// Child request, keyed by session and agent IDs.
    Subagent { session: String, agent: String },
}

impl RoutingIdentity {
    /// Builds a root or child identity from non-empty request metadata.
    ///
    /// A child request missing either ID returns `None`, so it keeps no routing history
    /// rather than sharing the parent's.
    pub(crate) fn from_request(request: &Request) -> Option<Self> {
        let metadata = request.metadata.as_ref()?;
        let session = metadata.session_id.as_deref().filter(|id| !id.is_empty())?;
        if metadata.is_subagent {
            let agent = metadata.agent_id.as_deref().filter(|id| !id.is_empty())?;
            Some(Self::Subagent {
                session: session.to_string(),
                agent: agent.to_string(),
            })
        } else {
            Some(Self::Session(session.to_string()))
        }
    }
}

/// Routing policy independent of how external calls are executed.
/// Implement [`route`](Self::route), using [`Driver`] to request work from the host.
/// Hosts can use [`drive`] for the shared execution loop or consume
/// [`run_stream`](Self::run_stream) for custom execution.
///
/// Methods take `self: Arc<Self>`: one algorithm (`Arc<dyn Algorithm>`) is shared across
/// requests and run concurrently, so it owns its thread-safety and any shared state.
///
/// # Concurrency
///
/// A host may run the same algorithm concurrently for many requests. Implementations
/// must synchronize their own mutable shared state. Each call to [`run_stream`](Self::run_stream)
/// creates an independent [`Driver`], so model-call promises and emitted [`Step`]s cannot
/// cross between runs.
///
/// # Observability
///
/// [`run_stream`](Self::run_stream) creates a `libsy.run` span, and each offloaded model
/// call creates a nested span for its call kind, with outcome and available token usage.
/// Call metrics include host queueing and count unfulfilled drops as errors.
/// Successful outcomes record their
/// [`OutcomeMetadata::outcome_id`](crate::OutcomeMetadata::outcome_id) on `libsy.run`,
/// alongside `selected_model_ids` (an ordered OpenTelemetry string array).
/// `algorithm` and `switchyard.algorithm` retain the run's [`Algorithm::name`].
/// Optional `evidence.source`, `evidence.verdict`, `evidence.trigger`, and
/// `evidence.reason_code` are strings; `evidence.score`, `evidence.confidence`,
/// `evidence.threshold`, `evidence.capable_score`, and `evidence.capable_threshold`
/// are numbers. Unknown evidence fields are not exported.
/// These fields are span attributes, never metric labels.
///
/// The run/call observability helpers retain `outcome` status and operational metrics,
/// but omit error details and arbitrary request extra metadata. Algorithms and hosts
/// may emit their own logs. Errors still reach the caller unchanged.
/// The host controls the tracing subscriber and global OpenTelemetry
/// meter provider; libsy installs no exporter and performs no telemetry network I/O.
#[async_trait]
pub trait Algorithm: Send + Sync + 'static {
    /// Stable, low-cardinality name identifying this algorithm — the
    /// `algorithm` attribute on every span, metric, and log line the crate
    /// emits for its runs.
    fn name(&self) -> &str;

    /// Select a route, requesting external work through [`Driver`] as needed.
    /// [`run_stream`](Self::run_stream) runs this method and exposes its work to the host.
    async fn route(self: Arc<Self>, driver: Driver, request: Request) -> Result<RoutingOutcome>;

    /// Whether the host should summarize served answers for [`Self::observe_answer`].
    fn tracks_answers(&self) -> bool {
        false
    }

    /// Records the answer the host served for a request in `session_id`.
    ///
    /// The host reports a streamed answer before forwarding its final chunk, so the
    /// session's next request is routed with this answer already recorded.
    async fn observe_answer(&self, _session_id: &str, _answer: AnswerSummary) {}

    /// Expose work requests and the final outcome so the host can control execution.
    ///
    /// Each call waits for its host reply. A run ends with one terminal item — [`Step::Done`] on
    /// success, an `Err` item on failure, including when the algorithm panics. Dropping
    /// the stream aborts the spawned algorithm task.
    ///
    /// Every invocation owns a separate [`Driver`].
    fn run_stream(
        self: Arc<Self>,
        request: Request,
        models: Arc<RuntimeModels>,
        state: State,
    ) -> StepStream {
        let (driver, step_rx) = Driver::new(self.name(), models);
        driver.set_state(state);
        let span = observability::run_span(self.name(), &request);
        let handle = tokio::spawn(
            async move {
                let algorithm = self.name().to_string();
                // Catch a panicking algorithm so the run still publishes a terminal step.
                let route = AssertUnwindSafe(self.route(driver.clone(), request)).catch_unwind();
                let result = observability::observe_run(&algorithm, async {
                    let outcome = route.await.unwrap_or_else(|payload| {
                        Err(LibsyError::AlgorithmError {
                            message: format!(
                                "algorithm task panicked: {}",
                                panic_message(payload.as_ref())
                            ),
                        })
                    })?;
                    if outcome.response.is_none() {
                        driver
                            .models
                            .check_request_targets(&outcome.request, &outcome.selected_model_ids)?;
                    }
                    Ok(outcome)
                })
                .await;

                let _ = driver.finish(result).await;
            }
            .instrument(span),
        );
        // Dropping the stream aborts the algorithm task when its consumer goes away.
        let abort_guard = AbortOnDrop(handle.abort_handle());
        Box::pin(ReceiverStream::new(step_rx).map(move |step| {
            // link abort guard to stream
            let _keep_alive = &abort_guard;
            step
        }))
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;
    use crate::core::testing::{Serve, ServeResult, echo, reply, serve_decision, test_drive};
    use futures::StreamExt;
    use switchyard_protocol::{
        LlmResponse, LlmResponseChunk, completion_text, text_request, text_response,
    };

    #[test]
    fn a_final_answer_has_text_and_no_unfinished_or_tool_output() {
        let mut answer = text_response(None, "done");
        assert!(AnswerSummary::of(&answer).final_reply);
        answer.outputs[0].stop_reason = Some(StopReason::MaxTokens);
        assert!(!AnswerSummary::of(&answer).final_reply);
        answer.outputs[0].stop_reason = Some(StopReason::EndTurn);
        let mut tool = answer.outputs[0].clone();
        tool.content = vec![ContentBlock::ToolCall(switchyard_protocol::ToolCall {
            id: "call".into(),
            name: "bash".into(),
            arguments: serde_json::json!({}),
        })];
        answer.outputs.push(tool);
        assert!(!AnswerSummary::of(&answer).final_reply);
        answer.outputs.pop();
        answer.outputs[0].content = vec![ContentBlock::Text { text: "  ".into() }];
        assert!(!AnswerSummary::of(&answer).final_reply);
        assert!(!AnswerSummary::of(&AggLlmResponse::default()).final_reply);
    }

    #[derive(Debug, thiserror::Error)]
    #[error("{0}")]
    struct TestError(&'static str);

    fn test_error(message: &'static str) -> LibsyError {
        LibsyError::external("test", TestError(message))
    }

    /// Trivial algo used only to exercise the orchestrator: calls the first target
    /// and returns its response as the routing outcome.
    struct TestAlgo {
        target_set: Vec<ModelId>,
    }

    #[async_trait]
    impl Algorithm for TestAlgo {
        fn name(&self) -> &str {
            "test"
        }

        async fn route(
            self: Arc<Self>,
            driver: Driver,
            request: Request,
        ) -> Result<RoutingOutcome> {
            let target = self
                .target_set
                .first()
                .ok_or(LibsyError::NoTargets)?
                .clone();
            let response = driver
                .call_model(request.clone(), vec![target.clone()])
                .await?;
            driver.record_judgment(
                Some(crate::Score {
                    target: target.clone(),
                    category: Some(Category::Capable),
                    confidence: 1.0,
                }),
                Some(serde_json::json!({"source": "judge"})),
            );
            driver.set_evidence(serde_json::json!({"source": "test"}));
            driver.set_evidence_if_empty(serde_json::json!({"source": "ignored"}));
            Ok(RoutingOutcome::answered(target, request, response))
        }
    }

    /// Build a shared `TestAlgo` over the given target set.
    fn orch(target_set: Vec<ModelId>) -> Arc<dyn Algorithm> {
        Arc::new(TestAlgo { target_set })
    }

    fn request() -> Request {
        Request {
            llm_request: text_request(Some("auto".to_string()), "hi".to_string()),
            raw_request: None,
            metadata: None,
        }
    }

    #[test]
    fn routing_outcome_constructors_stamp_selection_and_preserve_payloads() {
        let outcome = RoutingOutcome::route_to(
            "selected".into(),
            target_set(&["fallback-one", "fallback-two"]),
            request(),
        );

        assert_eq!(
            outcome.selected_model_ids,
            target_set(&["selected", "fallback-one", "fallback-two"])
        );
        assert_eq!(outcome.request.model_id().as_deref(), Some("selected"));
        assert!(outcome.response.is_none());
        assert!(outcome.metadata.is_none());

        let outcome = RoutingOutcome::route_to("only".into(), Vec::new(), request());
        assert_eq!(outcome.selected_model_ids, target_set(&["only"]));

        let outcome = RoutingOutcome::answered(
            "answered".into(),
            request(),
            Response {
                llm_response: LlmResponse::Agg(text_response(None, "existing")),
                metadata: None,
                upstream_headers: http::HeaderMap::new(),
            },
        );

        assert_eq!(outcome.selected_model_ids, target_set(&["answered"]));
        assert_eq!(outcome.request.model_id().as_deref(), Some("answered"));
        assert_eq!(
            outcome
                .response
                .as_ref()
                .and_then(|response| response.llm_response.as_agg())
                .map(completion_text),
            Some("existing".to_string())
        );
    }

    fn target_set(names: &[&str]) -> Vec<ModelId> {
        names.iter().map(|name| ModelId::from(*name)).collect()
    }

    enum TargetProbe {
        Model(&'static [&'static str]),
        Decision(&'static str),
        Completion(&'static [&'static str]),
        MismatchedCompletion,
    }

    #[async_trait]
    impl Algorithm for TargetProbe {
        fn name(&self) -> &str {
            "target_probe"
        }

        async fn route(
            self: Arc<Self>,
            driver: Driver,
            request: Request,
        ) -> Result<RoutingOutcome> {
            match &*self {
                Self::Model(models) => {
                    let response = driver
                        .call_model(request.clone(), target_set(models))
                        .await?;
                    Ok(RoutingOutcome::answered(
                        models[0].into(),
                        request,
                        response,
                    ))
                }
                Self::Decision(model) => {
                    driver
                        .call_decision(
                            DecisionRequest {
                                model: None,
                                input: Value::Null,
                                questions: Default::default(),
                            },
                            (*model).into(),
                        )
                        .await?;
                    Ok(RoutingOutcome::route_to("allowed".into(), vec![], request))
                }
                Self::Completion(models) => Ok(RoutingOutcome::route_to(
                    models[0].into(),
                    target_set(&models[1..]),
                    request,
                )),
                Self::MismatchedCompletion => {
                    let mut outcome = RoutingOutcome::route_to("allowed".into(), vec![], request);
                    outcome.request.llm_request.model = Some("outside".into());
                    Ok(outcome)
                }
            }
        }
    }

    struct RewrittenSteps(Arc<TargetProbe>, fn(&mut Step));

    #[async_trait]
    impl Algorithm for RewrittenSteps {
        fn name(&self) -> &str {
            "rewritten_steps"
        }

        async fn route(
            self: Arc<Self>,
            driver: Driver,
            request: Request,
        ) -> Result<RoutingOutcome> {
            Arc::clone(&self.0).route(driver, request).await
        }

        fn run_stream(
            self: Arc<Self>,
            request: Request,
            models: Arc<RuntimeModels>,
            state: State,
        ) -> StepStream {
            Box::pin(
                Arc::clone(&self.0)
                    .run_stream(request, models, state)
                    .map(move |step| {
                        step.map(|mut step| {
                            (self.1)(&mut step);
                            step
                        })
                    }),
            )
        }
    }

    fn restricted_models() -> RuntimeModels {
        RuntimeModels::new(HashMap::from([
            (Category::Any, target_set(&["allowed", "fallback"])),
            (Category::Judge, target_set(&["judge"])),
        ]))
        .with_target_restriction()
        .with_subagent(HashMap::from([(Category::Any, target_set(&["subagent"]))]))
    }

    #[tokio::test]
    async fn target_restriction_checks_default_streams() {
        for probe in [
            TargetProbe::Model(&["outside"]),
            TargetProbe::Model(&["allowed", "outside"]),
            TargetProbe::Decision("outside"),
            TargetProbe::Completion(&["outside"]),
            TargetProbe::Completion(&["allowed", "outside"]),
        ] {
            let mut stream = Arc::new(probe).run_stream(
                request(),
                Arc::new(restricted_models()),
                State::default(),
            );
            assert!(matches!(
                stream.next().await,
                Some(Err(LibsyError::TargetNotFound { target })) if target == "outside"
            ));
            assert!(stream.next().await.is_none());
        }
        let mut stream = Arc::new(TargetProbe::MismatchedCompletion).run_stream(
            request(),
            Arc::new(restricted_models()),
            State::default(),
        );
        assert!(matches!(
            stream.next().await,
            Some(Err(LibsyError::AlgorithmError { .. }))
        ));

        let mut stream = Arc::new(TargetProbe::Model(&["outside"])).run_stream(
            request(),
            Arc::new(RuntimeModels::default()),
            State::default(),
        );
        assert!(matches!(stream.next().await, Some(Ok(Step::CallModel(_)))));
    }

    #[tokio::test]
    async fn target_restriction_rechecks_rewritten_steps() {
        for probe in [
            TargetProbe::Model(&["allowed"]),
            TargetProbe::Decision("judge"),
            TargetProbe::Completion(&["allowed"]),
        ] {
            let probe = Arc::new(probe);
            let run = |rewrite| {
                drive(
                    Arc::new(RewrittenSteps(Arc::clone(&probe), rewrite)),
                    request(),
                    Arc::new(restricted_models()),
                    |_| async { panic!("invalid work must not reach the host") },
                )
            };
            let result = run(|step| match step {
                Step::CallModel(call) => call.models.push("outside".into()),
                Step::CallDecision(call) => call.model = "outside".into(),
                Step::Done(outcome) => outcome.selected_model_ids.push("outside".into()),
            })
            .await;
            assert!(
                matches!(result, Err(LibsyError::TargetNotFound { target }) if target == "outside")
            );

            let result = run(|step| match step {
                Step::CallModel(call) => call.request.llm_request.model = Some("outside".into()),
                Step::CallDecision(call) => call.request.model = Some("allowed".into()),
                Step::Done(outcome) => outcome.request.llm_request.model = Some("outside".into()),
            })
            .await;
            assert!(matches!(result, Err(LibsyError::AlgorithmError { .. })));
        }
    }

    #[tokio::test]
    async fn target_restriction_preserves_allowed_work() -> Result<()> {
        for (probe, expected) in [
            (
                TargetProbe::Model(&["allowed", "fallback"]),
                &["allowed"][..],
            ),
            (TargetProbe::Model(&["judge"]), &["judge"][..]),
            (TargetProbe::Decision("judge"), &["allowed"][..]),
            (
                TargetProbe::Completion(&["allowed", "fallback"]),
                &["allowed", "fallback"][..],
            ),
            (TargetProbe::Completion(&["subagent"]), &["subagent"][..]),
        ] {
            let outcome = drive(
                Arc::new(probe),
                request(),
                Arc::new(restricted_models()),
                |call| async move {
                    match call {
                        Call::Model(call) => {
                            call.respond(std::future::ready(Ok(reply("ok")))).await
                        }
                        Call::Decision(call) => serve_decision(*call).await,
                    }
                },
            )
            .await?;
            assert_eq!(outcome.selected_model_ids, target_set(expected));
        }
        let outcome = drive(
            Arc::new(crate::Noop {}),
            request(),
            Arc::new(RuntimeModels::default().with_target_restriction()),
            |_| async { panic!("Noop must not request model calls") },
        )
        .await?;
        assert!(outcome.response.is_some());
        Ok(())
    }

    #[tokio::test]
    async fn typed_driver_preserves_call_and_stream_boundaries() -> Result<()> {
        tokio::time::timeout(std::time::Duration::from_secs(1), async {
            // Distinct oneshots keep reverse-order replies paired with their producers, and a
            // retained call remains pending until the host responds.
            let (driver, mut step_rx) = Driver::new("test", Arc::new(RuntimeModels::default()));
            let first_driver = driver.clone();
            let mut first = tokio::spawn(async move {
                first_driver
                    .call_model(request(), vec![ModelId::from("first")])
                    .await
            });
            let second = tokio::spawn(async move {
                driver
                    .call_model(request(), vec![ModelId::from("second")])
                    .await
            });

            let mut calls = HashMap::new();
            for _ in 0..2 {
                let step = step_rx.recv().await.ok_or(DriverError::StreamClosed)??;
                let Step::CallModel(call) = step else {
                    return Err(test_error("expected a CallModel step"));
                };
                let selected_model = call
                    .models
                    .first()
                    .ok_or_else(|| test_error("model call has no candidates"))?
                    .to_string();
                calls.insert(selected_model, call);
            }
            assert!(
                tokio::time::timeout(std::time::Duration::from_millis(20), &mut first)
                    .await
                    .is_err(),
                "call completed before the host responded"
            );
            calls
                .remove("second")
                .ok_or_else(|| test_error("missing second call"))?
                .respond(std::future::ready(Ok(reply("second response")))).await?;
            calls
                .remove("first")
                .ok_or_else(|| test_error("missing first call"))?
                .respond(std::future::ready(Ok(reply("first response")))).await?;

            let first_response = first
                .await
                .map_err(|source| LibsyError::external("joining a test task", source))??;
            let second_response = second
                .await
                .map_err(|source| LibsyError::external("joining a test task", source))??;
            assert_eq!(
                first_response.llm_response.as_agg().map(completion_text),
                Some("first response".to_string())
            );
            assert_eq!(
                second_response.llm_response.as_agg().map(completion_text),
                Some("second response".to_string())
            );

            // Dropping the host-facing promise closes only that call's reply channel.
            let (driver, mut step_rx) = Driver::new("test", Arc::new(RuntimeModels::default()));
            let producer = tokio::spawn(async move {
                driver
                    .call_model(request(), vec![ModelId::from("dropped")])
                    .await
            });
            let step = step_rx.recv().await.ok_or(DriverError::StreamClosed)??;
            let Step::CallModel(call) = step else {
                return Err(test_error("expected a CallModel step"));
            };
            drop(call);
            let result = producer
                .await
                .map_err(|source| LibsyError::external("joining a test task", source))?;
            assert!(matches!(
                result,
                Err(LibsyError::Driver(DriverError::ResponseDropped))
            ));

            // A standalone driver reports the typed step receiver disappearing at its next call.
            let (driver, step_rx) = Driver::new("test", Arc::new(RuntimeModels::default()));
            drop(step_rx);
            let result = driver
                .call_model(request(), vec![ModelId::from("closed")])
                .await;
            assert!(matches!(
                result,
                Err(LibsyError::Driver(DriverError::StreamClosed))
            ));

            fn decision_request() -> DecisionRequest {
                DecisionRequest {
                    model: Some("overwritten".into()),
                    input: serde_json::json!({"task": "choose a route"}),
                    questions: Default::default(),
                }
            }

            fn decision_response() -> DecisionResponse {
                DecisionResponse {
                    id: Some("decision-1".to_string()),
                    model: Some("provider-model".into()),
                    answers: [(
                        "p_solve".to_string(),
                        switchyard_protocol::DecisionAnswer {
                            value: switchyard_protocol::DecisionValue::Boolean(
                                switchyard_protocol::BooleanEstimate::ProbabilityTrue(
                                    switchyard_protocol::Probability(0.8),
                                ),
                            ),
                            provider_confidence: None,
                        },
                    )]
                    .into(),
                    usage: Default::default(),
                }
            }

            struct MixedCalls(&'static str);

            #[async_trait]
            impl Algorithm for MixedCalls {
                fn name(&self) -> &str {
                    "mixed"
                }

                async fn route(
                    self: Arc<Self>,
                    driver: Driver,
                    request: Request,
                ) -> Result<RoutingOutcome> {
                    let (llm, decision) = tokio::join!(
                        driver.call_model(request.clone(), vec!["llm".into()]),
                        driver.call_decision(decision_request(), "decision".into()),
                    );
                    assert_eq!(
                        llm?.llm_response.as_agg().map(completion_text),
                        Some("llm reply".into())
                    );
                    match self.0 {
                        "mock" => assert_eq!(decision?, DecisionResponse {
                            id: None,
                            model: Some("decision".into()),
                            answers: Default::default(),
                            usage: Default::default(),
                        }),
                        "reply" => assert_eq!(decision?, decision_response()),
                        "error" => assert!(matches!(
                            decision,
                            Err(LibsyError::AlgorithmError { message }) if message == "provider failed"
                        )),
                        "drop" => assert!(matches!(
                            decision,
                            Err(LibsyError::Driver(DriverError::ResponseDropped))
                        )),
                        "abort" => return std::future::pending().await,
                        _ => unreachable!(),
                    }
                    Ok(RoutingOutcome::route_to("answer".into(), vec![], request))
                }
            }

            for mode in ["mock", "reply", "error", "drop", "abort"] {
                // Serial dispatch would deadlock here and fail the enclosing timeout.
                let barrier = Arc::new(tokio::sync::Barrier::new(2));
                let outcome = drive(
                    Arc::new(MixedCalls(mode)),
                    request(),
                    Arc::new(RuntimeModels::default()),
                    move |call| {
                        let barrier = barrier.clone();
                        async move {
                            barrier.wait().await;
                            let call = match call {
                                Call::Model(call) => return call.respond(std::future::ready(Ok(reply("llm reply")))).await,
                                Call::Decision(call) => *call,
                            };
                            assert_eq!(call.algorithm, "mixed");
                            assert_eq!(call.model, "decision");
                            assert_eq!(call.request.model, Some("decision".into()));
                            assert_eq!(call.request.input, decision_request().input);
                            match mode {
                                "mock" => serve_decision(call).await,
                                "reply" => call.respond(std::future::ready(Ok(decision_response()))).await,
                                "error" => call.respond(std::future::ready(Err(LibsyError::AlgorithmError {
                                    message: "provider failed".into(),
                                }))).await,
                                "drop" => {
                                    drop(call);
                                    Ok(())
                                }
                                "abort" => call.fail(test_error("host aborted")),
                                _ => unreachable!(),
                            }
                        }
                    },
                )
                .await;
                if mode == "abort" {
                    assert!(matches!(outcome, Err(LibsyError::External { .. })));
                } else {
                    assert_eq!(outcome?.selected_model_id()?, "answer");
                }
            }

            let (driver, mut steps) = Driver::new("test", Arc::new(RuntimeModels::default()));
            let mut pending = Box::pin(driver.call_decision(decision_request(), "decision".into()));
            assert!(futures::poll!(&mut pending).is_pending());
            let Some(Ok(Step::CallDecision(call))) = steps.recv().await else {
                return Err(test_error("expected a decision call"));
            };
            drop(pending);
            call.respond(async { panic!("cancelled work must not be polled") }).await?;
            drop(steps);
            assert!(matches!(
                driver.call_decision(decision_request(), "decision".into()).await,
                Err(LibsyError::Driver(DriverError::StreamClosed))
            ));
            Ok(())
        })
        .await
        .map_err(|error| LibsyError::external("waiting for typed driver boundaries", error))?
    }

    #[test]
    fn target_lookup_returns_the_missing_target() {
        let error = ensure_model_is_target(&target_set(&[]), &ModelId::from("missing")).err();
        assert!(matches!(
            error,
            Some(LibsyError::TargetNotFound { target }) if target == "missing"
        ));
    }

    /// Build a single-target algo, plus a `serve` that answers it as a token stream
    /// replaying `chunks` in order (as `Ok` items).
    fn streaming_orch(chunks: Vec<LlmResponseChunk>) -> (Arc<dyn Algorithm>, impl Serve) {
        let algo = orch(target_set(&["stream/model"]));
        let serve = move |_target: ModelId, _request: Request| {
            let chunks = chunks.clone();
            async move {
                let stream =
                    futures::stream::iter(chunks.into_iter().map(|chunk| Ok(chunk.into()))).boxed();
                Ok(Response {
                    llm_response: LlmResponse::Stream(stream),
                    metadata: None,
                    upstream_headers: http::HeaderMap::new(),
                })
            }
        };
        (algo, serve)
    }

    #[tokio::test]
    async fn run_returns_a_streamed_response_the_caller_aggregates() -> Result<()> {
        // A streaming client -> its chunks flow through the promise and `Done`,
        // and `run` returns the live stream untouched for the caller to fold.
        let (orch, serve) = streaming_orch(vec![
            LlmResponseChunk::MessageStart {
                id: Some("m1".to_string()),
                model: Some("stream/model".to_string()),
            },
            LlmResponseChunk::TextDelta {
                index: 0,
                text: "hel".to_string(),
            },
            LlmResponseChunk::TextDelta {
                index: 0,
                text: "lo".to_string(),
            },
            LlmResponseChunk::MessageStop {
                reason: Some("stop".to_string()),
            },
        ]);
        let (selected_model, response) = test_drive(orch, request(), serve).await?;
        // The run handed back the live stream; the caller folds it to a buffered aggregate.
        let agg = response
            .llm_response
            .into_agg()
            .await
            .map_err(|error| LibsyError::external("aggregating response stream", error))?;
        assert_eq!(completion_text(&agg), "hello");
        assert_eq!(agg.model.as_deref(), Some("stream/model"));
        assert_eq!(selected_model, "stream/model");
        Ok(())
    }

    #[tokio::test]
    async fn aggregating_a_streamed_response_propagates_a_mid_stream_error() -> Result<()> {
        // The run succeeds and returns the stream; the in-band `Error` chunk surfaces only
        // when the caller aggregates it.
        let (orch, serve) = streaming_orch(vec![
            LlmResponseChunk::TextDelta {
                index: 0,
                text: "partial".to_string(),
            },
            LlmResponseChunk::StreamError {
                message: "upstream exploded".to_string(),
            },
        ]);
        let (_, response) = test_drive(orch, request(), serve).await?;
        match response.llm_response.into_agg().await {
            Ok(_) => panic!("expected a mid-stream error, got an aggregate"),
            Err(err) => {
                assert!(err.to_string().contains("upstream exploded"));
                Ok(())
            }
        }
    }

    #[tokio::test]
    async fn run_offloads_via_promise_then_finishes() -> Result<()> {
        // Every call is offloaded via a promise the orchestrator surfaces as a
        // `CallModel` step for us to fulfill.
        let stream = orch(target_set(&["offload/model"])).run_stream(
            request(),
            Arc::new(RuntimeModels::default()),
            State::default(),
        );
        tokio::pin!(stream);

        let mut saw_call = false;
        let mut final_completion = None;
        while let Some(step) = stream.next().await {
            match step? {
                Step::CallDecision(_) => return Err(test_error("unexpected decision call")),
                Step::CallModel(call) => {
                    saw_call = true;
                    assert_eq!(call.models, vec![ModelId::from("offload/model")]);
                    // Fulfilling the promise is the "real" model call the caller makes.
                    call.respond(std::future::ready(Ok(Response {
                        llm_response: LlmResponse::Agg(text_response(
                            None,
                            "fulfilled".to_string(),
                        )),
                        metadata: None,
                        upstream_headers: http::HeaderMap::new(),
                    })))
                    .await?;
                }
                Step::Done(outcome) => {
                    let metadata = outcome
                        .metadata
                        .as_ref()
                        .expect("run_stream should attach outcome metadata");
                    assert_eq!(metadata.algorithm, "test");
                    assert_eq!(
                        uuid::Uuid::parse_str(metadata.outcome_id())
                            .expect("outcome id should be a UUID")
                            .get_version_num(),
                        7
                    );
                    assert_eq!(
                        metadata.evidence,
                        Some(serde_json::json!({"source": "test"}))
                    );
                    assert_eq!(
                        metadata.judgments[0].evidence,
                        Some(serde_json::json!({"source": "judge"}))
                    );
                    let response = outcome
                        .response
                        .ok_or_else(|| test_error("expected an answered outcome"))?;
                    final_completion = Some(
                        response
                            .llm_response
                            .as_agg()
                            .map(completion_text)
                            .unwrap_or_default(),
                    );
                }
            }
        }

        assert!(saw_call, "expected a CallModel step before Done");
        assert_eq!(
            final_completion.ok_or_else(|| test_error("no Done step"))?,
            "fulfilled"
        );
        Ok(())
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 12)]
    async fn requests_are_processed_in_parallel() -> Result<()> {
        use std::time::Duration;
        use tokio::sync::Barrier;

        const N: usize = 12;

        // Serving blocks until all N concurrent calls have arrived. If requests were
        // serialized (one algorithm behind a `Mutex`), only one call could be in flight,
        // the barrier would never reach N, and the test would time out. It passes only
        // because the shared algorithm is driven concurrently across requests.
        let barrier = Arc::new(Barrier::new(N));
        // One shared algorithm driven by many concurrent requests.
        let algo = orch(target_set(&["m"]));

        let mut handles = Vec::new();
        for _ in 0..N {
            let algo = algo.clone();
            let barrier = barrier.clone();
            let serve = move |target: ModelId, _request: Request| {
                let barrier = barrier.clone();
                async move {
                    barrier.wait().await;
                    Ok(reply(target))
                }
            };
            handles.push(tokio::spawn(async move {
                test_drive(algo, request(), serve)
                    .await
                    .map(|(_, response)| {
                        response
                            .llm_response
                            .as_agg()
                            .map(completion_text)
                            .unwrap_or_default()
                    })
            }));
        }

        for handle in handles {
            // The timeout turns a serialization deadlock into a failure, not a hang.
            let completion = tokio::time::timeout(Duration::from_secs(5), handle)
                .await
                .map_err(|error| LibsyError::external("waiting for test task", error))?
                .map_err(|source| LibsyError::external("joining a test task", source))??;
            assert_eq!(completion, "m");
        }
        Ok(())
    }

    #[tokio::test]
    async fn offload_error_propagates_back_to_the_algorithm() -> Result<()> {
        // A client-less target offloads its call; we fulfill the promise with an
        // Err, which must flow back through `call_model_target` into the algorithm and
        // out as an error step — not a response.
        let stream = orch(target_set(&["offload/model"])).run_stream(
            request(),
            Arc::new(RuntimeModels::default()),
            State::default(),
        );
        tokio::pin!(stream);

        let mut saw_error = false;
        while let Some(step) = stream.next().await {
            match step {
                Ok(Step::CallDecision(_)) => return Err(test_error("unexpected decision call")),
                Ok(Step::CallModel(mut call)) => {
                    call.recover_errors = true;
                    call.respond(std::future::ready(Err(test_error(
                        "upstream model call failed",
                    ))))
                    .await?;
                }
                Ok(Step::Done(..)) => {
                    return Err(test_error(
                        "expected the offload error to propagate, got a response",
                    ));
                }
                Err(err) => {
                    // The algorithm's `call_model_target` saw the error via the promise.
                    assert!(err.to_string().contains("upstream model call failed"));
                    saw_error = true;
                }
            }
        }

        assert!(saw_error, "expected an error step");
        Ok(())
    }

    #[tokio::test]
    async fn dropping_the_stream_cancels_the_algorithm_task() -> Result<()> {
        use std::sync::atomic::{AtomicBool, Ordering};
        use std::time::Duration;
        use tokio::sync::mpsc;

        // Sets a flag when dropped, so we can observe whether the algorithm task was
        // cancelled/dropped.
        struct DropGuard(Arc<AtomicBool>);
        impl Drop for DropGuard {
            fn drop(&mut self) {
                self.0.store(true, Ordering::SeqCst);
            }
        }

        struct StuckAlgo {
            started: mpsc::UnboundedSender<()>,
            dropped: Arc<AtomicBool>,
        }

        #[async_trait]
        impl Algorithm for StuckAlgo {
            fn name(&self) -> &str {
                "stuck"
            }

            async fn route(
                self: Arc<Self>,
                _driver: Driver,
                _request: Request,
            ) -> Result<RoutingOutcome> {
                let _guard = DropGuard(self.dropped.clone());
                let _ = self.started.send(());
                // Await forever without ever touching the driver.
                std::future::pending::<()>().await;
                unreachable!()
            }
        }

        let (started_tx, mut started_rx) = mpsc::unbounded_channel();
        let dropped = Arc::new(AtomicBool::new(false));
        let algo: Arc<dyn Algorithm> = Arc::new(StuckAlgo {
            started: started_tx,
            dropped: dropped.clone(),
        });

        let stream = algo.run_stream(
            request(),
            Arc::new(RuntimeModels::default()),
            State::default(),
        );
        started_rx
            .recv()
            .await
            .ok_or_else(|| test_error("task never started"))?;
        drop(stream);
        tokio::time::sleep(Duration::from_millis(100)).await;

        assert!(
            dropped.load(Ordering::SeqCst),
            "algorithm task was NOT cancelled after dropping the stream"
        );
        Ok(())
    }

    #[tokio::test]
    async fn route_panic_surfaces_as_a_stream_error() -> Result<()> {
        // An algorithm whose task panics must surface an `Err` step carrying the panic
        // message, not abort the process from an unobserved detached task.
        struct Panicky;

        #[async_trait]
        impl Algorithm for Panicky {
            fn name(&self) -> &str {
                "panicky"
            }

            async fn route(
                self: Arc<Self>,
                _driver: Driver,
                _request: Request,
            ) -> Result<RoutingOutcome> {
                panic!("boom");
            }
        }

        let algo: Arc<dyn Algorithm> = Arc::new(Panicky);
        let stream = algo.run_stream(
            request(),
            Arc::new(RuntimeModels::default()),
            State::default(),
        );
        tokio::pin!(stream);

        let mut saw_error = false;
        while let Some(step) = stream.next().await {
            match step {
                Err(err) => {
                    // The panic message is preserved, not flattened into an opaque failure.
                    assert!(err.to_string().contains("algorithm task panicked: boom"));
                    saw_error = true;
                }
                Ok(_) => return Err(test_error("expected the panic to surface as an error step")),
            }
        }

        assert!(saw_error, "expected an error step from the panicked task");
        Ok(())
    }

    /// A panicking algorithm must publish its terminal step even when it left a `Driver`
    /// clone alive in another task. That clone holds the step channel open, so a run that
    /// merely unwound would never terminate and the consumer would wait forever.
    #[tokio::test]
    async fn a_panic_with_a_leaked_driver_clone_still_terminates_the_run() -> Result<()> {
        struct LeakyPanic;

        #[async_trait]
        impl Algorithm for LeakyPanic {
            fn name(&self) -> &str {
                "leaky_panic"
            }

            async fn route(
                self: Arc<Self>,
                driver: Driver,
                _request: Request,
            ) -> Result<RoutingOutcome> {
                tokio::spawn(async move {
                    // Outlives the panic below, keeping a sender clone alive.
                    let _keep_alive = driver;
                    std::future::pending::<()>().await;
                });
                tokio::task::yield_now().await;
                panic!("boom");
            }
        }

        let algo: Arc<dyn Algorithm> = Arc::new(LeakyPanic);
        // The timeout turns the hang this guards against into a failure rather than a hang.
        let result = tokio::time::timeout(
            std::time::Duration::from_secs(1),
            test_drive(algo, request(), echo()),
        )
        .await
        .map_err(|error| LibsyError::external("waiting for the panicked run to end", error))?;

        match result {
            Ok(_) => Err(test_error(
                "expected the panic to end the run with an error",
            )),
            Err(err) => {
                assert!(err.to_string().contains("algorithm task panicked: boom"));
                Ok(())
            }
        }
    }

    #[tokio::test]
    async fn cancelling_run_cancels_the_algorithm_task() -> Result<()> {
        use std::sync::atomic::{AtomicBool, Ordering};
        use std::time::Duration;
        use tokio::sync::mpsc;

        // Sets a flag when dropped, so we can observe whether the algorithm task was
        // cancelled once the `run` future driving it is dropped.
        struct DropGuard(Arc<AtomicBool>);
        impl Drop for DropGuard {
            fn drop(&mut self) {
                self.0.store(true, Ordering::SeqCst);
            }
        }

        struct StuckAlgo {
            started: mpsc::UnboundedSender<()>,
            dropped: Arc<AtomicBool>,
        }

        #[async_trait]
        impl Algorithm for StuckAlgo {
            fn name(&self) -> &str {
                "stuck"
            }

            async fn route(
                self: Arc<Self>,
                _driver: Driver,
                _request: Request,
            ) -> Result<RoutingOutcome> {
                let _guard = DropGuard(self.dropped.clone());
                let _ = self.started.send(());
                // Hang forever without ever touching the driver, so only cancellation
                // (not a dropped step channel) can stop this task.
                std::future::pending::<()>().await;
                unreachable!()
            }
        }

        let (started_tx, mut started_rx) = mpsc::unbounded_channel();
        let dropped = Arc::new(AtomicBool::new(false));
        let algo: Arc<dyn Algorithm> = Arc::new(StuckAlgo {
            started: started_tx,
            dropped: dropped.clone(),
        });

        // Drive the run on its own task, wait until the algorithm task is up, then cancel
        // it — dropping its future (and the `run_stream` stream it holds).
        let run_task = tokio::spawn(async move { test_drive(algo, request(), echo()).await });
        started_rx
            .recv()
            .await
            .ok_or_else(|| test_error("task never started"))?;
        run_task.abort();
        tokio::time::sleep(Duration::from_millis(100)).await;

        assert!(
            dropped.load(Ordering::SeqCst),
            "algorithm task was NOT cancelled after cancelling run"
        );
        Ok(())
    }

    // --- first-wins hedging: `run` must not wait on losing speculative calls -------------

    /// Offloads two targets concurrently and returns the first to resolve, dropping the
    /// loser's call (first-wins hedging).
    struct Hedge {
        winner: String,
        loser: String,
    }

    #[async_trait]
    impl Algorithm for Hedge {
        fn name(&self) -> &str {
            "hedge"
        }

        async fn route(
            self: Arc<Self>,
            driver: Driver,
            request: Request,
        ) -> Result<RoutingOutcome> {
            let outcome_request = request.clone();
            let win = driver.call_model(request.clone(), vec![self.winner.clone().into()]);
            let lose = driver.call_model(request, vec![self.loser.clone().into()]);
            // First to resolve wins; `select!` drops the losing future (and its promise).
            tokio::select! {
                res = win => Ok(RoutingOutcome::answered(
                    self.winner.clone().into(),
                    outcome_request,
                    res?,
                )),
                res = lose => Ok(RoutingOutcome::answered(
                    self.loser.clone().into(),
                    outcome_request,
                    res?,
                )),
            }
        }
    }

    /// Builds a hedging algo and the `serve` that drives it: the winner is gated behind the
    /// loser starting (so the loser's serve is guaranteed in flight when the winner wins),
    /// and the loser finishes after `loser_delay` — or never, when `None`.
    fn hedge(loser_delay: Option<std::time::Duration>) -> (Arc<dyn Algorithm>, impl Serve) {
        let started = Arc::new(tokio::sync::Notify::new());
        let algo = Arc::new(Hedge {
            winner: "winner".to_string(),
            loser: "loser".to_string(),
        });
        let serve = move |target: ModelId, _request: Request| {
            let started = started.clone();
            async move {
                if target == "loser" {
                    started.notify_one();
                    match loser_delay {
                        Some(delay) => tokio::time::sleep(delay).await,
                        None => std::future::pending::<()>().await,
                    }
                } else {
                    started.notified().await;
                }
                Ok(reply(target))
            }
        };
        (algo, serve)
    }

    #[tokio::test]
    async fn run_returns_the_winner_without_a_late_loser_overwriting_it() -> Result<()> {
        // The loser responds 50ms after the winner has already won. `run` must return the
        // winner, not the loser's `respond`-to-a-dropped-receiver error.
        let (algo, serve) = hedge(Some(std::time::Duration::from_millis(50)));
        let (_, response) = test_drive(algo, request(), serve).await?;
        assert_eq!(
            response
                .llm_response
                .as_agg()
                .map(completion_text)
                .unwrap_or_default(),
            "winner"
        );
        Ok(())
    }

    #[tokio::test]
    async fn run_returns_the_winner_without_hanging_on_a_pending_loser() -> Result<()> {
        // The loser never resolves. `run` must return the winner promptly, not hang
        // waiting for the in-flight loser.
        let (algo, serve) = hedge(None);
        let run = test_drive(algo, request(), serve);
        let (_, response) = tokio::time::timeout(std::time::Duration::from_secs(1), run)
            .await
            .map_err(|error| LibsyError::external("waiting for pending loser", error))??;
        assert_eq!(
            response
                .llm_response
                .as_agg()
                .map(completion_text)
                .unwrap_or_default(),
            "winner"
        );
        Ok(())
    }

    #[tokio::test]
    async fn run_surfaces_a_terminal_error_with_many_calls_in_flight() -> Result<()> {
        use std::sync::atomic::{AtomicUsize, Ordering};

        // A large fan-out (10 matched the old, now-removed concurrency cap). The terminal
        // error must still reach the caller with all of these calls pending.
        const N: usize = 10;

        // Fans out N calls, then errors as soon as all N are in flight — exercising a
        // terminal failure emitted while the offloaded calls are still pending.
        struct FanOutThenError {
            all_started: Arc<tokio::sync::Notify>,
            n: usize,
        }

        #[async_trait]
        impl Algorithm for FanOutThenError {
            fn name(&self) -> &str {
                "fan_out_then_error"
            }

            async fn route(
                self: Arc<Self>,
                driver: Driver,
                request: Request,
            ) -> Result<RoutingOutcome> {
                let offloads = futures::future::join_all(
                    (0..self.n)
                        .map(|i| driver.call_model(request.clone(), vec![format!("m{i}").into()])),
                );
                tokio::select! {
                    _ = offloads => Err(test_error("offloads unexpectedly completed")),
                    _ = self.all_started.notified() => {
                        Err(test_error("terminal error while calls pending"))
                    }
                }
            }
        }

        let all_started = Arc::new(tokio::sync::Notify::new());
        let algo: Arc<dyn Algorithm> = Arc::new(FanOutThenError {
            all_started: all_started.clone(),
            n: N,
        });

        // Serving enters each call; once all N are in flight it signals, then pends forever.
        let started = Arc::new(AtomicUsize::new(0));
        let serve = move |_target: ModelId, _request: Request| {
            let started = started.clone();
            let all_started = all_started.clone();
            async move {
                if started.fetch_add(1, Ordering::SeqCst) + 1 == N {
                    all_started.notify_one();
                }
                std::future::pending::<ServeResult>().await
            }
        };

        // With the cap gone, the driver keeps polling the stream even with N calls in
        // flight, so the terminal error surfaces promptly instead of hanging.
        let run = test_drive(algo, request(), serve);
        let result = tokio::time::timeout(std::time::Duration::from_millis(500), run)
            .await
            .map_err(|error| {
                LibsyError::external("waiting for terminal error with full call cap", error)
            })?;
        match result {
            Ok(_) => Err(test_error("expected the terminal error, got a response")),
            Err(err) => {
                assert!(
                    err.to_string()
                        .contains("terminal error while calls pending")
                );
                Ok(())
            }
        }
    }
}