hws 0.1.1

Client library and kubectl-shaped CLI for the app-lb admin API
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
//! Read-side views of the admin API's JSON.
//!
//! Deliberately lenient: every field defaults, so a client a version behind the
//! app-lb it is talking to still renders what it understands instead of failing
//! to parse. Writes go through `serde_json::Value` so nothing is dropped on a
//! round trip — see [`crate::api`].
//!
//! # `extra`, and why leniency needed a counterweight
//!
//! Leniency alone is how these types silently fell behind: to a defaulting
//! deserializer an unknown field and an absent one are the same thing, so
//! `DeploymentView::urls`, `PoolStatus::boot_timeout_secs`,
//! `MetricsResponse::matched` and two more went missing without a single test
//! failing.
//!
//! Every response type therefore carries a `#[serde(flatten)] extra` map. It
//! keeps the leniency — an unknown field parses fine, and is *reachable* rather
//! than discarded — and it makes the gap visible: the tests in
//! `tests/wire_contract.rs` read `testdata/wire/*.json`, written by app-lb's own
//! response types, and assert `extra` is empty. A field this crate stops
//! understanding fails a test instead of blanking a column.

use serde::Deserialize;
use serde_json::Value;
use std::collections::BTreeMap;

/// Fields the server sent that this build has no name for.
///
/// Empty in a matched pair of versions. Non-empty means app-lb is ahead, and
/// what is in here is exactly what this crate is not yet reading.
pub type Extra = serde_json::Map<String, Value>;

// -- GET /deployments, GET /deployments/:id --------------------------------

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct DeploymentStatus {
    pub rollout_revision: String,
    pub spec: DeploymentSpec,
    /// `"vm"` (managed pool), `"static"` (fixed proxy_pass upstreams) or
    /// `"site"` (files served off disk).
    pub kind: String,
    pub desired_replicas: u32,
    pub ready: usize,
    pub pending: usize,
    pub total_in_flight: usize,
    pub vms: Vec<VmStatus>,
    /// Present when the spec declares `vm.workspace`.
    pub workspace: Option<WorkspaceStatus>,
    #[serde(flatten)]
    pub extra: Extra,
}

/// `vm.workspace`, mirrored read-only like [`MountSpec`].
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct WorkspaceSpec {
    /// Defaults to `/workspace` server-side.
    pub path: Option<String>,
    pub store: String,
    #[serde(rename = "ref")]
    pub artifact_ref: Option<String>,
    pub auth: Option<SecretRef>,
    /// Recycle the replica for a snapshot at least this often. Unset: only
    /// when it retires for another reason.
    pub snapshot_interval_secs: Option<u64>,
}

/// Where a deployment's workspace stands: the snapshot its pool runs from,
/// whether the store has it, and what is holding the pool if anything is.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct WorkspaceStatus {
    pub path: String,
    pub store: String,
    pub digest: Option<String>,
    pub captured_at: Option<u64>,
    pub captured_from: Option<String>,
    pub files: u64,
    pub bytes: u64,
    pub pushed: Option<String>,
    pub pushed_at: Option<u64>,
    pub push_pending: bool,
    /// `idle`, `restoring`, `capturing`, `pushing` or `blocked`.
    pub phase: String,
    pub blocked: Option<String>,
    pub pending: Vec<PendingCapture>,
    pub last_error: Option<String>,
}

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct PendingCapture {
    pub sandbox_id: String,
    pub then: String,
    pub queued_at: u64,
    pub attempts: u32,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct VmStatus {
    pub sandbox_id: String,
    pub addr: String,
    pub in_flight: usize,
    pub healthy: bool,
    pub draining: bool,
    #[serde(flatten)]
    pub extra: Extra,
}

impl VmStatus {
    /// The single-word status column, in precedence order: a draining VM is
    /// still serving, so that fact outranks its health.
    pub fn status(&self) -> &'static str {
        if self.draining {
            "Draining"
        } else if self.healthy {
            "Ready"
        } else {
            "NotReady"
        }
    }
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct DeploymentSpec {
    pub id: String,
    /// The namespace wall this deployment sits behind. Omitted on the wire when
    /// it is `default` — a single-tenant app-lb's specs look exactly as they did
    /// before namespaces — so read it through
    /// [`namespace()`](DeploymentSpec::namespace) rather than directly.
    pub namespace: String,
    /// The heyo account this deployment's VMs are metered to, and the user
    /// who registered it. Set by the managed service; absent on a
    /// self-hosted app-lb.
    pub account_id: Option<String>,
    pub user_id: Option<String>,
    pub routes: Vec<RouteRule>,
    pub maintenance: bool,
    pub vm: Option<VmSpec>,
    pub scaling: ScalingPolicy,
    pub health: HealthCheck,
    pub upstreams: Vec<String>,
    pub discovery: Option<DiscoverySpec>,
    pub build: Option<BuildSpec>,
    pub artifact: Option<ArtifactSpec>,
    pub update: Option<UpdateSpec>,
    pub auth: Option<AuthGate>,
    pub site: Option<SiteSpec>,
}

impl DeploymentSpec {
    /// The namespace this deployment is in, filling in the default the server
    /// omits.
    pub fn namespace(&self) -> &str {
        if self.namespace.is_empty() {
            crate::DEFAULT_NAMESPACE
        } else {
            &self.namespace
        }
    }
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct DiscoverySpec {
    pub service_id: String,
}

/// A static site: a directory on the app-lb host, served straight off disk.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct SiteSpec {
    pub root: String,
    pub index: String,
    pub not_found: Option<String>,
    pub spa: bool,
    pub cache_control: String,
}

impl DeploymentSpec {
    /// Forwards to fixed upstreams. **Not** true for a site, which has no
    /// backends of any kind.
    pub fn is_static(&self) -> bool {
        !self.upstreams.is_empty() || self.discovery.is_some()
    }

    /// Serves files off disk rather than proxying anywhere.
    pub fn is_site(&self) -> bool {
        self.site.is_some()
    }

    /// The routes as one column: `secrets.local`, `*.apps.example.com/api`, …
    pub fn routes_summary(&self) -> String {
        if self.routes.is_empty() {
            return "<none>".into();
        }
        let shown: Vec<String> = self.routes.iter().take(3).map(RouteRule::render).collect();
        let extra = self.routes.len().saturating_sub(shown.len());
        if extra > 0 {
            format!("{} +{extra} more", shown.join(","))
        } else {
            shown.join(",")
        }
    }

    /// What traffic actually lands on: the image for a managed pool, the
    /// upstream list for a static one, the directory for a site.
    pub fn backend_summary(&self) -> String {
        if let Some(site) = &self.site {
            return site.root.clone();
        }
        if self.is_static() {
            if let Some(discovery) = &self.discovery {
                let upstreams = self.upstreams.join(",");
                return if upstreams.is_empty() {
                    format!("discovery:{}", discovery.service_id)
                } else {
                    format!("discovery:{} ({upstreams})", discovery.service_id)
                };
            }
            return self.upstreams.join(",");
        }
        match &self.vm {
            Some(vm) => {
                let image = vm.image.as_deref().unwrap_or("ubuntu:24.04 (default)");
                format!("{image}:{}", vm.port)
            }
            None => "<none>".into(),
        }
    }
}

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct RouteRule {
    pub host: Option<String>,
    pub host_suffix: Option<String>,
    pub path_prefix: Option<String>,
    pub strip_prefix: bool,
}

impl RouteRule {
    /// One rule as a single token. A `host_suffix` is shown with the `*.` that
    /// its semantics imply (apex *and* any subdomain).
    pub fn render(&self) -> String {
        let mut s = String::new();
        if let Some(h) = &self.host {
            s.push_str(h);
        }
        if let Some(suffix) = &self.host_suffix {
            if !s.is_empty() {
                s.push('+');
            }
            s.push_str("*.");
            s.push_str(suffix.trim_start_matches('.'));
        }
        if let Some(p) = &self.path_prefix {
            if s.is_empty() {
                s.push('*');
            }
            s.push_str(p);
        }
        if s.is_empty() { "<empty>".into() } else { s }
    }
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct VmSpec {
    pub driver: String,
    pub image: Option<String>,
    pub port: u16,
    pub start_command: Option<String>,
    pub size_class: Option<String>,
    pub disk_size_gb: Option<u32>,
    pub working_directory: Option<String>,
    pub env_vars: Option<BTreeMap<String, String>>,
    pub setup_hooks: Option<Vec<String>>,
    pub open_ports: Vec<u16>,
    /// Directories the guests boot with, unpacked from tarballs in an artifact
    /// store. Empty on a deployment that declares none.
    pub mounts: Vec<MountSpec>,
    /// A writable directory owned by the deployment, captured when a replica
    /// retires and seeded into the next. See the server's `WorkspaceSpec`.
    pub workspace: Option<WorkspaceSpec>,
    pub ttl_seconds: u64,
}

/// One directory handed to every replica, unpacked from a tarball in an artifact
/// store.
///
/// Read-only here, like every other spec mirror in this file: `heyctl apply`
/// sends the file the user wrote rather than a reserialization of this struct,
/// so a field this build has no name for still reaches the server intact.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct MountSpec {
    /// Where it appears inside the guest.
    pub path: String,
    pub store: String,
    #[serde(rename = "ref")]
    pub artifact_ref: String,
    pub auth: Option<SecretRef>,
    pub strip_components: Option<usize>,
    /// Defaults to true server-side; a spec that omits it is read-only.
    pub read_only: bool,
    /// What `ref` resolved to on the last pull. `None` means nothing has been
    /// pulled yet — and a deployment whose mounts have no digest has no pool
    /// either, because the autoscaler will not boot a guest without its data.
    pub digest: Option<String>,
}

impl MountSpec {
    /// One line: `/data/corpus <- 127.0.0.1:8080/corpus-2026-08 (ro)`.
    ///
    /// The store is trimmed the same way [`ArtifactSpec::summary`] trims it, so
    /// the two read alike when a deployment has both.
    pub fn summary(&self) -> String {
        let store = self
            .store
            .trim_end_matches('/')
            .trim_start_matches("https://")
            .trim_start_matches("http://");
        format!(
            "{} <- {store}/{} ({})",
            self.path,
            self.artifact_ref,
            if self.read_only { "ro" } else { "rw" },
        )
    }
}

/// Where a managed deployment's image is built from: a git checkout, or a
/// Dockerfile manifest in an artifact store. Exactly one of `repo` and `store`
/// is set.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct BuildSpec {
    /// Empty when the recipe comes from `store`. A `String` rather than an
    /// `Option` because `#[serde(default)]` already renders an absent field
    /// empty, and every read of it on this side is a display.
    pub repo: String,
    /// Empty when the recipe comes from `repo`.
    pub store: String,
    #[serde(rename = "ref")]
    pub git_ref: Option<String>,
    pub dockerfile: Option<String>,
    pub context: Option<String>,
    pub image_name: Option<String>,
    pub image_size_mb: Option<u64>,
    pub auth: Option<SecretRef>,
}

