thndrs 0.1.0

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

use std::collections::BTreeMap;
use std::fs;
use std::io::{BufRead, Write};
use std::net::{TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::{Mutex, OnceLock};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use serde::Deserialize;
use sha2::Digest;

use crate::utils;

/// Environment variable name for the Umans API key.
pub const UMANS_API_KEY_ENV: &str = "UMANS_API_KEY";

/// Environment variable name for the OpenCode Go API key.
pub const OPENCODE_GO_KEY_ENV: &str = "OPENCODE_GO_KEY";

/// Environment variable name for the OpenCode Zen API key.
pub const OPENCODE_ZEN_KEY_ENV: &str = "OPENCODE_ZEN_KEY";

/// Environment variable name for a process-local ChatGPT Codex access token.
pub const CHATGPT_CODEX_ACCESS_TOKEN_ENV: &str = "CHATGPT_CODEX_ACCESS_TOKEN";

/// source: codex-rs/login/src/auth/manager.rs
const CHATGPT_CODEX_CLIENT_ID: &str = "app_EMoamEEZ73f0CkXaXp7hrann";
const DEVICE_USER_CODE_URL: &str = "https://auth.openai.com/api/accounts/deviceauth/usercode";
const DEVICE_TOKEN_URL: &str = "https://auth.openai.com/api/accounts/deviceauth/token";
const DEVICE_VERIFICATION_URL: &str = "https://auth.openai.com/codex/device";
const DEVICE_REDIRECT_URI: &str = "https://auth.openai.com/deviceauth/callback";
const DEVICE_CODE_TIMEOUT_SECONDS: u64 = 15 * 60;
const PKCE_CALLBACK_ADDR: &str = "127.0.0.1:1455";
const PKCE_CALLBACK_URL: &str = "http://localhost:1455/auth/callback";
const PKCE_CALLBACK_TIMEOUT: Duration = Duration::from_secs(5 * 60);
const PKCE_AUTHORIZE_URL: &str = "https://auth.openai.com/oauth/authorize";
const OAUTH_TOKEN_URL: &str = "https://auth.openai.com/oauth/token";
const OAUTH_SCOPE: &str = "openid profile email offline_access";
const OAUTH_ORIGINATOR: &str = "thndrs";

/// Describes where a credential value was found, without leaking the value.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CredentialSource {
    /// Found in a process environment variable.
    Environment,
    /// Found in the global credential store at `~/.thndrs/credentials.env`.
    GlobalStore,
    /// Found in the project credential store at `<workspace>/.thndrs/credentials.env`.
    ProjectStore,
    /// Found in the workspace `.env` file (legacy fallback).
    DotEnvLegacy,
}

impl CredentialSource {
    /// Human-readable label for the source.
    pub fn label(self) -> &'static str {
        match self {
            CredentialSource::Environment => "environment",
            CredentialSource::GlobalStore => "global credentials",
            CredentialSource::ProjectStore => "project credentials",
            CredentialSource::DotEnvLegacy => ".env",
        }
    }
}

impl ChatGptCodexCredentials {
    fn is_expired(&self, now_ms: u64) -> bool {
        self.expires_at_ms <= now_ms.saturating_add(60_000)
    }

    fn is_usable(&self) -> bool {
        !self.access_token.trim().is_empty()
            && !self.refresh_token.trim().is_empty()
            && !self.account_id.trim().is_empty()
    }
}

/// Errors produced by credential store operations.
#[derive(Debug, thiserror::Error)]
pub enum AuthError {
    /// Failed to read a credential file.
    #[error("failed to read credentials {path}: {source}")]
    Read {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    /// Failed to write a credential file.
    #[error("failed to write credentials {path}: {source}")]
    Write {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    /// Malformed line in a credential file.
    #[error("malformed credential file {path}: {message}")]
    Malformed { path: PathBuf, message: String },
    /// Home directory is not available.
    #[error("home directory not found; set $HOME or $USERPROFILE")]
    NoHomeDirectory,
    /// Failed to update `.git/info/exclude`.
    #[error("git exclude failed: {0}")]
    GitExclude(String),
    /// ChatGPT Codex credential data is missing or malformed.
    #[error("chatgpt-codex auth failed: {0}")]
    ChatGptCodex(String),
    /// ChatGPT Codex credential verification could not reach the provider.
    #[error("chatgpt-codex authentication verification unavailable: {0}")]
    ChatGptCodexUnavailable(String),
}

impl AuthError {
    /// Whether retrying later may resolve this without replacing credentials.
    pub fn is_verification_unavailable(&self) -> bool {
        matches!(
            self,
            Self::ChatGptCodexUnavailable(_) | Self::Read { .. } | Self::Write { .. } | Self::NoHomeDirectory
        )
    }
}

/// File-backed ChatGPT Codex credential entry.
#[derive(Clone, serde::Deserialize, serde::Serialize, Eq, PartialEq)]
pub struct ChatGptCodexCredentials {
    /// ChatGPT backend access token.
    pub access_token: String,
    /// Refresh token used to obtain a new access token.
    pub refresh_token: String,
    /// Access-token expiry as Unix epoch milliseconds.
    pub expires_at_ms: u64,
    /// ChatGPT account id required by the Codex backend.
    pub account_id: String,
}

/// Auth material used by the ChatGPT Codex provider.
#[derive(Clone, Eq, PartialEq)]
pub struct ChatGptCodexAuth {
    /// ChatGPT backend access token.
    pub access_token: String,
    /// ChatGPT account id required by the Codex backend.
    pub account_id: String,
}

/// Response from the ChatGPT Codex device-code user-code endpoint.
#[derive(Clone, serde::Deserialize, Eq, PartialEq)]
pub struct ChatGptCodexDeviceCode {
    /// Opaque device-auth id sent only to the device-auth token endpoint.
    pub device_auth_id: String,
    /// User-facing short code entered on the verification page.
    #[serde(alias = "usercode")]
    pub user_code: String,
    /// User-facing verification page.
    #[serde(default = "default_device_verification_url")]
    pub verification_uri: Option<String>,
    /// Optional complete verification link.
    #[serde(default)]
    pub verification_uri_complete: Option<String>,
    /// Device-auth lifetime in seconds.
    #[serde(default = "default_device_code_expires_in")]
    pub expires_in: Option<u64>,
    /// Recommended polling interval in seconds.
    #[serde(default, deserialize_with = "deserialize_optional_u64_from_string_or_number")]
    pub interval: Option<u64>,
}

/// Result of one ChatGPT Codex device-code token poll.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ChatGptCodexDevicePoll {
    /// Authorization is not complete yet.
    Pending,
    /// Authorization is pending and the client should slow down polling.
    SlowDown,
    /// Authorization completed and credentials can be stored.
    Authorized(ChatGptCodexCredentials),
}

/// Token response from ChatGPT Codex OAuth endpoints.
#[derive(Clone, serde::Deserialize, Eq, PartialEq)]
pub struct ChatGptCodexTokenResponse {
    pub access_token: String,
    #[serde(default)]
    pub refresh_token: Option<String>,
    #[serde(default)]
    pub expires_in: Option<u64>,
}

/// A browser PKCE login waiting for its loopback callback.
pub struct ChatGptCodexBrowserLogin {
    listener: Option<TcpListener>,
    verifier: String,
    state: String,
    redirect_uri: String,
    authorization_url: String,
    expires_at: Instant,
}

impl std::fmt::Debug for ChatGptCodexBrowserLogin {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ChatGptCodexBrowserLogin")
            .field("listener", &self.listener.as_ref().map_or("[not bound]", |_| "[bound]"))
            .field("redirect_uri", &self.redirect_uri)
            .field("authorization_url", &"[redacted]")
            .field("expires_at", &self.expires_at)
            .finish()
    }
}

impl ChatGptCodexBrowserLogin {
    /// Copyable authorization URL for display or browser launch.
    pub fn authorization_url(&self) -> &str {
        &self.authorization_url
    }

    /// Registered redirect URI for this login.
    pub fn redirect_uri(&self) -> &str {
        &self.redirect_uri
    }

    /// Poll the loopback callback once and exchange a valid authorization code.
    pub fn poll(&mut self) -> Result<ChatGptCodexBrowserPoll, AuthError> {
        if Instant::now() >= self.expires_at {
            return Err(AuthError::ChatGptCodex("browser OAuth callback expired".to_string()));
        }

        let Some(listener) = self.listener.as_ref() else {
            return Ok(ChatGptCodexBrowserPoll::Pending);
        };
        let (mut stream, _) = match listener.accept() {
            Ok(connection) => connection,
            Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
                return Ok(ChatGptCodexBrowserPoll::Pending);
            }
            Err(error) => {
                return Err(AuthError::ChatGptCodex(format!(
                    "failed to accept browser callback: {error}"
                )));
            }
        };

        let redirect = read_browser_callback(&mut stream)?;
        let credentials = self.complete_redirect(&redirect)?;
        Ok(ChatGptCodexBrowserPoll::Authorized(credentials))
    }

    /// Complete this login from a pasted full redirect URL.
    pub fn complete_redirect(&self, redirect: &str) -> Result<ChatGptCodexCredentials, AuthError> {
        if Instant::now() >= self.expires_at {
            return Err(AuthError::ChatGptCodex("browser OAuth callback expired".to_string()));
        }
        let code = parse_chatgpt_codex_redirect(redirect, &self.state)?;
        let token = exchange_chatgpt_codex_authorization_code(&code, &self.verifier, &self.redirect_uri)?;
        credentials_from_token_response(token, None)
    }
}

/// Result of checking a browser PKCE callback without blocking.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ChatGptCodexBrowserPoll {
    /// No callback has arrived yet.
    Pending,
    /// The callback completed and credentials are ready to store.
    Authorized(ChatGptCodexCredentials),
}

