cnm-cli 0.31.0

CLI Tool for Verified Trust Agents operating in Verified Trust Communities
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
//! Onboarding `cnm`'s identities the way `pnm` onboards its own: a key minted
//! on this machine, granted by an administrator, then confirmed.
//!
//! Two identities, two lifecycles:
//!
//! - **The personal VTA** — one per machine. The cold start is `pnm`'s
//!   (CLAUDE.md "Admin credential cold-start", "Deferred VTA-DID setup"): mint
//!   an ephemeral `did:key`, park it, show the grant commands, and on
//!   `cnm setup continue` authenticate and **rotate** to a fresh `did:key`
//!   (`acl/swap-key`, done by the SDK on the first authentication of a
//!   `PendingRotation` session), so the DID that travelled to the
//!   administrator does not stay live.
//! - **A community** — one identity *per community*, so two communities an
//!   operator manages share no key that links them. Minted, parked in
//!   [`CnmConfig::pending_communities`], granted at the community's VTC, and
//!   confirmed by `cnm community continue`, which authenticates to the VTC
//!   (DI-signed `auth/authenticate`, the VTC's DID as audience) and then
//!   **rotates** the identity at the VTC (`acl/swap-key/0.1`, VTI-CLT-025 –
//!   032), so the DID that travelled to the granting administrator does not
//!   stay live. The successor entry carries exactly the granted authority — a
//!   rotation is never a grant, so it takes no second administrator. `cnm
//!   community rotate` rolls a configured community's identity the same way.
//!
//!   Two identities are not rotated, because their key is also held somewhere
//!   the VTC's swap does not reach: one shared with another community
//!   (`--reuse-identity`), and one bound to a community VTA, whose own ACL
//!   entry would be left naming the old DID.
//!
//!   The new key is written to a side slot of the session store **before**
//!   the swap is sent, and promoted only once the VTC accepts it
//!   (VTI-CLT-033): a crash between the two leaves the new key on disk, and
//!   the next `cnm community rotate` adopts it ([`recover_rotation`]).
//!
//! The functions here change a [`CnmConfig`] and a [`SessionStore`] handed in,
//! and never save the config: the caller does, after the step succeeds. That is
//! what lets the tests drive them against an in-memory store.

use serde::Serialize;
use vta_sdk::client::{ClientIdentity, CreateAclRequest, VtaClient};
use vta_sdk::error::VtaError;
use vta_sdk::session::{SessionInfo, SessionStore, TransportChoice};

use crate::config::{
    CnmConfig, CommunityConfig, PERSONAL_KEYRING_KEY, PendingCommunity, PersonalVtaConfig,
    community_keyring_key,
};

type CliResult<T = ()> = Result<T, Box<dyn std::error::Error>>;

/// The default name of the personal VTA when the operator gives none.
pub const DEFAULT_PERSONAL_NAME: &str = "personal";

// ── JSON stdout contract (non-interactive paths) ────────────────────

/// The one line a non-interactive step prints on stdout — `pnm setup`'s shape
/// (`{slug, admin_did, state}`, #1753), so a script that drives both reads
/// both the same way.
#[derive(Debug, Serialize)]
pub struct SetupOutput<'a> {
    pub slug: &'a str,
    pub admin_did: &'a str,
    pub state: &'static str,
}

pub fn emit_json(slug: &str, admin_did: &str, state: &'static str) -> std::io::Result<()> {
    use std::io::Write;
    let line = serde_json::to_string(&SetupOutput {
        slug,
        admin_did,
        state,
    })
    .expect("SetupOutput serializes");
    let mut stdout = std::io::stdout().lock();
    writeln!(stdout, "{line}")?;
    stdout.flush()
}

fn mint() -> (String, String) {
    vta_cli_common::local_keygen::generate_unbound_admin_did_key()
}

fn require_did(label: &str, did: &str) -> CliResult<String> {
    let did = did.trim();
    if !did.starts_with("did:") {
        return Err(format!(
            "{label} must start with `did:` (e.g. did:webvh:... or did:key:...), got `{did}`"
        )
        .into());
    }
    Ok(did.to_string())
}

// ── Personal VTA ────────────────────────────────────────────────────

/// Where the personal-VTA onboarding stands.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PersonalState {
    /// Nothing set up.
    None,
    /// An identity is minted but not yet confirmed. `bound_vta` is the VTA DID
    /// once supplied.
    Pending {
        admin_did: String,
        bound_vta: Option<String>,
    },
    /// Authenticated (and rotated, on the cold-start path).
    Complete { vta_did: String },
}

pub fn personal_state(config: &CnmConfig, store: &SessionStore) -> PersonalState {
    let Some(personal) = config.personal_vta.as_ref() else {
        return PersonalState::None;
    };
    if let Some(vta_did) = &personal.vta_did {
        return PersonalState::Complete {
            vta_did: vta_did.clone(),
        };
    }
    // Pending only if the session store agrees; a config entry with no key
    // behind it is orphaned and is set up afresh.
    match store.loaded_session(PERSONAL_KEYRING_KEY) {
        Some(info) => PersonalState::Pending {
            admin_did: info.client_did,
            bound_vta: info.vta_did,
        },
        None => PersonalState::None,
    }
}

/// Phase 1: mint the personal VTA's ephemeral identity and park it.
///
/// Refuses a completed personal VTA outright, and a pending one unless
/// `overwrite` — the pending key may already be in an administrator's hands.
/// Returns the minted DID.
pub fn begin_personal(
    config: &mut CnmConfig,
    store: &SessionStore,
    name: &str,
    overwrite: bool,
) -> CliResult<String> {
    match personal_state(config, store) {
        PersonalState::None => {}
        PersonalState::Complete { vta_did } => {
            return Err(format!(
                "the personal VTA is already set up (VTA DID: {vta_did}).\n\n\
                 Add a community with `cnm community add`, or run `cnm setup` and choose to \
                 keep it."
            )
            .into());
        }
        PersonalState::Pending { admin_did, .. } if !overwrite => {
            return Err(format!(
                "a personal-VTA setup is already pending (admin DID: {admin_did}).\n\n\
                 Finish it with `cnm setup continue --vta-did <did:...>`, or pass --overwrite \
                 to mint a fresh identity."
            )
            .into());
        }
        PersonalState::Pending { .. } => {}
    }
    let (did, key) = mint();
    store.store_pending_vta_binding(PERSONAL_KEYRING_KEY, &did, &key)?;
    config.personal_vta = Some(PersonalVtaConfig {
        vta_did: None,
        name: Some(name.to_string()),
    });
    Ok(did)
}

