a4-cli 0.39.0

CLI tool for generating TypeScript SDKs from Arete stream specifications
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
use anyhow::{Context, Result};
use colored::Colorize;
use std::io::{self, Write};
use std::path::{Component, Path, PathBuf};

use crate::api_client::{api_error_details, AgentMeResponse, ApiClient, PendingAgentSignup};
use crate::config;
use crate::ui;

fn credentials_path() -> String {
    ApiClient::credentials_file_path()
        .map(|path| path.display().to_string())
        .unwrap_or_else(|_| "~/.arete/credentials.toml".to_string())
}

fn trial_remaining_seconds(expires_at: Option<&str>) -> Option<i64> {
    let expires_at = chrono::DateTime::parse_from_rfc3339(expires_at?).ok()?;
    Some(
        expires_at
            .signed_duration_since(chrono::Utc::now())
            .num_seconds()
            .max(0),
    )
}

fn format_trial_remaining(expires_at: Option<&str>) -> Option<String> {
    let seconds = trial_remaining_seconds(expires_at)?;
    Some(format_remaining_seconds(seconds))
}

fn format_remaining_seconds(seconds: i64) -> String {
    if seconds == 0 {
        return "expired".to_string();
    }
    let days = seconds / 86_400;
    let hours = (seconds % 86_400) / 3_600;
    let minutes = (seconds % 3_600) / 60;
    if days > 0 {
        format!("{days}d {hours}h")
    } else if hours > 0 {
        format!("{hours}h {minutes}m")
    } else if minutes > 0 {
        format!("{minutes}m")
    } else {
        format!("{seconds}s")
    }
}

fn signup_expiry_matches(identity: Option<&str>, signup: &str) -> bool {
    // PostgreSQL stores timestamps at microsecond precision and rounds the
    // signup response's nanoseconds in either direction. Allow one complete
    // microsecond instead of truncating both values, which fails when the
    // database rounds across a microsecond boundary.
    const DATABASE_TIMESTAMP_TOLERANCE_NANOS: i64 = 1_000;

    let Some(identity) = identity else {
        return false;
    };
    let Ok(identity) = chrono::DateTime::parse_from_rfc3339(identity) else {
        return false;
    };
    let Ok(signup) = chrono::DateTime::parse_from_rfc3339(signup) else {
        return false;
    };
    identity
        .signed_duration_since(signup)
        .num_nanoseconds()
        .is_some_and(|delta| {
            (-DATABASE_TIMESTAMP_TOLERANCE_NANOS..=DATABASE_TIMESTAMP_TOLERANCE_NANOS)
                .contains(&delta)
        })
}

pub fn login(api_key: Option<String>, requested_profile: Option<&str>) -> Result<()> {
    let api_url = config::get_api_url(None);

    let api_key = if let Some(key) = api_key {
        key
    } else {
        if !ui::interactive() {
            anyhow::bail!(
                "Missing --key and no terminal to prompt on. Pass: a4 auth login --profile human --key <a4_sk_...> (or register as an agent: a4 auth signup)"
            );
        }
        println!("{}", "Login to Arete".bold());
        println!();
        println!("Target API: {}", api_url.yellow());
        println!();
        print!("API Key: ");
        io::stdout().flush()?;

        let mut key = String::new();
        io::stdin().read_line(&mut key)?;
        key.trim().to_string()
    };

    if api_key.is_empty() {
        anyhow::bail!("API key cannot be empty");
    }

    let profile = requested_profile
        .map(str::to_string)
        .unwrap_or_else(|| arete_mcp::credentials::inferred_profile_for_key(&api_key).to_string());
    arete_mcp::credentials::validate_key_for_profile(&profile, &api_key)?;

    // Verify before writing so a typo cannot overwrite a valid stored key.
    let spinner = ui::create_spinner("Verifying API key...");
    let client = ApiClient::with_base_url(&api_url).with_api_key(api_key.clone());

    let verification = if api_key.starts_with("a4_ak_") {
        client.agent_me().map(|_| ())
    } else {
        client.list_specs().map(|_| ())
    };
    match verification {
        Ok(_) => {
            let saved = ApiClient::save_api_key_for_profile(&api_key, Some(&api_url), &profile);
            spinner.finish_and_clear();
            saved?;
            ui::print_success("API key saved and verified!");
            println!();
            println!("  Profile:     {}", profile);
            println!("  Credentials: {}", credentials_path().dimmed());
            println!();
            println!("You are now ready to use Arete!");
        }
        Err(e) => {
            spinner.finish_and_clear();
            anyhow::bail!("Invalid API key: {}", e);
        }
    }

    Ok(())
}

pub fn logout() -> Result<()> {
    let api_url = config::get_api_url(None);

    let spinner = ui::create_spinner("Logging out...");

    // Delete only the selected/resolved profile. Removing every principal is
    // intentionally reserved for the explicit `logout-all` command.
    let result = ApiClient::delete_api_key_for_url(&api_url);
    spinner.finish_and_clear();
    result?;
    ui::print_success(&format!("Logged out from {}", api_url));
    println!("  The selected credential has been removed from this device.");

    Ok(())
}

pub fn logout_all() -> Result<()> {
    let spinner = ui::create_spinner("Logging out from all environments...");

    ApiClient::delete_all_api_keys()?;

    spinner.finish_and_clear();
    ui::print_success("Logged out from all environments!");
    println!("  All credentials have been removed from this device.");

    Ok(())
}

pub fn status(json: bool) -> Result<()> {
    let api_url = config::get_api_url(None);
    let selected_profile = ApiClient::selected_profile()?;

    if json {
        let key = ApiClient::load_optional_api_key_for_url(&api_url)?;
        let direct_env_key = selected_profile.is_none()
            && std::env::var("ARETE_API_KEY").is_ok_and(|value| !value.trim().is_empty());
        let principal_kind = match key.as_deref() {
            Some(key) if key.starts_with("a4_ak_") => "agent",
            Some(_) => "human",
            None => "unknown",
        };
        println!(
            "{}",
            serde_json::to_string(&serde_json::json!({
                "schemaVersion": 1,
                "authenticated": key.is_some(),
                "principalKind": principal_kind,
                "credentialSource": if direct_env_key { "env:ARETE_API_KEY" } else if key.is_some() { "credentials_file" } else { "none" },
                "profile": selected_profile,
                "agent": null,
            }))?
        );
        return Ok(());
    }

    println!("{}", "Authentication Status".bold());
    println!();
    println!("Current target API: {}", api_url.yellow());
    println!(
        "Credential profile: {}",
        selected_profile.as_deref().unwrap_or("auto").yellow()
    );
    println!();

    // Try to load key for current URL
    match ApiClient::load_api_key_for_url(&api_url) {
        Ok(api_key) => {
            println!(
                "{} {}",
                ui::symbols::SUCCESS.green().bold(),
                "Authenticated".green().bold()
            );
            println!();
            println!(
                "  API key: {}...{}",
                &api_key[..8.min(api_key.len())],
                if api_key.len() > 12 {
                    &api_key[api_key.len() - 4..]
                } else {
                    ""
                }
            );
            println!("  Credentials: {}", credentials_path().dimmed());
            println!(
                "  Principal:   {}",
                if api_key.starts_with("a4_ak_") {
                    "agent"
                } else {
                    "human"
                }
            );
            println!();
            println!(
                "  Run {} to verify with the server.",
                "a4 auth whoami".cyan()
            );
        }
        Err(_) => {
            println!(
                "{} {}",
                ui::symbols::FAILURE.red().bold(),
                "Not authenticated".red().bold()
            );
            println!();
            println!("Run 'a4 auth login' to authenticate.");
        }
    }

    // List all stored credentials
    match ApiClient::list_credentials() {
        Ok(creds) if !creds.is_empty() => {
            println!();
            println!("{}", "Stored credentials:".dimmed());
            for credential in creds {
                let is_current = credential.api_url == api_url
                    || (api_url.contains("localhost")
                        && (credential.api_url.contains("localhost")
                            || credential.api_url.contains("127.0.0.1")));
                let marker = if is_current { "→ " } else { "  " };
                let profile = credential.profile.as_deref().unwrap_or("legacy");
                println!(
                    "{}[{}] {} {} ({})",
                    marker,
                    profile,
                    credential.api_url,
                    if is_current {
                        "(current)".green()
                    } else {
                        "".normal()
                    },
                    credential.masked_key,
                );
            }
        }
        _ => {}
    }

    Ok(())
}