impl std::fmt::Debug for ChatGptCodexCredentials {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ChatGptCodexCredentials")
            .field("access_token", &redact_value(&self.access_token))
            .field("refresh_token", &redact_value(&self.refresh_token))
            .field("expires_at_ms", &self.expires_at_ms)
            .field("account_id", &redact_value(&self.account_id))
            .finish()
    }
}

impl std::fmt::Debug for ChatGptCodexAuth {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ChatGptCodexAuth")
            .field("access_token", &redact_value(&self.access_token))
            .field("account_id", &redact_value(&self.account_id))
            .finish()
    }
}

impl std::fmt::Debug for ChatGptCodexDeviceCode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ChatGptCodexDeviceCode")
            .field("device_auth_id", &redact_value(&self.device_auth_id))
            .field("user_code", &"[redacted]")
            .field(
                "verification_uri",
                &self.verification_uri.as_ref().map(|_| "[redacted]"),
            )
            .field(
                "verification_uri_complete",
                &self.verification_uri_complete.as_ref().map(|_| "[redacted]"),
            )
            .field("expires_in", &self.expires_in)
            .field("interval", &self.interval)
            .finish()
    }
}

impl std::fmt::Debug for ChatGptCodexTokenResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ChatGptCodexTokenResponse")
            .field("access_token", &redact_value(&self.access_token))
            .field(
                "refresh_token",
                &self.refresh_token.as_ref().map(|token| redact_value(token)),
            )
            .field("expires_in", &self.expires_in)
            .finish()
    }
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, Default)]
struct AuthJson {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    chatgpt_codex: Option<ChatGptCodexCredentials>,
    #[serde(flatten)]
    other: BTreeMap<String, serde_json::Value>,
}

#[derive(serde::Deserialize)]
struct ChatGptCodexDeviceAuthResponse {
    authorization_code: String,
    code_verifier: String,
}

static CHATGPT_CODEX_REFRESH_LOCK: OnceLock<Mutex<()>> = OnceLock::new();

/// Global credential store path: `~/.thndrs/credentials.env`.
pub fn global_credentials_path() -> Result<PathBuf, AuthError> {
    let home = utils::home_dir().ok_or(AuthError::NoHomeDirectory)?;
    Ok(home.join(".thndrs").join("credentials.env"))
}

/// ChatGPT Codex auth store path: `~/.thndrs/auth.json`.
pub fn chatgpt_codex_auth_path() -> Result<PathBuf, AuthError> {
    let home = utils::home_dir().ok_or(AuthError::NoHomeDirectory)?;
    Ok(home.join(".thndrs").join("auth.json"))
}

/// Resolve ChatGPT Codex auth, honoring `CHATGPT_CODEX_ACCESS_TOKEN` as a
/// non-persisted process override before reading `~/.thndrs/auth.json`.
pub fn resolve_chatgpt_codex_auth() -> Result<ChatGptCodexAuth, AuthError> {
    if let Ok(token) = std::env::var(CHATGPT_CODEX_ACCESS_TOKEN_ENV)
        && !token.trim().is_empty()
    {
        let account_id = chatgpt_account_id_from_jwt(&token)?;
        return Ok(ChatGptCodexAuth { access_token: token, account_id });
    }

    let path = chatgpt_codex_auth_path()?;
    resolve_chatgpt_codex_file_auth_at(&path, refresh_chatgpt_codex_credentials)
}

fn resolve_chatgpt_codex_file_auth_at(
    path: &Path, refresh: impl FnOnce(ChatGptCodexCredentials) -> Result<ChatGptCodexCredentials, AuthError>,
) -> Result<ChatGptCodexAuth, AuthError> {
    let _guard = CHATGPT_CODEX_REFRESH_LOCK
        .get_or_init(|| Mutex::new(()))
        .lock()
        .map_err(|_| AuthError::ChatGptCodex("refresh lock poisoned".to_string()))?;
    let mut credentials = read_chatgpt_codex_credentials_at(path)?
        .ok_or_else(|| AuthError::ChatGptCodex("run `thndrs login chatgpt-codex`".to_string()))?;
    if !credentials.is_usable() {
        return Err(AuthError::ChatGptCodex(
            "stored ChatGPT Codex credential is incomplete; run `thndrs login chatgpt-codex`".to_string(),
        ));
    }
    if credentials.is_expired(now_ms()) {
        credentials = refresh(credentials)?;
        write_chatgpt_codex_credentials_at(path, &credentials)?;
    }
    Ok(ChatGptCodexAuth { access_token: credentials.access_token, account_id: credentials.account_id })
}

/// Read the ChatGPT Codex credential entry from `~/.thndrs/auth.json`.
pub fn read_chatgpt_codex_credentials() -> Result<Option<ChatGptCodexCredentials>, AuthError> {
    read_chatgpt_codex_credentials_at(&chatgpt_codex_auth_path()?)
}

/// Write the ChatGPT Codex credential entry to `~/.thndrs/auth.json`.
pub fn write_chatgpt_codex_credentials(credentials: &ChatGptCodexCredentials) -> Result<(), AuthError> {
    write_chatgpt_codex_credentials_at(&chatgpt_codex_auth_path()?, credentials)
}

/// Delete only the ChatGPT Codex credential entry from `~/.thndrs/auth.json`.
pub fn remove_chatgpt_codex_credentials() -> Result<(), AuthError> {
    let path = chatgpt_codex_auth_path()?;
    remove_chatgpt_codex_credentials_at(&path)
}

fn remove_chatgpt_codex_credentials_at(path: &Path) -> Result<(), AuthError> {
    let mut store = read_auth_json_at(path)?;
    store.chatgpt_codex = None;
    write_auth_json_atomic(path, &store)
}

/// Decode a JWT payload and extract the ChatGPT account id claim.
pub fn chatgpt_account_id_from_jwt(jwt: &str) -> Result<String, AuthError> {
    let payload = jwt
        .split('.')
        .nth(1)
        .ok_or_else(|| AuthError::ChatGptCodex("access token is not a JWT".to_string()))?;
    let decoded = base64_url_decode(payload)?;
    let value: serde_json::Value = serde_json::from_slice(&decoded)
        .map_err(|_| AuthError::ChatGptCodex("access token payload is not valid JSON".to_string()))?;
    value
        .pointer("/https:~1~1api.openai.com~1auth/chatgpt_account_id")
        .and_then(|v| v.as_str())
        .filter(|s| !s.trim().is_empty())
        .map(str::to_string)
        .ok_or_else(|| AuthError::ChatGptCodex("access token is missing chatgpt_account_id".to_string()))
}

/// Request a ChatGPT Codex device code.
pub fn request_chatgpt_codex_device_code() -> Result<ChatGptCodexDeviceCode, AuthError> {
    let mut response = ureq::Agent::new_with_defaults()
        .post(DEVICE_USER_CODE_URL)
        .header("content-type", "application/json")
        .config()
        .http_status_as_error(false)
        .build()
        .send_json(serde_json::json!({ "client_id": CHATGPT_CODEX_CLIENT_ID }))
        .map_err(|e| AuthError::ChatGptCodexUnavailable(format!("device-code request failed: {e}")))?;
    read_json_response(&mut response, "device-code request")
}

/// Poll the device-code token endpoint until authorization succeeds.
pub fn poll_chatgpt_codex_device_code(code: &ChatGptCodexDeviceCode) -> Result<ChatGptCodexCredentials, AuthError> {
    let mut interval_seconds = code.interval.unwrap_or(5).max(1);
    let deadline = SystemTime::now() + Duration::from_secs(code.expires_in.unwrap_or(DEVICE_CODE_TIMEOUT_SECONDS));
    loop {
        match poll_chatgpt_codex_device_code_once(code)? {
            ChatGptCodexDevicePoll::Authorized(credentials) => return Ok(credentials),
            ChatGptCodexDevicePoll::Pending => {
                if SystemTime::now() >= deadline {
                    return Err(AuthError::ChatGptCodex("device-code login expired".to_string()));
                }
                std::thread::sleep(Duration::from_secs(interval_seconds));
            }
            ChatGptCodexDevicePoll::SlowDown => {
                interval_seconds = interval_seconds.saturating_add(5);
                if SystemTime::now() >= deadline {
                    return Err(AuthError::ChatGptCodex("device-code login expired".to_string()));
                }
                std::thread::sleep(Duration::from_secs(interval_seconds));
            }
        }
    }
}

/// Poll the device-code token endpoint once without sleeping.
pub fn poll_chatgpt_codex_device_code_once(code: &ChatGptCodexDeviceCode) -> Result<ChatGptCodexDevicePoll, AuthError> {
    let mut response = ureq::Agent::new_with_defaults()
        .post(DEVICE_TOKEN_URL)
        .header("content-type", "application/json")
        .config()
        .http_status_as_error(false)
        .build()
        .send_json(serde_json::json!({
            "device_auth_id": code.device_auth_id,
            "user_code": code.user_code,
        }))
        .map_err(|e| AuthError::ChatGptCodexUnavailable(format!("device-code poll failed: {e}")))?;
    let status = response.status().as_u16();
    let body = response
        .body_mut()
        .read_to_string()
        .map_err(|e| AuthError::ChatGptCodexUnavailable(format!("device-code poll body read failed: {e}")))?;

    if (200..=299).contains(&status) {
        let auth_response: ChatGptCodexDeviceAuthResponse = serde_json::from_str(&body)
            .map_err(|e| AuthError::ChatGptCodexUnavailable(format!("device-code poll JSON parse failed: {e}")))?;
        let token = exchange_chatgpt_codex_authorization_code(
            &auth_response.authorization_code,
            &auth_response.code_verifier,
            DEVICE_REDIRECT_URI,
        )?;
        return credentials_from_token_response(token, None).map(ChatGptCodexDevicePoll::Authorized);
    }

    match chatgpt_codex_error_code(&body).as_deref() {
        Some("deviceauth_authorization_pending" | "authorization_pending") => Ok(ChatGptCodexDevicePoll::Pending),
        Some("slow_down") => Ok(ChatGptCodexDevicePoll::SlowDown),
        _ if status == 403 || status == 404 => Ok(ChatGptCodexDevicePoll::Pending),
        _ => {
            let message = chatgpt_codex_error_summary(&body);
            Err(AuthError::ChatGptCodex(format!(
                "device-code poll failed with status {status}: {message}"
            )))
        }
    }
}