impl BuildSpec {
    /// One column: `github.com/acme/web@main`, or `art:8080/web-rootfs` when the
    /// recipe comes out of a store.
    ///
    /// The store form deliberately reads like [`ArtifactSpec::summary`] rather
    /// than like the git form: the two share the SOURCE column, and what a
    /// reader scanning a fleet wants from it is *where this came from*. Which
    /// column it appears in already says whether it is built or pulled.
    pub fn summary(&self) -> String {
        if !self.store.is_empty() {
            let store = self
                .store
                .trim_end_matches('/')
                .trim_start_matches("https://")
                .trim_start_matches("http://");
            return match &self.git_ref {
                Some(r) => format!("{store}/{r}"),
                // Refused by the server, so only reachable from a hand-edited
                // state file. Rendered rather than panicked on: a listing is how
                // somebody would find that out.
                None => format!("{store}/(no ref)"),
            };
        }
        let repo = self
            .repo
            .trim_end_matches(".git")
            .trim_start_matches("https://")
            .trim_start_matches("http://")
            .trim_start_matches("ssh://");
        match &self.git_ref {
            Some(r) => format!("{repo}@{r}"),
            None => format!("{repo}@(default branch)"),
        }
    }
}

/// Where a managed deployment's image is pulled from. The other image source,
/// and mutually exclusive with [`BuildSpec`].
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct ArtifactSpec {
    pub store: String,
    #[serde(rename = "ref")]
    pub artifact_ref: String,
    pub image_name: Option<String>,
    pub grow_gb: Option<u64>,
    pub auth: Option<SecretRef>,
    /// Site bundles only: leading path components to drop while unpacking.
    pub strip_components: Option<usize>,
}

impl ArtifactSpec {
    /// One column: `10.0.0.4:8080/web-v2`. Shaped like [`BuildSpec::summary`] on
    /// purpose — the two share the SOURCE column, and a reader scanning a fleet
    /// should be able to tell a repo from a store at a glance without the
    /// column changing format underneath them.
    pub fn summary(&self) -> String {
        let store = self
            .store
            .trim_end_matches('/')
            .trim_start_matches("https://")
            .trim_start_matches("http://");
        format!("{store}/{}", self.artifact_ref)
    }
}

/// What one guest mount's pull did.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct MountOutcome {
    /// The guest path, which is the mount's identity within the deployment.
    pub path: String,
    pub store: String,
    #[serde(rename = "ref")]
    pub artifact_ref: String,
    pub digest: Option<String>,
    /// The tree on this host the guests mount.
    pub tree: Option<String>,
    pub files: Option<usize>,
    /// Bytes transferred from the store. `0` with `reused` means the tree was
    /// already on the host.
    pub bytes: Option<u64>,
    /// Uncompressed size of the tree — what this mount costs the host's disk.
    pub unpacked: Option<u64>,
    pub reused: bool,
    /// Whether this mount's digest changed, which is what recycles the pool.
    pub changed: bool,
}

impl MountOutcome {
    /// The store side of the mount, trimmed the same way [`MountSpec::summary`]
    /// trims it. Without the guest path, which the caller has already used as
    /// the label.
    pub fn summary(&self) -> String {
        let store = self
            .store
            .trim_end_matches('/')
            .trim_start_matches("https://")
            .trim_start_matches("http://");
        format!("{store}/{}", self.artifact_ref)
    }
}

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct SecretRef {
    pub secret: String,
    pub key: String,
    pub username: Option<String>,
    /// The namespace the reference resolves in. Stamped by app-lb from the
    /// owning object's namespace, so it is never the client's choice — but it
    /// is worth rendering when it is *not* the namespace being looked at, which
    /// would otherwise read as a secret that does not exist.
    pub namespace: Option<String>,
}

impl SecretRef {
    pub fn render(&self) -> String {
        format!("{}/{}", self.secret, self.key)
    }

    /// The same thing, said from inside `ns`: qualified only when the reference
    /// resolves somewhere else.
    pub fn render_in(&self, ns: &str) -> String {
        match self.namespace.as_deref() {
            Some(other) if other != ns => format!("{}/{} in namespace {other}", self.secret, self.key),
            _ => self.render(),
        }
    }
}

/// Where a static deployment's backend is updated: a directory on the app-lb
/// host, and commands to run in it.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct UpdateSpec {
    pub working_dir: String,
    pub commands: Vec<String>,
    pub env: Option<BTreeMap<String, String>>,
    pub env_from: Vec<SecretEnv>,
    pub auth: Option<SecretRef>,
    pub timeout_secs: Option<u64>,
    pub verify_timeout_secs: Option<u64>,
}

impl UpdateSpec {
    /// One column: `/srv/app-obs (3 commands)`.
    pub fn summary(&self) -> String {
        format!(
            "{} ({} command{})",
            self.working_dir,
            self.commands.len(),
            if self.commands.len() == 1 { "" } else { "s" }
        )
    }
}

