exochain-node 0.2.0-beta

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

//! Telegram adjutant — agentic chief-of-staff for human operator oversight.
//!
//! A Telegram bot that acts as the operator's adjutant, presenting clear
//! choices via inline keyboard buttons for every material action.  The
//! bot receives alerts from the sentinel system and forwards them with
//! actionable options.
//!
//! ## Configuration
//!
//! Set environment variables:
//! - `TELEGRAM_BOT_TOKEN` — bot token from @BotFather
//! - `TELEGRAM_CHAT_ID`   — admin chat/group ID
//!
//! If either is unset, the adjutant logs a notice and does not start.
//!
//! ## Interactions
//!
//! | Command / Callback | Action |
//! |---------------------|--------|
//! | `/status`           | Node status with action buttons |
//! | `/receipts`         | Recent trust receipts |
//! | `/challenges`       | Active challenges with review/dismiss |
//! | `/sentinels`        | Sentinel health dashboard |
//! | Inline buttons      | Direct actions (review, dismiss, ack) |

use std::sync::{Arc, Mutex};

use exo_core::types::Did;
use serde::{Deserialize, Serialize};
use zeroize::Zeroizing;

use crate::{
    challenges::SharedChallengeStore,
    reactor::SharedReactorState,
    sentinels::{AlertReceiver, SentinelAlert, SharedSentinelState, now_ms},
    store::SqliteDagStore,
    zerodentity::store::SharedZerodentityStore,
};

// ---------------------------------------------------------------------------
// Telegram API types (minimal subset)
// ---------------------------------------------------------------------------

const TELEGRAM_HTTP_TIMEOUT_SECS: u64 = 30;
const TELEGRAM_POLL_FAILURE_BACKOFF_MS: u64 = 1_000;
const MAX_TELEGRAM_UPDATE_RESPONSE_BYTES: usize = 1024 * 1024;

#[derive(Debug, Serialize)]
struct SendMessageRequest {
    chat_id: String,
    text: String,
    parse_mode: Option<String>,
    reply_markup: Option<InlineKeyboardMarkup>,
}

#[derive(Debug, Serialize)]
struct InlineKeyboardMarkup {
    inline_keyboard: Vec<Vec<InlineKeyboardButton>>,
}

#[derive(Debug, Serialize)]
struct InlineKeyboardButton {
    text: String,
    callback_data: String,
}

#[derive(Debug, Deserialize)]
struct TelegramResponse<T> {
    ok: bool,
    result: Option<T>,
    description: Option<String>,
}

#[derive(Debug, PartialEq, Eq)]
enum TelegramUpdateParseError {
    Oversized { len: u64, max: u64 },
    Body(String),
    Json(String),
    ApiRejected { description: String },
    MissingResult,
}

impl std::fmt::Display for TelegramUpdateParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Oversized { len, max } => {
                write!(f, "telegram update response too large: {len} bytes > {max}")
            }
            Self::Body(error) => write!(f, "telegram update response body failed: {error}"),
            Self::Json(error) => write!(f, "telegram update response parse failed: {error}"),
            Self::ApiRejected { description } => {
                write!(f, "telegram API rejected update polling: {description}")
            }
            Self::MissingResult => write!(f, "telegram update response missing result field"),
        }
    }
}

fn usize_to_u64_saturating(value: usize) -> u64 {
    u64::try_from(value).unwrap_or(u64::MAX)
}

fn checked_committed_height(committed_len: usize) -> Result<u64, String> {
    u64::try_from(committed_len).map_err(|_| {
        format!("committed height {committed_len} exceeds maximum representable u64 height")
    })
}

fn u64_to_usize_cap(value: u64, cap: usize) -> usize {
    match usize::try_from(value) {
        Ok(converted) => converted.min(cap),
        Err(_) => cap,
    }
}

async fn read_bounded_response_body(
    mut resp: reqwest::Response,
    max: usize,
) -> Result<Vec<u8>, TelegramUpdateParseError> {
    let max_u64 = usize_to_u64_saturating(max);
    if let Some(content_length) = resp.content_length() {
        if content_length > max_u64 {
            return Err(TelegramUpdateParseError::Oversized {
                len: content_length,
                max: max_u64,
            });
        }
    }

    let initial_capacity = resp
        .content_length()
        .map_or(0, |len| u64_to_usize_cap(len, max));
    let mut body = Vec::with_capacity(initial_capacity);
    while let Some(chunk) = resp.chunk().await.map_err(|error| {
        TelegramUpdateParseError::Body(describe_telegram_transport_error(
            "update body read",
            &error,
        ))
    })? {
        let next_len = usize_to_u64_saturating(body.len())
            .saturating_add(usize_to_u64_saturating(chunk.len()));
        if next_len > max_u64 {
            return Err(TelegramUpdateParseError::Oversized {
                len: next_len,
                max: max_u64,
            });
        }
        body.extend_from_slice(&chunk);
    }

    Ok(body)
}

async fn read_telegram_update_body(
    resp: reqwest::Response,
) -> Result<Vec<u8>, TelegramUpdateParseError> {
    read_bounded_response_body(resp, MAX_TELEGRAM_UPDATE_RESPONSE_BYTES).await
}

fn parse_updates_response(bytes: &[u8]) -> Result<Vec<Update>, TelegramUpdateParseError> {
    let len = usize_to_u64_saturating(bytes.len());
    let max = usize_to_u64_saturating(MAX_TELEGRAM_UPDATE_RESPONSE_BYTES);
    if len > max {
        return Err(TelegramUpdateParseError::Oversized { len, max });
    }

    let parsed: TelegramResponse<Vec<Update>> = serde_json::from_slice(bytes)
        .map_err(|error| TelegramUpdateParseError::Json(error.to_string()))?;
    if !parsed.ok {
        return Err(TelegramUpdateParseError::ApiRejected {
            description: parsed
                .description
                .unwrap_or_else(|| "ok=false without description".into()),
        });
    }

    parsed.result.ok_or(TelegramUpdateParseError::MissingResult)
}

fn redact_telegram_bot_token_urls(input: &str) -> String {
    const TOKEN_URL_PREFIX: &str = "https://api.telegram.org/bot";

    let mut output = String::with_capacity(input.len());
    let mut remaining = input;
    while let Some(prefix_index) = remaining.find(TOKEN_URL_PREFIX) {
        output.push_str(&remaining[..prefix_index]);
        output.push_str(TOKEN_URL_PREFIX);
        output.push_str("<redacted>");

        let after_prefix = &remaining[prefix_index + TOKEN_URL_PREFIX.len()..];
        let Some(method_separator_index) = after_prefix.find('/') else {
            remaining = "";
            break;
        };
        remaining = &after_prefix[method_separator_index..];
    }
    output.push_str(remaining);
    output
}

fn telegram_transport_error_kind(error: &reqwest::Error) -> &'static str {
    if error.is_timeout() {
        "timeout"
    } else if error.is_connect() {
        "connect"
    } else if error.is_body() {
        "body"
    } else if error.is_decode() {
        "decode"
    } else if error.is_request() {
        "request"
    } else if error.is_status() {
        "status"
    } else {
        "transport"
    }
}

fn describe_telegram_transport_error(operation: &'static str, error: &reqwest::Error) -> String {
    let mut message = format!(
        "telegram {operation} failed: {}",
        telegram_transport_error_kind(error)
    );
    if let Some(status) = error.status() {
        message.push_str("; status ");
        message.push_str(status.as_str());
    }

    let detail = redact_telegram_bot_token_urls(&error.to_string());
    if !detail.is_empty() {
        message.push_str("; detail: ");
        message.push_str(&detail);
    }
    message
}

#[derive(Debug, Deserialize)]
pub(crate) struct Update {
    update_id: i64,
    message: Option<TgMessage>,
    callback_query: Option<CallbackQuery>,
}

#[derive(Debug, Deserialize)]
struct TgMessage {
    text: Option<String>,
    chat: TgChat,
}

#[derive(Debug, Deserialize)]
struct TgChat {
    id: i64,
}

#[derive(Debug, Deserialize)]
struct CallbackQuery {
    id: String,
    data: Option<String>,
    /// The message the inline keyboard was attached to — carries the
    /// originating chat so we can filter out queries from unauthorized
    /// chats. May be absent for very old keyboards; when absent we
    /// reject by default (fail-closed).
    #[serde(default)]
    message: Option<TgMessage>,
}

/// Return true iff `msg` came from the single authorized chat.
///
/// Fail-closed: if `expected_chat_id` is `None` (misconfigured env),
/// no message is authorized.
fn is_message_authorized(expected_chat_id: Option<i64>, msg: &TgMessage) -> bool {
    expected_chat_id == Some(msg.chat.id)
}