/// Start a browser-first ChatGPT Codex PKCE login.
///
/// The listener is bound before the authorization URL is returned, so a fast
/// browser redirect cannot race setup. It is non-blocking and expires after a
/// short interval; callers should check it with
/// [`poll_chatgpt_codex_browser_login_once`] and may use
/// [`ChatGptCodexBrowserLogin::complete_redirect`] when a pasted full redirect
/// is needed.
pub fn start_chatgpt_codex_browser_login() -> Result<ChatGptCodexBrowserLogin, AuthError> {
    let listener = TcpListener::bind(PKCE_CALLBACK_ADDR)
        .map_err(|e| AuthError::ChatGptCodex(format!("failed to bind localhost:1455 callback: {e}")))?;
    listener
        .set_nonblocking(true)
        .map_err(|e| AuthError::ChatGptCodex(format!("failed to prepare browser callback: {e}")))?;

    let verifier = random_pkce_verifier()?;
    let challenge = base64_url_encode(&sha2::Sha256::digest(verifier.as_bytes()));
    let state = random_pkce_verifier()?;
    let authorization_url = chatgpt_codex_pkce_authorize_url(&challenge, &state);

    Ok(ChatGptCodexBrowserLogin {
        listener: Some(listener),
        verifier,
        state,
        redirect_uri: PKCE_CALLBACK_URL.to_string(),
        authorization_url,
        expires_at: Instant::now() + PKCE_CALLBACK_TIMEOUT,
    })
}

#[cfg(test)]
pub fn test_chatgpt_codex_browser_login() -> ChatGptCodexBrowserLogin {
    ChatGptCodexBrowserLogin {
        listener: None,
        verifier: String::from("test-verifier"),
        state: String::from("test-state"),
        redirect_uri: PKCE_CALLBACK_URL.to_string(),
        authorization_url: String::from("https://auth.example.test/oauth/authorize?state=test-state"),
        expires_at: Instant::now() + PKCE_CALLBACK_TIMEOUT,
    }
}

/// Poll a browser login once without sleeping.
pub fn poll_chatgpt_codex_browser_login_once(
    login: &mut ChatGptCodexBrowserLogin,
) -> Result<ChatGptCodexBrowserPoll, AuthError> {
    login.poll()
}

/// Open a ChatGPT authorization URL using the host's conventional browser launcher.
pub fn open_chatgpt_codex_authorization_url(url: &str) -> Result<(), AuthError> {
    #[cfg(target_os = "macos")]
    let mut command = Command::new("open");
    #[cfg(target_os = "linux")]
    let mut command = Command::new("xdg-open");
    #[cfg(target_os = "windows")]
    let mut command = {
        let mut command = Command::new("cmd");
        command.args(["/C", "start", ""]);
        command
    };
    #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
    return Err(AuthError::ChatGptCodex(
        "automatic browser launch is unavailable".to_string(),
    ));

    command
        .arg(url)
        .status()
        .map_err(|_| AuthError::ChatGptCodex("automatic browser launch failed".to_string()))
        .and_then(|status| {
            if status.success() {
                Ok(())
            } else {
                Err(AuthError::ChatGptCodex("automatic browser launch failed".to_string()))
            }
        })
}

/// Browser PKCE login for headless CLI callers that cannot use the TUI recovery surface.
pub fn login_chatgpt_codex_with_browser_pkce() -> Result<ChatGptCodexCredentials, AuthError> {
    let mut login = start_chatgpt_codex_browser_login()?;
    eprintln!(
        "Open this URL to continue ChatGPT Codex browser login (copy/paste the URL if it does not open):\n{}",
        login.authorization_url()
    );
    let _ = open_chatgpt_codex_authorization_url(login.authorization_url());
    loop {
        match login.poll()? {
            ChatGptCodexBrowserPoll::Pending => std::thread::sleep(Duration::from_millis(100)),
            ChatGptCodexBrowserPoll::Authorized(credentials) => return Ok(credentials),
        }
    }
}

/// Convert a token response into persistable ChatGPT Codex credentials.
pub fn credentials_from_token_response(
    response: ChatGptCodexTokenResponse, previous_refresh_token: Option<String>,
) -> Result<ChatGptCodexCredentials, AuthError> {
    let account_id = chatgpt_account_id_from_jwt(&response.access_token)?;
    let refresh_token = response
        .refresh_token
        .or(previous_refresh_token)
        .ok_or_else(|| AuthError::ChatGptCodex("token response is missing refresh_token".to_string()))?;
    Ok(ChatGptCodexCredentials {
        access_token: response.access_token,
        refresh_token,
        expires_at_ms: now_ms() + response.expires_in.unwrap_or(3600).saturating_mul(1000),
        account_id,
    })
}

/// Project credential store path: `<workspace>/.thndrs/credentials.env`.
pub fn project_credentials_path(workspace: &Path) -> PathBuf {
    workspace.join(".thndrs").join("credentials.env")
}

/// Read all credential key-value pairs from a `.env`-format file.
///
/// Blank lines, comment lines (starting with `#`), and `export`-prefixed lines
/// are skipped. The first assignment for each key wins (duplicate keys are
/// ignored).
pub fn read_credentials(path: &Path) -> Result<BTreeMap<String, String>, AuthError> {
    if !path.is_file() {
        return Ok(BTreeMap::new());
    }
    let file = fs::File::open(path).map_err(|source| AuthError::Read { path: path.to_path_buf(), source })?;
    let reader = std::io::BufReader::new(file);
    let mut credentials = BTreeMap::new();

    for (i, line_result) in reader.lines().enumerate() {
        let line = line_result.map_err(|source| AuthError::Read { path: path.to_path_buf(), source })?;
        if let Some((key, value)) = parse_credential_line(&line) {
            credentials.entry(key).or_insert(value);
        } else if !line.trim().is_empty() && !line.trim().starts_with('#') {
            return Err(AuthError::Malformed {
                path: path.to_path_buf(),
                message: format!("line {}: invalid env format", i + 1),
            });
        }
    }

    Ok(credentials)
}

/// Write a single credential key-value pair to the credential file,
/// preserving all unrelated entries.
///
/// If the file does not exist, it is created. If the key already exists in the
/// file, its line is replaced. Otherwise the new assignment is appended.
///
/// The write is atomic: content is written to a temporary file in the same
/// directory, then renamed over the target path.
///
/// On Unix, the file is created with mode `0600`.
pub fn set_credential(path: &Path, key: &str, value: &str) -> Result<(), AuthError> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).map_err(|source| AuthError::Write { path: path.to_path_buf(), source })?;
    }

    let existing = if path.is_file() { read_lines(path)? } else { Vec::new() };

    let new_line = format!("{key}={value}");

    let mut replaced = false;
    let mut lines = existing;

    for line in &mut lines {
        if let Some((line_key, _)) = parse_credential_line(line)
            && line_key == key
            && !replaced
        {
            *line = new_line.clone();
            replaced = true;
        }
    }

    if !replaced {
        lines.push(new_line);
    }

    write_lines_atomic(path, &lines)
}

/// Remove a single credential key from the credential file, preserving all
/// other entries.
///
/// If the key appears multiple times, all matching lines are removed.
pub fn remove_credential(path: &Path, key: &str) -> Result<(), AuthError> {
    if !path.is_file() {
        return Ok(());
    }

    let lines = read_lines(path)?;
    let filtered: Vec<String> = lines
        .into_iter()
        .filter(
            |line| {
                if let Some((line_key, _)) = parse_credential_line(line) { line_key != key } else { true }
            },
        )
        .collect();

    write_lines_atomic(path, &filtered)
}

/// Redact a credential value for display/debug output.
///
/// This returns a fixed sentinel string so callers cannot accidentally leak
/// value prefixes, hashes, suffixes, or lengths.
pub fn redact_value(_value: &str) -> String {
    String::from("[redacted]")
}

/// Resolve a credential by checking all sources in precedence order.
///
/// Order: process environment → global credential store
/// (`~/.thndrs/credentials.env`) → project credential store
/// (`<workspace>/.thndrs/credentials.env`) → workspace `.env` (legacy).
///
/// Returns `None` when the key is not found in any source.
pub fn resolve_credential(key: &str, workspace: &Path) -> Option<(String, CredentialSource)> {
    if let Ok(value) = std::env::var(key)
        && !value.is_empty()
    {
        return Some((value, CredentialSource::Environment));
    }

    if let Ok(global_path) = global_credentials_path()
        && let Ok(creds) = read_credentials(&global_path)
        && let Some(value) = creds.get(key)
    {
        return Some((value.clone(), CredentialSource::GlobalStore));
    }

    let project_path = project_credentials_path(workspace);
    if let Ok(creds) = read_credentials(&project_path)
        && let Some(value) = creds.get(key)
    {
        return Some((value.clone(), CredentialSource::ProjectStore));
    }

    let dotenv_path = workspace.join(".env");
    if let Ok(creds) = read_credentials(&dotenv_path)
        && let Some(value) = creds.get(key)
    {
        return Some((value.clone(), CredentialSource::DotEnvLegacy));
    }

    None
}

/// Return the [`CredentialSource`] for a key without revealing its value.
pub fn credential_source(key: &str, workspace: &Path) -> Option<CredentialSource> {
    resolve_credential(key, workspace).map(|(_, source)| source)
}