/// Check `cnm setup continue <name>` names the setup that is pending.
pub fn check_personal_name(config: &CnmConfig, name: Option<&str>) -> CliResult {
    let (Some(given), Some(stored)) = (
        name,
        config.personal_vta.as_ref().and_then(|p| p.name.as_deref()),
    ) else {
        return Ok(());
    };
    if crate::setup::slugify(given) != crate::setup::slugify(stored) {
        return Err(format!(
            "the pending personal-VTA setup is named '{stored}', not '{given}'.\n\n\
             Run `cnm setup continue {stored}`."
        )
        .into());
    }
    Ok(())
}

/// Phase 2a: bind the pending identity to the VTA DID.
///
/// Idempotent for the same DID, so a `continue` that failed to authenticate
/// (the grant was not in place yet) is simply re-run. Binding marks the
/// session for rotation on its first authentication.
pub fn bind_personal(
    config: &CnmConfig,
    store: &SessionStore,
    vta_did: Option<&str>,
) -> CliResult<String> {
    match personal_state(config, store) {
        PersonalState::None => Err("no personal-VTA setup is pending.\n\n\
             Start one with `cnm setup` (or `cnm setup --name <name>` non-interactively)."
            .into()),
        PersonalState::Complete { vta_did } => Err(format!(
            "the personal VTA is already set up (VTA DID: {vta_did}). Nothing to continue."
        )
        .into()),
        PersonalState::Pending {
            bound_vta: Some(bound),
            ..
        } => match vta_did {
            Some(given) if require_did("VTA DID", given)? != bound => Err(format!(
                "this pending setup is already bound to {bound}, not {given}.\n\n\
                 Run `cnm setup continue` without --vta-did to finish it, or \
                 `cnm setup --name <name> --overwrite` to start over."
            )
            .into()),
            _ => Ok(bound),
        },
        PersonalState::Pending {
            bound_vta: None, ..
        } => {
            let Some(given) = vta_did else {
                return Err("the VTA DID is required: pass --vta-did <did:...>".into());
            };
            let given = require_did("VTA DID", given)?;
            store.bind_vta_did(PERSONAL_KEYRING_KEY, &given)?;
            Ok(given)
        }
    }
}

/// The grant routes for a pending personal-VTA identity, ready to print.
pub fn personal_grant_commands(admin_did: &str) -> String {
    format!(
        "Grant {admin_did} admin on the personal VTA — any one of:\n\
         \x20 a. A VTA not set up yet: in `vta setup --from setup.toml` set\n\
         \x20      admin_did = \"{admin_did}\"\n\
         \x20 b. On the VTA host:\n\
         \x20      vta import-did --did {admin_did} --role admin\n\
         \x20 c. From a machine whose pnm already administers that VTA:\n\
         \x20      pnm acl create --did {admin_did} --role admin --label cnm\n\
         \x20    (or run `cnm setup` there and choose \"Bootstrap from your existing pnm session\")"
    )
}

/// The ACL entry `cnm` needs on the personal VTA — exactly what it uses there.
///
/// `cnm`'s personal-VTA operations are: create a context per community it
/// manages (`contexts/create` — a *top-level* context, which the VTA gates on
/// super-admin), and create the admin ACL entry for that community's identity
/// in it (`acl/grant`). Both need an **unrestricted admin**: there is no
/// narrower role that creates a top-level context. Its capabilities are left
/// whole on purpose — every entry `cnm` writes there is an admin entry, and the
/// granter's bound (VTI-ACL-053/055) refuses an entry the granter does not
/// cover on every axis, so a capability-narrowed `cnm` could not issue them.
///
/// It is still less than copying `pnm`'s credential: it is a separate key with
/// its own entry, revocable on its own, labelled as `cnm`'s.
pub fn minimal_personal_grant(did: &str) -> CreateAclRequest {
    CreateAclRequest::new(did, "admin").label("cnm — personal VTA (granted from pnm)")
}

/// Grant `new_did` on the personal VTA using an existing `pnm` session's key.
///
/// The `pnm` key signs in memory, here, once: it is authenticated with directly
/// rather than through a [`SessionStore`], so nothing is cached into `pnm`'s
/// session and nothing rotates it. Over REST, deliberately: opening a DIDComm
/// or TSP session as `pnm`'s DID would take the mediator's one socket for that
/// DID from a `pnm` that may be running, and this is a single request.
pub async fn grant_from_pnm(
    pnm: &SessionInfo,
    vta_did: &str,
    url: &str,
    new_did: &str,
) -> CliResult {
    let token = vta_sdk::session::challenge_response(
        url,
        &pnm.client_did,
        &pnm.private_key_multibase,
        vta_did,
    )
    .await
    .map_err(|e| {
        format!(
            "could not sign in to {vta_did} as your pnm identity {}: {e}\n\n\
             Check `pnm health` works, or grant the cnm identity another way:\n{}",
            pnm.client_did,
            personal_grant_commands(new_did)
        )
    })?;
    let client = VtaClient::authenticated(
        url,
        ClientIdentity {
            client_did: pnm.client_did.clone(),
            private_key_multibase: pnm.private_key_multibase.clone(),
            vta_did: vta_did.to_string(),
            verification_method: None,
        },
        token.access_token,
    )
    .await;
    client
        .create_acl(minimal_personal_grant(new_did))
        .await
        .map_err(|e| -> Box<dyn std::error::Error> {
            let why = match &e {
                VtaError::Forbidden(_) => format!(
                    "your pnm identity {} may not grant an unrestricted admin on this VTA \
                     (it is scoped, or the grant needs an approval)",
                    pnm.client_did
                ),
                _ => "the grant failed".to_string(),
            };
            format!(
                "{why}: {e}\n\nAsk the VTA's super-admin to grant it instead:\n{}",
                personal_grant_commands(new_did)
            )
            .into()
        })?;
    Ok(())
}

/// Phase 2b: authenticate the bound personal identity, which rotates it.
///
/// The first authentication of a `PendingRotation` session swaps it for a
/// fresh `did:key` (`acl/swap-key`) and drops the temp DID's entry. Returns the
/// DID the session holds afterwards. On success the personal VTA is complete in
/// `config`; on failure nothing local changes, so `continue` can be re-run once
/// the grant is in place.
pub async fn authenticate_personal(
    config: &mut CnmConfig,
    store: &SessionStore,
    url_override: Option<&str>,
    transport: TransportChoice,
) -> CliResult<String> {
    let (admin_did, vta_did) = match personal_state(config, store) {
        PersonalState::Pending {
            admin_did,
            bound_vta: Some(vta),
        } => (admin_did, vta),
        PersonalState::Pending { .. } => {
            return Err("the VTA DID is required: pass --vta-did <did:...>".into());
        }
        PersonalState::None => return Err("no personal-VTA setup is pending".into()),
        PersonalState::Complete { vta_did } => {
            return Err(format!("the personal VTA ({vta_did}) is already set up").into());
        }
    };
    let client = store
        .connect_with_transport(PERSONAL_KEYRING_KEY, url_override, None, transport)
        .await
        .map_err(|e| personal_auth_guidance(e.as_ref(), &admin_did, &vta_did))?;
    client.shutdown().await;

    let rotated = store
        .loaded_session(PERSONAL_KEYRING_KEY)
        .map(|s| s.client_did)
        .ok_or("the personal session vanished during authentication")?;
    let name = config.personal_vta.as_ref().and_then(|p| p.name.clone());
    config.personal_vta = Some(PersonalVtaConfig {
        vta_did: Some(vta_did),
        name,
    });
    Ok(rotated)
}