/// Return true iff a callback query originated in the authorized chat.
///
/// Callback queries must carry their originating `message` with a
/// `chat` field. Fail-closed: missing message OR missing
/// `expected_chat_id` rejects.
fn is_callback_authorized(expected_chat_id: Option<i64>, cb: &CallbackQuery) -> bool {
    match (&cb.message, expected_chat_id) {
        (Some(m), Some(id)) => id == m.chat.id,
        _ => false,
    }
}

// ---------------------------------------------------------------------------
// Adjutant
// ---------------------------------------------------------------------------

/// Configuration for the Telegram adjutant.
#[derive(Clone)]
pub struct AdjutantConfig {
    bot_token: Zeroizing<String>,
    pub chat_id: String,
}

impl std::fmt::Debug for AdjutantConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AdjutantConfig")
            .field("bot_token", &"<redacted>")
            .field("chat_id", &self.chat_id)
            .finish()
    }
}

impl AdjutantConfig {
    fn from_parts(bot_token: Zeroizing<String>, chat_id: String) -> Option<Self> {
        if bot_token.is_empty() || chat_id.is_empty() {
            return None;
        }

        Some(Self { bot_token, chat_id })
    }

    /// Load from environment variables.  Returns `None` if not configured.
    #[must_use]
    pub fn from_env() -> Option<Self> {
        let token = Zeroizing::new(std::env::var("TELEGRAM_BOT_TOKEN").ok()?);
        let chat_id = std::env::var("TELEGRAM_CHAT_ID").ok()?;
        Self::from_parts(token, chat_id)
    }
}

/// The Telegram adjutant — chief-of-staff bot.
pub struct Adjutant {
    config: AdjutantConfig,
    client: reqwest::Client,
    last_update_id: i64,
}

fn next_update_offset(last_update_id: i64) -> Option<i64> {
    last_update_id.checked_add(1)
}

impl Adjutant {
    /// Create a new adjutant.
    pub fn new(config: AdjutantConfig) -> Result<Self, String> {
        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(TELEGRAM_HTTP_TIMEOUT_SECS))
            .build()
            .map_err(|error| format!("telegram HTTP client: {error}"))?;

        Ok(Self {
            config,
            client,
            last_update_id: 0,
        })
    }

    /// Telegram Bot API base URL.
    fn api_url(&self, method: &str) -> Zeroizing<String> {
        Zeroizing::new(format!(
            "https://api.telegram.org/bot{}/{}",
            self.config.bot_token.as_str(),
            method
        ))
    }

    /// Send a text message with optional inline keyboard.
    pub async fn send_message(
        &self,
        text: &str,
        keyboard: Option<Vec<Vec<(&str, &str)>>>,
    ) -> Result<(), String> {
        let reply_markup = keyboard.map(|rows| InlineKeyboardMarkup {
            inline_keyboard: rows
                .into_iter()
                .map(|row| {
                    row.into_iter()
                        .map(|(label, data)| InlineKeyboardButton {
                            text: label.to_string(),
                            callback_data: data.to_string(),
                        })
                        .collect()
                })
                .collect(),
        });

        let body = SendMessageRequest {
            chat_id: self.config.chat_id.clone(),
            text: text.to_string(),
            parse_mode: Some("HTML".to_string()),
            reply_markup,
        };

        let url = self.api_url("sendMessage");
        self.client
            .post(url.as_str())
            .json(&body)
            .send()
            .await
            .map_err(|e| describe_telegram_transport_error("send", &e))?;

        Ok(())
    }

    /// Send a message, logging a warning on failure instead of silently dropping.
    pub async fn send_or_log(&self, text: &str, keyboard: Option<Vec<Vec<(&str, &str)>>>) {
        if let Err(e) = self.send_message(text, keyboard).await {
            tracing::warn!(err = %e, "Telegram message delivery failed");
        }
    }

    async fn poll_updates_result(&mut self) -> Result<Vec<Update>, String> {
        let base_url = self.api_url("getUpdates");
        let Some(offset) = next_update_offset(self.last_update_id) else {
            return Err(format!(
                "telegram update offset overflow after update id {}",
                self.last_update_id
            ));
        };
        let url = Zeroizing::new(format!(
            "{}?offset={}&timeout=10",
            base_url.as_str(),
            offset
        ));

        let resp = match self.client.get(url.as_str()).send().await {
            Ok(r) => r,
            Err(e) => return Err(describe_telegram_transport_error("poll", &e)),
        };

        let bytes = match read_telegram_update_body(resp).await {
            Ok(bytes) => bytes,
            Err(e) => return Err(format!("telegram update body read failed: {e}")),
        };

        let updates = match parse_updates_response(bytes.as_ref()) {
            Ok(updates) => updates,
            Err(e) => return Err(format!("telegram update response rejected: {e}")),
        };

        if let Some(last) = updates.last() {
            self.last_update_id = last.update_id;
        }

        Ok(updates)
    }

    /// Acknowledge a callback query (removes the "loading" indicator).
    pub async fn answer_callback(&self, callback_id: &str) {
        let url = self.api_url("answerCallbackQuery");
        if let Err(e) = self
            .client
            .post(url.as_str())
            .json(&serde_json::json!({ "callback_query_id": callback_id }))
            .send()
            .await
        {
            let error = describe_telegram_transport_error("callback acknowledgement", &e);
            tracing::debug!(err = %error, "Telegram callback acknowledgement failed");
        }
    }

    /// Send a sentinel alert with action buttons.
    pub async fn send_alert(&self, alert: &SentinelAlert) {
        let emoji = match alert.severity {
            crate::sentinels::Severity::Critical => "\u{1f6a8}", // 🚨
            crate::sentinels::Severity::Warning => "\u{26a0}\u{fe0f}", // ⚠️
            crate::sentinels::Severity::Info => "\u{2139}\u{fe0f}", // ℹ️
        };
        let check = escape_telegram_html(&alert.check.to_string());
        let message = escape_telegram_html(&alert.message);

        let text = format!(
            "{emoji} <b>SENTINEL: {}</b>\n{}\n\nSeverity: {:?}",
            check, message, alert.severity
        );

        let keyboard = vec![vec![
            ("\u{2705} Acknowledge", "sentinel:ack"),
            ("\u{1f50d} Details", "cmd:sentinels"),
        ]];

        self.send_or_log(&text, Some(keyboard)).await;
    }
}

// ---------------------------------------------------------------------------
// Message builders
// ---------------------------------------------------------------------------

/// Escape dynamic text inserted into Telegram messages sent with HTML parse mode.
fn escape_telegram_html(input: &str) -> String {
    let mut escaped = String::with_capacity(input.len());
    for ch in input.chars() {
        match ch {
            '&' => escaped.push_str("&amp;"),
            '<' => escaped.push_str("&lt;"),
            '>' => escaped.push_str("&gt;"),
            '"' => escaped.push_str("&quot;"),
            '\'' => escaped.push_str("&#39;"),
            _ => escaped.push(ch),
        }
    }
    escaped
}

fn escape_telegram_alert_did(did: &Did) -> String {
    let did_str = did.as_str();
    if did_str.chars().count() <= MAX_ZERODENTITY_ALERT_DID_DISPLAY_CHARS {
        return escape_telegram_html(did_str);
    }

    let mut display = did_str
        .chars()
        .take(MAX_ZERODENTITY_ALERT_DID_DISPLAY_CHARS)
        .collect::<String>();
    display.push_str("...");
    escape_telegram_html(&display)
}

/// Format basis-point value as "XX.YY" (e.g. 5250 → "52.50").
fn fmt_bp(bp: u32) -> String {
    format!("{}.{:02}", bp / 100, bp % 100)
}

type TelegramKeyboard = Vec<Vec<(&'static str, &'static str)>>;
type TelegramMessage = (String, TelegramKeyboard);

