visdom-cli 1.2.0

Visdom developer CLI: checks a project's Visdom wiring and launches the coding agent
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
//! The AI Gateway, as a developer's agent uses it: the console issues the
//! developer a short-lived personal key for a project, and the agent calls the
//! LLM gateway with it.
//!
//! A new personal key revokes the developer's previous one for that project,
//! so a key is kept (in visdom's own config directory, readable by the user
//! only) and reused until shortly before it expires: sessions started side by
//! side share it instead of cutting each other off. A kept key belongs to the
//! account it was issued for and is not handed to another.
//!
//! A subscription key (`billing = subscription`) is the other kind: it only
//! works with the developer's own Claude subscription token, lives about 30
//! days, and a new one revokes nothing. Claude Code gets it once, in a header
//! fixed when it starts, so it is renewed a week before it expires, and the
//! older ones are kept until they expire for `visdom logout` to revoke.

use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use tracevault_cli::agent::Agent;

use crate::billing::Billing;

/// A key with less than this left is replaced rather than handed out.
pub const MIN_KEY_SECONDS_LEFT: i64 = 15 * 60;

/// A subscription key with less than this left is replaced rather than
/// handed out: a session started with it keeps it to its end. (Of a key that
/// lives shorter than twice this, half its life.)
pub const MIN_SUBSCRIPTION_KEY_SECONDS_LEFT: i64 = 7 * 24 * 60 * 60;

/// Assumed lifetime of a key whose expiry the console did not state in a form
/// this version reads. Short, so a wrong guess costs one early renewal.
const UNKNOWN_EXPIRY_SECONDS: i64 = 60 * 60;

/// A project the developer may issue themselves a personal key for.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
pub struct OpenProject {
    /// The Control Plane project id.
    pub id: String,
    pub slug: String,
    pub name: String,
    /// The project's model aliases, each with whether the gateway takes a
    /// call paid by a Claude subscription on it. `None`: the gateway does not
    /// say (a version from before the list).
    #[serde(default)]
    pub models: Option<Vec<ProjectModel>>,
}

/// One alias of a project's allow-list, as `GET /api/me/keys` lists it.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
pub struct ProjectModel {
    pub alias: String,
    /// Served by direct Anthropic deployments only: the gateway takes a call
    /// paid by a Claude subscription on it, and refuses one on any other
    /// alias (HTTP 403, code `subscription_model`).
    #[serde(default)]
    pub subscription: bool,
}

impl OpenProject {
    /// The aliases a call paid by a Claude subscription may use, when the
    /// gateway says which.
    pub fn subscription_aliases(&self) -> Option<Vec<String>> {
        self.models.as_ref().map(|models| {
            models
                .iter()
                .filter(|m| m.subscription)
                .map(|m| m.alias.clone())
                .collect()
        })
    }
}

/// Whether the gateway takes calls paid by a Claude subscription, as its
/// console publishes it (`subscriptionTraffic` in `GET /api/settings`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SubscriptionTraffic {
    Allowed,
    /// The deployment refuses them (`VISDOM_SUBSCRIPTION_TRAFFIC=deny`).
    Denied,
    /// The console says nothing about it: a gateway from before the setting,
    /// which does not support subscription traffic either. A value this
    /// version does not know is read the same way.
    Unpublished,
}

impl SubscriptionTraffic {
    /// Whether a session paid by a Claude subscription can go through it.
    pub fn supported(self) -> bool {
        self == SubscriptionTraffic::Allowed
    }
}

/// The code the console and the gateway refuse subscription traffic with
/// while the deployment denies it.
pub const SUBSCRIPTION_TRAFFIC_DENIED: &str = "subscription_traffic_denied";

/// What the console's settings say about the gateway.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConsoleSettings {
    /// The LLM gateway's public address.
    pub llm_url: String,
    pub subscription_traffic: SubscriptionTraffic,
}

/// The machine-readable code of a refusal (`{"code": …}` as the console
/// answers, or `{"error": {"code": …}}` as the LLM gateway does), if any.
fn refusal_code(body: &str) -> Option<String> {
    let value: serde_json::Value = serde_json::from_str(body).ok()?;
    value
        .get("code")
        .or_else(|| value.get("error").and_then(|e| e.get("code")))
        .and_then(|c| c.as_str())
        .map(String::from)
}

/// A personal key as issued.
#[derive(Clone, PartialEq, Eq)]
pub struct IssuedKey {
    pub secret: String,
    /// Unix seconds.
    pub expires_at: i64,
    /// The console's id of the key, to revoke it by.
    pub id: Option<String>,
}

/// The console's refusal, in its own words when it gave any. A refusal
/// because the deployment denies subscription traffic is
/// [`crate::subscription::NOT_SUPPORTED`], word for word.
fn refusal(what: &str, status: reqwest::StatusCode, body: &str) -> String {
    if refusal_code(body).as_deref() == Some(SUBSCRIPTION_TRAFFIC_DENIED) {
        return crate::subscription::NOT_SUPPORTED.to_string();
    }
    #[derive(Deserialize)]
    struct Refusal {
        error: String,
    }
    let said = serde_json::from_str::<Refusal>(body)
        .map(|r| r.error)
        .unwrap_or_default();
    let code = status.as_u16();
    match (said.is_empty(), code) {
        (false, _) => format!("{what}: {said} (HTTP {code})"),
        (true, 401) => format!("{what}: the AI Gateway did not accept this login (HTTP 401)"),
        (true, _) => format!("{what}: HTTP {code}"),
    }
}

async fn read(
    what: &str,
    console_url: &str,
    request: reqwest::RequestBuilder,
) -> Result<String, String> {
    let response = request
        .send()
        .await
        .map_err(|e| format!("{what}: the AI Gateway ({console_url}) did not answer: {e}"))?;
    let status = response.status();
    let body = response.text().await.unwrap_or_default();
    if status.is_success() {
        Ok(body)
    } else {
        Err(refusal(what, status, &body))
    }
}

fn parse<T: for<'de> Deserialize<'de>>(what: &str, body: &str) -> Result<T, String> {
    serde_json::from_str(body).map_err(|e| format!("{what}: unreadable answer ({e})"))
}

fn base(url: &str) -> &str {
    url.trim_end_matches('/')
}

/// The LLM gateway's public address and whether it takes subscription
/// traffic, from the console's settings.
pub async fn console_settings(
    http: &reqwest::Client,
    console_url: &str,
    token: &str,
) -> Result<ConsoleSettings, String> {
    #[derive(Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct Settings {
        llm_gateway_url: String,
        #[serde(default)]
        subscription_traffic: Option<serde_json::Value>,
    }
    let what = "cannot read the AI Gateway's address";
    let request = http
        .get(format!("{}/api/settings", base(console_url)))
        .bearer_auth(token);
    let settings: Settings = parse(what, &read(what, console_url, request).await?)?;
    let url = base(&settings.llm_gateway_url).to_string();
    if !crate::components::bazaar::is_http_url(&url) {
        return Err(format!("{what}: `{url}` is not an http(s) URL"));
    }
    let subscription_traffic = match settings
        .subscription_traffic
        .as_ref()
        .and_then(|v| v.as_str())
    {
        Some("allowed") => SubscriptionTraffic::Allowed,
        Some("denied") => SubscriptionTraffic::Denied,
        _ => SubscriptionTraffic::Unpublished,
    };
    Ok(ConsoleSettings {
        llm_url: url,
        subscription_traffic,
    })
}

/// The projects the developer may issue a personal key for.
pub async fn open_projects(
    http: &reqwest::Client,
    console_url: &str,
    token: &str,
) -> Result<Vec<OpenProject>, String> {
    #[derive(Deserialize)]
    struct Mine {
        projects: Vec<OpenProject>,
    }
    let what = "cannot list your AI Gateway projects";
    let request = http
        .get(format!("{}/api/me/keys", base(console_url)))
        .bearer_auth(token);
    Ok(parse::<Mine>(what, &read(what, console_url, request).await?)?.projects)
}