/// One entry in [`AuthGate::public_paths`]: a path prefix, and what app-lb
/// requires on it in place of the sign-in gate.
///
/// No `#[serde(default)]`: app-lb always sends both fields, and a row with an
/// empty `path` would silently match every request when rendered.
#[derive(Debug, Clone, Deserialize)]
pub struct PublicPath {
    pub path: String,
    /// `public` | `none` | `view` | `admin`. Only `public` admits a request
    /// that presents no credential at all.
    pub scope: String,
}

/// An optional sign-in gate in front of a deployment.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct AuthGate {
    /// One provider (`"google"`) or several (`["google", "app-token"]`). A
    /// `Value` rather than a `String` because the server serializes a
    /// single-provider gate as a bare string and a multi-provider one as an
    /// array — modelling only the first would fail to parse the second, and
    /// with `#[serde(default)]` that failure would look like an *absent* gate.
    pub provider: Value,
    /// Required for `google`, absent on a token-only gate.
    pub client_id: Option<String>,
    pub client_secret: Option<SecretRef>,
    pub allowed_domains: Vec<String>,
    pub allowed_emails: Vec<String>,
    /// Path prefixes the *sign-in* gate does not sit in front of, and what
    /// app-lb requires in its place.
    ///
    /// Read as objects rather than strings since scopes were added. app-lb
    /// always serializes the object form, so a bare string only ever appears in
    /// a spec somebody wrote by hand — where it means `scope: "admin"`, the
    /// fail-closed default.
    pub public_paths: Vec<PublicPath>,
    /// When set, signing in at this gate mints an app-token with this scope,
    /// which app-lb presents upstream for the life of the session. Absent on
    /// every gate that does not — which is most of them.
    pub session_scope: Option<String>,
    pub base_path: String,
    pub session_ttl_secs: u64,
    pub cookie_name: String,
    /// Set to share one sign-in across every deployment under a parent domain;
    /// `None` is a per-host session.
    pub cookie_domain: Option<String>,
    pub redirect_url: Option<String>,
    pub forward_identity: bool,
    /// How a JWT is verified, when `jwt` is among the providers.
    pub jwt: Option<JwtSpec>,
    /// The namespace auth provider this gate inherits its identity from, if it
    /// inherits one. Mutually exclusive with every identity field above — the
    /// server refuses a gate that sets both — so a gate with this set looks
    /// empty until the provider is fetched.
    pub provider_ref: Option<String>,
}

/// How a gate verifies a JWT somebody else issued, and which ones it admits.
///
/// Exactly one of `secret`, `public_key` and `jwks_url` is set — the server
/// refuses a spec with none or several.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct JwtSpec {
    pub secret: Option<SecretRef>,
    pub public_key: Option<String>,
    pub jwks_url: Option<String>,
    pub algorithms: Vec<String>,
    pub issuer: String,
    pub audience: Option<String>,
    /// Claims a token must satisfy. Free-form: the keys are whatever the issuer
    /// sends, so this stays a `Value` rather than being modelled.
    pub require: BTreeMap<String, Value>,
    pub subject_claim: String,
    pub email_claim: String,
    pub name_claim: String,
    pub leeway_secs: Option<u64>,
    pub cookie: Option<String>,
    /// Where a token-less *browser* is sent to acquire one. Requires `cookie`:
    /// a navigation can only carry a credential in one.
    pub login_url: Option<String>,
    /// The query parameter that sign-in page reads the return URL from.
    /// `redirect_uri` when unset.
    pub login_redirect_param: Option<String>,
}

impl JwtSpec {
    /// Where the verifying key comes from, in one column.
    pub fn key_summary(&self) -> String {
        match (&self.secret, &self.public_key, &self.jwks_url) {
            (Some(r), _, _) => format!("shared secret {}", r.render()),
            (_, Some(_), _) => "an inline public key".to_string(),
            (_, _, Some(url)) => format!("the key set at {url}"),
            _ => "<none>".to_string(),
        }
    }

    /// The claims a token has to satisfy, rendered the way the spec reads:
    /// `role=admin|owner, accountId=acct_7f3c`.
    pub fn require_summary(&self) -> String {
        if self.require.is_empty() {
            return "any token this issuer signed".to_string();
        }
        self.require
            .iter()
            .map(|(claim, wanted)| {
                let rendered = match wanted {
                    Value::Array(vs) => vs
                        .iter()
                        .map(render_claim_value)
                        .collect::<Vec<_>>()
                        .join("|"),
                    single => render_claim_value(single),
                };
                format!("{claim}={rendered}")
            })
            .collect::<Vec<_>>()
            .join(", ")
    }
}

/// A claim value without JSON's quotes around a string, since every other value
/// in this view is printed bare.
fn render_claim_value(v: &Value) -> String {
    match v {
        Value::String(s) => s.clone(),
        other => other.to_string(),
    }
}

impl AuthGate {
    /// Who may enter, as one line.
    pub fn allow_summary(&self) -> String {
        let mut parts: Vec<String> = Vec::new();
        if self.allowed_domains.iter().any(|d| d == "*") {
            parts.push("any Google account".into());
        } else {
            parts.extend(self.allowed_domains.iter().map(|d| format!("@{d}")));
        }
        parts.extend(self.allowed_emails.iter().cloned());
        if parts.is_empty() {
            "<nobody>".into()
        } else {
            parts.join(", ")
        }
    }

    /// The providers this gate accepts, however the server spelled them.
    ///
    /// Empty in the payload means Google, which is what an `auth` block written
    /// before app-tokens existed says by omission.
    pub fn providers(&self) -> Vec<String> {
        match &self.provider {
            Value::String(s) if !s.is_empty() => vec![s.clone()],
            Value::Array(items) => items
                .iter()
                .filter_map(|v| v.as_str().map(str::to_string))
                .collect(),
            _ => vec!["google".to_string()],
        }
    }

    /// Whether a program can get past this gate with an app-token.
    pub fn accepts_app_token(&self) -> bool {
        self.providers().iter().any(|p| p == "app-token")
    }

    /// Whether a JWT from the configured issuer gets past this gate.
    pub fn accepts_jwt(&self) -> bool {
        self.providers().iter().any(|p| p == "jwt")
    }

    /// The URL that has to be registered with the provider, given a hostname.
    pub fn callback_url(&self, host: &str) -> String {
        format!(
            "https://{host}{}/callback",
            self.base_path.trim_end_matches('/')
        )
    }
}

// -- GET /auth-providers, GET /auth-providers/:namespace/:name -------------

/// A declared auth provider: the identity half of a gate, named and owned by a
/// namespace.
///
/// The same fields an [`AuthGate`] carries for identity — who may enter and how
/// they are verified — with none of the route-scoped ones. A deployment
/// inherits it with `auth.provider_ref`, and app-lb resolves it on every gated
/// request, so editing this object reaches every deployment that names it.
///
/// Secrets appear as references, never values, which is what makes the object
/// safe to print.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct AuthProviderView {
    pub name: String,
    pub namespace: String,
    pub description: Option<String>,
    pub created_at: u64,
    /// One provider or several, spelled as the server spells it — a `Value` for
    /// the reason [`AuthGate::provider`] is one.
    pub provider: Value,
    pub client_id: Option<String>,
    pub client_secret: Option<SecretRef>,
    pub allowed_domains: Vec<String>,
    pub allowed_emails: Vec<String>,
    pub jwt: Option<JwtSpec>,
    pub cookie_domain: Option<String>,
    #[serde(flatten)]
    pub extra: Extra,
}

impl AuthProviderView {
    /// The providers this object admits, however the server spelled them.
    /// Shares [`AuthGate::providers`]'s rule, including that an empty value
    /// means Google.
    pub fn providers(&self) -> Vec<String> {
        match &self.provider {
            Value::String(s) if !s.is_empty() => vec![s.clone()],
            Value::Array(items) => items
                .iter()
                .filter_map(|v| v.as_str().map(str::to_string))
                .collect(),
            _ => vec!["google".to_string()],
        }
    }