/// Build the `/0dentity <did>` response.
///
/// Shows the 8-axis polar table, composite, symmetry and claim count.
/// Spec §10.5.
pub fn build_zerodentity_score_message(
    zerodentity: &SharedZerodentityStore,
    did_str: &str,
) -> (String, Vec<Vec<(&'static str, &'static str)>>) {
    let did = match Did::new(did_str) {
        Ok(d) => d,
        Err(_) => {
            let did_html = escape_telegram_html(did_str);
            return (
                format!("\u{274c} Invalid DID: <code>{did_html}</code>"),
                vec![],
            );
        }
    };
    let did_html = escape_telegram_html(did.as_str());

    let zstore = match zerodentity.lock() {
        Ok(s) => s,
        Err(_) => {
            return (
                "\u{274c} 0dentity store temporarily unavailable".to_string(),
                vec![],
            );
        }
    };
    let score = match zstore.get_score(&did) {
        Some(s) => s.clone(),
        None => {
            return (
                format!(
                    "\u{1f194} <b>0dentity Score</b>\n\
                     No score data for <code>{did_html}</code>"
                ),
                vec![],
            );
        }
    };
    drop(zstore);

    let a = &score.axes;
    let text = format!(
        "\u{1f194} <b>0dentity Score</b>\n\
         <code>{did_html}</code>\n\
         \u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\n\
         Communication:       {}\n\
         CredentialDepth:     {}\n\
         DeviceTrust:         {}\n\
         Behavioral:          {}\n\
         NetworkReputation:   {}\n\
         TemporalStability:   {}\n\
         CryptographicStr:    {}\n\
         Constitutional:      {}\n\
         \u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\n\
         Composite: <b>{}</b> | Symmetry: {}\n\
         Claims: {} verified",
        fmt_bp(a.communication),
        fmt_bp(a.credential_depth),
        fmt_bp(a.device_trust),
        fmt_bp(a.behavioral_signature),
        fmt_bp(a.network_reputation),
        fmt_bp(a.temporal_stability),
        fmt_bp(a.cryptographic_strength),
        fmt_bp(a.constitutional_standing),
        fmt_bp(score.composite),
        fmt_bp(score.symmetry),
        score.claim_count,
    );

    let keyboard = vec![vec![
        ("\u{1f504} Refresh", "cmd:sentinels"),
        ("\u{1f6e1}\u{fe0f} Sentinels", "cmd:sentinels"),
    ]];

    (text, keyboard)
}

/// Severity threshold constants for `/0dentity-alerts`.
/// Composite drop > 1500 bp (= 15.00 pts).
const ALERT_COMPOSITE_DROP_BP: u32 = 1_500;
/// Fingerprint consistency below 2000 bp (= 20.00%).
const ALERT_FINGERPRINT_LOW_BP: u32 = 2_000;
/// OTP lockout window: 24 hours in ms.
const ALERT_OTP_WINDOW_MS: u64 = 86_400_000;
/// Maximum scored DIDs loaded from the 0dentity store in one scan page.
const MAX_ZERODENTITY_ALERT_SCAN_PAGE_DIDS: usize = 1_000;
/// Maximum 0dentity score pages evaluated by one Telegram alert request.
const MAX_ZERODENTITY_ALERT_SCAN_PAGES: usize = 4;
/// Maximum alert lines included in one Telegram response.
const MAX_ZERODENTITY_ALERT_MESSAGE_ITEMS: usize = 64;
/// Maximum DID characters rendered in a single 0dentity alert line.
const MAX_ZERODENTITY_ALERT_DID_DISPLAY_CHARS: usize = 96;

/// Build the `/0dentity-alerts` response.
///
/// Scans all scored DIDs and flags:
/// - Composite drop > 15 pts (1500 bp) since last snapshot
/// - Fingerprint consistency < 20% (2000 bp)
/// - OTP lockout in the last 24 h
///
/// Spec §10.5.
pub fn build_zerodentity_alerts_message(
    zerodentity: &SharedZerodentityStore,
) -> (String, Vec<Vec<(&'static str, &'static str)>>) {
    let zstore = match zerodentity.lock() {
        Ok(s) => s,
        Err(_) => {
            return (
                "\u{274c} 0dentity store temporarily unavailable".to_string(),
                vec![],
            );
        }
    };
    let scored_did_count = zstore.scored_did_count();
    drop(zstore);

    let since_ms = now_ms().saturating_sub(ALERT_OTP_WINDOW_MS);
    let mut alerts: Vec<String> = Vec::new();
    let mut total_alert_count = 0usize;
    let mut scanned_did_count = 0usize;
    let mut pages_scanned = 0usize;
    let mut after_did: Option<Did> = None;

    loop {
        if pages_scanned >= MAX_ZERODENTITY_ALERT_SCAN_PAGES {
            break;
        }

        let zstore = match zerodentity.lock() {
            Ok(s) => s,
            Err(_) => {
                return (
                    "\u{274c} 0dentity store temporarily unavailable".to_string(),
                    vec![],
                );
            }
        };
        let dids =
            zstore.scored_dids_page_after(after_did.as_ref(), MAX_ZERODENTITY_ALERT_SCAN_PAGE_DIDS);
        drop(zstore);

        if dids.is_empty() {
            break;
        }

        pages_scanned = pages_scanned.saturating_add(1);
        scanned_did_count = scanned_did_count.saturating_add(dids.len());
        after_did = dids.last().cloned();

        for did in dids {
            let (current_score, previous_score, fingerprints, has_recent_otp_lockout) = {
                let zstore = match zerodentity.lock() {
                    Ok(s) => s,
                    Err(_) => {
                        return (
                            "\u{274c} 0dentity store temporarily unavailable".to_string(),
                            vec![],
                        );
                    }
                };
                let current_score = zstore.get_score(&did).cloned();
                let previous_score = zstore.get_previous_score(&did).cloned();
                let fingerprints = match zstore.get_fingerprints(&did) {
                    Ok(fps) => fps,
                    Err(e) => {
                        let did_html = escape_telegram_alert_did(&did);
                        let error_html = escape_telegram_html(&e.to_string());
                        return (
                            format!(
                                "\u{274c} <b>0dentity Alerts</b>\n\
                                 0dentity alert scan unavailable while reading fingerprints for <code>{}</code>: {}",
                                did_html, error_html
                            ),
                            vec![],
                        );
                    }
                };
                let has_recent_otp_lockout = zstore.has_otp_lockout_since(&did, since_ms);

                (
                    current_score,
                    previous_score,
                    fingerprints,
                    has_recent_otp_lockout,
                )
            };

            // 1. Score regression.
            if let (Some(curr), Some(prev)) = (current_score, previous_score) {
                if prev.composite > curr.composite
                    && prev.composite - curr.composite > ALERT_COMPOSITE_DROP_BP
                {
                    let did_html = escape_telegram_alert_did(&did);
                    total_alert_count = total_alert_count.saturating_add(1);
                    if alerts.len() < MAX_ZERODENTITY_ALERT_MESSAGE_ITEMS {
                        alerts.push(format!(
                            "\u{26a0}\u{fe0f} <code>{}</code> score dropped {} bp ({}\u{2192}{})",
                            did_html,
                            prev.composite - curr.composite,
                            fmt_bp(prev.composite),
                            fmt_bp(curr.composite),
                        ));
                    }
                }
            }

            // 2. Fingerprint consistency.
            if let Some(latest) = fingerprints.last() {
                if let Some(consistency) = latest.consistency_score_bp {
                    if consistency < ALERT_FINGERPRINT_LOW_BP {
                        let did_html = escape_telegram_alert_did(&did);
                        total_alert_count = total_alert_count.saturating_add(1);
                        if alerts.len() < MAX_ZERODENTITY_ALERT_MESSAGE_ITEMS {
                            alerts.push(format!(
                                "\u{26a0}\u{fe0f} <code>{}</code> fingerprint consistency low: {}",
                                did_html,
                                fmt_bp(consistency),
                            ));
                        }
                    }
                }
            }

            // 3. OTP lockout in last 24h.
            if has_recent_otp_lockout {
                let did_html = escape_telegram_alert_did(&did);
                total_alert_count = total_alert_count.saturating_add(1);
                if alerts.len() < MAX_ZERODENTITY_ALERT_MESSAGE_ITEMS {
                    alerts.push(format!(
                        "\u{1f512} <code>{}</code> OTP lockout in last 24h",
                        did_html,
                    ));
                }
            }
        }
    }

    let incomplete_scan_note = if scored_did_count > scanned_did_count {
        format!(
            "\nScan paused after {} of {} scored DIDs.\n\
             Unscanned DIDs may still have active alerts.",
            scanned_did_count, scored_did_count
        )
    } else {
        String::new()
    };

    let output_cap_note = if total_alert_count > alerts.len() {
        format!(
            "\nShowing first {} of {} alert(s) found in scanned DIDs.",
            alerts.len(),
            total_alert_count
        )
    } else {
        String::new()
    };

    let text = if total_alert_count == 0 && !incomplete_scan_note.is_empty() {
        format!(
            "\u{26a0}\u{fe0f} <b>0dentity Alerts</b>\n\
             \u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\n\
             No alerts found in scanned DIDs.{incomplete_scan_note}",
        )
    } else if total_alert_count == 0 {
        "\u{2705} <b>0dentity Alerts</b>\n\
             \u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\n\
             No 0dentity alerts."
            .to_string()
    } else {
        let body = alerts.join("\n");
        format!(
            "\u{1f6a8} <b>0dentity Alerts</b>\n\
             \u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\n\
             {body}\n\
             \u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\n\
             {total_alert_count} alert(s) found.{output_cap_note}{incomplete_scan_note}"
        )
    };

    let keyboard = vec![vec![
        ("\u{1f504} Refresh", "0d_alerts"),
        ("\u{1f6e1}\u{fe0f} Sentinels", "cmd:sentinels"),
    ]];

    (text, keyboard)
}