/// Ensure `.thndrs/credentials.env` is listed in `.git/info/exclude` so it
/// cannot be accidentally committed.
///
/// If the workspace is not inside a git repository, this is a no-op.
/// Repeated calls are idempotent: the exclude entry is never duplicated.
pub fn ensure_git_exclude(workspace: &Path) -> Result<(), AuthError> {
    let git_dir = workspace.join(".git");
    let exclude_path = git_dir.join("info").join("exclude");
    if !exclude_path.is_file() {
        return Ok(());
    }

    let exclude_entry = ".thndrs/credentials.env";
    let content = fs::read_to_string(&exclude_path)
        .map_err(|source| AuthError::GitExclude(format!("failed to read {}: {source}", exclude_path.display())))?;

    let already_excluded = content.lines().any(|line| line.trim() == exclude_entry);
    if already_excluded {
        return Ok(());
    }

    let mut file = fs::OpenOptions::new()
        .append(true)
        .open(&exclude_path)
        .map_err(|source| AuthError::GitExclude(format!("failed to open {}: {source}", exclude_path.display())))?;

    writeln!(file, "{exclude_entry}")
        .map_err(|source| AuthError::GitExclude(format!("failed to write {}: {source}", exclude_path.display())))?;

    Ok(())
}

/// Parse a single `.env`-format assignment line.
///
/// Supports `KEY=value`, `export KEY=value`, and quoted values
/// (`KEY="value"`, `KEY='value'`).
fn parse_credential_line(line: &str) -> Option<(String, String)> {
    let line = line.trim();
    if line.is_empty() || line.starts_with('#') {
        return None;
    }
    let line = line.strip_prefix("export ").unwrap_or(line);
    let (key, raw_value) = line.split_once('=')?;
    let key = key.trim();
    if key.is_empty() {
        return None;
    }
    let raw_value = raw_value.trim();
    let value = raw_value
        .strip_prefix('"')
        .and_then(|v| v.strip_suffix('"'))
        .or_else(|| raw_value.strip_prefix('\'').and_then(|v| v.strip_suffix('\'')))
        .unwrap_or(raw_value);
    if value.is_empty() {
        return None;
    }
    Some((key.to_string(), value.to_string()))
}

/// Read all lines from a text file.
fn read_lines(path: &Path) -> Result<Vec<String>, AuthError> {
    let file = fs::File::open(path).map_err(|source| AuthError::Read { path: path.to_path_buf(), source })?;
    let reader = std::io::BufReader::new(file);
    reader
        .lines()
        .collect::<Result<Vec<_>, _>>()
        .map_err(|source| AuthError::Read { path: path.to_path_buf(), source })
}

/// Write lines to a temporary file in the same directory, then atomically
/// rename over the target path.
///
/// On Unix, sets file mode `0600`.
fn write_lines_atomic(path: &Path, lines: &[String]) -> Result<(), AuthError> {
    let dir = path.parent().unwrap_or_else(|| Path::new("."));
    fs::create_dir_all(dir).map_err(|source| AuthError::Write { path: path.to_path_buf(), source })?;

    let mut tmp =
        tempfile::NamedTempFile::new_in(dir).map_err(|source| AuthError::Write { path: path.to_path_buf(), source })?;

    for line in lines {
        writeln!(tmp, "{line}").map_err(|source| AuthError::Write { path: path.to_path_buf(), source })?;
    }
    tmp.flush()
        .map_err(|source| AuthError::Write { path: path.to_path_buf(), source })?;

    set_unix_permissions(tmp.path());

    tmp.persist(path)
        .map(|_| ())
        .map_err(|e| AuthError::Write { path: path.to_path_buf(), source: e.error })
}

fn read_chatgpt_codex_credentials_at(path: &Path) -> Result<Option<ChatGptCodexCredentials>, AuthError> {
    Ok(read_auth_json_at(path)?.chatgpt_codex)
}

fn write_chatgpt_codex_credentials_at(path: &Path, credentials: &ChatGptCodexCredentials) -> Result<(), AuthError> {
    let mut store = read_auth_json_at(path)?;
    store.chatgpt_codex = Some(credentials.clone());
    write_auth_json_atomic(path, &store)
}

fn read_auth_json_at(path: &Path) -> Result<AuthJson, AuthError> {
    if !path.is_file() {
        return Ok(AuthJson::default());
    }
    let content = fs::read_to_string(path).map_err(|source| AuthError::Read { path: path.to_path_buf(), source })?;
    serde_json::from_str(&content).map_err(|e| AuthError::ChatGptCodex(format!("malformed {}: {e}", path.display())))
}

fn write_auth_json_atomic(path: &Path, store: &AuthJson) -> Result<(), AuthError> {
    let dir = path.parent().unwrap_or_else(|| Path::new("."));
    fs::create_dir_all(dir).map_err(|source| AuthError::Write { path: path.to_path_buf(), source })?;
    let mut tmp =
        tempfile::NamedTempFile::new_in(dir).map_err(|source| AuthError::Write { path: path.to_path_buf(), source })?;
    serde_json::to_writer_pretty(&mut tmp, store)
        .map_err(|e| AuthError::ChatGptCodex(format!("serialize auth store: {e}")))?;
    writeln!(tmp).map_err(|source| AuthError::Write { path: path.to_path_buf(), source })?;
    tmp.flush()
        .map_err(|source| AuthError::Write { path: path.to_path_buf(), source })?;
    set_unix_permissions(tmp.path());
    tmp.persist(path)
        .map(|_| ())
        .map_err(|e| AuthError::Write { path: path.to_path_buf(), source: e.error })
}

fn refresh_chatgpt_codex_credentials(current: ChatGptCodexCredentials) -> Result<ChatGptCodexCredentials, AuthError> {
    let token = exchange_chatgpt_codex_token_form(&[
        ("grant_type", "refresh_token"),
        ("refresh_token", &current.refresh_token),
        ("client_id", CHATGPT_CODEX_CLIENT_ID),
    ])?;
    credentials_from_token_response(token, Some(current.refresh_token))
}

fn exchange_chatgpt_codex_authorization_code(
    code: &str, verifier: &str, redirect_uri: &str,
) -> Result<ChatGptCodexTokenResponse, AuthError> {
    exchange_chatgpt_codex_token_form(&[
        ("grant_type", "authorization_code"),
        ("client_id", CHATGPT_CODEX_CLIENT_ID),
        ("code", code),
        ("code_verifier", verifier),
        ("redirect_uri", redirect_uri),
    ])
}

fn exchange_chatgpt_codex_token_form(params: &[(&str, &str)]) -> Result<ChatGptCodexTokenResponse, AuthError> {
    let body = form_urlencoded(params);
    let mut response = ureq::Agent::new_with_defaults()
        .post(OAUTH_TOKEN_URL)
        .header("content-type", "application/x-www-form-urlencoded")
        .config()
        .http_status_as_error(false)
        .build()
        .send(&body)
        .map_err(|e| AuthError::ChatGptCodexUnavailable(format!("token request failed: {e}")))?;
    read_json_response(&mut response, "token request")
}

fn form_urlencoded(params: &[(&str, &str)]) -> String {
    let mut serializer = url::form_urlencoded::Serializer::new(String::new());
    for (key, value) in params {
        serializer.append_pair(key, value);
    }
    serializer.finish()
}

fn read_json_response<T>(response: &mut ureq::http::Response<ureq::Body>, context: &str) -> Result<T, AuthError>
where
    T: serde::de::DeserializeOwned,
{
    let status = response.status().as_u16();
    let body = response
        .body_mut()
        .read_to_string()
        .map_err(|e| AuthError::ChatGptCodexUnavailable(format!("{context} body read failed: {e}")))?;
    if !(200..=299).contains(&status) {
        return Err(chatgpt_codex_response_error(context, status, &body));
    }
    serde_json::from_str(&body)
        .map_err(|e| AuthError::ChatGptCodexUnavailable(format!("{context} JSON parse failed: {e}")))
}

fn chatgpt_codex_response_error(context: &str, status: u16, body: &str) -> AuthError {
    let message = format!(
        "{context} failed with status {status}: {}",
        chatgpt_codex_error_summary(body)
    );
    if status == 408 || status == 429 || (500..=599).contains(&status) {
        AuthError::ChatGptCodexUnavailable(message)
    } else {
        AuthError::ChatGptCodex(message)
    }
}

fn chatgpt_codex_error_code(body: &str) -> Option<String> {
    let value = serde_json::from_str::<serde_json::Value>(body).ok()?;
    let error = value.get("error")?;
    if let Some(code) = error.as_str() {
        return Some(code.to_string());
    }
    error.get("code").and_then(|v| v.as_str()).map(str::to_string)
}

fn chatgpt_codex_error_summary(body: &str) -> &'static str {
    match chatgpt_codex_error_code(body).as_deref() {
        Some("deviceauth_authorization_pending" | "authorization_pending") => "authorization pending",
        Some("slow_down") => "provider requested slower polling",
        Some("invalid_grant" | "invalid_request") => "authorization was rejected or expired",
        Some("access_denied") => "authorization was denied",
        _ => "provider returned an authentication error",
    }
}

fn chatgpt_codex_pkce_authorize_url(challenge: &str, state: &str) -> String {
    let mut url = url::Url::parse(PKCE_AUTHORIZE_URL).expect("valid ChatGPT Codex authorize URL");
    url.query_pairs_mut()
        .append_pair("response_type", "code")
        .append_pair("client_id", CHATGPT_CODEX_CLIENT_ID)
        .append_pair("redirect_uri", PKCE_CALLBACK_URL)
        .append_pair("scope", OAUTH_SCOPE)
        .append_pair("code_challenge", challenge)
        .append_pair("code_challenge_method", "S256")
        .append_pair("state", state)
        .append_pair("id_token_add_organizations", "true")
        .append_pair("codex_cli_simplified_flow", "true")
        .append_pair("originator", OAUTH_ORIGINATOR);
    url.to_string()
}