/// What to say when the personal VTA did not accept the pending identity.
fn personal_auth_guidance(
    err: &(dyn std::error::Error + 'static),
    admin_did: &str,
    vta_did: &str,
) -> String {
    let refused = err
        .downcast_ref::<VtaError>()
        .is_some_and(VtaError::is_auth);
    let lead = if refused {
        format!("{vta_did} did not accept {admin_did} — the grant is not in place yet.")
    } else {
        format!("could not authenticate to {vta_did} as {admin_did}: {err}")
    };
    format!(
        "{lead}\n\n{}\n\nThen re-run: cnm setup continue\n\n({err})",
        personal_grant_commands(admin_did)
    )
}

// ── Communities ─────────────────────────────────────────────────────

/// The grant routes for a community identity, ready to print.
pub fn community_grant_commands(slug: &str, admin_did: &str) -> String {
    format!(
        "Grant {admin_did} the admin role at the community's VTC — any one of:\n\
         \x20 a. A community not set up yet: in the `vtc setup --from <toml>` file set\n\
         \x20      co_admin_did = \"{admin_did}\"\n\
         \x20 b. On the VTC host, with the daemon stopped:\n\
         \x20      vtc --config <config.toml> acl add --did {admin_did} --role admin --label cnm\n\
         \x20 c. Online, by an existing community administrator:\n\
         \x20      cnm --community <their-profile> access grant {admin_did} --admin-role community-admin --label cnm\n\
         \x20    or in the admin console: Access control → Add entry (that DID, admin role\n\
         \x20    community-admin).\n\
         \x20    A community-wide admin grant made online waits in the action list for a\n\
         \x20    second administrator's approval (`cnm actions list`).\n\n\
         Then: cnm community continue {slug} --vtc-did <vtc-did>"
    )
}

/// Refuse a slug that is already a community, or already pending without
/// `overwrite`.
fn check_new_community_slug(config: &CnmConfig, slug: &str, overwrite: bool) -> CliResult {
    if slug.is_empty() {
        return Err("the community name must produce a non-empty slug".into());
    }
    if config.communities.contains_key(slug) {
        return Err(format!(
            "community '{slug}' is already set up.\n\n\
             Use a different name, or remove it first: cnm community delete {slug}"
        )
        .into());
    }
    if let Some(p) = config.pending_communities.get(slug)
        && !overwrite
    {
        return Err(format!(
            "community '{slug}' is already pending (admin DID: {}).\n\n\
             Finish it with `cnm community continue {slug} --vtc-did <did>`, or pass \
             --overwrite to mint a fresh identity.",
            p.admin_did
        )
        .into());
    }
    Ok(())
}

/// What a new community's identity is.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommunityIdentity {
    /// Mint a fresh `did:key` for this community alone. The default.
    Fresh,
    /// Use the identity of the community named — explicitly, because the
    /// shared key links the two communities to anyone who sees both ACLs.
    Reuse { from: String },
}

/// Phase 1 of a community: give it an identity and park it as pending.
///
/// Returns the admin DID the operator needs granted.
#[allow(clippy::too_many_arguments)]
pub fn begin_community(
    config: &mut CnmConfig,
    store: &SessionStore,
    name: &str,
    slug: &str,
    identity: &CommunityIdentity,
    vtc_did: Option<&str>,
    vta_did: Option<&str>,
    overwrite: bool,
) -> CliResult<String> {
    check_new_community_slug(config, slug, overwrite)?;
    let vtc_did = vtc_did.map(|d| require_did("VTC DID", d)).transpose()?;
    let vta_did = vta_did
        .map(|d| require_did("community VTA DID", d))
        .transpose()?;

    let (did, key) = match identity {
        CommunityIdentity::Fresh => mint(),
        CommunityIdentity::Reuse { from } => {
            if from == slug {
                return Err("a community cannot reuse its own identity".into());
            }
            if !config.communities.contains_key(from) {
                return Err(format!(
                    "no confirmed community '{from}' to reuse an identity from.\n\n\
                     `cnm community list` shows them."
                )
                .into());
            }
            let s = store
                .loaded_session(&community_keyring_key(from))
                .ok_or_else(|| format!("community '{from}' has no stored identity to reuse"))?;
            (s.client_did, s.private_key_multibase)
        }
    };
    // Held with no VTA DID: a community identity is confirmed at the VTC, and
    // the binding to a community VTA (if there is one) is made at `continue`.
    store.store_pending_vta_binding(&community_keyring_key(slug), &did, &key)?;
    config.pending_communities.insert(
        slug.to_string(),
        PendingCommunity {
            name: name.to_string(),
            admin_did: did.clone(),
            vtc_did,
            vta_did,
        },
    );
    Ok(did)
}