/// Build the /status response.
pub fn build_status_message(
    reactor: &SharedReactorState,
    store: &Arc<Mutex<SqliteDagStore>>,
) -> (String, Vec<Vec<(&'static str, &'static str)>>) {
    let (round, height, validator_count, is_validator) = match reactor.lock() {
        Ok(s) => match checked_committed_height(s.consensus.committed.len()) {
            Ok(height) => (
                s.consensus.current_round,
                height,
                s.consensus.config.validators.len(),
                s.is_validator,
            ),
            Err(e) => {
                let error_html = escape_telegram_html(&e);
                return (
                    format!("\u{274c} Reactor height unavailable: {error_html}"),
                    vec![],
                );
            }
        },
        Err(_) => {
            return (
                "\u{274c} Reactor state temporarily unavailable".to_string(),
                vec![],
            );
        }
    };

    let store_height = match store.lock() {
        Ok(st) => match st.committed_height_value() {
            Ok(height) => height,
            Err(e) => {
                let error_html = escape_telegram_html(&e.to_string());
                return (
                    format!("\u{274c} Store height unavailable: {error_html}"),
                    vec![],
                );
            }
        },
        Err(_) => {
            return (
                "\u{274c} Store state temporarily unavailable".to_string(),
                vec![],
            );
        }
    };

    let role = if is_validator {
        "Validator"
    } else {
        "Observer"
    };

    let text = format!(
        "\u{1f4ca} <b>EXOCHAIN Node Status</b>\n\
         \u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\n\
         Round: <code>{round}</code> | Height: <code>{height}</code>\n\
         Store Height: <code>{store_height}</code>\n\
         Validators: <code>{validator_count}</code> | Role: {role}",
    );

    let keyboard = vec![
        vec![
            ("\u{1f4dd} Receipts", "cmd:receipts"),
            ("\u{26a0}\u{fe0f} Challenges", "cmd:challenges"),
        ],
        vec![
            ("\u{1f6e1}\u{fe0f} Sentinels", "cmd:sentinels"),
            ("\u{1f504} Refresh", "cmd:status"),
        ],
    ];

    (text, keyboard)
}

/// Build the /sentinels response.
pub fn build_sentinels_message(
    sentinel_state: &SharedSentinelState,
) -> (String, Vec<Vec<(&'static str, &'static str)>>) {
    let statuses = match sentinel_state.lock() {
        Ok(s) => s,
        Err(_) => {
            return (
                "\u{274c} Sentinel state temporarily unavailable".to_string(),
                vec![],
            );
        }
    };

    let mut text = String::from(
        "\u{1f6e1}\u{fe0f} <b>Sentinel Status</b>\n\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\n",
    );

    if statuses.is_empty() {
        text.push_str("No sentinel data yet — checks run every 30s.");
    } else {
        for s in statuses.iter() {
            let icon = if s.healthy { "\u{2705}" } else { "\u{274c}" };
            let check = escape_telegram_html(&s.check.to_string());
            let message = escape_telegram_html(&s.message);
            text.push_str(&format!("{icon} <b>{check}</b>: {message}\n"));
        }
    }

    let keyboard = vec![vec![
        ("\u{1f504} Refresh", "cmd:sentinels"),
        ("\u{1f4ca} Status", "cmd:status"),
    ]];

    (text, keyboard)
}

/// Build the /challenges response.
pub fn build_challenges_message(
    challenge_store: &SharedChallengeStore,
) -> (String, Vec<Vec<(&'static str, &'static str)>>) {
    let st = match challenge_store.lock() {
        Ok(s) => s,
        Err(_) => {
            return (
                "\u{274c} Challenge store temporarily unavailable".to_string(),
                vec![],
            );
        }
    };
    let holds = st.list();

    let mut text = String::from(
        "\u{26a0}\u{fe0f} <b>Active Challenges</b>\n\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\n",
    );

    if holds.is_empty() {
        text.push_str("No active challenges.");
    } else {
        for h in holds {
            let id = h.id.to_string();
            let id_short = escape_telegram_html(&id[..8]);
            let ground = escape_telegram_html(&h.ground.to_string());
            let status = escape_telegram_html(h.status.as_str());
            text.push_str(&format!(
                "\u{2022} <code>{id_short}</code>\n  Ground: {ground}\n  Status: {status}\n\n",
            ));
        }
    }

    let keyboard = vec![vec![
        ("\u{1f504} Refresh", "cmd:challenges"),
        ("\u{1f4ca} Status", "cmd:status"),
    ]];

    (text, keyboard)
}

fn telegram_message_builder_failed(
    label: &'static str,
    error: tokio::task::JoinError,
) -> TelegramMessage {
    tracing::error!(%label, err = %error, "Telegram message builder task failed");
    (
        "\u{274c} Telegram message builder temporarily unavailable".to_string(),
        vec![],
    )
}

async fn status_message_blocking(
    reactor: SharedReactorState,
    store: Arc<Mutex<SqliteDagStore>>,
) -> TelegramMessage {
    tokio::task::spawn_blocking(move || build_status_message(&reactor, &store))
        .await
        .unwrap_or_else(|e| telegram_message_builder_failed("status", e))
}

async fn sentinels_message_blocking(state: SharedSentinelState) -> TelegramMessage {
    tokio::task::spawn_blocking(move || build_sentinels_message(&state))
        .await
        .unwrap_or_else(|e| telegram_message_builder_failed("sentinels", e))
}

async fn challenges_message_blocking(challenge_store: SharedChallengeStore) -> TelegramMessage {
    tokio::task::spawn_blocking(move || build_challenges_message(&challenge_store))
        .await
        .unwrap_or_else(|e| telegram_message_builder_failed("challenges", e))
}

async fn zerodentity_score_message_blocking(
    zerodentity: SharedZerodentityStore,
    did_str: String,
) -> TelegramMessage {
    tokio::task::spawn_blocking(move || build_zerodentity_score_message(&zerodentity, &did_str))
        .await
        .unwrap_or_else(|e| telegram_message_builder_failed("0dentity-score", e))
}

async fn zerodentity_alerts_message_blocking(
    zerodentity: SharedZerodentityStore,
) -> TelegramMessage {
    tokio::task::spawn_blocking(move || build_zerodentity_alerts_message(&zerodentity))
        .await
        .unwrap_or_else(|e| telegram_message_builder_failed("0dentity-alerts", e))
}

// ---------------------------------------------------------------------------
// Main adjutant loop
// ---------------------------------------------------------------------------