/// Unix seconds of an expiry as the console states it (RFC 3339, or the same
/// without an offset, meaning UTC).
fn expiry_seconds(expires_at: Option<&str>, now: i64) -> i64 {
    let parsed = expires_at.and_then(|text| {
        chrono::DateTime::parse_from_rfc3339(text)
            .map(|t| t.timestamp())
            .or_else(|_| {
                chrono::NaiveDateTime::parse_from_str(text, "%Y-%m-%dT%H:%M:%S%.f")
                    .map(|t| t.and_utc().timestamp())
            })
            .ok()
    });
    parsed.unwrap_or(now + UNKNOWN_EXPIRY_SECONDS)
}

/// Issue the developer a personal key of the kind `billing` for
/// `project_id`. An ordinary key revokes their previous ordinary one for that
/// project; a subscription key revokes nothing.
///
/// A subscription key that comes back as anything else (a console from before
/// subscription keys ignores the request's `billing`) is revoked again and is
/// an error: such a key would let the project pay.
pub async fn issue_key(
    http: &reqwest::Client,
    console_url: &str,
    token: &str,
    project_id: &str,
    billing: Billing,
    now: i64,
) -> Result<IssuedKey, String> {
    #[derive(Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct Issued {
        secret: String,
        #[serde(default)]
        id: Option<String>,
        #[serde(default)]
        expires_at: Option<String>,
        #[serde(default)]
        billing: Option<String>,
    }
    let what = match billing {
        Billing::Project => "cannot get a personal AI Gateway key",
        Billing::Subscription => "cannot get a subscription key from the AI Gateway",
    };
    // An ordinary key is asked for as before subscription keys existed.
    let body = match billing {
        Billing::Project => serde_json::json!({ "projectId": project_id }),
        Billing::Subscription => {
            serde_json::json!({ "projectId": project_id, "billing": billing.name() })
        }
    };
    let request = http
        .post(format!("{}/api/me/keys", base(console_url)))
        .bearer_auth(token)
        .json(&body);
    let issued: Issued = parse(what, &read(what, console_url, request).await?)?;
    if issued.secret.trim().is_empty() {
        return Err(format!("{what}: the answer carried no key"));
    }
    let id = issued.id.filter(|id| !id.trim().is_empty());
    if billing == Billing::Subscription && issued.billing.as_deref() != Some(billing.name()) {
        if let Some(id) = &id {
            // Best effort: the key is not used either way.
            let _ = revoke_key(http, console_url, token, id).await;
        }
        return Err(format!(
            "{what}: this AI Gateway issued an ordinary personal key instead, so it does not \
             support billing = subscription yet (its console needs a newer version). Use \
             --billing project, or set billing to project"
        ));
    }
    Ok(IssuedKey {
        expires_at: expiry_seconds(issued.expires_at.as_deref(), now),
        secret: issued.secret,
        id,
    })
}

/// Revoke the developer's key `id` at the console. A key the console no
/// longer has (expired, or revoked already) is gone, which is what was asked.
pub async fn revoke_key(
    http: &reqwest::Client,
    console_url: &str,
    token: &str,
    id: &str,
) -> Result<(), String> {
    let what = "cannot revoke an AI Gateway key";
    let mut url = url::Url::parse(&format!("{}/api/me/keys", base(console_url)))
        .map_err(|e| format!("{what}: `{console_url}` is not a URL ({e})"))?;
    url.path_segments_mut()
        .map_err(|()| format!("{what}: `{console_url}` is not an http(s) URL"))?
        .push(id);
    let response = http
        .delete(url)
        .bearer_auth(token)
        .send()
        .await
        .map_err(|e| format!("{what}: the AI Gateway ({console_url}) did not answer: {e}"))?;
    let status = response.status();
    // The console answers 400 for a key of yours it does not have.
    if status.is_success() || matches!(status.as_u16(), 400 | 404) {
        return Ok(());
    }
    let body = response.text().await.unwrap_or_default();
    Err(refusal(what, status, &body))
}

/// Why the LLM gateway's models could not be listed.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ModelsError {
    pub why: String,
    /// The gateway answered that it does not take this key (HTTP 401 or 403):
    /// another key may work where this one does not.
    pub rejected: bool,
}

/// The model aliases `key` may call. This is also the proof that the LLM
/// gateway answers and takes the key, before an agent is pointed at it.
pub async fn models(
    http: &reqwest::Client,
    llm_url: &str,
    key: &str,
) -> Result<Vec<String>, ModelsError> {
    #[derive(Deserialize)]
    struct Model {
        id: String,
    }
    #[derive(Deserialize)]
    struct List {
        data: Vec<Model>,
    }
    let what = "cannot list the AI Gateway's models";
    let failed = |why: String, rejected: bool| ModelsError { why, rejected };
    let response = http
        .get(format!("{}/v1/models", base(llm_url)))
        .bearer_auth(key)
        .send()
        .await
        .map_err(|e| {
            failed(
                format!("{what}: the AI Gateway ({llm_url}) did not answer: {e}"),
                false,
            )
        })?;
    let status = response.status();
    let body = response.text().await.unwrap_or_default();
    if !status.is_success() {
        if refusal_code(&body).as_deref() == Some(SUBSCRIPTION_TRAFFIC_DENIED) {
            // Another key would be refused the same way.
            return Err(failed(
                crate::subscription::NOT_SUPPORTED.to_string(),
                false,
            ));
        }
        let rejected = matches!(status.as_u16(), 401 | 403);
        let why = if rejected {
            format!(
                "{what}: the AI Gateway did not accept the personal key (HTTP {})",
                status.as_u16()
            )
        } else {
            format!("{what}: HTTP {}", status.as_u16())
        };
        return Err(failed(why, rejected));
    }
    let list: List = parse(what, &body).map_err(|why| failed(why, false))?;
    Ok(list.data.into_iter().map(|m| m.id).collect())
}

/// The project to use: the configured one (its slug, id or name), else the
/// only one there is. Otherwise the choice is the developer's, and the error
/// names what to choose from.
pub fn pick_project(configured: Option<&str>, open: &[OpenProject]) -> Result<OpenProject, String> {
    let names = || {
        open.iter()
            .map(|p| p.slug.as_str())
            .collect::<Vec<_>>()
            .join(", ")
    };
    if open.is_empty() {
        return Err(
            "no AI Gateway project lets you issue a personal key: ask a project operator to \
             switch personal keys on (Budgets) and to grant you access"
                .to_string(),
        );
    }
    match configured.map(str::trim).filter(|c| !c.is_empty()) {
        Some(wanted) => open
            .iter()
            .find(|p| p.slug == wanted || p.id == wanted || p.name == wanted)
            .cloned()
            .ok_or_else(|| {
                format!(
                    "AI Gateway project `{wanted}` does not let you issue a personal key (you \
                     can for: {})",
                    names()
                )
            }),
        None if open.len() == 1 => Ok(open[0].clone()),
        None => Err(format!(
            "several AI Gateway projects are open to you ({}): set `project` under \
             `[gateway]` in .visdom/config.toml or in your visdom config, or \
             VISDOM_GATEWAY_PROJECT (`visdom init` asks and saves it)",
            names()
        )),
    }
}

/// The model alias an agent should ask for: the configured one, else the
/// first alias that looks made for that agent, else none (the agent's own
/// default, which the project must then allow).
pub fn pick_model(agent: Agent, configured: Option<&str>, available: &[String]) -> Option<String> {
    if let Some(model) = configured.map(str::trim).filter(|m| !m.is_empty()) {
        return Some(model.to_string());
    }
    let hints: &[&str] = match agent {
        Agent::ClaudeCode => &["claude", "sonnet", "opus"],
        Agent::Codex => &["codex", "gpt"],
        _ => &[],
    };
    hints.iter().find_map(|hint| {
        available
            .iter()
            .find(|alias| alias.to_ascii_lowercase().contains(hint))
            .cloned()
    })
}