/// Phase 2 of a community: confirm the grant at the VTC and promote it.
///
/// Authenticates to the VTC as the pending identity — which only succeeds once
/// an administrator has granted it — then moves the community from pending to
/// configured. On a refusal nothing local changes; the error carries the grant
/// commands. Then the identity is rotated at the VTC (module docs); a rotation
/// that fails leaves the confirmed, minted key in place and says why.
pub async fn continue_community(
    config: &mut CnmConfig,
    store: &SessionStore,
    slug: &str,
    vtc_did: Option<&str>,
    vta_did: Option<&str>,
    url: Option<&str>,
) -> CliResult<CommunityConfirmed> {
    let pending = config
        .pending_communities
        .get(slug)
        .cloned()
        .ok_or_else(|| {
            if config.communities.contains_key(slug) {
                format!("community '{slug}' is already set up; nothing to continue.")
            } else {
                format!(
                    "no pending community '{slug}'.\n\n\
                 Start one with `cnm community add {slug}`; `cnm community list` shows \
                 what is configured."
                )
            }
        })?;
    let key = community_keyring_key(slug);
    let session = store.loaded_session(&key).ok_or_else(|| {
        format!(
            "community '{slug}' is pending but its key is missing from the session store \
             (was the keyring cleared?).\n\n\
             Mint a fresh identity: cnm community add \"{}\" --overwrite",
            pending.name
        )
    })?;

    let vtc_did = match vtc_did.or(pending.vtc_did.as_deref()) {
        Some(d) => require_did("VTC DID", d)?,
        None => {
            return Err(format!(
                "the community's VTC DID is needed to confirm the grant.\n\n\
                 Run: cnm community continue {slug} --vtc-did <vtc-did>\n\
                 (the DID `vtc setup` printed as `VTC DID`)"
            )
            .into());
        }
    };
    let vta_did = vta_did
        .map(|d| require_did("community VTA DID", d))
        .transpose()?
        .or(pending.vta_did.clone());

    let target = crate::vtc::resolve_target(Some(&vtc_did), url).await?;
    // A rotation an earlier `continue` committed but did not finish recording.
    let session = match recover_rotation(store, &key, &target).await? {
        Some(_) => store
            .loaded_session(&key)
            .ok_or("the recovered identity vanished from the session store")?,
        None => session,
    };
    crate::vtc::confirm_identity(&target, &session.client_did, &session.private_key_multibase)
        .await
        .map_err(|e| {
            format!(
                "{e}\n\n{}",
                community_grant_commands(slug, &session.client_did)
            )
        })?;

    // Remote effect confirmed; now commit locally. With a community VTA the
    // session is bound to it directly — not for rotation, which would move the
    // key the VTC just accepted out from under it.
    if let Some(vta) = &vta_did {
        store.store_direct(
            &key,
            &session.client_did,
            &session.private_key_multibase,
            vta,
        )?;
    }
    let context_id = None;
    config.pending_communities.remove(slug);
    config.communities.insert(
        slug.to_string(),
        CommunityConfig {
            name: pending.name,
            context_id,
            vta_did: vta_did.clone(),
            vtc_did: Some(vtc_did),
        },
    );
    if config.default_community.is_none() {
        config.default_community = Some(slug.to_string());
    }
    let rotation =
        match rotation_blocker(config, store, slug, &session.client_did, vta_did.as_deref()) {
            Some(reason) => Rotation::Skipped(reason),
            None => match rotate_identity(
                store,
                &key,
                &target,
                &session.client_did,
                &session.private_key_multibase,
            )
            .await
            {
                Ok(to) => Rotation::Rotated {
                    from: session.client_did.clone(),
                    to,
                },
                Err(e) => Rotation::Failed(e.to_string()),
            },
        };
    let did = match &rotation {
        Rotation::Rotated { to, .. } => to.clone(),
        _ => session.client_did,
    };
    Ok(CommunityConfirmed { did, rotation })
}

/// What `continue_community` confirmed: the DID cnm now authenticates to the
/// community as, and what became of the rotation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommunityConfirmed {
    pub did: String,
    pub rotation: Rotation,
}

/// What a community identity's rotation at its VTC came to.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Rotation {
    /// The VTC moved the entry onto a fresh key.
    Rotated { from: String, to: String },
    /// Not attempted, and why: the key is also held where the swap does not
    /// reach.
    Skipped(String),
    /// Attempted and refused; the previous key is still the one in use.
    Failed(String),
}

/// Why a community identity must not be rotated at its VTC alone, if it must
/// not: its key is bound to a community VTA, or shared with another community.
fn rotation_blocker(
    config: &CnmConfig,
    store: &SessionStore,
    slug: &str,
    did: &str,
    vta_did: Option<&str>,
) -> Option<String> {
    if let Some(vta) = vta_did {
        return Some(format!(
            "this identity is bound to the community VTA {vta}, whose ACL entry the VTC's swap \
             does not move; rotating at the VTC alone would strand it there"
        ));
    }
    let shared = config
        .communities
        .keys()
        .chain(config.pending_communities.keys())
        .filter(|s| s.as_str() != slug)
        .find(|s| {
            store
                .loaded_session(&community_keyring_key(s))
                .is_some_and(|o| o.client_did == did)
        });
    shared.map(|other| {
        format!(
            "this identity is shared with community '{other}' (--reuse-identity); rotating it \
             here would leave '{other}' holding a key the VTC no longer accepts"
        )
    })
}

/// The session-store slot a rotation's new key waits in until the VTC has
/// accepted it.
fn rotation_slot(keyring_key: &str) -> String {
    format!("{keyring_key}.rotating")
}

/// Roll the identity stored under `keyring_key` onto a fresh `did:key` at
/// `target` (`acl/swap-key/0.1`). Returns the new DID.
///
/// Ordering (VTI-CLT-033 – 035): mint; write the new key to the side slot;
/// send the swap; only once the VTC accepts it, write the new key over the
/// identity and drop the slot. A refusal drops the slot and leaves the old
/// key — still the authoritative one — exactly as it was.
async fn rotate_identity(
    store: &SessionStore,
    keyring_key: &str,
    target: &crate::vtc::VtcTarget,
    old_did: &str,
    old_key: &str,
) -> CliResult<String> {
    let (new_did, new_key) = mint();
    let slot = rotation_slot(keyring_key);
    store.store_pending_vta_binding(&slot, &new_did, &new_key)?;
    if let Err(e) = crate::vtc::swap_key(target, old_did, old_key, &new_did, &new_key).await {
        store.logout(&slot);
        return Err(e);
    }
    store.store_pending_vta_binding(keyring_key, &new_did, &new_key)?;
    store.logout(&slot);
    Ok(new_did)
}

/// Finish a rotation a crash interrupted: a new key left in the side slot is
/// adopted when the VTC accepts it (the swap committed), and discarded when it
/// does not (it never did). Returns the adopted DID.
pub async fn recover_rotation(
    store: &SessionStore,
    keyring_key: &str,
    target: &crate::vtc::VtcTarget,
) -> CliResult<Option<String>> {
    let slot = rotation_slot(keyring_key);
    let Some(waiting) = store.loaded_session(&slot) else {
        return Ok(None);
    };
    let accepted =
        crate::vtc::confirm_identity(target, &waiting.client_did, &waiting.private_key_multibase)
            .await
            .is_ok();
    if accepted {
        store.store_pending_vta_binding(
            keyring_key,
            &waiting.client_did,
            &waiting.private_key_multibase,
        )?;
    }
    store.logout(&slot);
    Ok(accepted.then_some(waiting.client_did))
}