pub fn whoami(json: bool) -> Result<()> {
    let api_url = config::get_api_url(None);

    let api_key = match ApiClient::load_api_key_for_url(&api_url) {
        Ok(key) => key,
        Err(_) => {
            ui::print_error("Not authenticated");
            println!();
            println!("  Run {} to authenticate.", "a4 auth login".cyan());
            return Ok(());
        }
    };

    let spinner = (!json).then(|| ui::create_spinner("Verifying authentication..."));
    let client = ApiClient::new()?;

    if api_key.starts_with("a4_ak_") {
        let identity = client.agent_me()?;
        if json {
            print_agent_identity_json(&identity)?;
            return Ok(());
        }
        if let Some(spinner) = spinner {
            spinner.finish_and_clear();
        }
        println!(
            "{} {}",
            ui::symbols::SUCCESS.green().bold(),
            "Authenticated agent".green().bold()
        );
        println!();
        println!("  Slug:        {}", identity.slug);
        println!("  Name:        {}", identity.display_name);
        println!("  Claim state: {}", identity.claim_state);
        println!("  Status:      {}", identity.status);
        if let Some(plan) = &identity.plan {
            println!("  Plan:        {plan}");
        }
        if let Some(expires_at) = &identity.entitlement_expires_at {
            println!("  Expires:     {expires_at}");
            if let Some(remaining) = format_trial_remaining(Some(expires_at)) {
                println!("  Remaining:   {remaining}");
            }
        }
        if let Some(usage) = &identity.usage {
            println!(
                "  Usage:       {}",
                if usage.exhausted {
                    "exhausted".red().bold()
                } else {
                    "active".green().normal()
                }
            );
            for meter in &usage.meters {
                println!(
                    "    {:<25} {:>12} / {}{}",
                    meter.meter,
                    meter.consumed,
                    meter.allowance,
                    if meter.exhausted { " (exhausted)" } else { "" }
                );
            }
        }
        println!("  Target API:  {}", api_url.yellow());
        println!("  Credentials: {}", credentials_path().dimmed());
        return Ok(());
    }

    match client.list_specs() {
        Ok(specs) => {
            if let Some(spinner) = spinner.as_ref() {
                spinner.finish_and_clear();
            }
            if json {
                println!(
                    "{}",
                    serde_json::to_string(&serde_json::json!({
                        "schemaVersion": 1,
                        "principalKind": "human",
                        "credentialSource": "credentials_file",
                        "stackCount": specs.len(),
                    }))?
                );
                return Ok(());
            }
            println!(
                "{} {}",
                ui::symbols::SUCCESS.green().bold(),
                "Authenticated".green().bold()
            );
            println!();
            println!(
                "  API key: {}...{}",
                &api_key[..8.min(api_key.len())],
                &api_key[api_key.len().saturating_sub(4)..]
            );
            println!("  Stacks: {}", specs.len());
            println!("  Target API: {}", api_url.yellow());
            println!("  Credentials: {}", credentials_path().dimmed());
        }
        Err(e) => {
            if let Some(spinner) = spinner.as_ref() {
                spinner.finish_and_clear();
            }
            ui::print_error("API key invalid or expired");
            println!();
            println!("  Error: {}", e);
            println!();
            println!("  Run {} to re-authenticate.", "a4 auth login".cyan());
        }
    }

    Ok(())
}

fn print_agent_identity_json(identity: &AgentMeResponse) -> Result<()> {
    println!(
        "{}",
        serde_json::to_string(&serde_json::json!({
            "schemaVersion": 1,
            "principalKind": "agent",
            "slug": identity.slug,
            "displayName": identity.display_name,
            "status": identity.status,
            "claimState": identity.claim_state,
            "plan": identity.plan,
            "entitlementExpiresAt": identity.entitlement_expires_at,
            "trialRemainingSeconds": trial_remaining_seconds(identity.entitlement_expires_at.as_deref()),
            "trialAccessEnabled": identity.trial_access_enabled,
            "starterGuidance": identity.starter_guidance,
            "usage": identity.usage,
            "credentialSource": "credentials_file",
        }))?
    );
    Ok(())
}

pub fn claim_link(json: bool) -> Result<()> {
    let client = ApiClient::new()?;
    let ready = client.agent_claim_link()?;
    let expected_origin = ApiClient::configured_claim_app_origin()?;
    if !ready.action.is_safe_claim_url_for_origin(&expected_origin) {
        anyhow::bail!("Server returned an unsafe agent claim URL");
    }

    if json {
        println!("{}", serde_json::to_string(&ready)?);
    } else {
        println!("A human owner must open this link to claim the agent:");
        println!("{}", ready.action.url);
        println!("Expires: {}", ready.action.expires_at);
        println!("The agent must not open or complete this link itself.");
    }
    Ok(())
}

// ============================================================================
// Publishable Key Management
// ============================================================================

pub fn list_keys(json: bool) -> Result<()> {
    let client = ApiClient::new()?;

    let spinner = (!json).then(|| ui::create_spinner("Fetching API keys..."));
    let result = client.list_api_keys();
    if let Some(spinner) = spinner {
        spinner.finish_and_clear();
    }

    match result {
        Ok(keys) => {
            if json {
                let keys = keys
                    .iter()
                    .map(|key| {
                        serde_json::json!({
                            "id": key.id,
                            "name": key.name,
                            "keyClass": key.key_class,
                            "origins": key.origin_allowlist.clone().unwrap_or_default(),
                            "expiresAt": key.expires_at,
                            "lastUsedAt": key.last_used_at,
                            "createdAt": key.created_at,
                        })
                    })
                    .collect::<Vec<_>>();
                let payload = serde_json::json!({ "schemaVersion": 1, "keys": keys });
                println!("{}", serde_json::to_string_pretty(&payload)?);
                return Ok(());
            }

            if keys.is_empty() {
                println!("{}", "No API keys found.".yellow());
                println!();
                println!(
                    "  Run {} to create a publishable key for browser use.",
                    "a4 auth keys create-publishable".cyan()
                );
                return Ok(());
            }

            println!("{}", "API Keys:".bold());
            println!();

            for key in keys {
                let key_type = match key.key_class.as_str() {
                    "publishable" => "publishable".green(),
                    "secret" => "secret".cyan(),
                    _ => key.key_class.normal(),
                };

                println!(
                    "  {} {}",
                    "•".bold(),
                    key.name.unwrap_or_else(|| "Unnamed".to_string())
                );
                println!("    ID:    {}", key.id);
                println!("    Type:  {}", key_type);

                if let Some(origins) = key.origin_allowlist {
                    if !origins.is_empty() {
                        println!("    Origins: {}", origins.join(", "));
                    }
                }

                if let Some(expires) = key.expires_at {
                    println!(
                        "    Expires: {}",
                        expires.split('T').next().unwrap_or(&expires)
                    );
                }

                if let Some(last_used) = key.last_used_at {
                    println!(
                        "    Last used: {}",
                        last_used.split('T').next().unwrap_or(&last_used)
                    );
                }

                println!();
            }
        }
        Err(error) => {
            let message = format!("Failed to list keys: {error}");
            return Err(error.context(message));
        }
    }

    Ok(())
}

/// `a4 auth keys create`: create a secret API key. Agent accounts create it
/// through their own key routes.
pub fn create_secret_key(name: Option<String>, expiry_days: Option<i64>, json: bool) -> Result<()> {
    let client = ApiClient::new()?;
    let spinner = (!json).then(|| ui::create_spinner("Creating API key..."));
    let result = client.create_secret_key(name.clone(), expiry_days);
    if let Some(spinner) = spinner {
        spinner.finish_and_clear();
    }
    let response = result.map_err(|error| {
        let message = format!("Failed to create API key: {error}");
        error.context(message)
    })?;
    let name = response.name.clone().or(name);

    if json {
        let payload = serde_json::json!({
            "schemaVersion": 1,
            "id": response.id,
            "name": name,
            "keyClass": response.key_class,
            "expiresAt": response.expires_at,
            "key": response.key,
        });
        println!("{}", serde_json::to_string_pretty(&payload)?);
        return Ok(());
    }

    println!("{}", "✓ API key created".green().bold());
    println!();
    println!(
        "{}",
        "Save this key now; it won't be shown again."
            .yellow()
            .bold()
    );
    println!();
    if let Some(name) = &name {
        println!("  Name:       {}", name);
    }
    println!("  Key ID:     {}", response.id);
    println!("  Type:       {}", response.key_class.cyan());
    println!(
        "  Expires:    {}",
        response
            .expires_at
            .split('T')
            .next()
            .unwrap_or(&response.expires_at)
    );
    println!();
    println!("  {}", "Secret Key:".bold());
    println!("  {}", response.key.green().bold());
    println!();
    println!(
        "{}",
        "Keep this key secret: never put it in browser code or commit it.".dimmed()
    );
    Ok(())
}