/// Run the Telegram adjutant as a background Tokio task.
///
/// Handles:
/// 1. Incoming commands from the operator (`/status`, `/receipts`, etc.)
/// 2. Callback queries from inline keyboard buttons
/// 3. Sentinel alerts forwarded from the alert channel
#[allow(clippy::too_many_arguments)]
pub async fn run_adjutant(
    mut adjutant: Adjutant,
    mut alert_rx: AlertReceiver,
    reactor: SharedReactorState,
    store: Arc<Mutex<SqliteDagStore>>,
    challenge_store: SharedChallengeStore,
    sentinel_state: SharedSentinelState,
    zerodentity: SharedZerodentityStore,
) {
    // Announce startup.
    adjutant
        .send_or_log(
            "\u{1f916} <b>EXOCHAIN Adjutant Online</b>\n\nType /status for node overview.",
            Some(vec![vec![
                ("\u{1f4ca} Status", "cmd:status"),
                ("\u{1f6e1}\u{fe0f} Sentinels", "cmd:sentinels"),
            ]]),
        )
        .await;

    loop {
        tokio::select! {
            // Forward sentinel alerts to Telegram.
            Some(alert) = alert_rx.recv() => {
                adjutant.send_alert(&alert).await;
            }

            // Poll for Telegram updates.
            poll_result = adjutant.poll_updates_result() => {
                let updates = match poll_result {
                    Ok(updates) => updates,
                    Err(e) => {
                        tracing::debug!(err = %e, "Telegram poll failed; backing off before retry");
                        tokio::time::sleep(std::time::Duration::from_millis(
                            TELEGRAM_POLL_FAILURE_BACKOFF_MS,
                        ))
                        .await;
                        Vec::new()
                    }
                };

                // Parse the configured authorized chat id once per batch.
                // If it fails to parse (misconfigured env), we fail-closed:
                // no commands are dispatched.
                let expected_chat_id: Option<i64> =
                    adjutant.config.chat_id.parse::<i64>().ok();
                if expected_chat_id.is_none() {
                    tracing::error!(
                        configured = %adjutant.config.chat_id,
                        "TELEGRAM_CHAT_ID is not a valid i64 — rejecting ALL inbound updates (fail-closed)"
                    );
                }

                for update in updates {
                    // Handle text commands.
                    if let Some(msg) = &update.message {
                        // GAP-015 defense: reject messages from any chat other
                        // than the configured authorized chat. Without this,
                        // any holder of the bot token could DM the bot and
                        // receive full node internal state.
                        if !is_message_authorized(expected_chat_id, msg) {
                            tracing::warn!(
                                incoming_chat = msg.chat.id,
                                expected = %adjutant.config.chat_id,
                                "Rejected Telegram message from unauthorized chat"
                            );
                        } else if let Some(text) = &msg.text {
                            handle_command(
                                &adjutant,
                                text,
                                &reactor,
                                &store,
                                &challenge_store,
                                &sentinel_state,
                                &zerodentity,
                            )
                            .await;
                        }
                    }

                    // Handle callback queries (button presses).
                    if let Some(cb) = &update.callback_query {
                        // Callback queries must carry an originating message
                        // whose chat matches. Missing chat info = reject
                        // (fail-closed).
                        if !is_callback_authorized(expected_chat_id, cb) {
                            tracing::warn!(
                                callback_id = %cb.id,
                                "Rejected Telegram callback from unauthorized or unknown chat"
                            );
                            // Still answer the callback so the user's UI
                            // clears (prevents their Telegram from showing a
                            // perpetual spinner), but don't dispatch.
                            adjutant.answer_callback(&cb.id).await;
                        } else {
                            adjutant.answer_callback(&cb.id).await;
                            if let Some(data) = &cb.data {
                                handle_callback(
                                    &adjutant,
                                    data,
                                    &reactor,
                                    &store,
                                    &challenge_store,
                                    &sentinel_state,
                                    &zerodentity,
                                )
                                .await;
                            }
                        }
                    }
                }
            }
        }
    }
}

async fn handle_command(
    adjutant: &Adjutant,
    text: &str,
    reactor: &SharedReactorState,
    store: &Arc<Mutex<SqliteDagStore>>,
    challenge_store: &SharedChallengeStore,
    sentinel_state: &SharedSentinelState,
    zerodentity: &SharedZerodentityStore,
) {
    let mut parts = text.split_whitespace();
    let cmd = parts.next().unwrap_or("");
    match cmd {
        "/status" | "/start" => {
            let (msg, kb) = status_message_blocking(Arc::clone(reactor), Arc::clone(store)).await;
            adjutant.send_or_log(&msg, Some(kb)).await;
        }
        "/sentinels" => {
            let (msg, kb) = sentinels_message_blocking(Arc::clone(sentinel_state)).await;
            adjutant.send_or_log(&msg, Some(kb)).await;
        }
        "/challenges" => {
            let (msg, kb) = challenges_message_blocking(Arc::clone(challenge_store)).await;
            adjutant.send_or_log(&msg, Some(kb)).await;
        }
        "/0dentity" => {
            let did_str = parts.next().unwrap_or("");
            if did_str.is_empty() {
                adjutant
                    .send_or_log(
                        "Usage: /0dentity &lt;did&gt;\nExample: /0dentity did:exo:alice",
                        None,
                    )
                    .await;
            } else {
                let (msg, kb) = zerodentity_score_message_blocking(
                    Arc::clone(zerodentity),
                    did_str.to_string(),
                )
                .await;
                adjutant.send_or_log(&msg, Some(kb)).await;
            }
        }
        "/0dentity-alerts" => {
            let (msg, kb) = zerodentity_alerts_message_blocking(Arc::clone(zerodentity)).await;
            adjutant.send_or_log(&msg, Some(kb)).await;
        }
        "/help" => {
            adjutant
                .send_or_log(
                    "\u{1f4d6} <b>Commands</b>\n\
                     /status — Node overview\n\
                     /sentinels — Health checks\n\
                     /challenges — Active disputes\n\
                     /0dentity &lt;did&gt; — Identity score for a DID\n\
                     /0dentity-alerts — Active 0dentity alerts\n\
                     /help — This message",
                    None,
                )
                .await;
        }
        _ => {}
    }
}