    /// Who this provider lets in, in one column: the Google allow-list, the
    /// JWT `require` map, or — for `app-token` — a statement that the token's
    /// own scope decides.
    pub fn admits(&self) -> String {
        let mut parts: Vec<String> = Vec::new();
        if !self.allowed_domains.is_empty() || !self.allowed_emails.is_empty() {
            if self.allowed_domains.iter().any(|d| d == "*") {
                parts.push("any Google account".into());
            } else {
                parts.extend(self.allowed_domains.iter().map(|d| format!("@{d}")));
            }
            parts.extend(self.allowed_emails.iter().cloned());
        }
        if let Some(jwt) = &self.jwt {
            parts.push(jwt.require_summary());
        }
        if parts.is_empty() && self.providers().iter().any(|p| p == "app-token") {
            parts.push("whatever the app-token is scoped to".into());
        }
        if parts.is_empty() {
            "<nobody>".into()
        } else {
            parts.join(", ")
        }
    }

    /// Where the trust comes from, for the listing's one column: the issuer for
    /// a JWT provider, the OAuth client for Google.
    pub fn trust_summary(&self) -> String {
        match (&self.jwt, &self.client_id) {
            (Some(jwt), _) => jwt.issuer.clone(),
            (None, Some(id)) => id.clone(),
            (None, None) => "—".into(),
        }
    }
}

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct SecretEnv {
    pub secret: String,
    pub key: String,
    #[serde(rename = "as")]
    pub env: Option<String>,
}

impl SecretEnv {
    /// The variable the command sees. Mirrors the server's default of the
    /// upper-cased key.
    pub fn env_name(&self) -> String {
        self.env
            .clone()
            .unwrap_or_else(|| self.key.to_ascii_uppercase())
    }

    pub fn render(&self) -> String {
        format!("{}={}/{}", self.env_name(), self.secret, self.key)
    }
}

// -- GET /secrets ----------------------------------------------------------

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct SecretSummary {
    pub id: String,
    pub description: Option<String>,
    /// Key names only. app-lb has no endpoint that returns a value.
    pub keys: Vec<String>,
    pub updated_at: u64,
    pub encrypted_at_rest: bool,
    #[serde(flatten)]
    pub extra: Extra,
}

// -- GET /workflows, GET /workflows/:id ------------------------------------

/// A CI workflow object.
///
/// Lenient like every other read type here: unknown fields land in `extra`, and
/// `tests/wire_contract.rs` asserts `extra` is empty against app-lb's own
/// fixture. That assertion is the only thing that catches a field this struct
/// stopped understanding — to a defaulting deserializer, an unknown field and an
/// absent one are the same thing.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct WorkflowView {
    pub id: String,
    pub repo: String,
    /// `ref` on the wire; a Rust keyword here.
    #[serde(rename = "ref")]
    pub git_ref: String,
    pub path: String,
    pub network: String,
    /// A reference to a stored secret, never a value.
    pub auth: Option<Value>,
    pub secrets_prefix: Option<String>,
    pub enabled: bool,
    #[serde(flatten)]
    pub extra: Extra,
}

/// `GET /workflows` is enveloped so it can grow a cursor later without that
/// being a breaking change.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct WorkflowList {
    pub workflows: Vec<WorkflowView>,
    #[serde(flatten)]
    pub extra: Extra,
}

// -- GET /jobs, POST /deployments/:id/{build,update} -----------------------

/// One deploy job. Two kinds share the type, and the fields the other kind uses
/// are simply absent — `image-build` carries `image`/`commit`, `host-update`
/// carries `working_dir`/`commands_*`.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct JobRecord {
    pub id: String,
    pub deployment: String,
    /// `image-build` or `host-update`.
    pub kind: String,
    /// `running`, `succeeded` or `failed`.
    pub status: String,
    pub started_at: u64,
    pub finished_at: Option<u64>,

    // image-build
    pub repo: String,
    #[serde(rename = "ref")]
    pub git_ref: Option<String>,
    pub commit: Option<String>,
    pub dockerfile: Option<String>,
    pub image: Option<String>,
    pub rolled_out: bool,

    // artifact-pull
    pub store: Option<String>,
    /// What was asked for: a tag or a digest. Spelled `artifact` on the wire so
    /// it does not collide with a build's `ref`.
    #[serde(rename = "artifact")]
    pub artifact_ref: Option<String>,
    /// What it resolved to — the pull's answer to "which bytes are live?".
    pub digest: Option<String>,
    /// Bytes transferred. `0` with `reused` is a skipped fetch, not a no-op job;
    /// `0` without it is a local store hardlinking the blob rather than copying.
    pub bytes: Option<u64>,
    pub reused: bool,
    /// Set only when the pull was a *site* pull — a bundle unpacked into this
    /// directory rather than a rootfs written to an image.
    pub site_root: Option<String>,
    /// Regular files unpacked, for the same kind of pull. The answer `bytes`
    /// cannot give when the blob was hardlinked and the transfer was free.
    pub files: Option<usize>,

    // mount-pull
    /// One entry per guest mount. A mount pull covers all of a deployment's
    /// mounts in one job, so the single `digest`/`store` fields above stay empty
    /// on this kind and this carries the outcome instead.
    pub mounts: Vec<MountOutcome>,

    // host-update
    pub working_dir: Option<String>,
    pub commands_total: Option<usize>,
    pub commands_run: Option<usize>,
    pub verified: Option<bool>,

    pub error: Option<String>,
    pub log: Vec<String>,
    #[serde(flatten)]
    pub extra: Extra,
}

impl JobRecord {
    pub fn is_running(&self) -> bool {
        self.status == "running"
    }

    pub fn succeeded(&self) -> bool {
        self.status == "succeeded"
    }

    pub fn is_update(&self) -> bool {
        self.kind == "host-update"
    }

    /// Whether this job pulled its image from an artifact store rather than
    /// building it. The two produce the same thing — a new `vm.image` and a
    /// recycled pool — but describe it with different fields, so every renderer
    /// has to tell them apart.
    pub fn is_pull(&self) -> bool {
        self.kind == "artifact-pull"
    }

    /// Whether this job materialized the deployment's guest mounts. Distinct
    /// from [`Self::is_pull`] even though both fetch from a store: this one
    /// changes what the guests *hold*, not what they boot, and its outcome is in
    /// `mounts` rather than in `image`.
    pub fn is_mount_pull(&self) -> bool {
        self.kind == "mount-pull"
    }

    /// The commit, short enough for a column.
    pub fn short_commit(&self) -> String {
        match &self.commit {
            Some(c) => c.chars().take(12).collect(),
            None => "—".into(),
        }
    }

    /// The resolved digest, short enough for a column. The pull's counterpart of
    /// [`short_commit`](Self::short_commit).
    pub fn short_digest(&self) -> String {
        match &self.digest {
            Some(d) => d.chars().take(12).collect(),
            None => "—".into(),
        }
    }

    /// What this job produced, as one column: the image for either image
    /// source, how far the commands got for an update.
    pub fn result_summary(&self) -> String {
        if self.is_update() {
            return match (self.commands_run, self.commands_total) {
                (Some(run), Some(total)) => format!("{run}/{total} commands"),
                _ => "—".into(),
            };
        }
        if self.is_mount_pull() {
            // What changed, not what was pulled: a mount pull that moved
            // nothing is the common case, and reporting "3 mounts" for it would
            // read as three deployments' worth of work.
            let changed = self.mounts.iter().filter(|m| m.changed).count();
            return match (self.mounts.len(), changed) {
                (0, _) => "—".into(),
                (total, 0) => format!("{total} unchanged"),
                (total, n) => format!("{n}/{total} updated"),
            };
        }
        self.image.clone().unwrap_or_else(|| "—".into())
    }