/// `a4 auth keys delete <id>`: revoke one of the account's API keys.
pub fn delete_key(key_id: i32, json: bool) -> Result<()> {
    if !ui::assume_yes() {
        if !ui::interactive() {
            anyhow::bail!(
                "Deleting a key requires -y/--yes when not interactive. Pass: a4 auth keys delete {key_id} --yes"
            );
        }
        let confirmed = dialoguer::Confirm::new()
            .with_prompt(format!(
                "Delete API key {key_id}? Anything using it stops working immediately"
            ))
            .default(false)
            .interact()?;
        if !confirmed {
            anyhow::bail!("Key deletion cancelled");
        }
    }

    let client = ApiClient::new()?;
    client.delete_api_key(key_id).map_err(|error| {
        let message = format!("Failed to delete API key {key_id}: {error}");
        error.context(message)
    })?;

    if json {
        let payload = serde_json::json!({ "schemaVersion": 1, "id": key_id, "deleted": true });
        println!("{}", serde_json::to_string_pretty(&payload)?);
    } else {
        println!("{} Deleted API key {key_id}", "✓".green().bold());
    }
    Ok(())
}

/// Options for `a4 auth keys create-publishable`.
pub struct CreatePublishableArgs {
    pub name: Option<String>,
    pub origins: Vec<String>,
    pub expiry_days: Option<i64>,
    /// Write only the key's environment variable into this file.
    pub env_file: Option<String>,
}

/// The browser framework a project uses, which decides the environment
/// variable its bundler exposes to client code.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Framework {
    NextJs,
    Vite,
    Generic,
}

impl Framework {
    fn id(self) -> &'static str {
        match self {
            Framework::NextJs => "nextjs",
            Framework::Vite => "vite",
            Framework::Generic => "generic",
        }
    }

    pub(crate) fn env_var(self) -> &'static str {
        match self {
            Framework::NextJs => "NEXT_PUBLIC_ARETE_PUBLISHABLE_KEY",
            Framework::Vite => "VITE_ARETE_PUBLISHABLE_KEY",
            Framework::Generic => "ARETE_PUBLISHABLE_KEY",
        }
    }

    fn how(self) -> &'static str {
        match self {
            Framework::NextJs => "Next.js exposes NEXT_PUBLIC_* variables to browser code",
            Framework::Vite => "Vite exposes VITE_* variables to browser code as import.meta.env",
            Framework::Generic => "read it from the environment and pass it as auth.publishableKey",
        }
    }
}

const NEXT_CONFIGS: [&str; 5] = [
    "next.config.js",
    "next.config.mjs",
    "next.config.cjs",
    "next.config.ts",
    "next.config.mts",
];
const VITE_CONFIGS: [&str; 6] = [
    "vite.config.js",
    "vite.config.mjs",
    "vite.config.cjs",
    "vite.config.ts",
    "vite.config.mts",
    "vite.config.cts",
];

/// Detect the framework from config files and package.json dependencies in
/// the project directory. Next.js wins over Vite, because a Next.js app may
/// carry Vite as a test dependency.
pub(crate) fn detect_framework(root: &Path) -> Framework {
    let package_deps = std::fs::read_to_string(root.join("package.json"))
        .ok()
        .and_then(|text| serde_json::from_str::<serde_json::Value>(&text).ok())
        .map(|package| {
            ["dependencies", "devDependencies"]
                .iter()
                .filter_map(|key| package.get(*key).and_then(serde_json::Value::as_object))
                .flat_map(|deps| deps.keys().cloned())
                .collect::<Vec<_>>()
        })
        .unwrap_or_default();
    let has_dep = |name: &str| package_deps.iter().any(|dep| dep == name);
    let has_file = |names: &[&str]| names.iter().any(|name| root.join(name).is_file());
    if has_file(&NEXT_CONFIGS) || has_dep("next") {
        Framework::NextJs
    } else if has_file(&VITE_CONFIGS) || has_dep("vite") {
        Framework::Vite
    } else {
        Framework::Generic
    }
}

/// Check the one origin a publishable key allows. The browser sends
/// `scheme://host[:port]` with no path, so anything else could never match.
fn validate_origins(origins: &[String]) -> Result<String> {
    let origin = match origins {
        [origin] => origin.trim(),
        [] => anyhow::bail!(
            "A publishable key needs exactly one origin (e.g. https://example.com or http://localhost:5173)"
        ),
        many => anyhow::bail!(
            "A publishable key allows exactly one origin; got {} ({}). Create one key per origin.",
            many.len(),
            many.join(", ")
        ),
    };
    if !origin.starts_with("https://") && !origin.starts_with("http://") {
        anyhow::bail!("Invalid origin '{origin}'. Origins must start with https:// or http://");
    }
    let parsed = url::Url::parse(origin)
        .map_err(|error| anyhow::anyhow!("Invalid origin '{origin}': {error}"))?;
    let normalized = parsed.origin().ascii_serialization();
    if normalized != origin {
        anyhow::bail!(
            "Invalid origin '{origin}'. Browsers send the origin as scheme://host[:port] with no path or trailing slash; use --origin {normalized}"
        );
    }
    Ok(normalized)
}

/// Resolve `--env-file`. A relative path is taken from the project directory
/// and must stay inside it, including through symlinked directories, and must
/// not name a symlink; it resolves to its real location. An absolute path is
/// an explicit choice and is used as given, following a symlink to the file
/// it points to.
fn resolve_env_file(root: &Path, raw: &str) -> Result<PathBuf> {
    let raw = raw.trim();
    if raw.is_empty() {
        anyhow::bail!("--env-file must not be empty");
    }
    let path = Path::new(raw);
    if path.is_absolute() {
        if is_symlink(path) {
            return std::fs::canonicalize(path).map_err(|error| {
                anyhow::anyhow!("--env-file {raw} is a symlink that cannot be followed: {error}")
            });
        }
        return Ok(path.to_path_buf());
    }
    let project = std::fs::canonicalize(root).map_err(|error| {
        anyhow::anyhow!(
            "Failed to resolve the project directory {}: {error}",
            root.display()
        )
    })?;
    let outside = || {
        anyhow::anyhow!(
            "--env-file {raw} points outside the project directory ({}). Pass an absolute path to write there explicitly.",
            project.display()
        )
    };
    let mut parts = Vec::new();
    for component in path.components() {
        match component {
            Component::Normal(part) => parts.push(part),
            Component::CurDir => {}
            _ => return Err(outside()),
        }
    }
    let Some(file_name) = parts.pop() else {
        anyhow::bail!("--env-file {raw} must name a file");
    };
    // The lexical check alone would follow a symlinked directory out of the
    // project: resolve the directory and check where it really is.
    let parent = parts
        .iter()
        .fold(root.to_path_buf(), |directory, part| directory.join(part));
    let directory = match std::fs::canonicalize(&parent) {
        Ok(directory) => directory,
        Err(error) if error.kind() == io::ErrorKind::NotFound => anyhow::bail!(
            "--env-file {raw}: directory {} does not exist",
            parent.display()
        ),
        Err(error) => anyhow::bail!("Failed to resolve --env-file {raw}: {error}"),
    };
    if !directory.starts_with(&project) {
        return Err(outside());
    }
    let resolved = directory.join(file_name);
    if is_symlink(&resolved) {
        anyhow::bail!(
            "--env-file {raw} is a symlink; pass the absolute path of the file it points to"
        );
    }
    Ok(resolved)
}

fn is_symlink(path: &Path) -> bool {
    std::fs::symlink_metadata(path).is_ok_and(|meta| meta.file_type().is_symlink())
}

/// Read the current env file (if any) before the key is created, so a path
/// problem fails without creating a key that could then be lost.
fn read_env_file(path: &Path) -> Result<Option<String>> {
    if path.is_dir() {
        anyhow::bail!("--env-file {} is a directory", path.display());
    }
    let parent = path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
        .unwrap_or_else(|| Path::new("."));
    if !parent.is_dir() {
        anyhow::bail!(
            "--env-file {}: directory {} does not exist",
            path.display(),
            parent.display()
        );
    }
    match std::fs::read_to_string(path) {
        Ok(text) => Ok(Some(text)),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(error) => Err(anyhow::anyhow!(
            "Failed to read --env-file {}: {error}",
            path.display()
        )),
    }
}