/// `cnm community rotate <slug>`: roll a configured community's identity at
/// its VTC onto a fresh key. Refused for an identity bound to a community VTA
/// or shared with another community (module docs). Returns `(from, to)`.
pub async fn rotate_community(
    config: &CnmConfig,
    store: &SessionStore,
    slug: &str,
    vtc_did: Option<&str>,
    url: Option<&str>,
) -> CliResult<(String, String)> {
    let community = config.communities.get(slug).ok_or_else(|| {
        format!("no configured community '{slug}'; `cnm community list` shows them")
    })?;
    let key = community_keyring_key(slug);
    let vtc_did = vtc_did
        .map(str::to_string)
        .or_else(|| community.vtc_did.clone())
        .ok_or_else(|| {
            format!(
                "community '{slug}' names no VTC. Record it with `cnm community set-vtc <did>` \
                 or pass --vtc-did"
            )
        })?;
    let target = crate::vtc::resolve_target(Some(&vtc_did), url).await?;
    recover_rotation(store, &key, &target).await?;
    let session = store
        .loaded_session(&key)
        .ok_or_else(|| format!("community '{slug}' has no stored identity"))?;
    if let Some(reason) = rotation_blocker(
        config,
        store,
        slug,
        &session.client_did,
        community.vta_did.as_deref(),
    ) {
        return Err(format!("not rotating: {reason}").into());
    }
    let new_did = rotate_identity(
        store,
        &key,
        &target,
        &session.client_did,
        &session.private_key_multibase,
    )
    .await?;
    Ok((session.client_did, new_did))
}

/// `did:key:z6MkhaXg…uQ` — enough of a DID to tell two apart in a list.
pub fn short_did(did: &str) -> String {
    let chars: Vec<char> = did.chars().collect();
    if chars.len() <= 24 {
        return did.to_string();
    }
    let head: String = chars[..16].iter().collect();
    let tail: String = chars[chars.len() - 6..].iter().collect();
    format!("{head}…{tail}")
}

#[cfg(test)]
mod tests {
    use super::*;
    use vta_sdk::session::testing::InMemorySessionBackend;

    fn store() -> SessionStore {
        SessionStore::with_backend(Box::new(InMemorySessionBackend::new()))
    }

    const VTA: &str = "did:webvh:QmPersonal:vta.example.com";

    // ── personal ────────────────────────────────────────────────────

    #[test]
    fn the_personal_cold_start_parks_a_minted_key_and_survives_a_reload() {
        let store = store();
        let mut config = CnmConfig::default();
        let did = begin_personal(&mut config, &store, "home", false).unwrap();
        assert!(did.starts_with("did:key:z6Mk"), "{did}");

        // The config the caller saves records the pending setup by name …
        let toml_str = toml::to_string_pretty(&config).unwrap();
        let reloaded: CnmConfig = toml::from_str(&toml_str).unwrap();
        assert_eq!(
            reloaded.personal_vta.as_ref().unwrap().name.as_deref(),
            Some("home")
        );
        assert!(reloaded.personal_vta.as_ref().unwrap().vta_did.is_none());
        // … and the key is in the session store, unbound.
        assert_eq!(
            personal_state(&reloaded, &store),
            PersonalState::Pending {
                admin_did: did,
                bound_vta: None
            }
        );
    }

    #[test]
    fn a_pending_personal_setup_is_kept_unless_overwritten() {
        let store = store();
        let mut config = CnmConfig::default();
        let first = begin_personal(&mut config, &store, "home", false).unwrap();
        let err = begin_personal(&mut config, &store, "home", false)
            .unwrap_err()
            .to_string();
        assert!(err.contains(&first) && err.contains("--overwrite"), "{err}");
        let second = begin_personal(&mut config, &store, "home", true).unwrap();
        assert_ne!(first, second);
    }

    #[test]
    fn a_complete_personal_vta_is_never_overwritten() {
        let store = store();
        let mut config = CnmConfig {
            personal_vta: Some(PersonalVtaConfig {
                vta_did: Some(VTA.into()),
                name: None,
            }),
            ..Default::default()
        };
        let err = begin_personal(&mut config, &store, "x", true)
            .unwrap_err()
            .to_string();
        assert!(err.contains("already set up"), "{err}");
    }

    #[test]
    fn binding_is_idempotent_for_the_same_vta_and_refuses_another() {
        let store = store();
        let mut config = CnmConfig::default();
        begin_personal(&mut config, &store, "home", false).unwrap();
        assert!(
            bind_personal(&config, &store, None)
                .unwrap_err()
                .to_string()
                .contains("--vta-did")
        );
        assert_eq!(bind_personal(&config, &store, Some(VTA)).unwrap(), VTA);
        // Re-running continue (the grant was late) needs no DID again.
        assert_eq!(bind_personal(&config, &store, None).unwrap(), VTA);
        assert_eq!(bind_personal(&config, &store, Some(VTA)).unwrap(), VTA);
        let err = bind_personal(&config, &store, Some("did:web:other"))
            .unwrap_err()
            .to_string();
        assert!(err.contains("already bound"), "{err}");
    }

    #[test]
    fn continue_checks_the_name_it_was_given() {
        let store = store();
        let mut config = CnmConfig::default();
        begin_personal(&mut config, &store, "Home VTA", false).unwrap();
        assert!(check_personal_name(&config, Some("home-vta")).is_ok());
        assert!(check_personal_name(&config, None).is_ok());
        let err = check_personal_name(&config, Some("work"))
            .unwrap_err()
            .to_string();
        assert!(err.contains("cnm setup continue Home VTA"), "{err}");
    }

    #[test]
    fn the_grant_cnm_asks_of_pnm_is_an_unrestricted_admin_labelled_as_cnms() {
        let req = minimal_personal_grant("did:key:z6MkCnm");
        assert_eq!(req.did, "did:key:z6MkCnm");
        assert_eq!(req.role, "admin");
        // Unrestricted: `contexts/create` of a top-level context is super-admin.
        assert!(req.allowed_contexts.is_empty());
        assert!(req.capabilities.is_empty());
        assert!(req.label.unwrap().contains("cnm"));
        assert!(req.expires_at.is_none());
    }