    /// What it was asked to act on: a git ref for a build, a store reference for
    /// a pull, the directory for an update.
    pub fn target_summary(&self) -> String {
        if self.is_update() {
            return self.working_dir.clone().unwrap_or_else(|| "—".into());
        }
        if self.is_pull() {
            return self.artifact_ref.clone().unwrap_or_else(|| "—".into());
        }
        if self.is_mount_pull() {
            let paths: Vec<&str> = self.mounts.iter().map(|m| m.path.as_str()).take(2).collect();
            return match (paths.is_empty(), self.mounts.len().saturating_sub(paths.len())) {
                (true, _) => "—".into(),
                (false, 0) => paths.join(","),
                (false, more) => format!("{} +{more} more", paths.join(",")),
            };
        }
        self.git_ref.clone().unwrap_or_else(|| "(default)".into())
    }

    /// How long it ran (or has been running), given the server's clock.
    pub fn elapsed_secs(&self, now: u64) -> u64 {
        self.finished_at
            .unwrap_or(now)
            .saturating_sub(self.started_at)
    }
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct ScalingPolicy {
    pub min_replicas: u32,
    pub max_replicas: u32,
    pub warm_pool: u32,
    pub target_concurrency: u32,
    pub scale_to_zero_after_secs: u64,
    pub cold_start_timeout_secs: u64,
    pub drain_timeout_secs: u64,
    pub boot_timeout_secs: u64,
    /// `destroy` or `retain` — what becomes of a VM the autoscaler retires.
    /// A `String` rather than an enum so a value this build has not heard of
    /// displays as-is instead of failing the whole read.
    pub idle_action: String,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct HealthCheck {
    pub expected_header: Option<ExpectedHeader>,
    pub path: Option<String>,
    pub port: Option<u16>,
    pub timeout_secs: u64,
}

#[derive(Debug, Default, Deserialize)]
pub struct ExpectedHeader {
    pub name: String,
    pub value: String,
}

// -- GET /metrics ----------------------------------------------------------

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct MetricsResponse {
    pub generated_at: u64,
    pub uptime_secs: u64,
    pub host: HostUsage,
    pub fleet: FleetPool,
    pub global: DeploymentMetrics,
    /// Absent when the LB is not shipping logs to app-obs.
    pub obs: Option<ObsStats>,
    /// Absent when the LB has security monitoring off (`APP_LB_SIEM=0`).
    pub security: Option<SecuritySummary>,
    /// Whether app-lb can reach the VM daemon. When it cannot, the autoscaler
    /// abandons every tick, so nothing scales or boots and every other number
    /// here is frozen at whatever it was when the daemon went away.
    pub daemon: DaemonStatus,
    pub deployments: Vec<DeploymentView>,
    /// How many deployments matched before `limit`/`offset`, so a caller can
    /// page without guessing.
    pub matched: usize,
    /// How many deployments hold their own counters. Climbing past the number
    /// registered means retirement is not keeping up.
    pub tracked_deployments: usize,
    /// Sandboxes on the host that no deployment owns — created through the
    /// heyvm CLI, the cloud API or the desktop rather than by app-lb. They
    /// share the host with every pool; absent from an older app-lb, emptied by
    /// `summary=true`, and narrowed to the caller's own accounts for a
    /// namespace caller.
    #[serde(default)]
    pub host_sandboxes: Vec<HostSandboxView>,
    #[serde(flatten)]
    pub extra: Extra,
}

/// One sandbox on the host that app-lb reports but does not manage.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct HostSandboxView {
    pub sandbox_id: String,
    pub name: String,
    /// The daemon's status string: `running`, `stopped`, `provisioning`, ….
    pub status: String,
    pub image: String,
    pub size_class: Option<String>,
    pub guest_ip: Option<String>,
    pub uptime_secs: u64,
    pub cpu_percent: Option<f64>,
    pub memory_bytes: Option<u64>,
    /// The heyo account the sandbox is billed to, when the daemon knows.
    pub account_id: Option<String>,
    /// RFC 3339, when the daemon reports it.
    pub created_at: Option<String>,
    #[serde(flatten)]
    pub extra: Extra,
}

/// Alert counts from the detection engine, carried on `/metrics` so a status
/// display can show one without a second request. The alerts themselves are on
/// `GET /security`.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct SecuritySummary {
    /// Alerts currently held in the LB's in-memory ring.
    pub open: usize,
    /// How many of those are `high` or `critical`.
    pub urgent: u64,
    /// Observations dropped because the analysis queue was full. Non-zero means
    /// detection is sampling rather than complete — the same failure mode, and
    /// the same warning, as [`ObsStats::dropped`].
    pub dropped: u64,
    /// Whether the per-source table is full, which means the same for addresses
    /// as `dropped` does for events.
    pub clients_at_capacity: bool,
    /// Guard rules the data plane is enforcing, and how many requests they have
    /// refused. Reported beside the alert counts because "we are blocking
    /// traffic" belongs next to "we are seeing attacks".
    pub rules: usize,
    pub blocked: u64,
    #[serde(flatten)]
    pub extra: Extra,
}