/// Replace the env file with `content` atomically: write a temporary file
/// beside it and rename it over the original, so a failed write leaves every
/// other variable in place. An existing file keeps its permissions (env files
/// are often private); a new one gets the default mode.
fn write_env_file(path: &Path, content: &str) -> io::Result<()> {
    let directory = path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
        .unwrap_or_else(|| Path::new("."));
    let mut temporary_name = std::ffi::OsString::from(".");
    temporary_name.push(path.file_name().unwrap_or_default());
    temporary_name.push(format!(".{}.tmp", uuid::Uuid::new_v4()));
    let temporary = directory.join(temporary_name);
    let permissions = match std::fs::metadata(path) {
        Ok(metadata) => Some(metadata.permissions()),
        Err(error) if error.kind() == io::ErrorKind::NotFound => None,
        Err(error) => return Err(error),
    };
    let result = (|| {
        let mut options = std::fs::OpenOptions::new();
        options.write(true).create_new(true);
        // Private until the original's permissions are applied.
        #[cfg(unix)]
        if permissions.is_some() {
            std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
        }
        let mut file = options.open(&temporary)?;
        file.write_all(content.as_bytes())?;
        if let Some(permissions) = permissions {
            file.set_permissions(permissions)?;
        }
        file.sync_all()?;
        drop(file);
        std::fs::rename(&temporary, path)
    })();
    if result.is_err() {
        let _ = std::fs::remove_file(&temporary);
    }
    result
}

/// How an env file changed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum EnvChange {
    Created,
    Replaced,
    Appended,
    Unchanged,
}

impl EnvChange {
    fn as_str(self) -> &'static str {
        match self {
            EnvChange::Created => "created",
            EnvChange::Replaced => "replaced",
            EnvChange::Appended => "appended",
            EnvChange::Unchanged => "unchanged",
        }
    }
}

/// Whether `line` assigns `name` (`NAME=…` or `export NAME=…`, any leading
/// whitespace).
fn assigns(line: &str, name: &str) -> bool {
    let line = line.trim_start();
    let line = line
        .strip_prefix("export")
        .filter(|rest| rest.starts_with(char::is_whitespace))
        .map(str::trim_start)
        .unwrap_or(line);
    line.strip_prefix(name)
        .is_some_and(|rest| rest.trim_start().starts_with('='))
}

/// Set `name=value` in env-file text, touching no other line: every existing
/// assignment of `name` is rewritten in place (keeping `export` and
/// indentation); otherwise one line is appended.
fn upsert_env_var(existing: Option<&str>, name: &str, value: &str) -> (String, EnvChange) {
    let Some(existing) = existing else {
        return (format!("{name}={value}\n"), EnvChange::Created);
    };
    let mut output = String::with_capacity(existing.len() + name.len() + value.len() + 2);
    let mut replaced = false;
    for line in existing.split_inclusive('\n') {
        let (body, ending) = match line.strip_suffix("\r\n") {
            Some(body) => (body, "\r\n"),
            None => match line.strip_suffix('\n') {
                Some(body) => (body, "\n"),
                None => (line, ""),
            },
        };
        if assigns(body, name) {
            let indent_len = body.len() - body.trim_start().len();
            let exported = body.trim_start().starts_with("export");
            output.push_str(&body[..indent_len]);
            if exported {
                output.push_str("export ");
            }
            output.push_str(&format!("{name}={value}{ending}"));
            replaced = true;
        } else {
            output.push_str(line);
        }
    }
    if replaced {
        let change = if output == existing {
            EnvChange::Unchanged
        } else {
            EnvChange::Replaced
        };
        return (output, change);
    }
    let newline = if existing.contains("\r\n") {
        "\r\n"
    } else {
        "\n"
    };
    if !output.is_empty() && !output.ends_with('\n') {
        output.push_str(newline);
    }
    output.push_str(&format!("{name}={value}{newline}"));
    (output, EnvChange::Appended)
}

pub fn create_publishable_key(
    args: CreatePublishableArgs,
    config_path: &str,
    json: bool,
) -> Result<()> {
    let origin = validate_origins(&args.origins)?;
    let root = super::init::project_root(config_path);
    let framework = detect_framework(&root);
    let env_var = framework.env_var();
    let env_target = match args.env_file.as_deref() {
        Some(raw) => {
            let path = resolve_env_file(&root, raw)?;
            let existing = read_env_file(&path)?;
            Some((path, existing))
        }
        None => None,
    };

    let client = ApiClient::new()?;
    let spinner = (!json).then(|| ui::create_spinner("Creating publishable key..."));
    let result =
        client.create_publishable_key(args.name.clone(), vec![origin.clone()], args.expiry_days);
    if let Some(spinner) = spinner {
        spinner.finish_and_clear();
    }
    let response = match result {
        Ok(response) => response,
        Err(error) => {
            let message = format!("Failed to create publishable key: {error}");
            return Err(error.context(message));
        }
    };

    // The key exists now and is shown only once: report it even if writing
    // the env file fails, then fail the command.
    let written = env_target.map(|(path, existing)| {
        let (content, change) = upsert_env_var(existing.as_deref(), env_var, &response.key);
        let result = if change == EnvChange::Unchanged {
            Ok(())
        } else {
            write_env_file(&path, &content)
        };
        (path, change, result)
    });
    let (env_file, env_change, write_error) = match written {
        Some((path, change, Ok(()))) => (Some(path), Some(change), None),
        Some((path, _, Err(error))) => (
            None,
            None,
            Some(anyhow::anyhow!(
                "Created the key, but failed to write {}: {error}. Set {env_var} yourself from the key above.",
                path.display()
            )),
        ),
        None => (None, None, None),
    };
    let name = response.name.clone().or(args.name);

    if json {
        let payload = serde_json::json!({
            "schemaVersion": 1,
            "id": response.id,
            "name": name,
            "keyClass": response.key_class,
            "origins": [origin],
            "expiresAt": response.expires_at,
            "key": response.key,
            "framework": framework.id(),
            "envVar": env_var,
            "envFile": env_file.as_ref().map(|path| path.display().to_string()),
            "envFileChange": env_change.map(EnvChange::as_str),
        });
        println!("{}", serde_json::to_string_pretty(&payload)?);
    } else {
        println!("{}", "✓ Publishable key created".green().bold());
        println!();
        println!(
            "{}",
            "Save this key now; it won't be shown again."
                .yellow()
                .bold()
        );
        println!();
        if let Some(name) = &name {
            println!("  Name:       {}", name);
        }
        println!("  Key ID:     {}", response.id);
        println!("  Type:       {}", "publishable".green());
        println!("  Origin:     {}", origin);
        println!(
            "  Expires:    {}",
            response
                .expires_at
                .split('T')
                .next()
                .unwrap_or(&response.expires_at)
        );
        println!();
        println!("  {}", "Publishable Key:".bold());
        println!("  {}", response.key.green().bold());
        println!();
        match (&env_file, env_change) {
            (Some(path), Some(change)) => println!(
                "  {} {env_var} in {} ({})",
                match change {
                    EnvChange::Created | EnvChange::Appended => "Wrote",
                    EnvChange::Replaced => "Replaced",
                    EnvChange::Unchanged => "Kept",
                },
                path.display(),
                framework.how()
            ),
            _ => println!(
                "  Set {} in your environment ({}), or rerun with --env-file .env.local",
                format!("{env_var}=<key>").cyan(),
                framework.how()
            ),
        }
        println!();
        println!(
            "{}",
            "This key is safe in browser code. Each publishable key allows exactly one origin;"
                .dimmed()
        );
        println!(
            "{}",
            "create another key for every other origin (e.g. production and localhost).".dimmed()
        );
    }

    match write_error {
        Some(error) => Err(error),
        None => Ok(()),
    }
}

// ============================================================================
// Agent self-registration (WP9)
// ============================================================================

/// What `a4 auth signup` produced, before any printing.
#[derive(Debug)]
struct SignupOutcome {
    identity: AgentMeResponse,
    credentials_path: std::path::PathBuf,
    created: bool,
    idempotent: Option<bool>,
}

fn generate_agent_credential() -> String {
    let random = format!(
        "{}{}",
        uuid::Uuid::new_v4().simple(),
        uuid::Uuid::new_v4().simple()
    );
    format!("a4_ak_{}", &random[..40])
}

fn pending_signup() -> PendingAgentSignup {
    PendingAgentSignup {
        credential: generate_agent_credential(),
        idempotency_key: uuid::Uuid::new_v4().to_string(),
    }
}