async fn handle_callback(
    adjutant: &Adjutant,
    data: &str,
    reactor: &SharedReactorState,
    store: &Arc<Mutex<SqliteDagStore>>,
    challenge_store: &SharedChallengeStore,
    sentinel_state: &SharedSentinelState,
    zerodentity: &SharedZerodentityStore,
) {
    if let Some(did_str) = data.strip_prefix("0d_score:") {
        let (msg, kb) =
            zerodentity_score_message_blocking(Arc::clone(zerodentity), did_str.to_string()).await;
        adjutant.send_or_log(&msg, Some(kb)).await;
        return;
    }
    match data {
        "cmd:status" => {
            let (msg, kb) = status_message_blocking(Arc::clone(reactor), Arc::clone(store)).await;
            adjutant.send_or_log(&msg, Some(kb)).await;
        }
        "cmd:sentinels" => {
            let (msg, kb) = sentinels_message_blocking(Arc::clone(sentinel_state)).await;
            adjutant.send_or_log(&msg, Some(kb)).await;
        }
        "cmd:challenges" => {
            let (msg, kb) = challenges_message_blocking(Arc::clone(challenge_store)).await;
            adjutant.send_or_log(&msg, Some(kb)).await;
        }
        "0d_alerts" => {
            let (msg, kb) = zerodentity_alerts_message_blocking(Arc::clone(zerodentity)).await;
            adjutant.send_or_log(&msg, Some(kb)).await;
        }
        "sentinel:ack" => {
            adjutant
                .send_or_log("\u{2705} Alert acknowledged.", None)
                .await;
        }
        _ => {
            tracing::debug!(callback_data = %data, "Unknown callback");
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use std::collections::BTreeSet;

    use exo_core::types::{Did, Signature};

    use super::*;
    use crate::{
        challenges::ChallengeStore,
        reactor::{ReactorConfig, create_reactor_state},
        sentinels::{SentinelCheck, SentinelStatus},
        store::SqliteDagStore,
    };

    fn make_sign_fn() -> Arc<dyn Fn(&[u8]) -> Signature + Send + Sync> {
        Arc::new(|data: &[u8]| {
            let h = blake3::hash(data);
            let mut sig = [0u8; 64];
            sig[..32].copy_from_slice(h.as_bytes());
            Signature::from_bytes(sig)
        })
    }

    async fn response_from_raw_http(raw: Vec<u8>) -> reqwest::Response {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let _server = tokio::spawn(async move {
            let (mut stream, _) = listener.accept().await.unwrap();
            let mut request = [0_u8; 1024];
            let _bytes_read = tokio::io::AsyncReadExt::read(&mut stream, &mut request).await;
            tokio::io::AsyncWriteExt::write_all(&mut stream, &raw)
                .await
                .unwrap();
        });

        reqwest::Client::new()
            .get(format!("http://{addr}/updates"))
            .send()
            .await
            .unwrap()
    }

    fn score_snapshot(
        did: &Did,
        composite: u32,
        computed_ms: u64,
    ) -> crate::zerodentity::types::ZerodentityScore {
        let mut score =
            crate::zerodentity::types::ZerodentityScore::compute(did, &[], &[], &[], computed_ms);
        score.composite = composite;
        score
    }

    fn test_reactor() -> SharedReactorState {
        let validators: BTreeSet<Did> = (0..4)
            .map(|i| Did::new(&format!("did:exo:v{i}")).unwrap())
            .collect();
        let config = ReactorConfig {
            node_did: Did::new("did:exo:v0").unwrap(),
            is_validator: true,
            validators,
            validator_public_keys: std::collections::BTreeMap::new(),
            round_timeout_ms: 5000,
        };
        create_reactor_state(&config, make_sign_fn(), None)
    }

    #[test]
    fn telegram_html_escape_encodes_special_chars() {
        assert_eq!(
            escape_telegram_html("<b>owned</b>&\"'"),
            "&lt;b&gt;owned&lt;/b&gt;&amp;&quot;&#39;"
        );
    }

    #[test]
    fn zerodentity_score_message_escapes_invalid_did_html() {
        let zerodentity = crate::zerodentity::store::new_shared_store();

        let (text, keyboard) =
            build_zerodentity_score_message(&zerodentity, "did:exo:<b>owned</b>&x");

        assert!(keyboard.is_empty());
        assert!(text.contains("&lt;b&gt;owned&lt;/b&gt;&amp;x"));
        assert!(!text.contains("<b>owned</b>&x"));
    }

    #[test]
    fn sentinels_message_escapes_status_text_html() {
        let sentinel_state = Arc::new(Mutex::new(vec![SentinelStatus {
            check: SentinelCheck::Liveness,
            healthy: false,
            message: "<b>owned</b>&\"'".to_string(),
            last_run_ms: 1,
        }]));

        let (text, keyboard) = build_sentinels_message(&sentinel_state);

        assert!(!keyboard.is_empty());
        assert!(text.contains("&lt;b&gt;owned&lt;/b&gt;&amp;&quot;&#39;"));
        assert!(!text.contains("<b>owned</b>&\"'"));
    }

    #[test]
    fn zerodentity_alerts_do_not_discard_store_read_errors() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("// ---------------------------------------------------------------------------\n// Tests")
            .next()
            .unwrap();
        let alerts = production
            .split("pub fn build_zerodentity_alerts_message")
            .nth(1)
            .and_then(|section| section.split("/// Build the /sentinels response.").next())
            .unwrap();

        assert!(!alerts.contains(".unwrap_or_default()"));
    }

    #[test]
    fn telegram_delivery_paths_do_not_silently_discard_send_failures() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("// ---------------------------------------------------------------------------\n// Tests")
            .next()
            .unwrap();

        for forbidden in [
            "let _ = adjutant\n        .send_message(",
            "let _ = adjutant\n                    .send_message(",
            "let _ = adjutant.send_message(",
            "let _ = self\n            .client\n            .post(url.as_str())",
        ] {
            assert!(
                !production.contains(forbidden),
                "Telegram delivery failures must be observed or logged: {forbidden}"
            );
        }
    }

    #[test]
    fn zerodentity_alerts_do_not_request_unbounded_score_sample() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("// ---------------------------------------------------------------------------\n// Tests")
            .next()
            .unwrap();
        let alerts = production
            .split("pub fn build_zerodentity_alerts_message")
            .nth(1)
            .and_then(|section| section.split("/// Build the /status response.").next())
            .unwrap();

        assert!(
            alerts.contains("scored_dids_page_after")
                && !alerts.contains("sample_scored_dids")
                && !alerts.contains("usize::MAX"),
            "Telegram 0dentity alerts must use bounded DID pages rather than an unbounded or prefix-only sample"
        );
        assert!(
            alerts.contains("MAX_ZERODENTITY_ALERT_SCAN_PAGES")
                && alerts.contains("MAX_ZERODENTITY_ALERT_MESSAGE_ITEMS"),
            "Telegram 0dentity alerts must bound both per-request scan work and message output"
        );
    }

    #[test]
    fn zerodentity_alerts_release_initial_store_lock_before_scanning_dids() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("// ---------------------------------------------------------------------------\n// Tests")
            .next()
            .unwrap();
        let alerts = production
            .split("pub fn build_zerodentity_alerts_message")
            .nth(1)
            .and_then(|section| section.split("/// Build the /status response.").next())
            .unwrap();
        let sample_index = alerts
            .find("scored_dids_page_after")
            .expect("alert builder pages scored DIDs");
        let loop_index = alerts
            .find("for did in dids")
            .expect("alert builder iterates paged DIDs");

        assert!(
            alerts[sample_index..loop_index].contains("drop(zstore)"),
            "Telegram 0dentity alerts must drop the initial store lock before per-DID scanning"
        );
    }

    #[test]
    fn zerodentity_alerts_scan_across_bounded_pages() {
        let zerodentity = crate::zerodentity::store::new_shared_store();
        let scan_limit = 1_000;
        {
            let mut store = zerodentity.lock().unwrap();
            for i in 0..=scan_limit {
                let did = Did::new(&format!("did:exo:alert{i:04}")).unwrap();
                store.put_score(score_snapshot(&did, 9_000, 1000)).unwrap();
                store.put_score(score_snapshot(&did, 7_000, 2000)).unwrap();
            }
        }

        let (text, keyboard) = build_zerodentity_alerts_message(&zerodentity);

        assert!(text.contains("Showing first"));
        assert!(text.contains("1001 alert(s) found in scanned DIDs."));
        assert!(text.contains("did:exo:alert0000"));
        assert!(!text.contains("did:exo:alert1000"));
        assert!(!text.contains("No 0dentity alerts."));
        assert!(!keyboard.is_empty());
    }

    #[test]
    fn zerodentity_alerts_scans_beyond_first_bounded_page() {
        let zerodentity = crate::zerodentity::store::new_shared_store();
        {
            let mut store = zerodentity.lock().unwrap();
            for i in 0..MAX_ZERODENTITY_ALERT_SCAN_PAGE_DIDS {
                let did = Did::new(&format!("did:exo:prefix{i:04}")).unwrap();
                store.put_score(score_snapshot(&did, 9_000, 1000)).unwrap();
            }

            let hidden_did = Did::new("did:exo:z-after-prefix").unwrap();
            store
                .put_score(score_snapshot(&hidden_did, 9_000, 1000))
                .unwrap();
            store
                .put_score(score_snapshot(&hidden_did, 7_000, 2000))
                .unwrap();
        }

        let (text, keyboard) = build_zerodentity_alerts_message(&zerodentity);

        assert!(text.contains("did:exo:z-after-prefix"));
        assert!(text.contains("1 alert(s) found."));
        assert!(!text.contains("No 0dentity alerts."));
        assert!(!keyboard.is_empty());
    }

    #[test]
    fn zerodentity_alerts_pauses_after_bounded_pages_without_false_all_clear() {
        let zerodentity = crate::zerodentity::store::new_shared_store();
        let expected_scan_cap =
            MAX_ZERODENTITY_ALERT_SCAN_PAGE_DIDS * MAX_ZERODENTITY_ALERT_SCAN_PAGES;
        {
            let mut store = zerodentity.lock().unwrap();
            for i in 0..expected_scan_cap {
                let did = Did::new(&format!("did:exo:prefix{i:04}")).unwrap();
                store.put_score(score_snapshot(&did, 9_000, 1000)).unwrap();
            }

            let hidden_did = Did::new("did:exo:z-after-scan-cap").unwrap();
            store
                .put_score(score_snapshot(&hidden_did, 9_000, 1000))
                .unwrap();
            store
                .put_score(score_snapshot(&hidden_did, 7_000, 2000))
                .unwrap();
        }

        let (text, keyboard) = build_zerodentity_alerts_message(&zerodentity);

        assert!(text.contains(&format!(
            "Scan paused after {expected_scan_cap} of {} scored DIDs.",
            expected_scan_cap + 1
        )));
        assert!(text.contains("Unscanned DIDs may still have active alerts."));
        assert!(!text.contains("No 0dentity alerts."));
        assert!(!text.contains("did:exo:z-after-scan-cap"));
        assert!(!keyboard.is_empty());
    }

    #[test]
    fn zerodentity_alerts_truncate_long_dids_before_message_rendering() {
        let zerodentity = crate::zerodentity::store::new_shared_store();
        let long_did_string = format!("did:exo:{}", "a".repeat(5_000));
        let long_did = Did::new(&long_did_string).unwrap();
        {
            let mut store = zerodentity.lock().unwrap();
            store
                .put_score(score_snapshot(&long_did, 9_000, 1000))
                .unwrap();
            store
                .put_score(score_snapshot(&long_did, 7_000, 2000))
                .unwrap();
        }

        let (text, keyboard) = build_zerodentity_alerts_message(&zerodentity);

        assert!(text.contains("did:exo:"));
        assert!(text.contains("..."));
        assert!(!text.contains(&long_did_string));
        assert!(
            text.chars().count() < 1_000,
            "single-DID alert messages must remain bounded, got {} chars",
            text.chars().count()
        );
        assert!(!keyboard.is_empty());
    }

    #[test]
    fn telegram_production_uses_checked_committed_height_conversion() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("// ---------------------------------------------------------------------------\n// Tests")
            .next()
            .unwrap();
        let status_builder = production
            .split("pub fn build_status_message")
            .nth(1)
            .and_then(|section| section.split("/// Build the /sentinels response.").next())
            .unwrap();

        assert!(
            !production.contains("clippy::as_conversions"),
            "Telegram production code must not suppress checked conversion linting"
        );
        assert!(
            !status_builder.contains("committed.len() as u64"),
            "Telegram status height must use a checked conversion from committed length"
        );
        assert!(
            status_builder.contains("checked_committed_height"),
            "Telegram status height must route conversion through the checked helper"
        );
    }

    #[test]
    fn telegram_async_dispatch_uses_blocking_message_builders() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("// ---------------------------------------------------------------------------\n// Tests")
            .next()
            .unwrap();

        assert!(
            production.contains("tokio::task::spawn_blocking"),
            "Telegram async dispatch must isolate synchronous store reads from Tokio workers"
        );

        let sync_builders = [
            "build_status_message(",
            "build_sentinels_message(",
            "build_challenges_message(",
            "build_zerodentity_score_message(",
            "build_zerodentity_alerts_message(",
        ];
        let command_handler = production
            .split("async fn handle_command")
            .nth(1)
            .and_then(|section| section.split("async fn handle_callback").next())
            .unwrap();
        for builder in sync_builders {
            assert!(
                !command_handler.contains(builder),
                "Telegram command handler must not call sync builder {builder} directly"
            );
        }

        let callback_handler = production
            .split("async fn handle_callback")
            .nth(1)
            .and_then(|section| section.split("// ---------------------------------------------------------------------------\n// Tests").next())
            .unwrap();
        for builder in sync_builders {
            assert!(
                !callback_handler.contains(builder),
                "Telegram callback handler must not call sync builder {builder} directly"
            );
        }
    }

    #[test]
    fn adjutant_config_source_uses_zeroizing_token_storage() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("// ---------------------------------------------------------------------------\n// Tests")
            .next()
            .unwrap();
        let config_source = production
            .split("pub struct AdjutantConfig")
            .nth(1)
            .and_then(|section| section.split("impl AdjutantConfig").next())
            .unwrap();

        assert!(production.contains("use zeroize::Zeroizing;"));
        assert!(config_source.contains("bot_token: Zeroizing<String>"));
        assert!(!config_source.contains("bot_token: String"));
    }

    #[test]
    fn adjutant_config_debug_redacts_bot_token() {
        let config = AdjutantConfig::from_parts(
            zeroize::Zeroizing::new("123456:secret-token-value".to_string()),
            "42".to_string(),
        )
        .expect("valid config");

        let debug = format!("{config:?}");

        assert!(debug.contains("AdjutantConfig"));
        assert!(debug.contains("bot_token"));
        assert!(debug.contains("<redacted>"));
        assert!(debug.contains("chat_id"));
        assert!(!debug.contains("123456"));
        assert!(!debug.contains("secret-token-value"));
    }

    #[test]
    fn adjutant_config_from_parts_rejects_empty_secret_or_chat() {
        assert!(
            AdjutantConfig::from_parts(zeroize::Zeroizing::new(String::new()), "42".to_string())
                .is_none()
        );
        assert!(
            AdjutantConfig::from_parts(
                zeroize::Zeroizing::new("123456:secret-token-value".to_string()),
                String::new(),
            )
            .is_none()
        );
    }

    #[test]
    fn telegram_api_url_source_uses_zeroizing_temporary_url() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("// ---------------------------------------------------------------------------\n// Tests")
            .next()
            .unwrap();
        let api_url_source = production
            .split("fn api_url(&self, method: &str)")
            .nth(1)
            .and_then(|section| section.split("/// Send a text message").next())
            .unwrap();

        assert!(api_url_source.contains("-> Zeroizing<String>"));
        assert!(api_url_source.contains("Zeroizing::new(format!("));
        assert!(api_url_source.contains("self.config.bot_token.as_str()"));
    }

    #[test]
    fn telegram_transport_errors_do_not_log_reqwest_display_with_token_url() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("// ---------------------------------------------------------------------------\n// Tests")
            .next()
            .unwrap();

        for forbidden in [
            "format!(\"telegram send: {e}\")",
            "format!(\"telegram poll failed: {e}\")",
            "TelegramUpdateParseError::Body(error.to_string())",
            "tracing::debug!(err = %e, \"Telegram callback acknowledgement failed\")",
        ] {
            assert!(
                !production.contains(forbidden),
                "Telegram transport logging must not format reqwest::Error directly because it can include the bot-token URL: {forbidden}"
            );
        }
        assert!(
            production.contains("describe_telegram_transport_error"),
            "Telegram token-bearing request failures must go through a redacted transport-error helper"
        );
    }

    #[test]
    fn telegram_bot_token_url_redaction_removes_token_from_transport_details() {
        let error = "request failed for https://api.telegram.org/bot123456:secret-token/sendMessage?x=1 and https://api.telegram.org/botabcdef/getUpdates";

        let redacted = redact_telegram_bot_token_urls(error);

        assert!(redacted.contains("https://api.telegram.org/bot<redacted>/sendMessage?x=1"));
        assert!(redacted.contains("https://api.telegram.org/bot<redacted>/getUpdates"));
        assert!(!redacted.contains("123456:secret-token"));
        assert!(!redacted.contains("botabcdef/getUpdates"));
    }

    #[test]
    fn adjutant_http_client_uses_timeout() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("// ---------------------------------------------------------------------------\n// Tests")
            .next()
            .unwrap();
        let new_adjutant = production
            .split("pub fn new(config: AdjutantConfig)")
            .nth(1)
            .and_then(|section| section.split("/// Telegram Bot API base URL.").next())
            .unwrap();

        assert!(new_adjutant.contains("reqwest::Client::builder()"));
        assert!(
            new_adjutant
                .contains(".timeout(std::time::Duration::from_secs(TELEGRAM_HTTP_TIMEOUT_SECS))")
        );
        assert!(!new_adjutant.contains("reqwest::Client::new()"));
    }

    #[test]
    fn poll_updates_uses_bounded_response_body_before_deserialization() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("// ---------------------------------------------------------------------------\n// Tests")
            .next()
            .unwrap();
        let poll_updates = production
            .split("async fn poll_updates_result")
            .nth(1)
            .and_then(|section| section.split("/// Acknowledge a callback query").next())
            .unwrap();

        assert!(!poll_updates.contains(".json().await"));
        assert!(!poll_updates.contains(".bytes().await"));
        assert!(poll_updates.contains("read_telegram_update_body"));
        assert!(poll_updates.contains("parse_updates_response"));
    }

    #[test]
    fn run_adjutant_backs_off_after_telegram_poll_failures() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("// ---------------------------------------------------------------------------\n// Tests")
            .next()
            .unwrap();
        let run_adjutant = production
            .split("pub async fn run_adjutant")
            .nth(1)
            .and_then(|section| section.split("async fn handle_command").next())
            .unwrap();

        assert!(
            run_adjutant.contains("poll_updates_result"),
            "run_adjutant must distinguish failed polls from successful empty long-poll responses"
        );
        assert!(
            run_adjutant.contains("TELEGRAM_POLL_FAILURE_BACKOFF_MS"),
            "failed Telegram polls must use a bounded backoff instead of immediate repolling"
        );
        assert!(
            run_adjutant.contains("tokio::time::sleep"),
            "Telegram poll failure backoff must sleep before the next poll attempt"
        );
    }

    #[test]
    fn next_update_offset_rejects_i64_max_without_wrapping() {
        assert_eq!(next_update_offset(41), Some(42));
        assert_eq!(next_update_offset(i64::MAX), None);
    }

    #[tokio::test]
    async fn read_bounded_response_body_rejects_oversized_content_length() {
        let max = 8;
        let raw = format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", max + 1);
        let resp = response_from_raw_http(raw.into_bytes()).await;

        let err = read_bounded_response_body(resp, max)
            .await
            .expect_err("oversized content-length must fail before body read");

        assert_eq!(
            err,
            TelegramUpdateParseError::Oversized {
                len: 9,
                max: usize_to_u64_saturating(max),
            }
        );
    }

    #[tokio::test]
    async fn read_bounded_response_body_rejects_chunked_body_after_limit() {
        let resp = response_from_raw_http(
            b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n8\r\n12345678\r\n1\r\n9\r\n0\r\n\r\n"
                .to_vec(),
        )
        .await;

        let err = read_bounded_response_body(resp, 8)
            .await
            .expect_err("streaming body exceeding the limit must fail");

        assert_eq!(err, TelegramUpdateParseError::Oversized { len: 9, max: 8 });
    }

    #[tokio::test]
    async fn read_bounded_response_body_accepts_body_within_limit() {
        let resp = response_from_raw_http(
            b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n0\r\n\r\n"
                .to_vec(),
        )
        .await;

        let body = read_bounded_response_body(resp, 8)
            .await
            .expect("body within limit must pass");

        assert_eq!(body, b"hello");
    }

    #[test]
    fn parse_updates_response_rejects_oversized_body() {
        let bytes = vec![b' '; MAX_TELEGRAM_UPDATE_RESPONSE_BYTES + 1];

        let err = parse_updates_response(&bytes).expect_err("oversized response must fail");

        assert_eq!(
            err,
            TelegramUpdateParseError::Oversized {
                len: usize_to_u64_saturating(MAX_TELEGRAM_UPDATE_RESPONSE_BYTES + 1),
                max: usize_to_u64_saturating(MAX_TELEGRAM_UPDATE_RESPONSE_BYTES),
            }
        );
    }

    #[test]
    fn parse_updates_response_accepts_valid_updates() {
        let updates = parse_updates_response(
            br#"{"ok":true,"result":[{"update_id":42,"message":{"text":"/status","chat":{"id":7}}}]}"#,
        )
        .expect("valid Telegram update response");

        assert_eq!(updates.len(), 1);
        assert_eq!(updates[0].update_id, 42);
        assert!(updates[0].message.is_some());
    }

    #[test]
    fn parse_updates_response_rejects_missing_result() {
        let err =
            parse_updates_response(br#"{"ok":true}"#).expect_err("missing result must fail closed");

        assert!(matches!(err, TelegramUpdateParseError::MissingResult));
    }

    #[test]
    fn parse_updates_response_rejects_api_failure() {
        let err = parse_updates_response(br#"{"ok":false,"description":"Unauthorized"}"#)
            .expect_err("Telegram API failure must fail closed");

        assert!(matches!(
            err,
            TelegramUpdateParseError::ApiRejected { description } if description == "Unauthorized"
        ));
    }

    #[test]
    fn zerodentity_alerts_fail_closed_on_fingerprint_read_error() {
        let zerodentity = crate::zerodentity::store::new_shared_store();
        {
            let did = Did::new("did:exo:alerted").unwrap();
            let mut store = zerodentity.lock().unwrap();
            store
                .put_score(crate::zerodentity::types::ZerodentityScore::compute(
                    &did,
                    &[],
                    &[],
                    &[],
                    1000,
                ))
                .unwrap();
            store.inject_read_failure(
                crate::zerodentity::store::ZerodentityReadFailure::Fingerprints,
            );
        }

        let (text, keyboard) = build_zerodentity_alerts_message(&zerodentity);

        assert!(text.contains("0dentity alert scan unavailable"));
        assert!(text.contains("did:exo:alerted"));
        assert!(keyboard.is_empty());
    }

    #[test]
    fn config_from_env_returns_none_when_unset() {
        // Env vars are not set in test environment.
        assert!(AdjutantConfig::from_env().is_none());
    }

    #[test]
    fn status_message_contains_key_metrics() {
        let reactor = test_reactor();
        let dir = tempfile::tempdir().unwrap();
        let store = Arc::new(Mutex::new(SqliteDagStore::open(dir.path()).unwrap()));

        let (text, keyboard) = build_status_message(&reactor, &store);
        assert!(text.contains("Round:"));
        assert!(text.contains("Height:"));
        assert!(text.contains("Validators:"));
        assert!(text.contains("Validator")); // role
        assert!(!keyboard.is_empty());
    }

    #[test]
    fn status_message_fails_closed_on_store_height_error() {
        let reactor = test_reactor();
        let dir = tempfile::tempdir().unwrap();
        let store = SqliteDagStore::open(dir.path()).unwrap();
        let conn = rusqlite::Connection::open(dir.path().join("dag.db")).unwrap();
        let hash = [0xA5u8; 32];
        conn.execute(
            "INSERT INTO committed (hash, height) VALUES (?1, ?2)",
            rusqlite::params![hash.as_slice(), -1_i64],
        )
        .unwrap();
        let store = Arc::new(Mutex::new(store));

        let (text, keyboard) = build_status_message(&reactor, &store);

        assert!(text.contains("Store height unavailable"));
        assert!(text.contains("committed.height"));
        assert!(keyboard.is_empty());
    }

    #[test]
    fn sentinels_message_shows_statuses() {
        let state: SharedSentinelState = Arc::new(Mutex::new(vec![SentinelStatus {
            check: SentinelCheck::Liveness,
            healthy: true,
            message: "ok".into(),
            last_run_ms: 0,
        }]));
        let (text, _) = build_sentinels_message(&state);
        assert!(text.contains("Liveness"));
        assert!(text.contains("ok"));
    }

    #[test]
    fn challenges_message_empty() {
        let store: SharedChallengeStore = Arc::new(Mutex::new(ChallengeStore::new()));
        let (text, _) = build_challenges_message(&store);
        assert!(text.contains("No active challenges"));
    }

    #[test]
    fn challenges_message_with_hold() {
        use exo_escalation::challenge::{
            self, ChallengeAdmission, SybilChallengeGround, sign_challenge_admission,
        };
        use exo_identity::did::did_from_public_key;

        let store: SharedChallengeStore = Arc::new(Mutex::new(ChallengeStore::new()));
        {
            let mut st = store.lock().unwrap();
            let keypair = exo_core::crypto::KeyPair::from_secret_bytes([7u8; 32]).unwrap();
            let admission = ChallengeAdmission {
                hold_id: uuid::Uuid::from_bytes([1u8; 16]),
                action_id: [1u8; 32],
                ground: SybilChallengeGround::QuorumContamination,
                admitted_at: exo_core::types::Timestamp::new(1000, 0),
                admitted_by: did_from_public_key(keypair.public_key()).unwrap(),
                admitter_public_key: *keypair.public_key(),
                evidence_hash: [0xEEu8; 32],
                authority_chain_hash: [0xACu8; 32],
            };
            let hold = challenge::admit_challenge(
                sign_challenge_admission(admission, keypair.secret_key()).unwrap(),
            )
            .unwrap();
            st.insert(hold);
        }
        let (text, _) = build_challenges_message(&store);
        assert!(text.contains("QuorumContamination"));
        assert!(text.contains("PauseEligible"));
    }

    #[test]
    fn challenges_message_uses_stable_status_labels() {
        let source = include_str!("telegram.rs");
        let production = source
            .split("#[cfg(test)]")
            .next()
            .expect("production section");
        assert!(
            !production.contains("format!(\"{:?}\", h.status)"),
            "Telegram challenge status output must use explicit stable labels"
        );
        assert!(
            production.contains("h.status.as_str()"),
            "Telegram challenge status output must use ContestStatus labels"
        );
    }

    // ==== GAP-015 chat_id auth tests ==================================

    fn msg_from_chat(id: i64, text: Option<&str>) -> TgMessage {
        TgMessage {
            text: text.map(ToOwned::to_owned),
            chat: TgChat { id },
        }
    }

    #[test]
    fn is_message_authorized_matches_expected_chat() {
        let msg = msg_from_chat(42, Some("/status"));
        assert!(is_message_authorized(Some(42), &msg));
    }

    #[test]
    fn is_message_authorized_rejects_other_chat() {
        let msg = msg_from_chat(999, Some("/status"));
        assert!(!is_message_authorized(Some(42), &msg));
    }

    #[test]
    fn is_message_authorized_fails_closed_when_unconfigured() {
        // TELEGRAM_CHAT_ID misconfigured / unparseable.
        let msg = msg_from_chat(42, Some("/status"));
        assert!(!is_message_authorized(None, &msg));
    }

    #[test]
    fn is_callback_authorized_matches_expected_chat() {
        let cb = CallbackQuery {
            id: "abc".into(),
            data: Some("cmd:status".into()),
            message: Some(msg_from_chat(42, None)),
        };
        assert!(is_callback_authorized(Some(42), &cb));
    }

    #[test]
    fn is_callback_authorized_rejects_other_chat() {
        let cb = CallbackQuery {
            id: "abc".into(),
            data: Some("cmd:status".into()),
            message: Some(msg_from_chat(999, None)),
        };
        assert!(!is_callback_authorized(Some(42), &cb));
    }

    #[test]
    fn is_callback_authorized_fails_closed_without_message() {
        let cb = CallbackQuery {
            id: "abc".into(),
            data: Some("cmd:status".into()),
            message: None,
        };
        assert!(!is_callback_authorized(Some(42), &cb));
    }

    #[test]
    fn is_callback_authorized_fails_closed_when_unconfigured() {
        let cb = CallbackQuery {
            id: "abc".into(),
            data: Some("cmd:status".into()),
            message: Some(msg_from_chat(42, None)),
        };
        assert!(!is_callback_authorized(None, &cb));
    }
}