fn read_browser_callback(stream: &mut TcpStream) -> Result<String, AuthError> {
    stream
        .set_read_timeout(Some(Duration::from_secs(2)))
        .map_err(|e| AuthError::ChatGptCodex(format!("failed to prepare browser callback: {e}")))?;
    let mut request_line = String::new();
    std::io::BufReader::new(
        stream
            .try_clone()
            .map_err(|e| AuthError::ChatGptCodex(format!("failed to read browser callback: {e}")))?,
    )
    .read_line(&mut request_line)
    .map_err(|e| AuthError::ChatGptCodex(format!("failed to read browser callback: {e}")))?;

    let target = request_line
        .split_whitespace()
        .nth(1)
        .ok_or_else(|| AuthError::ChatGptCodex("browser callback was malformed".to_string()))?;
    let redirect = if target.starts_with("http://") || target.starts_with("https://") {
        target.to_string()
    } else {
        format!("http://localhost:1455{target}")
    };
    let body = "You can return to thndrs.\n";
    let response = format!(
        "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: {}\r\n\r\n{body}",
        body.len()
    );
    let _ = stream.write_all(response.as_bytes());
    Ok(redirect)
}

fn parse_chatgpt_codex_redirect(redirect: &str, expected_state: &str) -> Result<String, AuthError> {
    let url = url::Url::parse(redirect)
        .map_err(|_| AuthError::ChatGptCodex("browser callback URL was malformed".to_string()))?;
    if url.scheme() != "http"
        || !matches!(url.host_str(), Some("localhost" | "127.0.0.1"))
        || url.port() != Some(1455)
        || url.path() != "/auth/callback"
    {
        return Err(AuthError::ChatGptCodex(
            "browser callback URL was unexpected".to_string(),
        ));
    }
    if url
        .query_pairs()
        .any(|(key, _)| key == "error" || key == "error_description")
    {
        return Err(AuthError::ChatGptCodex("ChatGPT authorization was denied".to_string()));
    }
    let state = url
        .query_pairs()
        .find_map(|(key, value)| if key == "state" { Some(value.into_owned()) } else { None })
        .ok_or_else(|| AuthError::ChatGptCodex("browser callback did not include state".to_string()))?;
    if state != expected_state {
        return Err(AuthError::ChatGptCodex(
            "browser callback state did not match".to_string(),
        ));
    }
    url.query_pairs()
        .find_map(|(key, value)| if key == "code" { Some(value.into_owned()) } else { None })
        .ok_or_else(|| AuthError::ChatGptCodex("browser callback did not include code".to_string()))
}

fn default_device_verification_url() -> Option<String> {
    Some(DEVICE_VERIFICATION_URL.to_string())
}

fn default_device_code_expires_in() -> Option<u64> {
    Some(DEVICE_CODE_TIMEOUT_SECONDS)
}

fn deserialize_optional_u64_from_string_or_number<'de, D>(deserializer: D) -> Result<Option<u64>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let value = Option::<serde_json::Value>::deserialize(deserializer)?;
    let Some(value) = value else {
        return Ok(None);
    };
    match value {
        serde_json::Value::Number(number) => number
            .as_u64()
            .ok_or_else(|| serde::de::Error::custom("expected non-negative integer"))
            .map(Some),
        serde_json::Value::String(value) => value.trim().parse::<u64>().map(Some).map_err(serde::de::Error::custom),
        _ => Err(serde::de::Error::custom("expected integer or string integer")),
    }
}

fn random_pkce_verifier() -> Result<String, AuthError> {
    let mut bytes = [0_u8; 32];
    fs::File::open("/dev/urandom")
        .and_then(|mut file| std::io::Read::read_exact(&mut file, &mut bytes))
        .map_err(|e| AuthError::ChatGptCodex(format!("failed to generate PKCE verifier: {e}")))?;
    Ok(base64_url_encode(&bytes))
}

fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis() as u64
}

fn base64_url_decode(input: &str) -> Result<Vec<u8>, AuthError> {
    let mut out = Vec::new();
    let mut buffer = 0_u32;
    let mut bits = 0_u8;
    for ch in input.chars().filter(|ch| *ch != '=') {
        let value = match ch {
            'A'..='Z' => ch as u32 - 'A' as u32,
            'a'..='z' => ch as u32 - 'a' as u32 + 26,
            '0'..='9' => ch as u32 - '0' as u32 + 52,
            '-' => 62,
            '_' => 63,
            _ => {
                return Err(AuthError::ChatGptCodex(
                    "access token payload is not base64url".to_string(),
                ));
            }
        };
        buffer = (buffer << 6) | value;
        bits += 6;
        if bits >= 8 {
            bits -= 8;
            out.push(((buffer >> bits) & 0xff) as u8);
        }
    }
    Ok(out)
}

fn base64_url_encode(bytes: &[u8]) -> String {
    const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
    let mut out = String::new();
    let mut i = 0;
    while i < bytes.len() {
        let b0 = bytes[i];
        let b1 = bytes.get(i + 1).copied().unwrap_or(0);
        let b2 = bytes.get(i + 2).copied().unwrap_or(0);
        out.push(ALPHABET[(b0 >> 2) as usize] as char);
        out.push(ALPHABET[(((b0 & 0b11) << 4) | (b1 >> 4)) as usize] as char);
        if i + 1 < bytes.len() {
            out.push(ALPHABET[(((b1 & 0b1111) << 2) | (b2 >> 6)) as usize] as char);
        }
        if i + 2 < bytes.len() {
            out.push(ALPHABET[(b2 & 0b11_1111) as usize] as char);
        }
        i += 3;
    }
    out
}

/// Set Unix file mode `0600` (owner read/write only) when supported.
#[cfg(unix)]
fn set_unix_permissions(path: &Path) {
    use std::os::unix::fs::PermissionsExt;
    let _ = fs::set_permissions(path, fs::Permissions::from_mode(0o600));
}