/// Register with `POST /api/agents/signup/v2`. Pending state is persisted
/// before the network request and reused after timeouts or process restarts.
fn perform_signup(
    client: &ApiClient,
    api_url: &str,
    name: Option<&str>,
    profile: &str,
    force: bool,
    if_missing: bool,
) -> Result<SignupOutcome> {
    if force && if_missing {
        anyhow::bail!("--force and --if-missing cannot be used together");
    }
    let _lock = ApiClient::lock_credentials()?;
    let existing = ApiClient::load_optional_api_key_for_profile(api_url, Some(profile))?;
    if let Some(existing) = existing {
        if if_missing {
            match ApiClient::with_base_url(api_url)
                .with_api_key(existing)
                .agent_me()
            {
                Ok(identity) => {
                    ApiClient::clear_pending_agent_signup(api_url, profile)?;
                    return Ok(SignupOutcome {
                        identity,
                        credentials_path: ApiClient::credentials_file_path()?,
                        created: false,
                        idempotent: None,
                    });
                }
                Err(error)
                    if matches!(
                        api_error_details(&error).map(|details| details.status),
                        Some(401 | 403)
                    ) =>
                {
                    anyhow::bail!(
                        "The stored `{profile}` credential for {api_url} is invalid or disabled ({error}). It was left untouched; rerun `a4 auth signup --force` to replace it"
                    );
                }
                Err(error) => {
                    return Err(error).with_context(|| {
                        format!(
                            "Could not verify the stored `{profile}` credential for {api_url}; it was left untouched. Retry `a4 auth signup --if-missing`"
                        )
                    });
                }
            }
        } else if !force {
            anyhow::bail!(
                "Credentials already exist for profile `{profile}` at {api_url}. Run: a4 auth status (or pass --force to replace them)"
            );
        }
    }

    let pending = match ApiClient::load_pending_agent_signup(api_url, profile)? {
        Some(pending) => pending,
        None => {
            let pending = pending_signup();
            ApiClient::save_pending_agent_signup(api_url, profile, &pending)?;
            pending
        }
    };
    let response = client
        .agent_signup(name, &pending.credential, &pending.idempotency_key)
        .with_context(|| {
            "Agent signup did not complete. Pending state was retained; retry the same command safely"
        })?;
    if response.schema_version != 1 {
        anyhow::bail!(
            "Agent signup returned unsupported schema version {}; pending state was retained",
            response.schema_version
        );
    }
    let identity = ApiClient::with_base_url(api_url)
        .with_api_key(pending.credential.clone())
        .agent_me()
        .with_context(|| {
            "The new agent credential could not be verified. Pending state was retained; retry the same command safely"
        })?;
    if identity.slug != response.slug
        || identity.plan.as_deref() != Some(response.plan.as_str())
        || !signup_expiry_matches(
            identity.entitlement_expires_at.as_deref(),
            &response.entitlement_expires_at,
        )
        || identity.claim_state != response.claim_state
    {
        anyhow::bail!(
            "Agent signup verification did not match the signup response; pending state was retained"
        );
    }
    ApiClient::promote_pending_agent_signup(api_url, profile, &pending)?;
    let credentials_path = ApiClient::credentials_file_path()?;

    Ok(SignupOutcome {
        identity,
        credentials_path,
        created: true,
        idempotent: Some(response.idempotent),
    })
}

fn signup_json_payload(outcome: &SignupOutcome, profile: &str) -> serde_json::Value {
    serde_json::json!({
        "schemaVersion": 1,
        "slug": outcome.identity.slug,
        "displayName": outcome.identity.display_name,
        "createdAt": outcome.identity.created_at,
        "credentialStored": true,
        "credentialPathKind": if std::env::var_os("ARETE_CREDENTIALS_PATH").is_some() { "override" } else { "default" },
        "profile": profile,
        "accountStatus": outcome.identity.status,
        "plan": outcome.identity.plan,
        "entitlementExpiresAt": outcome.identity.entitlement_expires_at,
        "trialRemainingSeconds": trial_remaining_seconds(outcome.identity.entitlement_expires_at.as_deref()),
        "claimState": outcome.identity.claim_state,
        "trialAccessEnabled": outcome.identity.trial_access_enabled,
        "starterGuidance": outcome.identity.starter_guidance,
        "usage": outcome.identity.usage,
        "created": outcome.created,
        "idempotent": outcome.idempotent,
    })
}

/// `a4 auth signup`: agent self-registration (WP9).
///
/// Both human and JSON modes omit the key after it has been stored.
pub fn signup(
    name: Option<String>,
    force: bool,
    if_missing: bool,
    json: bool,
    requested_profile: Option<&str>,
) -> Result<()> {
    let api_url = config::get_api_url(None);
    let profile = requested_profile.unwrap_or(arete_mcp::credentials::AGENT_PROFILE);
    if profile != arete_mcp::credentials::AGENT_PROFILE {
        anyhow::bail!(
            "a4 auth signup creates an agent credential and must use profile `agent`; pass --profile agent or unset ARETE_PROFILE"
        );
    }
    let client = ApiClient::with_base_url(&api_url);

    let spinner = (!json).then(|| ui::create_spinner("Registering agent..."));
    let outcome = perform_signup(
        &client,
        &api_url,
        name.as_deref(),
        profile,
        force,
        if_missing,
    );
    if let Some(spinner) = spinner {
        spinner.finish_and_clear();
    }
    let outcome = outcome?;

    if json {
        let payload = signup_json_payload(&outcome, profile);
        println!("{}", serde_json::to_string(&payload)?);
        return Ok(());
    }

    ui::print_success(&format!(
        "{} agent {} ({})",
        if outcome.created {
            "Registered"
        } else {
            "Verified existing"
        },
        outcome.identity.slug.bold(),
        outcome.identity.display_name
    ));
    println!("  Target API:  {}", api_url.yellow());
    println!("  Profile:     {}", profile);
    println!(
        "  Credentials: {}",
        outcome.credentials_path.display().to_string().dimmed()
    );
    if let Some(plan) = &outcome.identity.plan {
        println!("  Plan:        {plan}");
    }
    if let Some(expires_at) = &outcome.identity.entitlement_expires_at {
        println!("  Expires:     {expires_at}");
        if let Some(remaining) = format_trial_remaining(Some(expires_at)) {
            println!("  Remaining:   {remaining}");
        }
    }
    println!();
    println!("Next: {}", "a4 explore --json".cyan());
    Ok(())
}

#[cfg(test)]
mod publishable_tests {
    use super::*;
    use crate::api_client::test_support::{MockServer, ENV_LOCK};

    #[test]
    fn framework_detection_prefers_next_then_vite() {
        let dir = tempfile::tempdir().unwrap();
        assert_eq!(detect_framework(dir.path()), Framework::Generic);
        std::fs::write(
            dir.path().join("package.json"),
            r#"{"devDependencies":{"vite":"^5"}}"#,
        )
        .unwrap();
        assert_eq!(detect_framework(dir.path()), Framework::Vite);
        assert_eq!(Framework::Vite.env_var(), "VITE_ARETE_PUBLISHABLE_KEY");
        std::fs::write(dir.path().join("next.config.mjs"), "export default {}").unwrap();
        assert_eq!(detect_framework(dir.path()), Framework::NextJs);
        assert_eq!(
            Framework::NextJs.env_var(),
            "NEXT_PUBLIC_ARETE_PUBLISHABLE_KEY"
        );
        assert_eq!(Framework::Generic.env_var(), "ARETE_PUBLISHABLE_KEY");
    }

    #[test]
    fn exactly_one_bare_origin_is_accepted() {
        assert_eq!(
            validate_origins(&["http://localhost:5173".into()]).unwrap(),
            "http://localhost:5173"
        );
        let many = validate_origins(&["https://a.test".into(), "https://b.test".into()])
            .unwrap_err()
            .to_string();
        assert!(many.contains("exactly one origin"), "{many}");
        assert!(validate_origins(&[]).is_err());
        assert!(validate_origins(&["example.com".into()]).is_err());
        let slash = validate_origins(&["https://example.com/".into()])
            .unwrap_err()
            .to_string();
        assert!(slash.contains("--origin https://example.com"), "{slash}");
    }

    #[test]
    fn env_file_paths_stay_in_the_project_unless_absolute() {
        let dir = tempfile::tempdir().unwrap();
        let project = std::fs::canonicalize(dir.path()).unwrap();
        assert_eq!(
            resolve_env_file(dir.path(), ".env.local").unwrap(),
            project.join(".env.local")
        );
        std::fs::create_dir_all(dir.path().join("apps/web")).unwrap();
        assert_eq!(
            resolve_env_file(dir.path(), "./apps/web/.env").unwrap(),
            project.join("apps/web/.env")
        );
        assert!(resolve_env_file(dir.path(), "../.env").is_err());
        assert!(resolve_env_file(dir.path(), "web/../../.env").is_err());
        assert!(resolve_env_file(dir.path(), " ").is_err());
        assert!(resolve_env_file(dir.path(), ".").is_err());
        let absolute = dir.path().join("elsewhere.env");
        assert_eq!(
            resolve_env_file(Path::new("/unrelated"), absolute.to_str().unwrap()).unwrap(),
            absolute
        );
    }