/// Log-shipping counters. Worth surfacing because the pipeline drops rather than
/// blocks by design, and `dropped` is the only trace a lost record leaves.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct ObsStats {
    pub queued: u64,
    pub dropped: u64,
    pub shipped: u64,
    pub failed: u64,
    pub healthy: bool,
    #[serde(flatten)]
    pub extra: Extra,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct HostUsage {
    /// False until the daemon has produced a sample.
    pub available: bool,
    pub cpu_count: u64,
    pub cpu_percent: f64,
    pub memory_total_bytes: u64,
    pub memory_used_bytes: u64,
    pub sampled_at_ms: u64,
    #[serde(flatten)]
    pub extra: Extra,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct FleetPool {
    pub deployments: usize,
    pub ready: usize,
    pub draining: usize,
    pub pending: usize,
    pub total_in_flight: usize,
    #[serde(flatten)]
    pub extra: Extra,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct DeploymentView {
    pub id: String,
    /// Empty when the server omitted the default namespace.
    pub namespace: String,
    /// `"vm"`, `"static"` or `"site"`.
    pub kind: String,
    pub upstreams: Vec<String>,
    /// Whether at least one data-plane route points at this deployment.
    pub routed: bool,
    /// Exact hostnames this deployment is routed on — `host` rules only, since
    /// a `host_suffix` names no single certificate subject.
    pub hosts: Vec<String>,
    /// The same routes as URLs, with the *data plane's* scheme and port. Built
    /// server-side because the admin listener knows neither.
    pub urls: Vec<String>,
    /// For a site, the directory it serves. Absent for every other kind.
    pub site_root: Option<String>,
    pub site_spa: bool,
    /// `"build"` or `"update"` — which deploy job this deployment accepts, if
    /// either.
    pub job_kind: Option<String>,
    pub pool: PoolStatus,
    pub vms: Vec<VmView>,
    /// Booting VMs, oldest first — the ones holding a cold start open. A count
    /// alone cannot tell a 3-second boot from a guest that has been failing its
    /// health check for six minutes.
    pub pending_vms: Vec<PendingVmView>,
    pub metrics: DeploymentMetrics,
    #[serde(flatten)]
    pub extra: Extra,
}

/// A VM created but not yet in the pool.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct PendingVmView {
    pub sandbox_id: String,
    /// Seconds since the daemon accepted the create call.
    pub age_secs: u64,
    /// The daemon's last reported status, absent before the first observation:
    /// `provisioning`, `running`, `stopped`, `paused`, `failed`, `cold-stored`
    /// or `unknown`.
    pub status: Option<String>,
    #[serde(flatten)]
    pub extra: Extra,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct PoolStatus {
    pub desired_replicas: u32,
    pub ready: usize,
    pub draining: usize,
    pub pending: usize,
    pub total_in_flight: usize,
    pub target_concurrency: u32,
    pub min_replicas: u32,
    pub max_replicas: u32,
    pub warm_pool: u32,
    /// `None` when there is no available capacity to divide by — rendered as
    /// "—", never as a fake 0%.
    pub utilization: Option<f64>,
    pub cpu_percent: Option<f64>,
    pub memory_bytes: Option<u64>,
    /// How long a booting VM gets before the autoscaler kills it; `0` waits
    /// indefinitely.
    pub boot_timeout_secs: u64,
    /// How long a request waits on a cold start. A pending VM older than this
    /// has already cost somebody a 503.
    pub cold_start_timeout_secs: u64,
    #[serde(flatten)]
    pub extra: Extra,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct VmView {
    pub sandbox_id: String,
    pub addr: String,
    pub in_flight: usize,
    pub healthy: bool,
    pub draining: bool,
    pub uptime_secs: u64,
    pub cpu_percent: Option<f64>,
    pub memory_bytes: Option<u64>,
    #[serde(flatten)]
    pub extra: Extra,
}

impl VmView {
    pub fn status(&self) -> &'static str {
        if self.draining {
            "Draining"
        } else if self.healthy {
            "Ready"
        } else {
            "NotReady"
        }
    }
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct DeploymentMetrics {
    pub requests: StatusCounts,
    pub latency_ms: Histogram,
    pub cold_start_s: Histogram,
    pub autoscale: AutoscaleCounts,
    #[serde(flatten)]
    pub extra: Extra,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct StatusCounts {
    pub total: u64,
    pub c2xx: u64,
    pub c3xx: u64,
    pub c4xx: u64,
    pub c5xx: u64,
    pub errors: u64,
    #[serde(flatten)]
    pub extra: Extra,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct Histogram {
    pub count: u64,
    pub sum: u64,
    pub mean: f64,
    pub p50: f64,
    pub p90: f64,
    pub p99: f64,
    #[serde(flatten)]
    pub extra: Extra,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct AutoscaleCounts {
    pub vms_created: u64,
    pub vms_drained: u64,
    pub vms_reaped: u64,
    pub scale_up_events: u64,
    pub scale_down_events: u64,
    pub cold_start_waits: u64,
    pub cold_start_hits: u64,
    pub cold_start_timeouts: u64,
    /// VMs killed for never passing their health check inside the boot timeout.
    /// Was absent from this struct while app-lb had been sending it — the exact
    /// silent drift `extra` exists to catch, and the reason `describe` could not
    /// tell "the guest never became healthy" from "no VM was ever created".
    pub boot_timeouts: u64,
    /// Creates the daemon refused outright. The distinguishing counter: with
    /// `vms_created` and `boot_timeouts` both zero, a non-zero value here means
    /// no VM ever existed, so the guest image is not the thing to debug.
    pub create_failures: u64,
    /// What the daemon said the last time it refused.
    pub last_create_error: Option<String>,
    #[serde(flatten)]
    pub extra: Extra,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct DaemonStatus {
    pub reachable: bool,
    pub last_error: Option<String>,
    #[serde(flatten)]
    pub extra: Extra,
}

// -- GET /certs ------------------------------------------------------------

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct CertStatus {
    pub host: String,
    pub not_after: String,
    pub issuer: String,
    pub needs_renewal: bool,
    #[serde(flatten)]
    pub extra: Extra,
}

// -- GET /disks ------------------------------------------------------------

/// What the daemon thinks of the sandbox a disk belongs to.
///
/// `Unknown` is not a parse failure — it is what app-lb reports when it could
/// not reach the daemon, and such a disk is never reclaimed. A state this build
/// does not recognise degrades to `Unknown` for the same reason: an unfamiliar
/// word must not blank the whole listing.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DiskState {
    Running,
    Stopped,
    Orphan,
    #[default]
    #[serde(other)]
    Unknown,
}

impl DiskState {
    pub fn label(self) -> &'static str {
        match self {
            Self::Running => "running",
            Self::Stopped => "stopped",
            Self::Orphan => "orphan",
            Self::Unknown => "unknown",
        }
    }
}

/// One file on the host and what it is for.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct DiskPart {
    pub kind: String,
    pub path: String,
    /// Blocks actually allocated. The number that matters: a `data.ext4` is
    /// created sparse at its full nominal size, so `apparent_bytes` routinely
    /// reads tens of GiB for a fraction of that in real occupancy.
    pub bytes: u64,
    pub apparent_bytes: u64,
    pub modified_at: u64,
    #[serde(flatten)]
    pub extra: Extra,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct DiskArchiveRecord {
    pub uri: String,
    pub at: u64,
    pub bytes: u64,
    #[serde(flatten)]
    pub extra: Extra,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct DiskArchiveView {
    pub id: String,
    pub sandbox_id: String,
    pub uri: String,
    pub started_at: u64,
    pub finished_at: Option<u64>,
    pub status: String,
    pub bytes: u64,
    pub expected_bytes: u64,
    pub error: Option<String>,
    pub purged: bool,
    #[serde(flatten)]
    pub extra: Extra,
}

/// Everything on the host belonging to one sandbox.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct DiskInfo {
    pub sandbox_id: String,
    /// The daemon's name for it, when the daemon still has a record.
    pub name: Option<String>,
    /// The deployment that owns it, from an `applb-<deployment>-<nonce>` name.
    /// `None` for a sandbox app-lb did not create, or one whose name is gone.
    pub deployment: Option<String>,
    pub state: DiskState,
    /// A live deployment lists this sandbox as one it intends to resume. A
    /// claimed disk is never reclaimed, at any age.
    pub claimed: bool,
    /// Set by an operator: never reclaim this, whatever its age.
    pub retain: bool,
    pub note: Option<String>,
    pub bytes: u64,
    pub apparent_bytes: u64,
    pub modified_at: u64,
    /// When the sweep would reclaim this, or `None` if it never would.
    pub expires_at: Option<u64>,
    /// Why it will not be reclaimed, in a phrase meant to be shown verbatim.
    pub held_by: Option<String>,
    pub archived: Option<DiskArchiveRecord>,
    pub parts: Vec<DiskPart>,
    /// The directories a purge would remove.
    pub roots: Vec<String>,
    #[serde(flatten)]
    pub extra: Extra,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct DiskTotals {
    pub disks: usize,
    pub bytes: u64,
    pub apparent_bytes: u64,
    pub running: usize,
    pub stopped: usize,
    pub orphan: usize,
    pub retained: usize,
    /// Disks the sweep would reclaim right now, and what that would free.
    pub expiring_now: usize,
    pub reclaimable_bytes: u64,
    #[serde(flatten)]
    pub extra: Extra,
}

/// `GET /disks` — the host's per-sandbox disk inventory.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct DiskInventory {
    /// Whether both daemon listings succeeded. When false nothing is classified
    /// as an orphan and the sweep declines to run — so a listing that says
    /// `complete: false` must not be read as "these disks are residue".
    pub complete: bool,
    pub incomplete_reason: Option<String>,
    pub data_dir: String,
    pub tmp_dir: String,
    /// `0` when expiry is disabled.
    pub ttl_secs: u64,
    pub sweep_secs: u64,
    pub archive_enabled: bool,
    pub archive_on_expire: bool,
    pub archive_target: Option<String>,
    /// Free and total bytes on the filesystem holding the guest disks, when it
    /// could be measured. `None` means unknown, never "full".
    pub free_bytes: Option<u64>,
    pub filesystem_bytes: Option<u64>,
    /// How long a disk with no daemon record survives, against `ttl_secs` for
    /// everything else. Far shorter: an orphan is the disk of a VM that failed
    /// to create or died without a trace, and nothing will ever resume it.
    pub orphan_ttl_secs: u64,
    pub totals: DiskTotals,
    pub disks: Vec<DiskInfo>,
    pub archives: Vec<DiskArchiveView>,
    #[serde(flatten)]
    pub extra: Extra,
}

/// `GET /api/plugins` and `/api/plugins/:id` — a built-in plugin, its stored
/// record and its live status.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct PluginView {
    pub id: String,
    pub name: String,
    pub description: String,
    /// JSON Schema for `config`. Advisory; app-lb validates on write.
    pub config_schema: Value,
    pub enabled: bool,
    /// Kept while disabled, so re-enabling does not lose it.
    pub config: Value,
    pub updated_at: u64,
    /// Why the last apply failed. A plugin can be enabled *and* failing.
    pub last_error: Option<String>,
    /// Whatever the plugin reports about itself; shape is per plugin.
    pub status: Value,
    #[serde(flatten)]
    pub extra: Extra,
}

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

    #[test]
    fn a_deployment_parses_from_a_partial_body() {
        // Everything defaults, so a server that grows a field — or drops one
        // this build knows about — still renders.
        let d: DeploymentStatus = serde_json::from_str(r#"{"spec":{"id":"web"},"ready":2}"#).unwrap();
        assert_eq!(d.spec.id, "web");
        assert_eq!(d.ready, 2);
        assert_eq!(d.desired_replicas, 0);
    }

    #[test]
    fn routes_render_with_their_matching_semantics() {
        let exact = RouteRule {
            host: Some("secrets.local".into()),
            ..Default::default()
        };
        assert_eq!(exact.render(), "secrets.local");

        let wild = RouteRule {
            host_suffix: Some(".apps.example.com".into()),
            path_prefix: Some("/api".into()),
            ..Default::default()
        };
        assert_eq!(wild.render(), "*.apps.example.com/api");

        let path_only = RouteRule {
            path_prefix: Some("/legacy".into()),
            ..Default::default()
        };
        assert_eq!(path_only.render(), "*/legacy");

        assert_eq!(RouteRule::default().render(), "<empty>");
    }

    #[test]
    fn many_routes_collapse_to_a_summary() {
        let spec = DeploymentSpec {
            routes: (0..5)
                .map(|i| RouteRule {
                    host: Some(format!("h{i}.local")),
                    ..Default::default()
                })
                .collect(),
            ..Default::default()
        };
        assert_eq!(spec.routes_summary(), "h0.local,h1.local,h2.local +2 more");
    }

    #[test]
    fn a_build_source_renders_as_one_column() {
        let b = BuildSpec {
            repo: "https://github.com/acme/web.git".into(),
            git_ref: Some("main".into()),
            ..Default::default()
        };
        assert_eq!(b.summary(), "github.com/acme/web@main");

        let default_branch = BuildSpec {
            repo: "git@github.com:acme/web.git".into(),
            ..Default::default()
        };
        assert_eq!(
            default_branch.summary(),
            "git@github.com:acme/web@(default branch)"
        );

        // A recipe out of a store reads like the store column, not like a repo:
        // both share SOURCE, and which column it lands in already says whether
        // this is built or pulled.
        let from_store = BuildSpec {
            store: "http://art.internal:8080".into(),
            git_ref: Some("web-rootfs".into()),
            ..Default::default()
        };
        assert_eq!(from_store.summary(), "art.internal:8080/web-rootfs");

        // `store` wins over an empty `repo` without either being an Option: an
        // absent field is empty under #[serde(default)], and that is the whole
        // signal.
        let parsed: BuildSpec =
            serde_json::from_str(r#"{"store":"/srv/artifacts","ref":"web"}"#).unwrap();
        assert_eq!(parsed.summary(), "/srv/artifacts/web");
    }

    #[test]
    fn a_job_record_parses_from_a_partial_body() {
        let r: JobRecord =
            serde_json::from_str(r#"{"id":"job-1","status":"running","started_at":100}"#).unwrap();
        assert!(r.is_running());
        assert!(!r.succeeded());
        assert_eq!(r.short_commit(), "—");
        assert_eq!(r.elapsed_secs(130), 30, "a running job measures against now");

        let done: JobRecord = serde_json::from_str(
            r#"{"id":"job-1","kind":"image-build","status":"succeeded","started_at":100,
                "finished_at":160,"commit":"0123456789abcdef","ref":"main","image":"web-0123"}"#,
        )
        .unwrap();
        assert!(done.succeeded());
        assert_eq!(done.short_commit(), "0123456789ab");
        assert_eq!(done.elapsed_secs(999), 60, "a finished one measures its own span");
        assert_eq!(done.target_summary(), "main");
        assert_eq!(done.result_summary(), "web-0123");
    }

    /// The two kinds share the type, so the columns have to mean the right thing
    /// for each: a host update has no image and no commit.
    #[test]
    fn an_update_record_summarizes_its_own_fields() {
        let update: JobRecord = serde_json::from_str(
            r#"{"id":"job-2","kind":"host-update","status":"failed","started_at":100,
                "working_dir":"/srv/app-obs","commands_total":3,"commands_run":1,
                "verified":false}"#,
        )
        .unwrap();
        assert!(update.is_update());
        assert_eq!(update.target_summary(), "/srv/app-obs");
        assert_eq!(update.result_summary(), "1/3 commands");
        assert_eq!(update.verified, Some(false));
    }

    #[test]
    fn an_artifact_source_renders_as_one_column_shaped_like_a_build_one() {
        let a = ArtifactSpec {
            store: "http://10.0.0.4:8080/".into(),
            artifact_ref: "web-v2".into(),
            ..Default::default()
        };
        assert_eq!(a.summary(), "10.0.0.4:8080/web-v2");

        // A store root keeps its leading slash — it is a path, and stripping it
        // would make an absolute one look relative in the SOURCE column.
        let local = ArtifactSpec {
            store: "/srv/artifacts".into(),
            artifact_ref: "debian-hermes".into(),
            ..Default::default()
        };
        assert_eq!(local.summary(), "/srv/artifacts/debian-hermes");
    }

    #[test]
    fn a_pull_record_summarizes_the_store_side_fields_not_the_git_ones() {
        let pull: JobRecord = serde_json::from_str(
            r#"{"id":"job-3","kind":"artifact-pull","status":"succeeded","started_at":100,
                "store":"http://127.0.0.1:8080","artifact":"debian-hermes",
                "digest":"c74abee2ce8409f1aaaa","image":"web-c74abee2ce84",
                "bytes":609222656,"rolled_out":true}"#,
        )
        .unwrap();
        assert!(pull.is_pull());
        assert!(!pull.is_update());
        // The reference asked for, not a git ref it does not have.
        assert_eq!(pull.target_summary(), "debian-hermes");
        assert_eq!(pull.result_summary(), "web-c74abee2ce84");
        assert_eq!(pull.short_digest(), "c74abee2ce84");
        assert_eq!(pull.short_commit(), "—", "a pull has no commit");
        assert!(!pull.reused);
    }

    #[test]
    fn a_reused_image_is_distinguishable_from_a_pull_that_never_ran() {
        let reused: JobRecord = serde_json::from_str(
            r#"{"id":"job-4","kind":"artifact-pull","status":"succeeded","started_at":1,
                "bytes":0,"reused":true}"#,
        )
        .unwrap();
        assert!(reused.reused);
        assert_eq!(reused.bytes, Some(0));

        // A record with neither is one that failed before it got that far.
        let failed: JobRecord = serde_json::from_str(
            r#"{"id":"job-5","kind":"artifact-pull","status":"failed","started_at":1}"#,
        )
        .unwrap();
        assert!(!failed.reused);
        assert_eq!(failed.bytes, None);
    }

    #[test]
    fn an_update_source_renders_its_directory_and_command_count() {
        let u = UpdateSpec {
            working_dir: "/srv/app-obs".into(),
            commands: vec!["git pull".into(), "cargo build --release".into()],
            ..Default::default()
        };
        assert_eq!(u.summary(), "/srv/app-obs (2 commands)");

        let one = UpdateSpec {
            working_dir: "/srv/x".into(),
            commands: vec!["make deploy".into()],
            ..Default::default()
        };
        assert_eq!(one.summary(), "/srv/x (1 command)");
    }

    #[test]
    fn a_gate_says_who_may_enter_and_where_the_provider_redirects() {
        let g = AuthGate {
            allowed_domains: vec!["example.com".into()],
            allowed_emails: vec!["contractor@gmail.com".into()],
            base_path: "/__applb/auth".into(),
            ..Default::default()
        };
        assert_eq!(g.allow_summary(), "@example.com, contractor@gmail.com");
        assert_eq!(
            g.callback_url("app.example.com"),
            "https://app.example.com/__applb/auth/callback"
        );

        // The escape hatch reads as what it is, not as a literal `@*`.
        let any = AuthGate {
            allowed_domains: vec!["*".into()],
            ..Default::default()
        };
        assert_eq!(any.allow_summary(), "any Google account");

        // A server a version ahead could send a gate this build understands
        // nothing about; it must still render rather than fail to parse.
        let unknown: AuthGate = serde_json::from_str(r#"{"provider":"okta","client_id":"x"}"#).unwrap();
        assert_eq!(unknown.provider, "okta");
        assert_eq!(unknown.allow_summary(), "<nobody>");
    }

    #[test]
    fn a_secret_env_defaults_to_the_upper_cased_key() {
        let e = SecretEnv {
            secret: "obs".into(),
            key: "ingest_token".into(),
            env: None,
        };
        assert_eq!(e.env_name(), "INGEST_TOKEN");
        assert_eq!(e.render(), "INGEST_TOKEN=obs/ingest_token");

        let renamed = SecretEnv {
            env: Some("APP_OBS_INGEST_TOKEN".into()),
            ..e
        };
        assert_eq!(renamed.render(), "APP_OBS_INGEST_TOKEN=obs/ingest_token");
    }

    #[test]
    fn vm_status_puts_draining_ahead_of_health() {
        let vm = VmStatus {
            healthy: true,
            draining: true,
            ..Default::default()
        };
        assert_eq!(vm.status(), "Draining");
    }
}

// -- POST /deployments/:id/exec --------------------------------------------

/// What a command did. A non-zero `exit_code` is a successful *request* — the
/// command ran and failed, which is not the same as being unable to run it.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct ExecOutput {
    /// Which VM ran it. Worth having even for a single-VM sandbox: after a
    /// resume or a rebuild it is a different sandbox than last time.
    pub sandbox_id: String,
    pub exit_code: i32,
    pub stdout: String,
    pub stderr: String,
    /// stdout and stderr interleaved as the guest wrote them. Not
    /// `stdout + stderr` — the only faithful rendering of interleaved output.
    pub output: String,
    #[serde(flatten)]
    pub extra: Extra,
}

impl ExecOutput {
    /// Whether the command itself succeeded.
    pub fn ok(&self) -> bool {
        self.exit_code == 0
    }
}

// -- DELETE /deployments/:id/vms/:sandbox_id -------------------------------

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct EvictOutcome {
    pub sandbox_id: String,
    /// `"killed"` (immediate) or `"draining"` (still serving what it has).
    pub outcome: String,
    #[serde(flatten)]
    pub extra: Extra,
}

impl EvictOutcome {
    pub fn is_draining(&self) -> bool {
        self.outcome == "draining"
    }
}

// -- PUT/DELETE /deployments/:id/upstreams/:upstream/drain ----------------

#[derive(Debug, Default, Deserialize, serde::Serialize)]
#[serde(default)]
pub struct UpstreamTrafficStatus {
    pub deployment_id: String,
    pub upstream: String,
    /// `accepting`, `draining`, or `drained`.
    pub state: String,
    /// Probe state, independent from the administrative traffic state above.
    pub healthy: bool,
    pub in_flight: usize,
    pub reason: Option<String>,
    pub started_at: Option<u64>,
    #[serde(flatten)]
    pub extra: Extra,
}

impl UpstreamTrafficStatus {
    pub fn is_drained(&self) -> bool {
        self.state == "drained" && self.in_flight == 0
    }

    pub fn is_accepting(&self) -> bool {
        self.state == "accepting"
    }
}

// -- app-tokens -------------------------------------------------------------

/// What a token may do on the admin API.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, serde::Serialize)]
#[serde(rename_all = "lowercase")]
pub enum AdminScope {
    /// Nothing. Still usable against a deployment's own gate, which is the
    /// point of a token handed to an application.
    #[default]
    None,
    /// `/metrics` and `/dashboard`.
    View,
    /// Everything, within the token's `deployments` scope.
    Admin,
}

impl std::fmt::Display for AdminScope {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::None => "none",
            Self::View => "view",
            Self::Admin => "admin",
        })
    }
}

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct TokenSummary {
    pub id: String,
    pub name: String,
    pub admin: AdminScope,
    /// The namespace this token is confined to, if any. Inside it, an empty
    /// `deployments` list means every deployment there; outside it the token
    /// reaches nothing.
    pub namespace: Option<String>,
    /// Deployment ids, or `["*"]` for all of them.
    pub deployments: Vec<String>,
    pub created_at: u64,
    pub expires_at: Option<u64>,
    /// `None` also means "not used since the store was last written" — the
    /// stamp is flushed opportunistically, not per request, so a busy token can
    /// still read as unused.
    pub last_used_at: Option<u64>,
    #[serde(flatten)]
    pub extra: Extra,
}