/// Whose key it is: the issuer and subject of the developer's access token.
/// A key is the person's, so one kept for somebody else who logged in on this
/// machine before must not be handed out. Empty for a token that is not a
/// readable JWT, which then cannot be told apart from another such token.
pub fn account_of(token: &str) -> String {
    use base64::Engine;
    #[derive(Deserialize)]
    struct Claims {
        #[serde(default)]
        iss: String,
        sub: String,
    }
    let mut parts = token.split('.');
    let (Some(_), Some(payload), Some(_), None) =
        (parts.next(), parts.next(), parts.next(), parts.next())
    else {
        return String::new();
    };
    base64::engine::general_purpose::URL_SAFE_NO_PAD
        .decode(payload.trim_end_matches('='))
        .ok()
        .and_then(|bytes| serde_json::from_slice::<Claims>(&bytes).ok())
        .filter(|claims| !claims.sub.is_empty())
        .map(|claims| format!("{}|{}", claims.iss, claims.sub))
        .unwrap_or_default()
}

/// One stored personal key.
#[derive(Clone, Serialize, Deserialize)]
struct StoredKey {
    console_url: String,
    project_id: String,
    /// [`account_of`] the token it was issued for.
    #[serde(default)]
    account: String,
    secret: String,
    expires_at: i64,
    /// The key's kind; a key kept before there were kinds is ordinary.
    #[serde(default)]
    billing: Billing,
    /// The console's id of the key, to revoke it by on logout.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    id: Option<String>,
    /// Unix seconds when it was kept, which is when it was issued.
    #[serde(default)]
    issued_at: i64,
    /// The gateway refused it (revoked elsewhere): never handed out again,
    /// but kept until it expires, for logout to revoke.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    refused: bool,
}

impl StoredKey {
    fn is_for(&self, console_url: &str, project_id: &str, account: &str, billing: Billing) -> bool {
        self.console_url == base(console_url)
            && self.project_id == project_id
            && self.account == account
            && self.billing == billing
    }

    /// Whether it is still handed out at `now`, rather than replaced.
    fn lasts(&self, now: i64) -> bool {
        !self.refused
            && self.expires_at - now >= renew_before(self.billing, self.issued_at, self.expires_at)
    }
}

/// How long before it expires a key of the kind `billing` is replaced. A
/// subscription key the console gives a short life (shorter than twice
/// [`MIN_SUBSCRIPTION_KEY_SECONDS_LEFT`]) is replaced at half its life, not
/// at once.
fn renew_before(billing: Billing, issued_at: i64, expires_at: i64) -> i64 {
    match billing {
        Billing::Project => MIN_KEY_SECONDS_LEFT,
        Billing::Subscription => {
            let life = expires_at - issued_at;
            if issued_at > 0 && life > 0 {
                MIN_SUBSCRIPTION_KEY_SECONDS_LEFT.min(life / 2)
            } else {
                MIN_SUBSCRIPTION_KEY_SECONDS_LEFT
            }
        }
    }
}

/// A kept key, as `visdom logout` revokes it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct KeptKey {
    pub console_url: String,
    pub account: String,
    pub id: Option<String>,
    pub billing: Billing,
    pub expires_at: i64,
}

/// The personal keys `visdom` holds, one per console, project and account.
#[derive(Clone)]
pub struct KeyStore {
    path: PathBuf,
}

pub fn key_store_path_in(config_root: &Path) -> PathBuf {
    config_root.join("visdom").join("gateway-keys.json")
}

/// Held while a key is looked up, issued and stored; released when dropped.
pub struct KeyStoreLock(#[allow(dead_code)] std::fs::File);

impl KeyStore {
    pub fn at(path: PathBuf) -> KeyStore {
        KeyStore { path }
    }

    fn load(&self) -> Vec<StoredKey> {
        std::fs::read_to_string(&self.path)
            .ok()
            .and_then(|text| serde_json::from_str(&text).ok())
            .unwrap_or_default()
    }

    /// Wait for this store to be free of every other `visdom` process, and
    /// take it. Issuing a key revokes the previous one, so two sessions
    /// starting together must not both issue: the second waits here and then
    /// finds the first one's key.
    pub fn lock(&self) -> std::io::Result<KeyStoreLock> {
        let dir = self.path.parent().unwrap_or(Path::new("."));
        std::fs::create_dir_all(dir)?;
        let file = std::fs::OpenOptions::new()
            .create(true)
            .truncate(false)
            .write(true)
            .open(self.path.with_extension("lock"))?;
        file.lock()?;
        Ok(KeyStoreLock(file))
    }

    /// The stored key of the kind `billing` of `account` for this console
    /// and project, when it has enough left: [`MIN_KEY_SECONDS_LEFT`], or for
    /// a subscription key [`MIN_SUBSCRIPTION_KEY_SECONDS_LEFT`]. Of several,
    /// the one that lasts longest.
    pub fn usable(
        &self,
        console_url: &str,
        project_id: &str,
        account: &str,
        billing: Billing,
        now: i64,
    ) -> Option<IssuedKey> {
        self.load()
            .into_iter()
            .filter(|k| k.is_for(console_url, project_id, account, billing))
            .filter(|k| k.lasts(now))
            .max_by_key(|k| k.expires_at)
            .map(|k| IssuedKey {
                secret: k.secret,
                expires_at: k.expires_at,
                id: k.id,
            })
    }

    /// The stored key of the kind `billing` of `account` for this console
    /// and project that lasts longest, if any has not expired and was not
    /// refused, however little it has left: what a check tries, changing
    /// nothing. (A launch replaces one inside its renewal window; see
    /// [`Self::usable`].)
    pub fn unexpired(
        &self,
        console_url: &str,
        project_id: &str,
        account: &str,
        billing: Billing,
        now: i64,
    ) -> Option<IssuedKey> {
        self.load()
            .into_iter()
            .filter(|k| k.is_for(console_url, project_id, account, billing))
            .filter(|k| !k.refused && k.expires_at > now)
            .max_by_key(|k| k.expires_at)
            .map(|k| IssuedKey {
                secret: k.secret,
                expires_at: k.expires_at,
                id: k.id,
            })
    }

    /// Keep `key` of the kind `billing`, dropping keys that have expired. An
    /// ordinary key replaces the one for the same console and project
    /// (whoever's it was: this machine's user is now `account`), as issuing
    /// it revoked that one. A subscription key revoked nothing: `account`'s
    /// earlier ones stay until they expire, for logout to revoke; another
    /// account's go. The file is the user's alone.
    pub fn keep(
        &self,
        console_url: &str,
        project_id: &str,
        account: &str,
        billing: Billing,
        key: &IssuedKey,
        now: i64,
    ) -> Result<(), String> {
        let replaced = |k: &StoredKey| {
            k.console_url == base(console_url)
                && k.project_id == project_id
                && k.billing == billing
                && (billing == Billing::Project || k.account != account)
        };
        let mut keys: Vec<StoredKey> = self
            .load()
            .into_iter()
            .filter(|k| k.expires_at > now)
            .filter(|k| !replaced(k))
            .collect();
        keys.push(StoredKey {
            console_url: base(console_url).to_string(),
            project_id: project_id.to_string(),
            account: account.to_string(),
            secret: key.secret.clone(),
            expires_at: key.expires_at,
            billing,
            id: key.id.clone(),
            issued_at: now,
            refused: false,
        });
        let text = serde_json::to_string(&keys).map_err(|e| e.to_string())?;
        write_private(&self.path, &text)
            .map_err(|e| format!("cannot write {}: {e}", self.path.display()))
    }

    /// Mark the kept key `secret` as refused by the gateway: it is not handed
    /// out again, but stays for logout to revoke. Call under [`Self::lock`].
    pub fn refuse(&self, secret: &str) -> Result<(), String> {
        let mut keys = self.load();
        let Some(key) = keys.iter_mut().find(|k| k.secret == secret) else {
            return Ok(());
        };
        key.refused = true;
        let text = serde_json::to_string(&keys).map_err(|e| e.to_string())?;
        write_private(&self.path, &text)
            .map_err(|e| format!("cannot write {}: {e}", self.path.display()))
    }

    /// Every kept key that has not expired at `now`.
    pub fn kept(&self, now: i64) -> Vec<KeptKey> {
        self.load()
            .into_iter()
            .filter(|k| k.expires_at > now)
            .map(|k| KeptKey {
                console_url: k.console_url,
                account: k.account,
                id: k.id,
                billing: k.billing,
                expires_at: k.expires_at,
            })
            .collect()
    }

    /// Forget every kept key (on logout, after [`revoke_kept`]).
    pub fn clear(&self) -> std::io::Result<()> {
        match std::fs::remove_file(&self.path) {
            Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e),
            _ => Ok(()),
        }
    }
}