    #[cfg(unix)]
    #[test]
    fn env_file_paths_cannot_leave_the_project_through_a_symlink() {
        use std::os::unix::fs::symlink;
        let dir = tempfile::tempdir().unwrap();
        let outside = tempfile::tempdir().unwrap();
        let project = std::fs::canonicalize(dir.path()).unwrap();
        symlink(outside.path(), dir.path().join("web")).unwrap();
        for raw in ["web/.env", "./web/.env"] {
            let error = resolve_env_file(dir.path(), raw).unwrap_err().to_string();
            assert!(error.contains("outside the project directory"), "{error}");
        }
        std::fs::create_dir_all(dir.path().join("apps/site")).unwrap();
        symlink(dir.path().join("apps"), dir.path().join("linked")).unwrap();
        assert_eq!(
            resolve_env_file(dir.path(), "linked/site/.env").unwrap(),
            project.join("apps/site/.env"),
            "a symlinked directory that stays inside the project is fine"
        );
        symlink(outside.path().join(".env"), dir.path().join(".env.local")).unwrap();
        let error = resolve_env_file(dir.path(), ".env.local")
            .unwrap_err()
            .to_string();
        assert!(error.contains("is a symlink"), "{error}");
        // An absolute path is explicit: it follows the link.
        std::fs::write(outside.path().join(".env"), "A=1\n").unwrap();
        let linked = dir.path().join(".env.local");
        assert_eq!(
            resolve_env_file(dir.path(), linked.to_str().unwrap()).unwrap(),
            std::fs::canonicalize(outside.path().join(".env")).unwrap()
        );
    }

    #[cfg(unix)]
    #[test]
    fn rewriting_an_env_file_keeps_its_permissions() {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join(".env.local");
        for mode in [0o600, 0o640, 0o604] {
            std::fs::write(&path, "SECRET=keep\n").unwrap();
            std::fs::set_permissions(&path, std::fs::Permissions::from_mode(mode)).unwrap();
            write_env_file(&path, "SECRET=keep\nA=1\n").unwrap();
            assert_eq!(
                std::fs::read_to_string(&path).unwrap(),
                "SECRET=keep\nA=1\n"
            );
            let actual = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777;
            assert_eq!(actual, mode, "{actual:o} != {mode:o}");
        }
        let entries = std::fs::read_dir(dir.path()).unwrap().count();
        assert_eq!(entries, 1, "no temporary file is left behind");
    }

    #[test]
    fn env_upsert_touches_only_the_variable() {
        let name = "VITE_ARETE_PUBLISHABLE_KEY";
        assert_eq!(
            upsert_env_var(None, name, "hspk_new"),
            (format!("{name}=hspk_new\n"), EnvChange::Created)
        );
        let existing = "SECRET=keep\r\n  export VITE_ARETE_PUBLISHABLE_KEY = hspk_old\r\nVITE_ARETE_PUBLISHABLE_KEY_OTHER=x\r\n";
        let (content, change) = upsert_env_var(Some(existing), name, "hspk_new");
        assert_eq!(change, EnvChange::Replaced);
        assert_eq!(
            content,
            "SECRET=keep\r\n  export VITE_ARETE_PUBLISHABLE_KEY=hspk_new\r\nVITE_ARETE_PUBLISHABLE_KEY_OTHER=x\r\n"
        );
        let (content, change) = upsert_env_var(Some("A=1"), name, "hspk_new");
        assert_eq!(change, EnvChange::Appended);
        assert_eq!(content, format!("A=1\n{name}=hspk_new\n"));
        let same = format!("{name}=hspk_new\n");
        assert_eq!(
            upsert_env_var(Some(&same), name, "hspk_new").1,
            EnvChange::Unchanged
        );
    }

    struct Sandbox {
        _guard: std::sync::MutexGuard<'static, ()>,
        dir: tempfile::TempDir,
    }

    impl Sandbox {
        fn new(server: &MockServer) -> Self {
            let guard = ENV_LOCK
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            let dir = tempfile::tempdir().unwrap();
            let credentials = dir.path().join("credentials.toml");
            std::fs::write(
                &credentials,
                format!("[keys]\n\"{}\" = \"a4_sk_owner\"\n", server.base_url()),
            )
            .unwrap();
            std::env::set_var("ARETE_API_URL", server.base_url());
            std::env::set_var("ARETE_CREDENTIALS_PATH", &credentials);
            std::fs::write(
                dir.path().join("package.json"),
                r#"{"dependencies":{"vite":"^5"}}"#,
            )
            .unwrap();
            Self { _guard: guard, dir }
        }

        fn config(&self) -> String {
            self.dir.path().join("arete.toml").display().to_string()
        }
    }

    impl Drop for Sandbox {
        fn drop(&mut self) {
            std::env::remove_var("ARETE_API_URL");
            std::env::remove_var("ARETE_CREDENTIALS_PATH");
        }
    }

    fn args(env_file: Option<&str>) -> CreatePublishableArgs {
        CreatePublishableArgs {
            name: Some("web".into()),
            origins: vec!["http://localhost:5173".into()],
            expiry_days: None,
            env_file: env_file.map(str::to_string),
        }
    }

    #[test]
    fn api_failure_is_an_error_and_writes_nothing() {
        let server = MockServer::json(
            400,
            r#"{"error":"A publishable key must have exactly one allowed origin","code":"origin-allowlist-too-many"}"#,
        );
        let sandbox = Sandbox::new(&server);
        let env = sandbox.dir.path().join(".env.local");
        std::fs::write(&env, "OTHER=1\n").unwrap();
        let error = create_publishable_key(args(Some(".env.local")), &sandbox.config(), true)
            .unwrap_err()
            .to_string();
        assert!(
            error.contains("Failed to create publishable key")
                && error.contains("origin-allowlist-too-many"),
            "{error}"
        );
        assert_eq!(std::fs::read_to_string(&env).unwrap(), "OTHER=1\n");
    }

    #[test]
    fn created_key_is_written_to_the_env_file_for_the_detected_framework() {
        let server = MockServer::json(
            201,
            r#"{"id":7,"key":"hspk_fresh","name":"web","key_class":"publishable","expires_at":"2027-09-25T00:00:00Z","message":"ok"}"#,
        );
        let sandbox = Sandbox::new(&server);
        let env = sandbox.dir.path().join(".env.local");
        std::fs::write(&env, "OTHER=1\nVITE_ARETE_PUBLISHABLE_KEY=hspk_old\n").unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&env, std::fs::Permissions::from_mode(0o600)).unwrap();
        }
        create_publishable_key(args(Some(".env.local")), &sandbox.config(), true).unwrap();
        assert_eq!(
            std::fs::read_to_string(&env).unwrap(),
            "OTHER=1\nVITE_ARETE_PUBLISHABLE_KEY=hspk_fresh\n"
        );
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = std::fs::metadata(&env).unwrap().permissions().mode() & 0o777;
            assert_eq!(mode, 0o600, "a private env file stays private");
        }
        let body: serde_json::Value = serde_json::from_str(&server.request().body).unwrap();
        assert_eq!(
            body["origin_allowlist"],
            serde_json::json!(["http://localhost:5173"])
        );
    }

    #[test]
    fn a_bad_env_file_path_fails_before_any_key_is_created() {
        let server = MockServer::json(500, r#"{"error":"must not be called"}"#);
        let sandbox = Sandbox::new(&server);
        let error = create_publishable_key(args(Some("missing-dir/.env")), &sandbox.config(), true)
            .unwrap_err()
            .to_string();
        assert!(error.contains("does not exist"), "{error}");
        let error = create_publishable_key(args(Some("../.env")), &sandbox.config(), true)
            .unwrap_err()
            .to_string();
        assert!(error.contains("outside the project"), "{error}");
        #[cfg(unix)]
        {
            let outside = tempfile::tempdir().unwrap();
            std::os::unix::fs::symlink(outside.path(), sandbox.dir.path().join("web")).unwrap();
            let error = create_publishable_key(args(Some("web/.env")), &sandbox.config(), true)
                .unwrap_err()
                .to_string();
            assert!(error.contains("outside the project"), "{error}");
            assert!(!outside.path().join(".env").exists());
        }
    }
}

#[cfg(test)]
mod signup_tests {
    use super::*;
    use crate::api_client::test_support::MockServer;
    use crate::api_client::ApiClientError;
    use std::sync::MutexGuard;

    /// `ARETE_CREDENTIALS_PATH` is process-global; serialise the tests that set it.
    use crate::api_client::test_support::ENV_LOCK;

    struct CredentialsSandbox {
        _guard: MutexGuard<'static, ()>,
        dir: tempfile::TempDir,
    }