impl TokenSummary {
    /// Whether this token's scope covers the whole fleet. A namespace token
    /// never does — `["*"]` inside a namespace means everything *there*.
    pub fn covers_fleet(&self) -> bool {
        self.namespace.is_none() && self.deployments.iter().any(|d| d == "*")
    }

    pub fn allows(&self, deployment: &str) -> bool {
        self.deployments.iter().any(|d| d == "*" || d == deployment)
    }
}

// -- namespaces -------------------------------------------------------------

/// One namespace, from `GET /namespaces`.
///
/// Distinct from [`FeedIndexEntry`], which answers a narrower question: that one
/// lists namespaces with *feed events*, this one lists namespaces with
/// deployments the caller can see. A namespace appears here the moment
/// something is registered in it and disappears when the last thing leaves —
/// there is no namespace object to create or delete.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct NamespaceEntry {
    pub namespace: String,
    /// Deployments in it that *this credential* may see. Narrowed server-side,
    /// so a scoped token sees its own arithmetic rather than the fleet's.
    pub deployments: u64,
    /// Whether a namespace *object* exists, as opposed to the name being one a
    /// deployment happens to mention. Both scope identically; the difference is
    /// whether there is anything to delete or describe.
    pub declared: bool,
    pub description: Option<String>,
    /// Present only when `declared`.
    pub created_at: Option<u64>,
    #[serde(flatten)]
    pub extra: Extra,
}

// -- the event feed ---------------------------------------------------------

/// One namespace that has feed events, from `GET /feeds`.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct FeedIndexEntry {
    pub namespace: String,
    pub events: u64,
    #[serde(flatten)]
    pub extra: Extra,
}

/// One event from a namespace's feed, from `GET /feeds/:ns?format=json`.
///
/// The same entries the RSS document carries; `id` is the RSS `<guid>`.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default)]
pub struct FeedEvent {
    pub id: u64,
    /// When the event first happened.
    pub ts: u64,
    /// When it last happened — repeats of the same issue fold into one entry.
    pub last_ts: u64,
    pub count: u64,
    pub namespace: String,
    pub deployment: String,
    /// `deployed`, `updated`, `removed` or `issue`.
    pub kind: String,
    pub title: String,
    pub detail: String,
    #[serde(flatten)]
    pub extra: Extra,
}

/// The reply to a mint. **`token` is the only time the secret is ever
/// returned** — app-lb stores only its hash, and there is no endpoint that
/// reads it back. Store it here or mint another one.
#[derive(Debug, Default, Clone, Deserialize)]
pub struct MintedToken {
    #[serde(flatten)]
    pub summary: TokenSummary,
    pub token: String,
}