#[cfg(not(unix))]
fn set_unix_permissions(_path: &Path) {}

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

    fn env_test_lock() -> crate::test_env::Guard {
        crate::test_env::lock()
    }

    fn temp_cred_path() -> (tempfile::TempDir, PathBuf) {
        let dir = tempfile::tempdir().expect("create temp dir");
        let path = dir.path().join("credentials.env");
        (dir, path)
    }

    fn write_cred_file(path: &Path, content: &str) {
        let parent = path.parent().unwrap();
        fs::create_dir_all(parent).unwrap();
        fs::write(path, content).unwrap();
    }

    fn jwt_with_account(account_id: &str) -> String {
        let payload = serde_json::json!({
            "https://api.openai.com/auth": {
                "chatgpt_account_id": account_id,
            }
        });
        format!(
            "header.{}.sig",
            base64_url_encode(serde_json::to_string(&payload).unwrap().as_bytes())
        )
    }

    #[test]
    fn parses_simple_assignment() {
        assert_eq!(
            parse_credential_line("UMANS_API_KEY=sk-abc123"),
            Some(("UMANS_API_KEY".to_string(), "sk-abc123".to_string()))
        );
    }

    #[test]
    fn chatgpt_codex_account_id_decodes_from_jwt_without_logging_token() {
        let token = jwt_with_account("acct_test");
        assert_eq!(chatgpt_account_id_from_jwt(&token).unwrap(), "acct_test");
        assert!(matches!(
            chatgpt_account_id_from_jwt("header.e30.sig"),
            Err(AuthError::ChatGptCodex(_))
        ));
    }

    #[test]
    fn chatgpt_codex_account_id_rejects_missing_or_malformed_claims() {
        let missing_claim = format!(
            "header.{}.sig",
            base64_url_encode(br#"{"https://api.openai.com/auth":{}}"#)
        );
        let malformed_claim = format!(
            "header.{}.sig",
            base64_url_encode(br#"{"https://api.openai.com/auth":{"chatgpt_account_id":42}}"#)
        );

        assert!(matches!(
            chatgpt_account_id_from_jwt(&missing_claim),
            Err(AuthError::ChatGptCodex(message)) if message.contains("missing chatgpt_account_id")
        ));
        assert!(matches!(
            chatgpt_account_id_from_jwt(&malformed_claim),
            Err(AuthError::ChatGptCodex(message)) if message.contains("missing chatgpt_account_id")
        ));
        assert!(matches!(
            chatgpt_account_id_from_jwt("not-a-jwt"),
            Err(AuthError::ChatGptCodex(message)) if message.contains("not a JWT")
        ));
    }

    #[test]
    fn chatgpt_token_response_distinguishes_service_unavailability_from_rejection() {
        assert!(matches!(
            chatgpt_codex_response_error("token request", 503, r#"{"error":"server_error"}"#),
            AuthError::ChatGptCodexUnavailable(message) if message.contains("status 503")
        ));
        assert!(matches!(
            chatgpt_codex_response_error("token request", 429, r#"{"error":"rate_limited"}"#),
            AuthError::ChatGptCodexUnavailable(message) if message.contains("status 429")
        ));
        assert!(matches!(
            chatgpt_codex_response_error("token request", 401, r#"{"error":"invalid_grant"}"#),
            AuthError::ChatGptCodex(message) if message.contains("status 401")
        ));
    }

    #[test]
    fn chatgpt_codex_debug_output_redacts_tokens() {
        let credentials = ChatGptCodexCredentials {
            access_token: "access-secret-token".to_string(),
            refresh_token: "refresh-secret-token".to_string(),
            expires_at_ms: 123,
            account_id: "acct_file".to_string(),
        };
        let auth =
            ChatGptCodexAuth { access_token: "auth-secret-token".to_string(), account_id: "acct_file".to_string() };
        let token = ChatGptCodexTokenResponse {
            access_token: "response-access-secret".to_string(),
            refresh_token: Some("response-refresh-secret".to_string()),
            expires_in: Some(3600),
        };

        for debug in [format!("{credentials:?}"), format!("{auth:?}"), format!("{token:?}")] {
            assert!(debug.contains("[redacted]"));
            assert!(!debug.contains("secret-token"));
            assert!(!debug.contains("response-access-secret"));
            assert!(!debug.contains("response-refresh-secret"));
            assert!(!debug.contains("acct_file"));
        }
    }

    #[test]
    fn chatgpt_codex_device_code_accepts_string_interval() {
        let code: ChatGptCodexDeviceCode =
            serde_json::from_str(r#"{"device_auth_id":"device-auth-secret","user_code":"USER-CODE","interval":"5"}"#)
                .expect("parse device-auth response");

        assert_eq!(code.device_auth_id, "device-auth-secret");
        assert_eq!(code.user_code, "USER-CODE");
        assert_eq!(code.interval, Some(5));
        assert_eq!(code.expires_in, Some(DEVICE_CODE_TIMEOUT_SECONDS));
        assert_eq!(code.verification_uri.as_deref(), Some(DEVICE_VERIFICATION_URL));
    }

    #[test]
    fn chatgpt_codex_device_code_debug_redacts_device_auth_id() {
        let code = ChatGptCodexDeviceCode {
            device_auth_id: "device-auth-secret-from-test".to_string(),
            user_code: "USER-CODE".to_string(),
            verification_uri: Some(DEVICE_VERIFICATION_URL.to_string()),
            verification_uri_complete: Some("https://auth.example.test/device?user_code=USER-CODE".to_string()),
            expires_in: Some(DEVICE_CODE_TIMEOUT_SECONDS),
            interval: Some(5),
        };

        let debug = format!("{code:?}");
        assert!(debug.contains("[redacted]"));
        assert!(!debug.contains("device-auth-secret-from-test"));
        assert!(!debug.contains("USER-CODE"));
    }

    #[test]
    fn chatgpt_codex_pkce_authorize_url_uses_codex_app_client() {
        let url = url::Url::parse(&chatgpt_codex_pkce_authorize_url("challenge-test", "state-test")).unwrap();
        let params: BTreeMap<_, _> = url.query_pairs().into_owned().collect();

        assert_eq!(url.as_str().split('?').next(), Some(PKCE_AUTHORIZE_URL));
        assert_eq!(
            params.get("client_id").map(String::as_str),
            Some(CHATGPT_CODEX_CLIENT_ID)
        );
        assert_eq!(params.get("redirect_uri").map(String::as_str), Some(PKCE_CALLBACK_URL));
        assert_eq!(params.get("scope").map(String::as_str), Some(OAUTH_SCOPE));
        assert_eq!(params.get("code_challenge").map(String::as_str), Some("challenge-test"));
        assert_eq!(params.get("code_challenge_method").map(String::as_str), Some("S256"));
        assert_eq!(params.get("state").map(String::as_str), Some("state-test"));
        assert_eq!(
            params.get("id_token_add_organizations").map(String::as_str),
            Some("true")
        );
        assert_eq!(
            params.get("codex_cli_simplified_flow").map(String::as_str),
            Some("true")
        );
        assert_eq!(params.get("originator").map(String::as_str), Some(OAUTH_ORIGINATOR));
    }

    #[test]
    fn chatgpt_codex_browser_redirect_requires_matching_state_without_logging_query() {
        let redirect = "http://localhost:1455/auth/callback?code=authorization-code-secret&state=state-ok";
        assert_eq!(
            parse_chatgpt_codex_redirect(redirect, "state-ok").unwrap(),
            "authorization-code-secret"
        );

        let error = parse_chatgpt_codex_redirect(redirect, "state-wrong").expect_err("state mismatch");
        assert!(error.to_string().contains("state did not match"));
        assert!(!error.to_string().contains("authorization-code-secret"));
        assert!(!format!("{error:?}").contains("authorization-code-secret"));

        let error = parse_chatgpt_codex_redirect(
            "https://localhost:1455/auth/callback?code=authorization-code-secret&state=state-ok",
            "state-ok",
        )
        .expect_err("unexpected callback scheme");
        assert!(error.to_string().contains("URL was unexpected"));
    }

    #[test]
    fn chatgpt_codex_browser_redirect_rejects_denial_without_query_details() {
        let error = parse_chatgpt_codex_redirect(
            "http://localhost:1455/auth/callback?error=access_denied&error_description=secret-account-detail&state=state-ok",
            "state-ok",
        )
        .expect_err("denied callback");

        assert!(error.to_string().contains("authorization was denied"));
        assert!(!error.to_string().contains("secret-account-detail"));
    }

    #[test]
    fn chatgpt_codex_auth_json_preserves_unrelated_entries_and_removes_only_entry() {
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("auth.json");
        fs::write(&path, r#"{"other":{"keep":true}}"#).unwrap();
        let credentials = ChatGptCodexCredentials {
            access_token: jwt_with_account("acct_file"),
            refresh_token: "refresh-secret".to_string(),
            expires_at_ms: 123,
            account_id: "acct_file".to_string(),
        };

        write_chatgpt_codex_credentials_at(&path, &credentials).expect("write");
        assert_eq!(read_chatgpt_codex_credentials_at(&path).unwrap(), Some(credentials));
        let written: serde_json::Value = serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(written["chatgpt_codex"]["access_token"], jwt_with_account("acct_file"));
        assert_eq!(written["chatgpt_codex"]["refresh_token"], "refresh-secret");
        assert_eq!(written["chatgpt_codex"]["expires_at_ms"], 123);
        assert_eq!(written["chatgpt_codex"]["account_id"], "acct_file");
        assert_eq!(written["other"]["keep"], true);

        remove_chatgpt_codex_credentials_at(&path).expect("remove");
        assert_eq!(read_chatgpt_codex_credentials_at(&path).unwrap(), None);
        let removed: serde_json::Value = serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(removed["other"]["keep"], true);
    }

    #[test]
    fn chatgpt_codex_env_token_override_does_not_write_auth_json() {
        let _guard = env_test_lock();
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path().join("home");
        fs::create_dir_all(&home).unwrap();
        let old_home = std::env::var_os("HOME");
        let old_token = std::env::var_os(CHATGPT_CODEX_ACCESS_TOKEN_ENV);
        let token = jwt_with_account("acct_env");

        unsafe {
            std::env::set_var("HOME", &home);
            std::env::set_var(CHATGPT_CODEX_ACCESS_TOKEN_ENV, &token);
        }

        let auth = resolve_chatgpt_codex_auth().expect("resolve env auth");
        let auth_path = home.join(".thndrs").join("auth.json");

        unsafe {
            if let Some(home) = old_home {
                std::env::set_var("HOME", home);
            } else {
                std::env::remove_var("HOME");
            }
            if let Some(token) = old_token {
                std::env::set_var(CHATGPT_CODEX_ACCESS_TOKEN_ENV, token);
            } else {
                std::env::remove_var(CHATGPT_CODEX_ACCESS_TOKEN_ENV);
            }
        }

        assert_eq!(auth.access_token, token);
        assert_eq!(auth.account_id, "acct_env");
        assert!(!auth_path.exists());
    }

    #[test]
    fn chatgpt_codex_expired_credentials_refresh_under_locked_resolver() {
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("auth.json");
        let old_credentials = ChatGptCodexCredentials {
            access_token: jwt_with_account("acct_old"),
            refresh_token: "refresh-old".to_string(),
            expires_at_ms: 1,
            account_id: "acct_old".to_string(),
        };
        write_chatgpt_codex_credentials_at(&path, &old_credentials).expect("write old credentials");

        let refreshed_token = jwt_with_account("acct_new");
        let auth = resolve_chatgpt_codex_file_auth_at(&path, |current| {
            assert_eq!(current.refresh_token, "refresh-old");
            Ok(ChatGptCodexCredentials {
                access_token: refreshed_token.clone(),
                refresh_token: "refresh-new".to_string(),
                expires_at_ms: now_ms() + 3_600_000,
                account_id: "acct_new".to_string(),
            })
        })
        .expect("refresh");

        assert_eq!(auth.access_token, refreshed_token);
        assert_eq!(auth.account_id, "acct_new");
        let stored = read_chatgpt_codex_credentials_at(&path)
            .expect("read refreshed credentials")
            .expect("stored credentials");
        assert_eq!(stored.refresh_token, "refresh-new");
        assert_eq!(stored.account_id, "acct_new");
    }

    #[cfg(unix)]
    #[test]
    fn chatgpt_codex_auth_json_is_0600() {
        use std::os::unix::fs::PermissionsExt;

        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("auth.json");
        let credentials = ChatGptCodexCredentials {
            access_token: jwt_with_account("acct_file"),
            refresh_token: "refresh-secret".to_string(),
            expires_at_ms: 123,
            account_id: "acct_file".to_string(),
        };
        write_chatgpt_codex_credentials_at(&path, &credentials).expect("write");

        let mode = fs::metadata(path).unwrap().permissions().mode() & 0o777;
        assert_eq!(mode, 0o600);
    }

    #[test]
    fn parses_export_prefix() {
        assert_eq!(
            parse_credential_line("export UMANS_API_KEY=sk-abc123"),
            Some(("UMANS_API_KEY".to_string(), "sk-abc123".to_string()))
        );
    }

    #[test]
    fn parses_double_quoted_value() {
        assert_eq!(
            parse_credential_line(r#"UMANS_API_KEY="sk-abc123""#),
            Some(("UMANS_API_KEY".to_string(), "sk-abc123".to_string()))
        );
    }

    #[test]
    fn parses_single_quoted_value() {
        assert_eq!(
            parse_credential_line("UMANS_API_KEY='sk-abc123'"),
            Some(("UMANS_API_KEY".to_string(), "sk-abc123".to_string()))
        );
    }

    #[test]
    fn parses_export_quoted_value() {
        assert_eq!(
            parse_credential_line(r#"export UMANS_API_KEY="sk-abc123""#),
            Some(("UMANS_API_KEY".to_string(), "sk-abc123".to_string()))
        );
    }

    #[test]
    fn skips_blank_lines() {
        assert_eq!(parse_credential_line(""), None);
        assert_eq!(parse_credential_line("   "), None);
    }

    #[test]
    fn skips_comment_lines() {
        assert_eq!(parse_credential_line("# this is a comment"), None);
        assert_eq!(parse_credential_line("  # indented comment"), None);
    }

    #[test]
    fn skips_empty_values() {
        assert_eq!(parse_credential_line("UMANS_API_KEY="), None);
        assert_eq!(parse_credential_line("UMANS_API_KEY=\"\""), None);
    }

    #[test]
    fn rejects_lines_without_equals() {
        assert_eq!(parse_credential_line("just a string"), None);
    }

    #[test]
    fn trims_whitespace() {
        assert_eq!(
            parse_credential_line("  UMANS_API_KEY = sk-abc123  "),
            Some(("UMANS_API_KEY".to_string(), "sk-abc123".to_string()))
        );
    }

    #[test]
    fn value_can_contain_equals() {
        assert_eq!(
            parse_credential_line(r#"UMANS_API_KEY="sk-abc=xyz""#),
            Some(("UMANS_API_KEY".to_string(), "sk-abc=xyz".to_string()))
        );
    }

    #[test]
    fn reads_empty_file_as_empty_map() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(&path, "");
        let creds = read_credentials(&path).unwrap();
        assert!(creds.is_empty());
    }

    #[test]
    fn reads_multiple_credentials() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(&path, "UMANS_API_KEY=sk-umans-1\nOPENCODE_GO_KEY=sk-opencode-1\n");
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.get("UMANS_API_KEY").unwrap(), "sk-umans-1");
        assert_eq!(creds.get("OPENCODE_GO_KEY").unwrap(), "sk-opencode-1");
        assert_eq!(creds.len(), 2);
    }

    #[test]
    fn first_key_wins_on_duplicate() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(&path, "UMANS_API_KEY=first\nUMANS_API_KEY=second\n");
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.get("UMANS_API_KEY").unwrap(), "first");
    }

    #[test]
    fn reads_with_comments_and_blanks() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(
            &path,
            "# Umans key\nUMANS_API_KEY=sk-umans-1\n\n# OpenCode key\nOPENCODE_GO_KEY=sk-opencode-1\n",
        );
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.len(), 2);
    }

    #[test]
    fn missing_file_returns_empty() {
        let (_dir, path) = temp_cred_path();
        let creds = read_credentials(&path).unwrap();
        assert!(creds.is_empty());
    }

    #[test]
    fn malformed_file_is_rejected() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(&path, "UMANS_API_KEY=sk-valid\nthis is not valid\n");
        let err = read_credentials(&path).unwrap_err();
        assert!(matches!(err, AuthError::Malformed { .. }));
    }

    #[test]
    fn malformed_error_does_not_print_value() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(&path, "totally invalid line\n");
        let err = read_credentials(&path).unwrap_err();
        let msg = err.to_string();
        assert!(!msg.contains("sk-"), "error should not contain secret-like values");
    }

    #[test]
    fn creates_new_file() {
        let (_dir, path) = temp_cred_path();
        set_credential(&path, "UMANS_API_KEY", "sk-fresh").unwrap();
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.get("UMANS_API_KEY").unwrap(), "sk-fresh");
    }

    #[test]
    fn appends_new_key_to_existing_file() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(&path, "UMANS_API_KEY=sk-umans-1\n");
        set_credential(&path, "OPENCODE_GO_KEY", "sk-opencode-1").unwrap();
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.get("UMANS_API_KEY").unwrap(), "sk-umans-1");
        assert_eq!(creds.get("OPENCODE_GO_KEY").unwrap(), "sk-opencode-1");
    }

    #[test]
    fn replaces_existing_key_in_place() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(&path, "UMANS_API_KEY=sk-old\n");
        set_credential(&path, "UMANS_API_KEY", "sk-new").unwrap();
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.get("UMANS_API_KEY").unwrap(), "sk-new");
    }

    #[test]
    fn preserves_unrelated_entries() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(
            &path,
            "UMANS_API_KEY=sk-umans\nOTHER_VAR=keep-me\nOPENCODE_GO_KEY=sk-opencode\n",
        );
        set_credential(&path, "OPENCODE_GO_KEY", "sk-opencode-new").unwrap();
        let content = fs::read_to_string(&path).unwrap();
        assert!(
            content.contains("OTHER_VAR=keep-me"),
            "unrelated entries must be preserved"
        );
        assert!(
            content.contains("UMANS_API_KEY=sk-umans"),
            "other credentials must be preserved"
        );
    }

    #[test]
    fn preserves_comments_and_blanks() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(
            &path,
            "# Umans key\nUMANS_API_KEY=sk-umans\n\n# OpenCode key\nOPENCODE_GO_KEY=sk-opencode\n",
        );
        set_credential(&path, "OPENCODE_GO_KEY", "sk-opencode-new").unwrap();
        let content = fs::read_to_string(&path).unwrap();
        assert!(content.contains("# Umans key"));
        assert!(content.contains("# OpenCode key"));
    }

    #[test]
    fn set_is_idempotent() {
        let (_dir, path) = temp_cred_path();
        set_credential(&path, "UMANS_API_KEY", "sk-val").unwrap();
        set_credential(&path, "UMANS_API_KEY", "sk-val").unwrap();
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.get("UMANS_API_KEY").unwrap(), "sk-val");
        assert_eq!(creds.len(), 1);
    }

    #[test]
    fn removes_existing_key() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(&path, "UMANS_API_KEY=sk-umans\nOPENCODE_GO_KEY=sk-opencode\n");
        remove_credential(&path, "UMANS_API_KEY").unwrap();
        let creds = read_credentials(&path).unwrap();
        assert!(!creds.contains_key("UMANS_API_KEY"));
        assert_eq!(creds.get("OPENCODE_GO_KEY").unwrap(), "sk-opencode");
    }

    #[test]
    fn removing_missing_key_is_noop() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(&path, "UMANS_API_KEY=sk-umans\n");
        remove_credential(&path, "OPENCODE_GO_KEY").unwrap();
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.get("UMANS_API_KEY").unwrap(), "sk-umans");
    }

    #[test]
    fn removing_from_missing_file_is_noop() {
        let (_dir, path) = temp_cred_path();
        remove_credential(&path, "UMANS_API_KEY").unwrap();
    }

    #[test]
    fn remove_preserves_unrelated_entries() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(
            &path,
            "UMANS_API_KEY=sk-umans\nOTHER_VAR=keep-me\nOPENCODE_GO_KEY=sk-opencode\n",
        );
        remove_credential(&path, "OPENCODE_GO_KEY").unwrap();
        let content = fs::read_to_string(&path).unwrap();
        assert!(content.contains("OTHER_VAR=keep-me"));
        assert!(content.contains("UMANS_API_KEY=sk-umans"));
        assert!(!content.contains("OPENCODE_GO_KEY"));
    }

    #[test]
    fn redact_returns_fixed_string() {
        assert_eq!(redact_value(""), "[redacted]");
        assert_eq!(redact_value("sk-abc123"), "[redacted]");
        assert_eq!(redact_value("my-secret-key-12345"), "[redacted]");
    }

    #[test]
    fn global_path_uses_home_thndrs() {
        let _guard = env_test_lock();
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path().join("home");
        let old_home = std::env::var_os("HOME");
        unsafe { std::env::set_var("HOME", &home) };
        let path = global_credentials_path().unwrap();
        assert_eq!(path, home.join(".thndrs").join("credentials.env"));
        unsafe {
            if let Some(h) = old_home {
                std::env::set_var("HOME", h);
            } else {
                std::env::remove_var("HOME");
            }
        }
    }

    #[test]
    fn global_path_fails_without_home() {
        let _guard = env_test_lock();
        let old_home = std::env::var_os("HOME");
        unsafe { std::env::remove_var("HOME") };
        let old_profile = std::env::var_os("USERPROFILE");
        unsafe { std::env::remove_var("USERPROFILE") };
        let err = global_credentials_path().unwrap_err();
        assert!(matches!(err, AuthError::NoHomeDirectory));
        unsafe {
            if let Some(h) = old_home {
                std::env::set_var("HOME", h);
            }
            if let Some(p) = old_profile {
                std::env::set_var("USERPROFILE", p);
            }
        }
    }

    #[test]
    fn project_path_uses_workspace_thndrs() {
        let path = project_credentials_path(Path::new("/repo"));
        assert_eq!(path, PathBuf::from("/repo/.thndrs/credentials.env"));
    }

    #[test]
    fn git_exclude_creates_entry() {
        let tmp = tempfile::tempdir().unwrap();
        let git_dir = tmp.path().join(".git").join("info");
        fs::create_dir_all(&git_dir).unwrap();
        fs::write(git_dir.join("exclude"), "# git exclude\n").unwrap();

        ensure_git_exclude(tmp.path()).unwrap();
        let content = fs::read_to_string(git_dir.join("exclude")).unwrap();
        assert!(content.contains(".thndrs/credentials.env"));
    }

    #[test]
    fn git_exclude_is_idempotent() {
        let tmp = tempfile::tempdir().unwrap();
        let git_dir = tmp.path().join(".git").join("info");
        fs::create_dir_all(&git_dir).unwrap();
        fs::write(git_dir.join("exclude"), "# git exclude\n").unwrap();

        ensure_git_exclude(tmp.path()).unwrap();
        ensure_git_exclude(tmp.path()).unwrap();
        ensure_git_exclude(tmp.path()).unwrap();

        let content = fs::read_to_string(git_dir.join("exclude")).unwrap();
        let count = content
            .lines()
            .filter(|l| l.trim() == ".thndrs/credentials.env")
            .count();
        assert_eq!(count, 1, "entry should appear exactly once");
    }

    #[test]
    fn git_exclude_noop_without_git_dir() {
        let tmp = tempfile::tempdir().unwrap();
        ensure_git_exclude(tmp.path()).unwrap();
    }

    #[test]
    fn git_exclude_preserves_existing_content() {
        let tmp = tempfile::tempdir().unwrap();
        let git_dir = tmp.path().join(".git").join("info");
        fs::create_dir_all(&git_dir).unwrap();
        fs::write(git_dir.join("exclude"), "*.log\n.env\n").unwrap();

        ensure_git_exclude(tmp.path()).unwrap();
        let content = fs::read_to_string(git_dir.join("exclude")).unwrap();
        assert!(content.contains("*.log"));
        assert!(content.contains(".env"));
        assert!(content.contains(".thndrs/credentials.env"));
    }

    #[test]
    fn atomic_write_creates_parent_dirs() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("nested").join("dir").join("credentials.env");
        set_credential(&path, "UMANS_API_KEY", "sk-test").unwrap();
        assert!(path.is_file());
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.get("UMANS_API_KEY").unwrap(), "sk-test");
    }

    #[test]
    fn read_write_round_trip() {
        let (_dir, path) = temp_cred_path();
        set_credential(&path, "UMANS_API_KEY", "sk-round-trip").unwrap();
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.get("UMANS_API_KEY").unwrap(), "sk-round-trip");
    }

    #[test]
    fn source_labels_are_readable() {
        assert_eq!(CredentialSource::Environment.label(), "environment");
        assert_eq!(CredentialSource::GlobalStore.label(), "global credentials");
        assert_eq!(CredentialSource::ProjectStore.label(), "project credentials");
        assert_eq!(CredentialSource::DotEnvLegacy.label(), ".env");
    }

    #[test]
    fn debug_does_not_leak_values() {
        let label = format!("{:?}", CredentialSource::Environment);
        assert!(!label.contains("[redacted]"));
        assert!(label.contains("Environment"));
    }

    #[test]
    fn resolve_picks_env_var_first() {
        let _guard = env_test_lock();
        let key = "RESOLVE_TEST_ENV_FIRST";
        unsafe { std::env::set_var(key, "from-env") };
        let dir = tempfile::tempdir().unwrap();
        let (value, source) = resolve_credential(key, dir.path()).unwrap();
        assert_eq!(value, "from-env");
        assert_eq!(source, CredentialSource::Environment);
        unsafe { std::env::remove_var(key) };
    }

    #[test]
    fn resolve_falls_through_to_global_store() {
        let _guard = env_test_lock();
        let key = "RESOLVE_TEST_GLOBAL";
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path().join("home");

        let global_path = home.join(".thndrs").join("credentials.env");
        fs::create_dir_all(global_path.parent().unwrap()).unwrap();
        fs::write(&global_path, format!("{key}=from-global\n")).unwrap();

        let old_home = std::env::var_os("HOME");
        unsafe { std::env::set_var("HOME", &home) };

        let workspace = tmp.path().join("workspace");
        fs::create_dir_all(&workspace).unwrap();

        let (value, source) = resolve_credential(key, &workspace).unwrap();
        assert_eq!(value, "from-global");
        assert_eq!(source, CredentialSource::GlobalStore);

        unsafe {
            if let Some(h) = old_home {
                std::env::set_var("HOME", h);
            } else {
                std::env::remove_var("HOME");
            }
        }
    }

    #[test]
    fn resolve_falls_through_to_project_store() {
        let _guard = env_test_lock();
        let key = "RESOLVE_TEST_PROJECT";
        let tmp = tempfile::tempdir().unwrap();
        let workspace = tmp.path().join("workspace");
        fs::create_dir_all(&workspace).unwrap();

        let old_home = std::env::var_os("HOME");
        let test_home = tmp.path().join("home");
        fs::create_dir_all(&test_home).unwrap();
        unsafe { std::env::set_var("HOME", &test_home) };

        let project_path = workspace.join(".thndrs").join("credentials.env");
        fs::create_dir_all(project_path.parent().unwrap()).unwrap();
        fs::write(&project_path, format!("{key}=from-project\n")).unwrap();

        let (value, source) = resolve_credential(key, &workspace).unwrap();
        assert_eq!(value, "from-project");
        assert_eq!(source, CredentialSource::ProjectStore);

        unsafe {
            if let Some(h) = old_home {
                std::env::set_var("HOME", h);
            } else {
                std::env::remove_var("HOME");
            }
        }
    }

    #[test]
    fn resolve_falls_through_to_dotenv_legacy() {
        let _guard = env_test_lock();
        let key = "RESOLVE_TEST_DOTENV";
        let tmp = tempfile::tempdir().unwrap();
        let workspace = tmp.path().join("workspace");
        fs::create_dir_all(&workspace).unwrap();

        let old_home = std::env::var_os("HOME");
        let test_home = tmp.path().join("home");
        fs::create_dir_all(&test_home).unwrap();
        unsafe { std::env::set_var("HOME", &test_home) };

        fs::write(workspace.join(".env"), format!("{key}=from-dotenv\n")).unwrap();

        let (value, source) = resolve_credential(key, &workspace).unwrap();
        assert_eq!(value, "from-dotenv");
        assert_eq!(source, CredentialSource::DotEnvLegacy);

        unsafe {
            if let Some(h) = old_home {
                std::env::set_var("HOME", h);
            } else {
                std::env::remove_var("HOME");
            }
        }
    }

    #[test]
    fn resolve_returns_none_for_missing_key() {
        let _guard = env_test_lock();
        let key = "RESOLVE_TEST_NONEXISTENT";
        let tmp = tempfile::tempdir().unwrap();
        let workspace = tmp.path().join("workspace");
        fs::create_dir_all(&workspace).unwrap();

        let old_home = std::env::var_os("HOME");
        let test_home = tmp.path().join("home");
        fs::create_dir_all(&test_home).unwrap();
        unsafe { std::env::set_var("HOME", &test_home) };

        assert!(resolve_credential(key, &workspace).is_none());

        unsafe {
            if let Some(h) = old_home {
                std::env::set_var("HOME", h);
            } else {
                std::env::remove_var("HOME");
            }
        }
    }

    #[test]
    fn resolve_precedence_env_overrides_all() {
        let _guard = env_test_lock();
        let key = "RESOLVE_TEST_PRECEDENCE";
        let tmp = tempfile::tempdir().unwrap();
        let workspace = tmp.path().join("workspace");
        fs::create_dir_all(&workspace).unwrap();

        let old_home = std::env::var_os("HOME");
        let test_home = tmp.path().join("home");
        fs::create_dir_all(&test_home).unwrap();
        unsafe { std::env::set_var("HOME", &test_home) };

        let global_path = test_home.join(".thndrs").join("credentials.env");
        fs::create_dir_all(global_path.parent().unwrap()).unwrap();
        fs::write(&global_path, format!("{key}=from-global\n")).unwrap();

        let project_path = workspace.join(".thndrs").join("credentials.env");
        fs::create_dir_all(project_path.parent().unwrap()).unwrap();
        fs::write(&project_path, format!("{key}=from-project\n")).unwrap();

        fs::write(workspace.join(".env"), format!("{key}=from-dotenv\n")).unwrap();

        unsafe { std::env::set_var(key, "from-env") };
        let (value, source) = resolve_credential(key, &workspace).unwrap();
        assert_eq!(value, "from-env");
        assert_eq!(source, CredentialSource::Environment);
        unsafe { std::env::remove_var(key) };

        unsafe { std::env::set_var("HOME", &test_home) };
        let (value, source) = resolve_credential(key, &workspace).unwrap();
        assert_eq!(value, "from-global");
        assert_eq!(source, CredentialSource::GlobalStore);

        unsafe {
            if let Some(h) = old_home {
                std::env::set_var("HOME", h);
            } else {
                std::env::remove_var("HOME");
            }
        }
    }

    #[test]
    fn resolve_empty_env_var_is_skipped() {
        let _guard = env_test_lock();
        let key = "RESOLVE_TEST_EMPTY_ENV";
        unsafe { std::env::set_var(key, "") };
        let dir = tempfile::tempdir().unwrap();
        assert!(resolve_credential(key, dir.path()).is_none());
        unsafe { std::env::remove_var(key) };
    }

    #[test]
    fn credential_source_returns_label_without_value() {
        let _guard = env_test_lock();
        let key = "RESOLVE_SRC_LABEL";
        unsafe { std::env::set_var(key, "some-value") };
        let dir = tempfile::tempdir().unwrap();
        let source = credential_source(key, dir.path());
        assert_eq!(source, Some(CredentialSource::Environment));
        unsafe { std::env::remove_var(key) };
    }

    #[test]
    fn credential_source_returns_none_for_missing() {
        let dir = tempfile::tempdir().unwrap();
        assert_eq!(credential_source("THIS_KEY_DOES_NOT_EXIST", dir.path()), None);
    }

    #[test]
    fn remove_one_preserves_others() {
        let (_dir, path) = temp_cred_path();
        write_cred_file(
            &path,
            "UMANS_API_KEY=sk-umans\nOPENCODE_GO_KEY=sk-opencode\nOTHER_KEY=other-val\n",
        );
        remove_credential(&path, "UMANS_API_KEY").unwrap();
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.len(), 2);
        assert!(creds.contains_key("OPENCODE_GO_KEY"));
        assert!(creds.contains_key("OTHER_KEY"));
    }

    #[test]
    #[cfg(unix)]
    fn file_has_0600_permissions() {
        use std::os::unix::fs::PermissionsExt;
        let (_dir, path) = temp_cred_path();
        set_credential(&path, "UMANS_API_KEY", "sk-perm").unwrap();
        let metadata = fs::metadata(&path).unwrap();
        let mode = metadata.permissions().mode();
        assert_eq!(mode & 0o777, 0o600, "file permissions should be 0600");
    }
}