    impl CredentialsSandbox {
        fn new() -> Self {
            let guard = ENV_LOCK
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            let dir = tempfile::tempdir().expect("tempdir");
            std::env::set_var(
                "ARETE_CREDENTIALS_PATH",
                dir.path().join("creds").join("credentials.toml"),
            );
            CredentialsSandbox { _guard: guard, dir }
        }

        fn credentials_path(&self) -> std::path::PathBuf {
            self.dir.path().join("creds").join("credentials.toml")
        }
    }

    impl Drop for CredentialsSandbox {
        fn drop(&mut self) {
            std::env::remove_var("ARETE_CREDENTIALS_PATH");
        }
    }

    const OK_BODY: &str = r#"{"schemaVersion":1,"slug":"agent-7f3a","displayName":"Robo","createdAt":"2026-09-29T00:00:00Z","plan":"agent_trial","entitlementExpiresAt":"2026-10-06T00:00:00Z","claimState":"unclaimed","idempotent":false}"#;
    const ME_BODY: &str = r#"{"slug":"agent-7f3a","display_name":"Robo","status":"active","created_at":"2026-09-29T00:00:00Z","last_seen_at":null,"claimState":"unclaimed","plan":"agent_trial","entitlementExpiresAt":"2026-10-06T00:00:00Z","trialAccessEnabled":true,"starterGuidance":"Use starter stacks.","usage":{"window":"entitlement","windowStart":"2026-09-29T00:00:00Z","windowEnd":"2026-10-06T00:00:00Z","meters":[{"meter":"messages","consumed":7,"allowance":25,"remaining":18,"exhausted":false}],"exhausted":false}}"#;

    #[test]
    fn signup_rejects_non_agent_profile_before_registration() {
        let error = signup(None, false, false, true, Some("human")).unwrap_err();
        assert!(error.to_string().contains("must use profile `agent`"));
    }

    #[test]
    fn signup_stores_key_for_api_url_and_reports_slug() {
        let sandbox = CredentialsSandbox::new();
        let server =
            MockServer::json_sequence(vec![(200, OK_BODY.to_string()), (200, ME_BODY.to_string())]);
        let client = ApiClient::with_base_url(server.base_url());

        let outcome = perform_signup(
            &client,
            server.base_url(),
            Some("Robo"),
            "agent",
            false,
            false,
        )
        .expect("signup succeeds");

        assert_eq!(outcome.identity.slug, "agent-7f3a");
        assert_eq!(outcome.identity.display_name, "Robo");
        let usage = outcome.identity.usage.as_ref().expect("usage summary");
        assert!(!usage.exhausted);
        assert_eq!(usage.meters[0].remaining, 18);
        assert_eq!(outcome.credentials_path, sandbox.credentials_path());
        let stored_key = ApiClient::load_optional_api_key_for_url(server.base_url())
            .expect("credentials readable")
            .expect("key stored");
        let stored = std::fs::read_to_string(sandbox.credentials_path()).unwrap();
        assert!(stored.contains("[profiles.agent.keys]"), "{stored}");
        assert!(!stored.contains("pendingSignup"), "{stored}");
        let signup_request = server.request();
        let body: serde_json::Value =
            serde_json::from_str(&signup_request.body).expect("json body");
        assert_eq!(body["displayName"], "Robo");
        assert_eq!(body["credential"], stored_key);
        assert!(uuid::Uuid::parse_str(body["idempotencyKey"].as_str().unwrap()).is_ok());
        let me_request = server.request();
        assert_eq!(me_request.request_line, "GET /api/agents/me HTTP/1.1");
        assert_eq!(
            me_request.header("authorization"),
            Some(format!("Bearer {stored_key}").as_str())
        );
    }

    #[test]
    fn signup_accepts_database_precision_in_the_verified_expiry() {
        let _sandbox = CredentialsSandbox::new();
        let signup = OK_BODY.replace("2026-10-06T00:00:00Z", "2026-10-06T00:00:00.000000499Z");
        let server = MockServer::json_sequence(vec![(200, signup), (200, ME_BODY.to_string())]);
        let client = ApiClient::with_base_url(server.base_url());

        let outcome = perform_signup(
            &client,
            server.base_url(),
            Some("Robo"),
            "agent",
            false,
            false,
        )
        .expect("PostgreSQL microsecond precision verifies");

        assert!(outcome.created);
        assert_eq!(outcome.identity.slug, "agent-7f3a");
    }

    #[test]
    fn signup_accepts_database_precision_when_expiry_rounds_up() {
        let _sandbox = CredentialsSandbox::new();
        let signup = OK_BODY.replace("2026-10-06T00:00:00Z", "2026-10-06T00:00:00.000000501Z");
        let identity = ME_BODY.replace("2026-10-06T00:00:00Z", "2026-10-06T00:00:00.000001Z");
        let server = MockServer::json_sequence(vec![(200, signup), (200, identity)]);
        let client = ApiClient::with_base_url(server.base_url());

        let outcome = perform_signup(
            &client,
            server.base_url(),
            Some("Robo"),
            "agent",
            false,
            false,
        )
        .expect("PostgreSQL upward microsecond rounding verifies");

        assert!(outcome.created);
        assert_eq!(outcome.identity.slug, "agent-7f3a");
    }

    #[test]
    fn signup_refuses_to_replace_existing_credentials_unless_forced() {
        let _sandbox = CredentialsSandbox::new();
        let server =
            MockServer::json_sequence(vec![(200, OK_BODY.to_string()), (200, ME_BODY.to_string())]);
        let api_url = server.base_url().to_string();
        ApiClient::save_api_key("a4_ak_old", Some(&api_url)).expect("seed credentials");
        let credentials_path = ApiClient::credentials_file_path().unwrap();
        let mut duplicate = std::fs::read_to_string(&credentials_path).unwrap();
        duplicate.push_str(&format!("\"{api_url}/\" = \"a4_ak_stale\"\n"));
        std::fs::write(&credentials_path, duplicate).unwrap();
        let client = ApiClient::with_base_url(&api_url);

        let err = perform_signup(&client, &api_url, None, "agent", false, false)
            .expect_err("must refuse");
        assert_eq!(
            err.to_string(),
            format!(
                "Credentials already exist for profile `agent` at {api_url}. Run: a4 auth status (or pass --force to replace them)"
            )
        );
        assert_eq!(
            ApiClient::load_optional_api_key_for_url(&api_url)
                .unwrap()
                .as_deref(),
            Some("a4_ak_old"),
            "refusal must not touch the stored key"
        );

        perform_signup(&client, &api_url, None, "agent", true, false).expect("--force replaces");
        let replacement = ApiClient::load_optional_api_key_for_url(&api_url)
            .unwrap()
            .expect("replacement key");
        assert_ne!(replacement, "a4_ak_old");
        assert_eq!(
            ApiClient::load_optional_api_key_for_profile(&format!("{api_url}/"), Some("agent"))
                .unwrap()
                .as_deref(),
            Some(replacement.as_str()),
            "equivalent URL spellings must not retain a stale key"
        );
        let stored = std::fs::read_to_string(ApiClient::credentials_file_path().unwrap()).unwrap();
        let credentials = stored.parse::<toml::Value>().unwrap();
        let profile = credentials["profiles"]["agent"].as_table().unwrap();
        let active_keys = profile["keys"].as_table().unwrap();
        assert_eq!(active_keys.len(), 1, "{stored}");
        assert_eq!(
            active_keys.get(&api_url).and_then(toml::Value::as_str),
            Some(replacement.as_str()),
            "{stored}"
        );
        let backup_keys = profile["backupKeys"].as_table().unwrap();
        assert_eq!(
            backup_keys.get(&api_url).and_then(toml::Value::as_str),
            Some("a4_ak_old"),
            "{stored}"
        );
        assert_eq!(
            backup_keys
                .get(&format!("{api_url}/"))
                .and_then(toml::Value::as_str),
            Some("a4_ak_stale"),
            "{stored}"
        );
        let body: serde_json::Value =
            serde_json::from_str(&server.request().body).expect("json body");
        assert!(body.get("displayName").is_none());
        assert_eq!(body["credential"], replacement);
        let _ = server.request();
    }