/// Write `text` to `path` so that it is never readable by anyone else: a new
/// file created with the user's permissions only, then moved into place.
fn write_private(path: &Path, text: &str) -> std::io::Result<()> {
    use std::io::Write;
    let dir = path.parent().unwrap_or(Path::new("."));
    std::fs::create_dir_all(dir)?;
    let tmp = path.with_extension(format!("tmp{}", std::process::id()));
    let _ = std::fs::remove_file(&tmp);
    let mut options = std::fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    let written = options
        .open(&tmp)
        .and_then(|mut file| file.write_all(text.as_bytes()));
    match written.and_then(|()| std::fs::rename(&tmp, path)) {
        Ok(()) => Ok(()),
        Err(e) => {
            let _ = std::fs::remove_file(&tmp);
            Err(e)
        }
    }
}

/// A key from [`ensure_key`].
pub struct EnsuredKey {
    pub key: IssuedKey,
    /// Issued just now, rather than found in the store.
    pub issued: bool,
}

/// The developer's key for `project_id`: the stored one while it lasts, else a
/// new one, which is then stored (a key that cannot be stored is an error). `rejected` is a key the gateway no longer
/// takes (revoked by a newer one issued elsewhere): the stored key is used
/// only if it is another one.
///
/// Looking, issuing and storing happen under the store's lock, so sessions
/// starting side by side end up sharing one key instead of revoking each
/// other's.
#[allow(clippy::too_many_arguments)]
pub async fn ensure_key(
    http: &reqwest::Client,
    console_url: &str,
    token: &str,
    project_id: &str,
    billing: Billing,
    store: &KeyStore,
    now: i64,
    rejected: Option<&str>,
) -> Result<EnsuredKey, String> {
    let account = account_of(token);
    // Waiting for another process must not hold up this runtime's thread.
    let locking = store.clone();
    let lock = tokio::task::spawn_blocking(move || locking.lock())
        .await
        .map_err(|e| e.to_string())
        .and_then(|locked| locked.map_err(|e| e.to_string()));
    // A key the gateway refused is not offered again by any later session
    // (a subscription key stays kept, and may outlast its replacement).
    if let (Some(secret), Ok(_)) = (rejected, &lock) {
        let _ = store.refuse(secret);
    }
    let stored = store
        .usable(console_url, project_id, &account, billing, now)
        .filter(|key| Some(key.secret.as_str()) != rejected);
    if let Some(key) = stored {
        return Ok(EnsuredKey { key, issued: false });
    }
    // A kept key is read without the lock just as well. A new one is not
    // issued without it: issuing revokes the key another session, issuing at
    // the same moment, is about to use.
    let _lock = lock.map_err(|why| {
        format!(
            "the personal AI Gateway key store {} could not be locked: {why}",
            store.path.display()
        )
    })?;
    let key = issue_key(http, console_url, token, project_id, billing, now).await?;
    // A key that is not kept is issued again by the next session, and by the
    // agent's own key helper, each time revoking the one in use: that is no
    // working gateway session.
    store
        .keep(console_url, project_id, &account, billing, &key, now)
        .map_err(|why| format!("the personal AI Gateway key could not be kept: {why}"))?;
    Ok(EnsuredKey { key, issued: true })
}