    /// A mock VTA that issues a challenge and refuses the authenticate step.
    async fn refusing_vta() -> wiremock::MockServer {
        use wiremock::matchers::{body_string_contains, method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/trust-tasks"))
            .and(body_string_contains("auth/challenge"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "challenge": "c2VjcmV0LWNoYWxsZW5nZQ",
                "sessionId": "s-1",
                "expiresAt": "2099-01-01T00:00:00Z",
            })))
            .mount(&server)
            .await;
        Mock::given(method("POST"))
            .and(path("/trust-tasks"))
            .and(body_string_contains("auth/authenticate"))
            .respond_with(ResponseTemplate::new(401).set_body_json(serde_json::json!({
                "error": "unauthorized", "message": "unknown DID"
            })))
            .mount(&server)
            .await;
        server
    }

    /// `continue` against a VTA that has not granted the key yet: the error
    /// prints the grant commands for the pending DID, and the setup stays
    /// pending and bound, so re-running `continue` later is all it takes.
    #[tokio::test]
    async fn continue_before_the_grant_names_the_fix_and_stays_resumable() {
        let server = refusing_vta().await;
        let store = store();
        let mut config = CnmConfig::default();
        let did = begin_personal(&mut config, &store, "home", false).unwrap();
        bind_personal(&config, &store, Some(VTA)).unwrap();

        let err = authenticate_personal(
            &mut config,
            &store,
            Some(&server.uri()),
            TransportChoice::Rest,
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(
            err.contains(&format!("vta import-did --did {did} --role admin")),
            "{err}"
        );
        assert!(
            err.contains(&format!("pnm acl create --did {did} --role admin")),
            "{err}"
        );
        assert!(err.contains("cnm setup continue"), "{err}");

        assert_eq!(
            personal_state(&config, &store),
            PersonalState::Pending {
                admin_did: did,
                bound_vta: Some(VTA.into())
            }
        );
    }

    #[tokio::test]
    async fn a_pnm_grant_that_cannot_sign_in_points_at_the_other_routes() {
        let server = refusing_vta().await;
        let (pnm_did, pnm_key) = mint();
        let pnm = SessionInfo {
            client_did: pnm_did,
            vta_did: Some(VTA.into()),
            private_key_multibase: pnm_key,
        };
        let err = grant_from_pnm(&pnm, VTA, &server.uri(), "did:key:z6MkNew")
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("pnm health"), "{err}");
        assert!(
            err.contains("vta import-did --did did:key:z6MkNew --role admin"),
            "{err}"
        );
    }

    // ── communities ─────────────────────────────────────────────────

    #[test]
    fn two_communities_get_two_distinct_identities() {
        let store = store();
        let mut config = CnmConfig::default();
        let a = begin_community(
            &mut config,
            &store,
            "Alpha",
            "alpha",
            &CommunityIdentity::Fresh,
            None,
            None,
            false,
        )
        .unwrap();
        let b = begin_community(
            &mut config,
            &store,
            "Beta",
            "beta",
            &CommunityIdentity::Fresh,
            None,
            None,
            false,
        )
        .unwrap();
        assert_ne!(a, b);
        let ka = store
            .loaded_session(&community_keyring_key("alpha"))
            .unwrap();
        let kb = store
            .loaded_session(&community_keyring_key("beta"))
            .unwrap();
        assert_eq!(ka.client_did, a);
        assert_eq!(kb.client_did, b);
        assert_ne!(ka.private_key_multibase, kb.private_key_multibase);
        // Pending communities are not selectable yet.
        assert!(config.communities.is_empty());
        assert!(crate::config::resolve_community(Some("alpha"), &config).is_err());
    }

    #[test]
    fn selection_by_community_picks_that_communitys_identity() {
        let store = store();
        let mut config = CnmConfig::default();
        for slug in ["alpha", "beta"] {
            let (did, key) = mint();
            store
                .store_direct(&community_keyring_key(slug), &did, &key, "did:web:vta")
                .unwrap();
            config.communities.insert(
                slug.into(),
                CommunityConfig {
                    name: slug.into(),
                    context_id: None,
                    vta_did: None,
                    vtc_did: Some(format!("did:web:{slug}")),
                },
            );
        }
        config.default_community = Some("alpha".into());
        let did_of = |c: Option<&str>| {
            let (slug, _) = crate::config::resolve_community(c, &config).unwrap();
            store
                .loaded_session(&community_keyring_key(&slug))
                .unwrap()
                .client_did
        };
        assert_ne!(did_of(None), did_of(Some("beta")));
        assert_eq!(did_of(None), did_of(Some("alpha")));
    }

    #[test]
    fn reusing_an_identity_takes_an_explicit_source() {
        let store = store();
        let mut config = CnmConfig::default();
        // No confirmed community to reuse from: refused.
        let err = begin_community(
            &mut config,
            &store,
            "Beta",
            "beta",
            &CommunityIdentity::Reuse {
                from: "alpha".into(),
            },
            None,
            None,
            false,
        )
        .unwrap_err()
        .to_string();
        assert!(err.contains("no confirmed community 'alpha'"), "{err}");

        let (did, key) = mint();
        store
            .store_direct(&community_keyring_key("alpha"), &did, &key, "did:web:vta")
            .unwrap();
        config.communities.insert(
            "alpha".into(),
            CommunityConfig {
                name: "Alpha".into(),
                context_id: None,
                vta_did: None,
                vtc_did: None,
            },
        );
        // The default is a fresh key, never alpha's.
        let fresh = begin_community(
            &mut config,
            &store,
            "Gamma",
            "gamma",
            &CommunityIdentity::Fresh,
            None,
            None,
            false,
        )
        .unwrap();
        assert_ne!(fresh, did);
        // Only when asked for by name is the key shared.
        let reused = begin_community(
            &mut config,
            &store,
            "Beta",
            "beta",
            &CommunityIdentity::Reuse {
                from: "alpha".into(),
            },
            None,
            None,
            false,
        )
        .unwrap();
        assert_eq!(reused, did);
    }

    #[test]
    fn a_community_slug_is_not_minted_twice() {
        let store = store();
        let mut config = CnmConfig::default();
        let add = |config: &mut CnmConfig, overwrite| {
            begin_community(
                config,
                &store,
                "Alpha",
                "alpha",
                &CommunityIdentity::Fresh,
                Some("did:web:vtc"),
                None,
                overwrite,
            )
        };
        let first = add(&mut config, false).unwrap();
        let err = add(&mut config, false).unwrap_err().to_string();
        assert!(err.contains(&first) && err.contains("--overwrite"), "{err}");
        assert_ne!(add(&mut config, true).unwrap(), first);
        assert!(
            begin_community(
                &mut config,
                &store,
                "x",
                "x",
                &CommunityIdentity::Fresh,
                Some("not-a-did"),
                None,
                false
            )
            .is_err()
        );
    }

    async fn mock_vtc(accept: bool) -> wiremock::MockServer {
        use wiremock::matchers::{body_string_contains, method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/trust-tasks"))
            .and(body_string_contains("auth/challenge"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "challenge": "c2VjcmV0LWNoYWxsZW5nZQ",
                "sessionId": "s-1",
                "expiresAt": "2099-01-01T00:00:00Z",
            })))
            .mount(&server)
            .await;
        let authenticate = if accept {
            ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "session": {
                    "id": "sess", "subject": "did:key:z6MkHolder",
                    "issuedAt": "2026-01-01T00:00:00Z", "expiresAt": "2099-12-31T23:59:59Z",
                    "amr": ["did"], "acr": "aal1"
                },
                "tokens": { "accessToken": "acc", "tokenType": "Bearer", "expiresIn": 900_u64 }
            }))
        } else {
            ResponseTemplate::new(403).set_body_string("forbidden")
        };
        Mock::given(method("POST"))
            .and(path("/v1/trust-tasks"))
            .and(body_string_contains("auth/authenticate"))
            .respond_with(authenticate)
            .mount(&server)
            .await;
        server
    }

    #[tokio::test]
    async fn continue_before_the_vtc_grant_keeps_the_community_pending() {
        let server = mock_vtc(false).await;
        let store = store();
        let mut config = CnmConfig::default();
        let did = begin_community(
            &mut config,
            &store,
            "Alpha",
            "alpha",
            &CommunityIdentity::Fresh,
            Some("did:webvh:QmVtc:vtc.example.com"),
            None,
            false,
        )
        .unwrap();
        let base = format!("{}/v1", server.uri());
        let err = continue_community(&mut config, &store, "alpha", None, None, Some(&base))
            .await
            .unwrap_err()
            .to_string();
        assert!(
            err.contains(&format!("acl add --did {did} --role admin")),
            "{err}"
        );
        assert!(err.contains(&format!("co_admin_did = \"{did}\"")), "{err}");
        assert!(err.contains("cnm community continue alpha"), "{err}");
        assert!(config.pending_communities.contains_key("alpha"));
        assert!(config.communities.is_empty());
    }

    #[tokio::test]
    async fn continue_after_the_grant_promotes_the_community_with_its_own_key() {
        let server = mock_vtc(true).await;
        let store = store();
        let mut config = CnmConfig::default();
        let did = begin_community(
            &mut config,
            &store,
            "Alpha",
            "alpha",
            &CommunityIdentity::Fresh,
            None,
            None,
            false,
        )
        .unwrap();
        let base = format!("{}/v1", server.uri());
        // No VTC DID anywhere yet: the error says how to give one.
        let err = continue_community(&mut config, &store, "alpha", None, None, Some(&base))
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("--vtc-did"), "{err}");

        let confirmed = continue_community(
            &mut config,
            &store,
            "alpha",
            Some("did:webvh:QmVtc:vtc.example.com"),
            Some("did:webvh:QmCommunityVta:vta.example.com"),
            Some(&base),
        )
        .await
        .unwrap();
        // Bound to a community VTA, whose own entry the VTC's swap would not
        // move: the minted key is kept, and the reason said.
        assert_eq!(confirmed.did, did);
        assert!(
            matches!(&confirmed.rotation, Rotation::Skipped(why) if why.contains("community VTA")),
            "{:?}",
            confirmed.rotation
        );
        assert!(config.pending_communities.is_empty());
        let c = &config.communities["alpha"];
        assert_eq!(
            c.vtc_did.as_deref(),
            Some("did:webvh:QmVtc:vtc.example.com")
        );
        assert_eq!(
            c.vta_did.as_deref(),
            Some("did:webvh:QmCommunityVta:vta.example.com")
        );
        assert_eq!(config.default_community.as_deref(), Some("alpha"));
        let s = store
            .loaded_session(&community_keyring_key("alpha"))
            .unwrap();
        assert_eq!(s.client_did, did);
        assert_eq!(
            s.vta_did.as_deref(),
            Some("did:webvh:QmCommunityVta:vta.example.com")
        );
        // And now it is selectable.
        assert!(crate::config::resolve_community(Some("alpha"), &config).is_ok());
        // Nothing left to continue.
        assert!(
            continue_community(&mut config, &store, "alpha", None, None, Some(&base))
                .await
                .unwrap_err()
                .to_string()
                .contains("already set up")
        );
    }

    /// Answer `acl/swap-key/0.1` on `server`: success (`accept`), or the
    /// refusal a VTC sends when the link proof does not verify.
    async fn mock_swap(server: &wiremock::MockServer, accept: bool) {
        use wiremock::matchers::{body_string_contains, method, path};
        use wiremock::{Mock, ResponseTemplate};
        let reply = if accept {
            ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "id": "urn:uuid:22222222-2222-2222-2222-222222222222",
                "type": "https://trusttasks.org/spec/acl/swap-key/0.1#response",
                "payload": {
                    "entry": { "subject": "did:key:z6MkNew", "role": "admin" },
                    "previousSubject": "did:key:z6MkOld",
                },
            }))
        } else {
            ResponseTemplate::new(403).set_body_json(serde_json::json!({
                "id": "urn:uuid:33333333-3333-3333-3333-333333333333",
                "type": "https://trusttasks.org/spec/trust-task-error/0.5",
                "payload": {
                    "code": "acl/swap-key:linkProofInvalid",
                    "message": "the link proof is not signed by newSubject",
                    "retryable": false,
                },
            }))
        };
        Mock::given(method("POST"))
            .and(path("/v1/trust-tasks"))
            .and(body_string_contains("acl/swap-key"))
            .respond_with(reply)
            .mount(server)
            .await;
    }

    /// The `acl/swap-key` document the mock VTC received, if any.
    async fn swap_document(server: &wiremock::MockServer) -> Option<serde_json::Value> {
        server
            .received_requests()
            .await
            .unwrap_or_default()
            .iter()
            .filter_map(|r| serde_json::from_slice::<serde_json::Value>(&r.body).ok())
            .find(|d| {
                d["type"]
                    .as_str()
                    .is_some_and(|t| t.contains("acl/swap-key"))
            })
    }

    fn pending_alpha(store: &SessionStore, config: &mut CnmConfig) -> String {
        begin_community(
            config,
            store,
            "Alpha",
            "alpha",
            &CommunityIdentity::Fresh,
            Some("did:webvh:QmVtc:vtc.example.com"),
            None,
            false,
        )
        .unwrap()
    }

    /// VTI-CLT-025 – 033: with no community VTA bound, `continue` rolls the
    /// granted key onto a fresh one at the VTC — signed by the granted key,
    /// carrying the new key's link proof — and only then stores the new key.
    #[tokio::test]
    async fn continue_rotates_the_granted_key_at_the_vtc() {
        let server = mock_vtc(true).await;
        mock_swap(&server, true).await;
        let store = store();
        let mut config = CnmConfig::default();
        let granted = pending_alpha(&store, &mut config);
        let base = format!("{}/v1", server.uri());
        let confirmed = continue_community(&mut config, &store, "alpha", None, None, Some(&base))
            .await
            .unwrap();
        let Rotation::Rotated { from, to } = &confirmed.rotation else {
            panic!("expected a rotation, got {:?}", confirmed.rotation);
        };
        assert_eq!(from, &granted);
        assert_ne!(to, &granted);
        assert_eq!(&confirmed.did, to);

        let doc = swap_document(&server).await.expect("a swap was sent");
        assert_eq!(doc["payload"]["currentSubject"], granted.as_str());
        assert_eq!(doc["payload"]["newSubject"], to.as_str());
        assert_eq!(doc["issuer"], granted.as_str());
        let proof = doc["payload"]["linkProof"].as_str().expect("a VP-JWT");
        assert_eq!(
            vta_sdk::protocols::acl_management::swap::peek_presentation_holder(proof).unwrap(),
            *to
        );

        let key = community_keyring_key("alpha");
        assert_eq!(&store.loaded_session(&key).unwrap().client_did, to);
        assert!(
            store.loaded_session(&rotation_slot(&key)).is_none(),
            "the side slot is cleared once the VTC accepts"
        );
        assert!(config.communities.contains_key("alpha"));
    }

    /// A refused swap keeps the granted key — still the authoritative one —
    /// and the community is set up regardless; the error says why.
    #[tokio::test]
    async fn a_refused_swap_keeps_the_granted_key() {
        let server = mock_vtc(true).await;
        mock_swap(&server, false).await;
        let store = store();
        let mut config = CnmConfig::default();
        let granted = pending_alpha(&store, &mut config);
        let base = format!("{}/v1", server.uri());
        let confirmed = continue_community(&mut config, &store, "alpha", None, None, Some(&base))
            .await
            .unwrap();
        assert_eq!(confirmed.did, granted);
        assert!(
            matches!(&confirmed.rotation, Rotation::Failed(why) if why.contains("linkProofInvalid")),
            "{:?}",
            confirmed.rotation
        );
        let key = community_keyring_key("alpha");
        assert_eq!(store.loaded_session(&key).unwrap().client_did, granted);
        assert!(store.loaded_session(&rotation_slot(&key)).is_none());
        assert!(config.communities.contains_key("alpha"));
    }

    fn configured(store: &SessionStore, vta_did: Option<&str>) -> (CnmConfig, String) {
        let mut config = CnmConfig::default();
        let (did, key) = mint();
        store
            .store_pending_vta_binding(&community_keyring_key("alpha"), &did, &key)
            .unwrap();
        config.communities.insert(
            "alpha".into(),
            CommunityConfig {
                name: "Alpha".into(),
                context_id: None,
                vta_did: vta_did.map(str::to_string),
                vtc_did: Some("did:webvh:QmVtc:vtc.example.com".into()),
            },
        );
        (config, did)
    }

    /// `cnm community rotate` rolls a configured community's identity the
    /// same way.
    #[tokio::test]
    async fn rotate_rolls_a_configured_community_onto_a_fresh_key() {
        let server = mock_vtc(true).await;
        mock_swap(&server, true).await;
        let store = store();
        let (config, old) = configured(&store, None);
        let base = format!("{}/v1", server.uri());
        let (from, to) = rotate_community(&config, &store, "alpha", None, Some(&base))
            .await
            .unwrap();
        assert_eq!(from, old);
        assert_ne!(to, old);
        let key = community_keyring_key("alpha");
        assert_eq!(store.loaded_session(&key).unwrap().client_did, to);
        let doc = swap_document(&server).await.expect("a swap was sent");
        assert_eq!(doc["payload"]["currentSubject"], old.as_str());
    }

    /// Refused, with nothing sent, for an identity bound to a community VTA or
    /// shared with another community.
    #[tokio::test]
    async fn rotate_refuses_an_identity_held_elsewhere() {
        let server = mock_vtc(true).await;
        mock_swap(&server, true).await;
        let base = format!("{}/v1", server.uri());

        let store_a = store();
        let (config, old) = configured(&store_a, Some("did:webvh:QmVta:vta.example.com"));
        let err = rotate_community(&config, &store_a, "alpha", None, Some(&base))
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("community VTA"), "{err}");
        let key = community_keyring_key("alpha");
        assert_eq!(store_a.loaded_session(&key).unwrap().client_did, old);

        let store_b = store();
        let (mut config, shared) = configured(&store_b, None);
        let private = store_b.loaded_session(&key).unwrap().private_key_multibase;
        store_b
            .store_pending_vta_binding(&community_keyring_key("beta"), &shared, &private)
            .unwrap();
        config.communities.insert(
            "beta".into(),
            CommunityConfig {
                name: "Beta".into(),
                context_id: None,
                vta_did: None,
                vtc_did: Some("did:webvh:QmOther:vtc.example.com".into()),
            },
        );
        let err = rotate_community(&config, &store_b, "alpha", None, Some(&base))
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("shared with community 'beta'"), "{err}");
        assert!(swap_document(&server).await.is_none(), "nothing was sent");
    }

    /// A rotation the VTC committed but this machine did not finish recording
    /// is adopted from the side slot on the next run (VTI-CLT-033).
    #[tokio::test]
    async fn an_interrupted_rotation_is_adopted() {
        let server = mock_vtc(true).await;
        let store = store();
        let _ = configured(&store, None);
        let (new_did, new_key) = mint();
        let key = community_keyring_key("alpha");
        store
            .store_pending_vta_binding(&rotation_slot(&key), &new_did, &new_key)
            .unwrap();
        let target = crate::vtc::VtcTarget {
            did: "did:webvh:QmVtc:vtc.example.com".into(),
            base: format!("{}/v1", server.uri()),
        };
        let adopted = recover_rotation(&store, &key, &target).await.unwrap();
        assert_eq!(adopted.as_deref(), Some(new_did.as_str()));
        assert_eq!(store.loaded_session(&key).unwrap().client_did, new_did);
        assert!(store.loaded_session(&rotation_slot(&key)).is_none());
    }

    #[test]
    fn json_output_has_pnms_shape() {
        let line = serde_json::to_string(&SetupOutput {
            slug: "alpha",
            admin_did: "did:key:z6MkTest",
            state: "pending",
        })
        .unwrap();
        let v: serde_json::Value = serde_json::from_str(&line).unwrap();
        assert_eq!(v["slug"], "alpha");
        assert_eq!(v["admin_did"], "did:key:z6MkTest");
        assert_eq!(v["state"], "pending");
        assert_eq!(v.as_object().unwrap().len(), 3);
    }

    #[test]
    fn short_did_keeps_both_ends() {
        let d = "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK";
        let s = short_did(d);
        assert!(
            s.starts_with("did:key:z6MkhaXg") && s.ends_with("a2doK"),
            "{s}"
        );
        assert_eq!(short_did("did:key:z6Mk"), "did:key:z6Mk");
    }
}