    #[test]
    fn agent_and_human_profiles_coexist_without_implicit_selection() {
        let _sandbox = CredentialsSandbox::new();
        let api_url = "https://api.arete.run";
        ApiClient::save_api_key_for_profile("a4_ak_agent", Some(api_url), "agent").unwrap();
        ApiClient::save_api_key_for_profile("a4_sk_human", Some(api_url), "human").unwrap();

        assert_eq!(
            ApiClient::load_optional_api_key_for_profile(api_url, Some("agent"))
                .unwrap()
                .as_deref(),
            Some("a4_ak_agent")
        );
        assert_eq!(
            ApiClient::load_optional_api_key_for_profile(api_url, Some("human"))
                .unwrap()
                .as_deref(),
            Some("a4_sk_human")
        );
        assert!(ApiClient::load_optional_api_key_for_profile(api_url, None).is_err());

        ApiClient::delete_api_key_for_profile(api_url, Some("agent")).unwrap();
        assert_eq!(
            ApiClient::load_optional_api_key_for_profile(api_url, Some("agent")).unwrap(),
            None
        );
        assert_eq!(
            ApiClient::load_optional_api_key_for_profile(api_url, Some("human"))
                .unwrap()
                .as_deref(),
            Some("a4_sk_human")
        );
    }

    #[test]
    fn signup_preserves_rate_limit_problem_and_pending_retry_state() {
        let _sandbox = CredentialsSandbox::new();
        let server = MockServer::json(
            429,
            r#"{"schemaVersion":1,"error":"slow down","code":"rate_limit_exceeded","retryable":true,"retryAfterSeconds":60}"#,
        );
        let client = ApiClient::with_base_url(server.base_url());

        let err = perform_signup(&client, server.base_url(), None, "agent", false, false)
            .expect_err("429");
        let api_error = err
            .downcast_ref::<ApiClientError>()
            .expect("typed API error");
        assert_eq!(
            api_error.problem.code.as_deref(),
            Some("rate_limit_exceeded")
        );
        assert_eq!(api_error.retry_after_seconds(), Some(60));
        assert_eq!(
            ApiClient::load_optional_api_key_for_url(server.base_url()).unwrap(),
            None
        );
        assert!(
            ApiClient::load_pending_agent_signup(server.base_url(), "agent")
                .unwrap()
                .is_some()
        );
    }

    #[test]
    fn signup_reuses_pending_state_after_a_lost_response() {
        let _sandbox = CredentialsSandbox::new();
        let server = MockServer::json_sequence(vec![
            (
                500,
                r#"{"error":"response lost","code":"temporary","retryable":true}"#.to_string(),
            ),
            (200, OK_BODY.to_string()),
            (200, ME_BODY.to_string()),
        ]);
        let client = ApiClient::with_base_url(server.base_url());

        perform_signup(
            &client,
            server.base_url(),
            Some("Robo"),
            "agent",
            false,
            false,
        )
        .expect_err("first response is lost");
        let first: serde_json::Value =
            serde_json::from_str(&server.request().body).expect("first request JSON");
        let pending = ApiClient::load_pending_agent_signup(server.base_url(), "agent")
            .unwrap()
            .expect("pending state retained");
        assert_eq!(first["credential"], pending.credential);
        assert_eq!(first["idempotencyKey"], pending.idempotency_key);

        let outcome = perform_signup(
            &client,
            server.base_url(),
            Some("Robo"),
            "agent",
            false,
            false,
        )
        .expect("retry succeeds");
        assert!(outcome.created);
        let second: serde_json::Value =
            serde_json::from_str(&server.request().body).expect("second request JSON");
        assert_eq!(second["credential"], first["credential"]);
        assert_eq!(second["idempotencyKey"], first["idempotencyKey"]);
        let _ = server.request();
        assert!(
            ApiClient::load_pending_agent_signup(server.base_url(), "agent")
                .unwrap()
                .is_none()
        );
    }

    #[test]
    fn signup_if_missing_verifies_and_reuses_an_existing_agent() {
        let _sandbox = CredentialsSandbox::new();
        let server = MockServer::json(200, ME_BODY);
        let api_url = server.base_url();
        ApiClient::save_api_key_for_profile("a4_ak_existing", Some(api_url), "agent").unwrap();
        ApiClient::save_pending_agent_signup(
            api_url,
            "agent",
            &PendingAgentSignup {
                credential: "a4_ak_0123456789012345678901234567890123456789".to_string(),
                idempotency_key: "11111111-1111-4111-8111-111111111111".to_string(),
            },
        )
        .unwrap();
        let client = ApiClient::with_base_url(api_url);

        let outcome = perform_signup(&client, api_url, None, "agent", false, true)
            .expect("existing key is reused");

        assert!(!outcome.created);
        assert_eq!(outcome.identity.slug, "agent-7f3a");
        assert_eq!(server.request().request_line, "GET /api/agents/me HTTP/1.1");
        assert!(ApiClient::load_pending_agent_signup(api_url, "agent")
            .unwrap()
            .is_none());
        assert_eq!(
            ApiClient::load_optional_api_key_for_profile(api_url, Some("agent"))
                .unwrap()
                .as_deref(),
            Some("a4_ak_existing")
        );
    }

    #[test]
    fn signup_if_missing_never_replaces_an_invalid_key_implicitly() {
        let _sandbox = CredentialsSandbox::new();
        let server = MockServer::json(401, r#"{"error":"disabled","code":"agent-key-disabled"}"#);
        let api_url = server.base_url();
        ApiClient::save_api_key_for_profile("a4_ak_disabled", Some(api_url), "agent").unwrap();
        let client = ApiClient::with_base_url(api_url);

        let error = perform_signup(&client, api_url, None, "agent", false, true)
            .expect_err("invalid key needs explicit force");

        assert!(error.to_string().contains("--force"));
        assert!(error.to_string().contains("left untouched"));
        assert_eq!(
            ApiClient::load_optional_api_key_for_profile(api_url, Some("agent"))
                .unwrap()
                .as_deref(),
            Some("a4_ak_disabled")
        );
    }

    #[test]
    fn signup_if_missing_keeps_a_key_on_transient_verification_failure() {
        let _sandbox = CredentialsSandbox::new();
        let server = MockServer::json(
            503,
            r#"{"error":"temporarily unavailable","code":"service-unavailable"}"#,
        );
        let api_url = server.base_url();
        ApiClient::save_api_key_for_profile("a4_ak_existing", Some(api_url), "agent").unwrap();
        let client = ApiClient::with_base_url(api_url);

        let error = perform_signup(&client, api_url, None, "agent", false, true)
            .expect_err("transient verification failure is surfaced");

        assert!(error.to_string().contains("Retry"), "{error:#}");
        assert!(!error.to_string().contains("--force"), "{error:#}");
        assert_eq!(
            ApiClient::load_optional_api_key_for_profile(api_url, Some("agent"))
                .unwrap()
                .as_deref(),
            Some("a4_ak_existing")
        );
    }

    #[test]
    fn short_positive_trial_time_is_not_reported_as_zero_minutes() {
        assert_eq!(format_remaining_seconds(1), "1s");
        assert_eq!(format_remaining_seconds(59), "59s");
        assert_eq!(format_remaining_seconds(60), "1m");
    }

    #[test]
    fn signup_json_never_contains_secrets_or_an_absolute_credentials_path() {
        let outcome = SignupOutcome {
            identity: serde_json::from_str(ME_BODY).unwrap(),
            credentials_path: std::path::PathBuf::from("/Users/example/.arete/credentials.toml"),
            created: true,
            idempotent: Some(false),
        };
        let encoded = serde_json::to_string(&signup_json_payload(&outcome, "agent")).unwrap();

        assert!(!encoded.contains("credential\""), "{encoded}");
        assert!(!encoded.contains("idempotency"), "{encoded}");
        assert!(!encoded.contains("/Users/example"), "{encoded}");
        assert!(encoded.contains("starterGuidance"), "{encoded}");
        assert!(encoded.contains("entitlementExpiresAt"), "{encoded}");
        assert!(encoded.contains("\"usage\""), "{encoded}");
    }

    #[test]
    fn credentials_lock_serializes_concurrent_signup_processes() {
        let _sandbox = CredentialsSandbox::new();
        let first = ApiClient::lock_credentials().expect("first lock");
        let (sender, receiver) = std::sync::mpsc::channel();
        let join = std::thread::spawn(move || {
            let second = ApiClient::lock_credentials().expect("second lock");
            sender.send(()).unwrap();
            drop(second);
        });

        assert!(receiver
            .recv_timeout(std::time::Duration::from_millis(100))
            .is_err());
        drop(first);
        receiver
            .recv_timeout(std::time::Duration::from_secs(2))
            .expect("second process acquires the released lock");
        join.join().unwrap();
    }

    #[test]
    fn generated_agent_credentials_match_the_server_contract() {
        let credential = generate_agent_credential();
        let body = credential.strip_prefix("a4_ak_").expect("agent prefix");
        assert_eq!(body.len(), 40);
        assert!(body.bytes().all(|byte| byte.is_ascii_alphanumeric()));
    }
}