/// Revoke at their consoles the kept keys of the person whose access token
/// is `token` (on `visdom logout`, before the store is cleared). What could
/// not be revoked is returned as warnings; such a key stays valid at the
/// gateway until it expires. Keys of another account are not this token's
/// to revoke.
pub async fn revoke_kept(
    http: &reqwest::Client,
    store: &KeyStore,
    token: &str,
    now: i64,
) -> Vec<String> {
    let account = account_of(token);
    let mut warnings = Vec::new();
    for key in store.kept(now).into_iter().filter(|k| k.account == account) {
        let Some(id) = &key.id else { continue };
        if let Err(why) = revoke_key(http, &key.console_url, token, id).await {
            warnings.push(format!(
                "{why}; the {} key stays valid until it expires (revoke it in the AI Gateway \
                 console under My API key)",
                match key.billing {
                    Billing::Project => "personal",
                    Billing::Subscription => "subscription",
                }
            ));
        }
    }
    warnings
}

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

    fn project(id: &str, slug: &str) -> OpenProject {
        OpenProject {
            id: id.to_string(),
            slug: slug.to_string(),
            name: slug.to_uppercase(),
            models: None,
        }
    }

    #[tokio::test]
    async fn the_llm_address_comes_from_the_consoles_settings() {
        let http = reqwest::Client::new();
        let server = MockServer::start(&[(
            "GET /api/settings",
            200,
            r#"{"llmGatewayUrl":"https://llm.example.com/","mcpGatewayUrl":"https://mcp.example.com","suite":[],"controlPlaneUrl":null}"#,
        )]);
        assert_eq!(
            console_settings(&http, &format!("{}/", server.url), "tok").await,
            Ok(ConsoleSettings {
                llm_url: "https://llm.example.com".to_string(),
                // A console from before the setting: not supported.
                subscription_traffic: SubscriptionTraffic::Unpublished,
            })
        );
        assert_eq!(
            server.requests()[0].authorization.as_deref(),
            Some("Bearer tok")
        );
    }

    #[tokio::test]
    async fn the_settings_say_whether_subscription_traffic_is_taken() {
        let http = reqwest::Client::new();
        for (said, read) in [
            (r#""allowed""#, SubscriptionTraffic::Allowed),
            (r#""denied""#, SubscriptionTraffic::Denied),
            (r#""maybe""#, SubscriptionTraffic::Unpublished),
            ("null", SubscriptionTraffic::Unpublished),
            ("true", SubscriptionTraffic::Unpublished),
        ] {
            let body = format!(
                r#"{{"llmGatewayUrl":"https://llm.example.com","subscriptionTraffic":{said}}}"#
            );
            let server = MockServer::start(&[("GET /api/settings", 200, &body)]);
            let settings = console_settings(&http, &server.url, "tok").await.unwrap();
            assert_eq!(settings.subscription_traffic, read, "{said}");
        }
        assert!(SubscriptionTraffic::Allowed.supported());
        assert!(!SubscriptionTraffic::Denied.supported());
        assert!(!SubscriptionTraffic::Unpublished.supported());
    }

    #[tokio::test]
    async fn a_projects_models_say_which_take_a_subscription() {
        let http = reqwest::Client::new();
        let server = MockServer::start(&[(
            "GET /api/me/keys",
            200,
            r#"{"projects":[{"id":"p-1","slug":"alpha","name":"Alpha","models":[{"alias":"claude-sonnet","subscription":true},{"alias":"gpt-general","subscription":false}]}],"keys":[]}"#,
        )]);
        let open = open_projects(&http, &server.url, "tok").await.unwrap();
        assert_eq!(
            open[0].subscription_aliases(),
            Some(vec!["claude-sonnet".to_string()])
        );
        assert_eq!(project("p-2", "beta").subscription_aliases(), None);
    }

    #[tokio::test]
    async fn a_denied_subscription_key_is_the_platforms_message() {
        let http = reqwest::Client::new();
        let server = MockServer::start(&[(
            "POST /api/me/keys",
            403,
            r#"{"error":"This gateway does not take subscription traffic (VISDOM_SUBSCRIPTION_TRAFFIC=deny), so it issues no subscription keys; use an ordinary personal key","code":"subscription_traffic_denied"}"#,
        )]);
        let err = issue_key(&http, &server.url, "tok", "p-1", Billing::Subscription, 0)
            .await
            .err()
            .unwrap();
        assert_eq!(err, crate::subscription::NOT_SUPPORTED);
        let server = MockServer::start(&[(
            "GET /v1/models",
            403,
            r#"{"error":{"message":"no","type":"permission_error","code":"subscription_traffic_denied"}}"#,
        )]);
        let err = models(&http, &server.url, "sk").await.unwrap_err();
        assert_eq!(err.why, crate::subscription::NOT_SUPPORTED);
        assert!(!err.rejected, "another key would not help");
    }

    #[tokio::test]
    async fn open_projects_are_listed_with_the_persons_token() {
        let http = reqwest::Client::new();
        let server = MockServer::start(&[(
            "GET /api/me/keys",
            200,
            r#"{"hours":24,"projects":[{"id":"p-1","slug":"alpha","name":"Alpha","budget":null}],"keys":[]}"#,
        )]);
        assert_eq!(
            open_projects(&http, &server.url, "tok").await,
            Ok(vec![OpenProject {
                id: "p-1".to_string(),
                slug: "alpha".to_string(),
                name: "Alpha".to_string(),
                models: None,
            }])
        );
    }

    #[tokio::test]
    async fn a_key_is_issued_for_the_project() {
        let http = reqwest::Client::new();
        let server = MockServer::start(&[(
            "POST /api/me/keys",
            201,
            r#"{"secret":"sk-abc","id":"hash","name":"personal-x-1","expiresAt":"2026-11-20T10:00:00.250000+00:00"}"#,
        )]);
        let key = issue_key(&http, &server.url, "tok", "p-1", Billing::Project, 0)
            .await
            .unwrap();
        assert_eq!(key.secret, "sk-abc");
        assert_eq!(
            key.expires_at,
            chrono::DateTime::parse_from_rfc3339("2026-11-20T10:00:00Z")
                .unwrap()
                .timestamp()
        );
        let asked = &server.requests()[0];
        assert_eq!(asked.authorization.as_deref(), Some("Bearer tok"));
        assert_eq!(asked.body, r#"{"projectId":"p-1"}"#);
    }

    #[tokio::test]
    async fn the_consoles_refusal_is_passed_on_in_its_words() {
        let http = reqwest::Client::new();
        let server = MockServer::start(&[
            (
                "POST /api/me/keys",
                400,
                r#"{"error":"Alpha does not allow personal keys; ask an operator to switch them on (Budgets)"}"#,
            ),
            ("GET /api/me/keys", 401, ""),
        ]);
        let err = issue_key(&http, &server.url, "tok", "p-1", Billing::Project, 0)
            .await
            .err()
            .unwrap();
        assert!(err.contains("does not allow personal keys"), "{err}");
        assert!(err.contains("HTTP 400"), "{err}");
        let err = open_projects(&http, &server.url, "tok").await.unwrap_err();
        assert!(err.contains("did not accept this login"), "{err}");
        let err = open_projects(&http, "http://127.0.0.1:1", "tok")
            .await
            .unwrap_err();
        assert!(err.contains("did not answer"), "{err}");
    }

    #[test]
    fn expiry_is_read_in_the_forms_the_gateway_uses() {
        let at = chrono::DateTime::parse_from_rfc3339("2026-11-20T10:00:00Z")
            .unwrap()
            .timestamp();
        assert_eq!(expiry_seconds(Some("2026-11-20T10:00:00Z"), 0), at);
        assert_eq!(expiry_seconds(Some("2026-11-20T11:00:00+01:00"), 0), at);
        assert_eq!(expiry_seconds(Some("2026-11-20T10:00:00.123456"), 0), at);
        // Not stated, or not understood: a short assumed life.
        assert_eq!(expiry_seconds(None, 1000), 1000 + UNKNOWN_EXPIRY_SECONDS);
        assert_eq!(
            expiry_seconds(Some("tomorrow"), 1000),
            1000 + UNKNOWN_EXPIRY_SECONDS
        );
    }

    #[tokio::test]
    async fn models_are_the_aliases_the_key_may_call() {
        let http = reqwest::Client::new();
        let server = MockServer::start(&[(
            "GET /v1/models",
            200,
            r#"{"object":"list","data":[{"id":"gpt-general","object":"model"},{"id":"claude-sonnet","object":"model"}]}"#,
        )]);
        assert_eq!(
            models(&http, &server.url, "sk-abc").await,
            Ok(vec!["gpt-general".to_string(), "claude-sonnet".to_string()])
        );
        assert_eq!(
            server.requests()[0].authorization.as_deref(),
            Some("Bearer sk-abc")
        );
    }

    #[test]
    fn the_project_is_the_configured_one_or_the_only_one() {
        let one = [project("p-1", "alpha")];
        let two = [project("p-1", "alpha"), project("p-2", "beta")];
        assert_eq!(pick_project(None, &one).unwrap().id, "p-1");
        assert_eq!(pick_project(Some(" beta "), &two).unwrap().id, "p-2");
        assert_eq!(pick_project(Some("p-2"), &two).unwrap().slug, "beta");
        assert_eq!(pick_project(Some("BETA"), &two).unwrap().id, "p-2");
        let err = pick_project(None, &two).unwrap_err();
        assert!(
            err.contains("alpha, beta") && err.contains("`project` under `[gateway]`"),
            "{err}"
        );
        let err = pick_project(Some("gamma"), &two).unwrap_err();
        assert!(
            err.contains("`gamma`") && err.contains("alpha, beta"),
            "{err}"
        );
        let err = pick_project(Some("alpha"), &[]).unwrap_err();
        assert!(err.contains("personal keys on"), "{err}");
    }

    #[test]
    fn the_model_is_the_configured_one_or_one_named_for_the_agent() {
        let aliases: Vec<String> = ["demo-assistant", "gpt-general", "claude-sonnet"]
            .map(String::from)
            .to_vec();
        assert_eq!(
            pick_model(Agent::ClaudeCode, None, &aliases).as_deref(),
            Some("claude-sonnet")
        );
        assert_eq!(
            pick_model(Agent::Codex, None, &aliases).as_deref(),
            Some("gpt-general")
        );
        assert_eq!(
            pick_model(Agent::Codex, Some(" local-coder "), &aliases).as_deref(),
            Some("local-coder")
        );
        assert_eq!(
            pick_model(Agent::ClaudeCode, None, &["local-coder".to_string()]),
            None
        );
    }

    #[test]
    fn a_stored_key_is_reused_until_shortly_before_it_expires() {
        let tmp = tempfile::tempdir().unwrap();
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        let key = IssuedKey {
            secret: "sk-abc".to_string(),
            expires_at: 10_000,
            id: None,
        };
        assert!(store
            .usable("https://gw.example.com", "p-1", "", Billing::Project, 0)
            .is_none());
        store
            .keep(
                "https://gw.example.com/",
                "p-1",
                "",
                Billing::Project,
                &key,
                0,
            )
            .unwrap();
        let again = store
            .usable("https://gw.example.com", "p-1", "", Billing::Project, 0)
            .unwrap();
        assert_eq!(again.secret, "sk-abc");
        // Another project or console has its own key.
        assert!(store
            .usable("https://gw.example.com", "p-2", "", Billing::Project, 0)
            .is_none());
        assert!(store
            .usable("https://other.example.com", "p-1", "", Billing::Project, 0)
            .is_none());
        // Close to expiry it is no longer handed out.
        let late = 10_000 - MIN_KEY_SECONDS_LEFT + 1;
        assert!(store
            .usable("https://gw.example.com", "p-1", "", Billing::Project, late)
            .is_none());
        assert!(store
            .usable(
                "https://gw.example.com",
                "p-1",
                "",
                Billing::Project,
                late - 1
            )
            .is_some());
    }

    #[test]
    fn keeping_a_key_replaces_the_old_one_and_drops_expired_ones() {
        let tmp = tempfile::tempdir().unwrap();
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        let key = |secret: &str, expires_at| IssuedKey {
            secret: secret.to_string(),
            expires_at,
            id: None,
        };
        store
            .keep(
                "https://gw",
                "p-1",
                "",
                Billing::Project,
                &key("old", 5_000),
                0,
            )
            .unwrap();
        store
            .keep(
                "https://gw",
                "p-2",
                "",
                Billing::Project,
                &key("other", 9_000),
                0,
            )
            .unwrap();
        store
            .keep(
                "https://gw",
                "p-1",
                "",
                Billing::Project,
                &key("new", 90_000),
                6_000,
            )
            .unwrap();
        assert_eq!(
            store
                .usable("https://gw", "p-1", "", Billing::Project, 6_000)
                .unwrap()
                .secret,
            "new"
        );
        assert_eq!(
            store
                .usable("https://gw", "p-2", "", Billing::Project, 6_000)
                .unwrap()
                .secret,
            "other"
        );
        store
            .keep(
                "https://gw",
                "p-3",
                "",
                Billing::Project,
                &key("third", 99_000),
                9_500,
            )
            .unwrap();
        assert_eq!(store.load().len(), 2, "the expired p-2 key is gone");
    }

    #[cfg(unix)]
    #[test]
    fn the_key_file_is_the_users_alone() {
        use std::os::unix::fs::PermissionsExt;
        let tmp = tempfile::tempdir().unwrap();
        let path = key_store_path_in(tmp.path());
        let store = KeyStore::at(path.clone());
        let key = IssuedKey {
            secret: "sk-abc".to_string(),
            expires_at: 10_000,
            id: None,
        };
        store
            .keep("https://gw", "p-1", "", Billing::Project, &key, 0)
            .unwrap();
        store
            .keep("https://gw", "p-2", "", Billing::Project, &key, 0)
            .unwrap();
        let mode = std::fs::metadata(&path).unwrap().permissions().mode();
        assert_eq!(mode & 0o777, 0o600);
    }

    #[tokio::test]
    async fn a_key_is_issued_once_and_then_reused() {
        let http = reqwest::Client::new();
        let tmp = tempfile::tempdir().unwrap();
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        let server = MockServer::start(&[(
            "POST /api/me/keys",
            201,
            r#"{"secret":"sk-abc","id":"h","name":"n","expiresAt":"2026-11-20T10:00:00Z"}"#,
        )]);
        let now = chrono::DateTime::parse_from_rfc3339("2026-11-19T10:00:00Z")
            .unwrap()
            .timestamp();
        let first = ensure_key(
            &http,
            &server.url,
            "tok",
            "p-1",
            Billing::Project,
            &store,
            now,
            None,
        )
        .await
        .unwrap();
        assert_eq!((first.key.secret.as_str(), first.issued), ("sk-abc", true));
        let second = ensure_key(
            &http,
            &server.url,
            "tok",
            "p-1",
            Billing::Project,
            &store,
            now + 3600,
            None,
        )
        .await
        .unwrap();
        assert_eq!(
            (second.key.secret.as_str(), second.issued),
            ("sk-abc", false)
        );
        assert_eq!(server.routes(), ["POST /api/me/keys"], "issued once");
        // A day later it has expired: a new one is issued.
        ensure_key(
            &http,
            &server.url,
            "tok",
            "p-1",
            Billing::Project,
            &store,
            now + 86_400,
            None,
        )
        .await
        .unwrap();
        assert_eq!(server.routes().len(), 2);
    }

    /// A JWT as Keycloak issues one, unsigned: only its claims are read here.
    fn token_of(iss: &str, sub: &str) -> String {
        use base64::Engine;
        let part = |json: String| base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(json);
        format!(
            "{}.{}.signature",
            part(r#"{"alg":"RS256"}"#.to_string()),
            part(format!(r#"{{"iss":"{iss}","sub":"{sub}","exp":1}}"#))
        )
    }

    #[test]
    fn the_account_is_the_tokens_issuer_and_subject() {
        let alice = token_of("https://idp/realms/visdom", "alice-id");
        assert_eq!(account_of(&alice), "https://idp/realms/visdom|alice-id");
        assert_ne!(
            account_of(&alice),
            account_of(&token_of("https://idp/realms/visdom", "bob-id"))
        );
        assert_ne!(
            account_of(&alice),
            account_of(&token_of("https://idp/realms/other", "alice-id"))
        );
        // Not a JWT, or one without a subject: no account.
        for opaque in ["person-access-token", "a.b.c", "a.b", "", "a.b.c.d"] {
            assert_eq!(account_of(opaque), "", "{opaque}");
        }
        assert_eq!(account_of(&token_of("https://idp", "")), "");
    }

    #[tokio::test]
    async fn a_kept_key_is_not_handed_to_another_account() {
        let http = reqwest::Client::new();
        let tmp = tempfile::tempdir().unwrap();
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        let server = MockServer::start(&[
            (
                "POST /api/me/keys",
                201,
                r#"{"secret":"sk-alice","id":"h","name":"n","expiresAt":"2099-01-01T00:00:00Z"}"#,
            ),
            (
                "POST /api/me/keys",
                201,
                r#"{"secret":"sk-bob","id":"h","name":"n","expiresAt":"2099-01-01T00:00:00Z"}"#,
            ),
        ]);
        let alice = token_of("https://idp", "alice-id");
        let bob = token_of("https://idp", "bob-id");
        let first = ensure_key(
            &http,
            &server.url,
            &alice,
            "p-1",
            Billing::Project,
            &store,
            0,
            None,
        )
        .await
        .unwrap();
        assert_eq!(first.key.secret, "sk-alice");
        // Bob logs in on the same machine: Alice's key is not his.
        let second = ensure_key(
            &http,
            &server.url,
            &bob,
            "p-1",
            Billing::Project,
            &store,
            0,
            None,
        )
        .await
        .unwrap();
        assert_eq!(
            (second.key.secret.as_str(), second.issued),
            ("sk-bob", true)
        );
        let asked = server.requests();
        assert_eq!(asked.len(), 2);
        assert_eq!(asked[1].authorization, Some(format!("Bearer {bob}")));
        // And Alice's is gone from the file, not kept beside his.
        assert!(store
            .usable(&server.url, "p-1", &account_of(&alice), Billing::Project, 0)
            .is_none());
        assert_eq!(store.load().len(), 1);
    }

    #[tokio::test]
    async fn a_rejected_key_is_replaced_unless_somebody_already_did() {
        let http = reqwest::Client::new();
        let tmp = tempfile::tempdir().unwrap();
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        let server = MockServer::start(&[(
            "POST /api/me/keys",
            201,
            r#"{"secret":"sk-new","id":"h","name":"n","expiresAt":"2099-01-01T00:00:00Z"}"#,
        )]);
        let old = IssuedKey {
            secret: "sk-old".to_string(),
            expires_at: 4_000_000_000,
            id: None,
        };
        store
            .keep(&server.url, "p-1", "", Billing::Project, &old, 0)
            .unwrap();
        // The gateway refused sk-old: the stored copy is not offered again.
        let renewed = ensure_key(
            &http,
            &server.url,
            "tok",
            "p-1",
            Billing::Project,
            &store,
            0,
            Some("sk-old"),
        )
        .await
        .unwrap();
        assert_eq!(
            (renewed.key.secret.as_str(), renewed.issued),
            ("sk-new", true)
        );
        // Another session that also saw sk-old refused finds the new one.
        let other = ensure_key(
            &http,
            &server.url,
            "tok",
            "p-1",
            Billing::Project,
            &store,
            0,
            Some("sk-old"),
        )
        .await
        .unwrap();
        assert_eq!((other.key.secret.as_str(), other.issued), ("sk-new", false));
        assert_eq!(server.routes().len(), 1);
    }

    /// Issuing revokes the previous key, so sessions starting together must
    /// end up with one key: the later ones wait and find the first one's.
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn sessions_starting_together_issue_one_key() {
        let tmp = tempfile::tempdir().unwrap();
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        let server = MockServer::start(&[
            (
                "POST /api/me/keys",
                201,
                r#"{"secret":"sk-first","id":"h","name":"n","expiresAt":"2099-01-01T00:00:00Z"}"#,
            ),
            (
                "POST /api/me/keys",
                201,
                r#"{"secret":"sk-second","id":"h","name":"n","expiresAt":"2099-01-01T00:00:00Z"}"#,
            ),
        ]);
        let starts = (0..6).map(|_| {
            let (store, url) = (store.clone(), server.url.clone());
            tokio::spawn(async move {
                let http = reqwest::Client::new();
                ensure_key(&http, &url, "tok", "p-1", Billing::Project, &store, 0, None)
                    .await
                    .unwrap()
                    .key
                    .secret
            })
        });
        let mut secrets = Vec::new();
        for start in starts.collect::<Vec<_>>() {
            secrets.push(start.await.unwrap());
        }
        assert_eq!(secrets, ["sk-first"; 6]);
        assert_eq!(server.routes(), ["POST /api/me/keys"]);
    }

    #[tokio::test]
    async fn a_gateway_that_refuses_the_key_is_told_from_one_that_is_away() {
        let http = reqwest::Client::new();
        let server = MockServer::start(&[
            ("GET /v1/models", 401, r#"{"error":"bad key"}"#),
            ("GET /v1/models", 403, ""),
            ("GET /v1/models", 500, ""),
            ("GET /v1/models", 200, "not json"),
        ]);
        for (rejected, said) in [
            (true, "did not accept the personal key (HTTP 401)"),
            (true, "HTTP 403"),
            (false, "HTTP 500"),
            (false, "unreadable answer"),
        ] {
            let err = models(&http, &server.url, "sk").await.unwrap_err();
            assert_eq!(err.rejected, rejected, "{}", err.why);
            assert!(err.why.contains(said), "{}", err.why);
        }
        let err = models(&http, "http://127.0.0.1:1", "sk").await.unwrap_err();
        assert!(
            !err.rejected && err.why.contains("did not answer"),
            "{}",
            err.why
        );
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn a_key_that_cannot_be_kept_is_an_error() {
        let http = reqwest::Client::new();
        let tmp = tempfile::tempdir().unwrap();
        // The store's file cannot be replaced: a directory is in its place.
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        std::fs::create_dir_all(&store.path).unwrap();
        let server = MockServer::start(&[(
            "POST /api/me/keys",
            201,
            r#"{"secret":"sk-abc","id":"h","name":"n","expiresAt":"2099-01-01T00:00:00Z"}"#,
        )]);
        let err = ensure_key(
            &http,
            &server.url,
            "tok",
            "p-1",
            Billing::Project,
            &store,
            0,
            None,
        )
        .await
        .err()
        .unwrap();
        assert!(err.contains("could not be kept"), "{err}");
        assert!(!err.contains("sk-abc"), "{err}");
    }

    /// Issuing revokes the key in use, so it is not done without the lock
    /// that keeps two sessions from doing it at once; a kept key needs none.
    #[tokio::test]
    async fn no_key_is_issued_while_the_store_cannot_be_locked() {
        let http = reqwest::Client::new();
        let tmp = tempfile::tempdir().unwrap();
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        // The lock file cannot be opened: a directory is in its place.
        std::fs::create_dir_all(store.path.with_extension("lock")).unwrap();
        let server = MockServer::start(&[(
            "POST /api/me/keys",
            201,
            r#"{"secret":"sk-abc","id":"h","name":"n","expiresAt":"2099-01-01T00:00:00Z"}"#,
        )]);
        let err = ensure_key(
            &http,
            &server.url,
            "tok",
            "p-1",
            Billing::Project,
            &store,
            0,
            None,
        )
        .await
        .err()
        .unwrap();
        assert!(err.contains("could not be locked"), "{err}");
        assert!(server.routes().is_empty(), "{:?}", server.routes());

        let kept = IssuedKey {
            secret: "sk-kept".to_string(),
            expires_at: 1_000_000,
            id: None,
        };
        let account = account_of("tok");
        store
            .keep(&server.url, "p-1", &account, Billing::Project, &kept, 0)
            .unwrap();
        let ensured = ensure_key(
            &http,
            &server.url,
            "tok",
            "p-1",
            Billing::Project,
            &store,
            0,
            None,
        )
        .await
        .unwrap();
        assert_eq!(ensured.key.secret, "sk-kept");
        assert!(!ensured.issued);
        assert!(server.routes().is_empty(), "{:?}", server.routes());
    }

    #[test]
    fn clearing_the_store_forgets_every_key() {
        let tmp = tempfile::tempdir().unwrap();
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        assert!(store.clear().is_ok(), "nothing to forget is fine");
        let key = IssuedKey {
            secret: "sk-abc".to_string(),
            expires_at: 10_000,
            id: None,
        };
        store
            .keep("https://gw", "p-1", "", Billing::Project, &key, 0)
            .unwrap();
        store.clear().unwrap();
        assert!(store
            .usable("https://gw", "p-1", "", Billing::Project, 0)
            .is_none());
    }

    const DAY: i64 = 24 * 60 * 60;

    fn subscription_answer(secret: &str, id: &str, expires_at: &str) -> String {
        format!(
            r#"{{"secret":"{secret}","id":"{id}","name":"subscription-dev-1","expiresAt":"{expires_at}","billing":"subscription"}}"#
        )
    }

    #[tokio::test]
    async fn a_subscription_key_is_asked_for_by_its_kind() {
        let http = reqwest::Client::new();
        let answer = subscription_answer("sk-sub", "hash-1", "2026-12-26T10:00:00Z");
        let server = MockServer::start(&[("POST /api/me/keys", 201, &answer)]);
        let key = issue_key(&http, &server.url, "tok", "p-1", Billing::Subscription, 0)
            .await
            .unwrap();
        assert_eq!(key.secret, "sk-sub");
        assert_eq!(key.id.as_deref(), Some("hash-1"));
        let asked = &server.requests()[0];
        assert_eq!(
            asked.body,
            r#"{"projectId":"p-1","billing":"subscription"}"#
        );
        assert_eq!(asked.authorization.as_deref(), Some("Bearer tok"));
    }

    /// A console from before subscription keys ignores `billing` and issues
    /// an ordinary key, which would let the project pay: it is revoked again,
    /// and the answer is an error.
    #[tokio::test]
    async fn an_ordinary_key_given_for_a_subscription_is_given_back() {
        let http = reqwest::Client::new();
        let server = MockServer::start(&[
            (
                "POST /api/me/keys",
                201,
                r#"{"secret":"sk-ordinary","id":"hash-2","name":"personal-dev-1","expiresAt":"2099-01-01T00:00:00Z"}"#,
            ),
            ("DELETE /api/me/keys/hash-2", 204, ""),
        ]);
        let err = issue_key(&http, &server.url, "tok", "p-1", Billing::Subscription, 0)
            .await
            .err()
            .unwrap();
        assert!(
            err.contains("does not support billing = subscription"),
            "{err}"
        );
        assert!(!err.contains("sk-ordinary"), "{err}");
        assert_eq!(
            server.routes(),
            ["POST /api/me/keys", "DELETE /api/me/keys/hash-2"]
        );
    }

    #[test]
    fn a_subscription_key_is_renewed_a_week_before_it_expires() {
        let tmp = tempfile::tempdir().unwrap();
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        let key = |secret: &str, expires_at| IssuedKey {
            secret: secret.to_string(),
            expires_at,
            id: Some(format!("id-{secret}")),
        };
        let issued = 1_000_000;
        let expires = issued + 30 * DAY;
        let sub = Billing::Subscription;
        store
            .keep(
                "https://gw",
                "p-1",
                "a",
                sub,
                &key("first", expires),
                issued,
            )
            .unwrap();
        // Kinds are kept apart.
        assert!(store
            .usable("https://gw", "p-1", "a", Billing::Project, issued)
            .is_none());
        let week = MIN_SUBSCRIPTION_KEY_SECONDS_LEFT;
        assert_eq!(
            store
                .usable("https://gw", "p-1", "a", sub, expires - week)
                .unwrap()
                .secret,
            "first"
        );
        assert!(store
            .usable("https://gw", "p-1", "a", sub, expires - week + 1)
            .is_none());
        // A check still finds it, until it expires or is refused.
        assert_eq!(
            store
                .unexpired("https://gw", "p-1", "a", sub, expires - 1)
                .unwrap()
                .secret,
            "first"
        );
        assert!(store
            .unexpired("https://gw", "p-1", "a", sub, expires)
            .is_none());
        // The next one does not replace it in the store: the first stays
        // until it expires, for logout to revoke, and the new one is used.
        let now = expires - week + 1;
        store
            .keep(
                "https://gw",
                "p-1",
                "a",
                sub,
                &key("second", now + 30 * DAY),
                now,
            )
            .unwrap();
        assert_eq!(
            store
                .usable("https://gw", "p-1", "a", sub, now)
                .unwrap()
                .secret,
            "second"
        );
        let kept = store.kept(now);
        assert_eq!(kept.len(), 2, "{kept:?}");
        assert!(kept.iter().all(|k| k.billing == sub));
        // An ordinary key for the same project does not touch them.
        store
            .keep(
                "https://gw",
                "p-1",
                "a",
                Billing::Project,
                &key("plain", now + DAY),
                now,
            )
            .unwrap();
        assert_eq!(store.kept(now).len(), 3);
        // Once expired, it is dropped with the next write.
        store
            .keep(
                "https://gw",
                "p-2",
                "a",
                sub,
                &key("other", expires + 60 * DAY),
                expires + 1,
            )
            .unwrap();
        let left: Vec<_> = store.kept(expires + 1);
        assert_eq!(left.len(), 2, "{left:?}");
        // Another account's subscription keys go when this one keeps its own.
        store
            .keep(
                "https://gw",
                "p-2",
                "b",
                sub,
                &key("bobs", expires + 60 * DAY),
                expires + 2,
            )
            .unwrap();
        assert!(store
            .usable("https://gw", "p-2", "a", sub, expires + 2)
            .is_none());
    }

    #[test]
    fn a_short_lived_subscription_key_is_renewed_at_half_its_life() {
        assert_eq!(
            renew_before(Billing::Subscription, 1000, 1000 + 30 * DAY),
            MIN_SUBSCRIPTION_KEY_SECONDS_LEFT
        );
        assert_eq!(
            renew_before(Billing::Subscription, 1000, 1000 + 4 * DAY),
            2 * DAY
        );
        assert_eq!(
            renew_before(Billing::Subscription, 0, 30 * DAY),
            MIN_SUBSCRIPTION_KEY_SECONDS_LEFT,
            "issue time unknown"
        );
        assert_eq!(renew_before(Billing::Project, 0, DAY), MIN_KEY_SECONDS_LEFT);
    }

    #[tokio::test]
    async fn a_subscription_key_is_issued_once_and_shared() {
        let http = reqwest::Client::new();
        let tmp = tempfile::tempdir().unwrap();
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        let answer = subscription_answer("sk-sub", "h", "2099-01-01T00:00:00Z");
        let server = MockServer::start(&[("POST /api/me/keys", 201, &answer)]);
        let sub = Billing::Subscription;
        for issued in [true, false] {
            let ensured = ensure_key(&http, &server.url, "tok", "p-1", sub, &store, 0, None)
                .await
                .unwrap();
            assert_eq!(
                (ensured.key.secret.as_str(), ensured.issued),
                ("sk-sub", issued)
            );
        }
        assert_eq!(server.routes(), ["POST /api/me/keys"]);
        // An ordinary key for the same project is its own.
        assert!(store
            .usable(&server.url, "p-1", "", Billing::Project, 0)
            .is_none());
    }

    #[tokio::test]
    async fn logout_revokes_the_kept_keys_of_the_person_at_their_console() {
        let http = reqwest::Client::new();
        let tmp = tempfile::tempdir().unwrap();
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        let server = MockServer::start(&[
            ("DELETE /api/me/keys/sub-1", 204, ""),
            (
                "DELETE /api/me/keys/sub%2F2",
                400,
                r#"{"error":"No such personal key of yours"}"#,
            ),
            ("DELETE /api/me/keys/plain-1", 500, ""),
        ]);
        let alice = token_of("https://idp", "alice-id");
        let account = account_of(&alice);
        let key = |id: Option<&str>, expires_at| IssuedKey {
            secret: "sk".to_string(),
            expires_at,
            id: id.map(String::from),
        };
        let sub = Billing::Subscription;
        store
            .keep(
                &server.url,
                "p-1",
                &account,
                sub,
                &key(Some("sub-1"), 100),
                0,
            )
            .unwrap();
        store
            .keep(
                &server.url,
                "p-1",
                &account,
                sub,
                &key(Some("sub/2"), 200),
                0,
            )
            .unwrap();
        store
            .keep(
                &server.url,
                "p-1",
                &account,
                Billing::Project,
                &key(Some("plain-1"), 100),
                0,
            )
            .unwrap();
        // Kept before keys had ids: nothing to revoke it by.
        store
            .keep(
                &server.url,
                "p-2",
                &account,
                Billing::Project,
                &key(None, 100),
                0,
            )
            .unwrap();
        // Somebody else's, and an expired one: not asked about.
        store
            .keep(&server.url, "p-3", "other", sub, &key(Some("bobs"), 100), 0)
            .unwrap();
        let warnings = revoke_kept(&http, &store, &alice, 50).await;
        let mut asked = server.routes();
        asked.sort();
        assert_eq!(
            asked,
            [
                "DELETE /api/me/keys/plain-1",
                "DELETE /api/me/keys/sub%2F2",
                "DELETE /api/me/keys/sub-1"
            ]
        );
        for request in server.requests() {
            assert_eq!(request.authorization, Some(format!("Bearer {alice}")));
        }
        assert_eq!(warnings.len(), 1, "{warnings:?}");
        assert!(warnings[0].contains("HTTP 500"), "{warnings:?}");
        assert!(
            warnings[0].contains("personal key stays valid"),
            "{warnings:?}"
        );
    }

    /// A subscription key the gateway refused is not handed out again, even
    /// when it would outlast its replacement; it stays kept for logout.
    #[tokio::test]
    async fn a_refused_subscription_key_does_not_come_back() {
        let http = reqwest::Client::new();
        let tmp = tempfile::tempdir().unwrap();
        let store = KeyStore::at(key_store_path_in(tmp.path()));
        let answer = subscription_answer("sk-new", "h-new", "2099-01-01T00:00:00Z");
        let server = MockServer::start(&[("POST /api/me/keys", 201, &answer)]);
        let sub = Billing::Subscription;
        let old = IssuedKey {
            secret: "sk-old".to_string(),
            expires_at: 9_000_000_000,
            id: Some("h-old".to_string()),
        };
        store.keep(&server.url, "p-1", "", sub, &old, 0).unwrap();
        let renewed = ensure_key(
            &http,
            &server.url,
            "tok",
            "p-1",
            sub,
            &store,
            0,
            Some("sk-old"),
        )
        .await
        .unwrap();
        assert_eq!(
            (renewed.key.secret.as_str(), renewed.issued),
            ("sk-new", true)
        );
        // The next session, which was not told, gets the new one too.
        let next = ensure_key(&http, &server.url, "tok", "p-1", sub, &store, 0, None)
            .await
            .unwrap();
        assert_eq!((next.key.secret.as_str(), next.issued), ("sk-new", false));
        assert_eq!(server.routes().len(), 1);
        let ids: Vec<_> = store.kept(0).into_iter().filter_map(|k| k.id).collect();
        assert_eq!(ids, ["h-old", "h-new"]);
    }
}