subc-daemon 0.49.0

Embeddable subc daemon: bootstrap, module supervision, and opaque-byte splice routing.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
//! One test per row of the registration operations table in
//! `channel_operations.rs`, each driven through a real server connection on a
//! supervised spawn's registration channel in its current, candidate and
//! draining roles, plus the activity-read rules for channels and claims.

use super::channel_operations::{
    CLAIM_OPERATION_NOT_ALLOWED, REGISTRATION_CHANNEL_CLOSED, REGISTRATION_OPERATIONS,
};
use super::claims::tests::{
    mint, net_mint, network_primary, network_primary_with_hello, prefix_hello, prefix_wire,
    request, rig, rig_with, rig_with_module, swap_candidate, Net, Rig, SwapRig, Wire,
};
use super::*;
use crate::scopes::LaunchStanding;
use crate::supervise::{
    ModuleOverlap, ModuleSpec, RegistrationSpawn, RestartPolicy, SupervisedModule, Supervisor,
};
use serde_json::{json, Value};
use subc_control::{ClaimMintRequest, ClaimPresentRequest, ClaimPurpose};
use subc_protocol::scope::{RouteAuthorizeExpected, RouteAuthorizeScope, ScopeKind};
use tokio::sync::{mpsc, oneshot};

/// Correlation id `request` puts on every frame.
const CORR: u64 = 7;

fn reply_text(reply: &Frame) -> String {
    String::from_utf8_lossy(&reply.body).into_owned()
}

fn code(reply: &Frame) -> String {
    assert_eq!(reply.header.ty, FrameType::Error, "{}", reply_text(reply));
    serde_json::from_slice::<ErrorBody>(&reply.body)
        .unwrap()
        .code
}

fn ok(reply: &Frame) -> Value {
    assert_eq!(
        reply.header.ty,
        FrameType::Response,
        "{}",
        reply_text(reply)
    );
    serde_json::from_slice(&reply.body).unwrap()
}

/// Send one request and return its answer, skipping frames the daemon sends
/// the connection on its own, such as route binds and the drain notice.
async fn ask(net: &mut Net, body: impl Serialize) -> Frame {
    net.send(request(body)).await;
    answer(net).await
}

/// The answer to the request already sent with [`CORR`].
async fn answer(net: &mut Net) -> Frame {
    loop {
        let frame = net.recv().await.expect("the connection must answer");
        if frame.header.corr == CORR
            && frame.header.channel == 0
            && matches!(frame.header.ty, FrameType::Response | FrameType::Error)
        {
            return frame;
        }
    }
}

/// A consumer connection bound by a claim the primary minted.
async fn consumer(rig: &Rig, primary: &mut Net, purpose: ClaimPurpose) -> Net {
    let claim = net_mint(primary, purpose).await.claim;
    let mut net = Net::new(rig.router.clone()).await;
    ok(&net.call(request(ClaimPresentRequest::new(claim))).await);
    net
}

/// Promote the swap candidate and leave the incumbent draining. The returned
/// sender holds the drain open until it is dropped.
async fn cut_over(rig: &Rig, swap: &mut SwapRig) -> oneshot::Sender<()> {
    let release = rig.handler.forwarding.hold_route_closing_for_test();
    ok(&ask(
        &mut swap.candidate,
        json!({"op":"catalog.update","provides":[],"ready":true}),
    )
    .await);
    assert_eq!(
        tokio::time::timeout(Duration::from_secs(5), &mut swap.swap)
            .await
            .unwrap()
            .unwrap()
            .unwrap(),
        crate::supervise::SwapOutcome::Cutover
    );
    loop {
        let frame = swap.incumbent.recv().await.expect("drain notice");
        let body: Value = serde_json::from_slice(&frame.body).unwrap_or_default();
        if body["op"] == "module.draining" {
            break;
        }
    }
    release
}

/// Answer a request for `connection` on a fresh sink, as for a frame its
/// reader admitted just before retirement fenced the connection's own sink.
async fn replay(rig: &Rig, connection: ConnectionId, body: impl Serialize) -> Frame {
    let (sender, _replies) = mpsc::channel(8);
    let ctx = RouteCtx {
        connection_id: connection,
        egress: crate::FrameSink::new(sender),
    };
    let mut replies = rig
        .handler
        .handle_control_frame(&ctx, request(body))
        .await
        .unwrap();
    assert_eq!(replies.len(), 1, "{replies:?}");
    replies.pop().unwrap()
}

/// Answer a request on the fixture's primary channel after its spawn was seen
/// to exit.
async fn after_exit(rig: &Rig, body: impl Serialize) -> Frame {
    rig.spawn.retire();
    assert!(!rig.spawn.launch_ready());
    replay(rig, rig.primary.ctx.connection_id, body).await
}

fn hello(module_id: &str) -> Frame {
    let hello = subc_protocol::ModuleHelloBody {
        manifest: ModuleManifest::builder(module_id, "1.0.0").build(),
        protocol_ver: PROTOCOL_VERSION,
        control_ops: None,
        launch_nonce: None,
    };
    Frame::build(
        FrameType::Hello,
        control_flags(),
        0,
        0,
        CORR,
        serde_json::to_vec(&hello).unwrap(),
    )
    .unwrap()
}

#[tokio::test]
async fn channel_operations_hello_row_admits_one_hello_per_channel() {
    let rig = rig().await;
    let mut swap = swap_candidate(&rig, "roles-hello").await;
    // Both channels already sent their one HELLO in the fixture.
    for net in [&mut swap.incumbent, &mut swap.candidate] {
        assert_eq!(
            code(&net.call(hello("roles-hello")).await),
            "registration_channel_hello_repeated"
        );
    }
    let _release = cut_over(&rig, &mut swap).await;
    swap.incumbent.send(hello("roles-hello")).await;
    loop {
        let frame = swap.incumbent.recv().await.expect("HELLO refusal");
        if frame.header.ty == FrameType::Error {
            assert_eq!(code(&frame), "registration_channel_hello_repeated");
            break;
        }
    }
}

#[tokio::test]
async fn channel_operations_config_row_serves_current_and_candidate_not_draining() {
    let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../target/scratch")
        .join(format!("channel-config-roles-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&path);
    std::fs::create_dir_all(path.join("cortexkit")).unwrap();
    std::fs::write(path.join("subc.jsonc"), br#"{"version":1,"modules":{}}"#).unwrap();
    let owner = Arc::new(crate::config_gate::runtime::RuntimeOwner::new(
        "channel-config-roles".into(),
        crate::config_store::ConfigStore::open_digest_only_for_tests(
            crate::config_store::StoreRoot::from_service_definition(path.join("store")).unwrap(),
        ),
        crate::config_gate::capture::ConfigCapturer {
            daemon_path: path.join("subc.jsonc"),
            config_home: path.clone(),
            projects: Arc::new(crate::config_gate::capture::OfflineProjectRegistry),
        },
    ));
    let rig = rig_with(move |handler| handler.with_config_runtime(owner)).await;
    let mut swap = swap_candidate(&rig, "roles-config").await;
    // Without a declaration a served connection is refused by the declaration
    // order check, which runs only after the delivery-target role check.
    for net in [&mut swap.incumbent, &mut swap.candidate] {
        assert_eq!(
            code(&ask(net, json!({"op":"config.get"})).await),
            "declaration_order"
        );
        assert_eq!(
            code(&ask(net, json!({"op":"config.applied"})).await),
            "invalid_control_body"
        );
    }
    let _release = cut_over(&rig, &mut swap).await;
    for op in ["config.get", "config.applied"] {
        assert_eq!(
            code(&ask(&mut swap.incumbent, json!({ "op": op })).await),
            "not_delivery_target",
            "{op}"
        );
    }
    assert_eq!(
        code(&ask(&mut swap.candidate, json!({"op":"config.get"})).await),
        "declaration_order",
        "the promoted candidate is the current delivery target"
    );
    drop(swap);
    drop(rig);
    let _ = std::fs::remove_dir_all(path);
}

#[tokio::test]
async fn channel_operations_catalog_and_activity_record_rows_serve_every_live_role() {
    let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../target/scratch")
        .join(format!("channel-record-roles-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&root);
    std::fs::create_dir_all(&root).unwrap();
    let ledger = root.clone();
    let rig = rig_with(move |handler| handler.with_activity_ledger(&ledger, "roles".into())).await;
    let mut swap = swap_candidate(&rig, "roles-record").await;
    let update = json!({"op":"catalog.update","provides":[],"ready":false});
    let declarations = operation_body("catalog.update_role_declarations");
    let record =
        json!({"op":"activity.record","kind":"roles-record.settled","summary":"Step done"});
    ok(&ask(&mut swap.incumbent, &update).await);
    ok(&ask(&mut swap.incumbent, &declarations).await);
    ok(&ask(&mut swap.incumbent, &record).await);
    ok(&ask(&mut swap.candidate, &update).await);
    ok(&ask(&mut swap.candidate, &declarations).await);
    ok(&ask(&mut swap.candidate, &record).await);
    let _release = cut_over(&rig, &mut swap).await;
    ok(&ask(&mut swap.incumbent, &update).await);
    ok(&ask(&mut swap.incumbent, &declarations).await);
    ok(&ask(&mut swap.incumbent, &record).await);
    drop(swap);
    drop(rig);
    let _ = std::fs::remove_dir_all(root);
}

#[tokio::test]
async fn channel_operations_claim_mint_row_serves_every_live_role() {
    let rig = rig().await;
    let mut swap = swap_candidate(&rig, "roles-mint").await;
    let body = ClaimMintRequest::new(ClaimPurpose::SelfConnection);
    ok(&ask(&mut swap.incumbent, &body).await);
    ok(&ask(&mut swap.candidate, &body).await);
    let _release = cut_over(&rig, &mut swap).await;
    ok(&ask(&mut swap.incumbent, &body).await);
}

struct Approve;

impl crate::operator_confirm::OperatorProvider for Approve {
    fn prompt(
        &self,
        _: &str,
        _: Box<dyn FnOnce(Arc<dyn crate::operator_confirm::OperatorWithdraw>) + Send>,
    ) -> crate::operator_confirm::ProviderResult {
        crate::operator_confirm::ProviderResult::Approved
    }
}

/// Bind a route from a synthetic client to `module`'s active endpoint, which
/// `module_connection` serves, and return its module-side channel and epoch.
fn open_route(
    rig: &Rig,
    module_connection: ConnectionId,
    module: &str,
    client: u64,
) -> (u16, u32, mpsc::Receiver<crate::router::OutboundFrame>) {
    let (sender, replies) = mpsc::channel(8);
    let pending = rig
        .handler
        .forwarding
        .begin_route_bind_relay_for_test(
            ConnectionId::new(client),
            crate::FrameSink::new(sender),
            2,
            module,
        )
        .unwrap();
    rig.handler
        .forwarding
        .complete_pending_relay(
            module_connection,
            pending.corr,
            crate::forwarding::RouteBindRelayOutcome::Accepted,
        )
        .unwrap();
    (pending.module_channel, pending.module_epoch, replies)
}

#[tokio::test]
async fn channel_operations_operator_confirm_row_proves_live_channels_only() {
    let mut rig = rig().await;
    rig.handler.forwarding.operator_confirms().configure(
        Arc::new(Approve),
        crate::operator_confirm::OperatorLimits::default(),
    );
    let self_claim = mint(&mut rig, ClaimPurpose::SelfConnection).await.claim;
    let mut bound = Net::new(rig.router.clone()).await;
    ok(&bound
        .call(request(ClaimPresentRequest::new(self_claim)))
        .await);

    let mut swap = swap_candidate(&rig, "roles-confirm").await;
    let (channel, epoch, _client) = open_route(&rig, swap.incumbent_id, "roles-confirm", 900_001);
    let confirm = OperatorConfirmRequest::new("Reset the cache", channel, epoch);
    assert_eq!(
        ok(&ask(&mut swap.incumbent, &confirm).await)["outcome"],
        "confirmed",
        "the current spawn's channel proves the module"
    );
    let _release = cut_over(&rig, &mut swap).await;
    assert_eq!(
        ok(&ask(&mut swap.incumbent, &confirm).await)["outcome"],
        "confirmed",
        "the draining incumbent's channel still proves the module"
    );
    let (channel, epoch, _promoted_client) =
        open_route(&rig, swap.candidate_id, "roles-confirm", 900_002);
    assert_eq!(
        ok(&ask(
            &mut swap.candidate,
            &OperatorConfirmRequest::new("Reset the cache", channel, epoch)
        )
        .await)["outcome"],
        "confirmed",
        "the promoted candidate's channel proves the module"
    );

    assert_eq!(
        code(&ask(&mut bound, &confirm).await),
        "claim_operation_not_allowed"
    );
    // A module that registered with neither a channel nor a launch nonce
    // proves nothing about who it is.
    let mut unsupervised = Net::new(rig.router.clone()).await;
    assert_eq!(
        unsupervised
            .call(hello("roles-unsupervised"))
            .await
            .header
            .ty,
        FrameType::HelloAck
    );
    let peer = unsupervised.connection_id(&rig.handler);
    let (channel, epoch, _plain_client) = open_route(&rig, peer, "roles-unsupervised", 900_003);
    assert_eq!(
        code(
            &ask(
                &mut unsupervised,
                &OperatorConfirmRequest::new("Reset the cache", channel, epoch)
            )
            .await
        ),
        error_codes::OPERATOR_REQUEST_NOT_PERMITTED
    );
    assert_eq!(
        code(&after_exit(&rig, &confirm).await),
        "registration_channel_closed"
    );
}

fn sync_request(generation: u64) -> ModuleControlRequestFromModule {
    ModuleControlRequestFromModule::ScopeSync {
        generation,
        scopes: vec![ScopeRecord::new("s", 1, ScopeKind::Head)],
    }
}

fn apply_request(generation: u64) -> ModuleControlRequestFromModule {
    ModuleControlRequestFromModule::ScopeApply {
        generation,
        upsert: Vec::new(),
        end: Vec::new(),
    }
}

#[tokio::test]
async fn channel_operations_scope_sync_row_is_keyed_by_the_serving_spawn() {
    let rig = rig().await;
    let mut swap = swap_candidate(&rig, "roles-scope").await;
    let mut bound = consumer(&rig, &mut swap.incumbent, ClaimPurpose::SelfConnection).await;

    // Before anything holds authority, the warming candidate is refused
    // because it is not current, not because another connection holds it.
    let refused = ask(&mut swap.candidate, sync_request(100)).await;
    assert_eq!(code(&refused), error_codes::SCOPE_SYNC_NOT_AUTHORITY);
    assert!(
        reply_text(&refused).contains("not the owner's current supervised launch"),
        "{}",
        reply_text(&refused)
    );

    // The current spawn's channel holds authority: a full sync comes first,
    // and generations only move forward.
    assert_eq!(
        code(&ask(&mut swap.incumbent, apply_request(1)).await),
        error_codes::SCOPE_SYNC_REQUIRED
    );
    ok(&ask(&mut swap.incumbent, sync_request(5)).await);
    assert_eq!(
        code(&ask(&mut swap.incumbent, apply_request(5)).await),
        error_codes::SCOPE_SYNC_STALE
    );
    ok(&ask(&mut swap.incumbent, apply_request(6)).await);

    // The warming candidate and the launch's claim-bound consumer are refused.
    assert_eq!(
        code(&ask(&mut swap.candidate, sync_request(100)).await),
        error_codes::SCOPE_SYNC_NOT_AUTHORITY
    );
    assert_eq!(
        code(&ask(&mut bound, sync_request(100)).await),
        CLAIM_OPERATION_NOT_ALLOWED
    );
    ok(&ask(&mut swap.incumbent, sync_request(7)).await);

    // After cutover the draining incumbent is refused, although its spawn is
    // still alive, and the promoted spawn takes authority at any generation.
    let _release = cut_over(&rig, &mut swap).await;
    for request in [sync_request(8), apply_request(8)] {
        assert_eq!(
            code(&ask(&mut swap.incumbent, request).await),
            error_codes::SCOPE_SYNC_NOT_AUTHORITY
        );
    }
    ok(&ask(&mut swap.candidate, sync_request(1)).await);
    ok(&ask(&mut swap.candidate, apply_request(2)).await);
}

#[tokio::test]
async fn channel_operations_scope_describe_row_serves_every_live_role() {
    let rig = rig().await;
    let mut swap = swap_candidate(&rig, "roles-describe").await;
    let describe = ModuleControlRequestFromModule::ScopeDescribe {
        owner: Principal::Reserved {
            module_id: "roles-describe".into(),
        },
        scope_ref: "s".into(),
    };
    ok(&ask(&mut swap.incumbent, &describe).await);
    ok(&ask(&mut swap.candidate, &describe).await);
    let _release = cut_over(&rig, &mut swap).await;
    ok(&ask(&mut swap.incumbent, &describe).await);
}

#[tokio::test]
async fn channel_operations_route_authorize_row_serves_only_the_current_spawn() {
    let rig =
        rig_with(|handler| handler.with_federated_scope_owner("roles-authorize".into())).await;
    let mut swap = swap_candidate(&rig, "roles-authorize").await;
    let declare = json!({
        "op":"catalog.update","provides":[],
        "capabilities":{"provides":[subc_protocol::scope::ROUTE_AUTHORIZE_CAPABILITY]},
    });
    ok(&ask(&mut swap.incumbent, &declare).await);
    ok(&ask(&mut swap.candidate, &declare).await);
    let source = ScopeRecord::new("s", 1, ScopeKind::Head);
    let authorize = ModuleControlRequestFromModule::RouteAuthorize {
        principal: Principal::Direct,
        scope: RouteAuthorizeScope::new(
            Principal::Reserved {
                module_id: "roles-authorize".into(),
            },
            "s",
            1,
        ),
        target: "roles-authorize".into(),
        expected: RouteAuthorizeExpected::new(source.kind, &source.attributes),
    };
    assert_eq!(
        ok(&ask(&mut swap.incumbent, &authorize).await)["op"],
        "route.authorize"
    );
    assert_eq!(
        code(&ask(&mut swap.candidate, &authorize).await),
        error_codes::ROUTE_AUTHORIZE_NOT_PERMITTED
    );
    let _release = cut_over(&rig, &mut swap).await;
    assert_eq!(
        code(&ask(&mut swap.incumbent, &authorize).await),
        error_codes::ROUTE_AUTHORIZE_NOT_PERMITTED
    );
    assert_eq!(
        ok(&ask(&mut swap.candidate, &authorize).await)["op"],
        "route.authorize"
    );
}

#[tokio::test]
async fn channel_operations_registration_reads_row_keeps_each_existing_requirement() {
    let rig = rig().await;
    let mut swap = swap_candidate(&rig, "roles-reads").await;
    let live_roots = json!({"op":"supervisor.live_roots"});
    let served_by = subc_control::ScopeServedByRequest::new("s".into(), 1, "tool".into());
    let agents = json!({"op":"supervisor.agent_commands","started":[],"finished":[]});
    for net in [&mut swap.incumbent, &mut swap.candidate] {
        ok(&ask(net, &live_roots).await);
        ok(&ask(net, &served_by).await);
    }
    ok(&ask(&mut swap.incumbent, &agents).await);
    assert_eq!(
        code(&ask(&mut swap.candidate, &agents).await),
        "not_supervised",
        "agent commands come only from the module's current launch"
    );
    let _release = cut_over(&rig, &mut swap).await;
    ok(&ask(&mut swap.incumbent, &live_roots).await);
    ok(&ask(&mut swap.incumbent, &served_by).await);
    assert_eq!(
        code(&ask(&mut swap.incumbent, &agents).await),
        "not_supervised"
    );
    ok(&ask(&mut swap.candidate, &agents).await);
}

fn operation_body(op: &str) -> Value {
    if op == "claim.mint" {
        serde_json::to_value(ClaimMintRequest::new(ClaimPurpose::SelfConnection)).unwrap()
    } else if op == "catalog.update_role_declarations" {
        json!({"op":op,"role_declarations":{}})
    } else {
        json!({ "op": op })
    }
}

/// Every operation a module sends the daemon on channel 0, written out here
/// instead of being read from the daemon's own lists, each marked with whether
/// it needs the module's own registration.
const MODULE_TO_DAEMON_OPERATIONS: &[(&str, bool)] = &[
    ("config.declare", true),
    ("config.get", true),
    ("config.applied", true),
    ("catalog.update", true),
    ("catalog.update_role_declarations", true),
    ("activity.record", true),
    ("claim.mint", true),
    ("operator.confirm", true),
    ("scope.sync", true),
    ("scope.apply", true),
    ("scope.describe", true),
    ("scope.watch", true),
    ("route.authorize", true),
    ("supervisor.live_roots", true),
    ("supervisor.agent_commands", true),
    ("scope.served_by", true),
    // Connection setup, and reads a module makes as a client.
    ("claim.present", false),
    ("activity.query", false),
    ("activity.subscribe", false),
    ("catalog.watch", false),
];

#[test]
fn channel_operations_catalog_watch_is_a_client_read() {
    assert!(MODULE_TO_DAEMON_OPERATIONS.contains(&("catalog.watch", false)));
    assert!(!REGISTRATION_OPERATIONS.contains(&"catalog.watch"));
}

fn registration_operations() -> Vec<&'static str> {
    MODULE_TO_DAEMON_OPERATIONS
        .iter()
        .filter(|(_, registration)| *registration)
        .map(|(op, _)| *op)
        .collect()
}

#[test]
fn channel_operations_every_module_operation_is_classified() {
    for op in MODULE_TO_SUBC_CONTROL_OPS
        .iter()
        .chain(MODULE_TO_SUBC_UNADVERTISED_OPS)
    {
        assert!(
            MODULE_TO_DAEMON_OPERATIONS
                .iter()
                .any(|(known, _)| known == op),
            "{op} is a module operation this table does not classify"
        );
    }
    let mut expected = registration_operations();
    let mut gated = REGISTRATION_OPERATIONS.to_vec();
    expected.sort_unstable();
    gated.sort_unstable();
    assert_eq!(
        gated, expected,
        "the registration check covers exactly these"
    );
}

#[tokio::test]
async fn channel_operations_refuse_consumer_claims_and_exited_spawns() {
    let mut rig = rig().await;
    let mut consumers = Vec::new();
    for purpose in [
        ClaimPurpose::SelfConnection,
        ClaimPurpose::Helper,
        ClaimPurpose::Relay,
    ] {
        let claim = mint(&mut rig, purpose).await.claim;
        let mut net = Net::new(rig.router.clone()).await;
        ok(&net.call(request(ClaimPresentRequest::new(claim))).await);
        consumers.push(net);
    }
    // `claim.mint` is dispatched before the registration check and refuses
    // both kinds of connection with its own code.
    for op in registration_operations() {
        for net in &mut consumers {
            let expected = if op == "claim.mint" {
                error_codes::CLAIM_MINT_NOT_ALLOWED
            } else {
                CLAIM_OPERATION_NOT_ALLOWED
            };
            assert_eq!(code(&ask(net, operation_body(op)).await), expected, "{op}");
        }
    }
    for op in registration_operations() {
        let expected = if op == "claim.mint" {
            error_codes::CLAIM_MINT_NOT_ALLOWED
        } else {
            REGISTRATION_CHANNEL_CLOSED
        };
        assert_eq!(
            code(&after_exit(&rig, operation_body(op)).await),
            expected,
            "{op}"
        );
    }
}

#[tokio::test]
async fn channel_operations_role_declarations_share_catalog_update_refusals() {
    let mut rig = rig().await;
    let claim = mint(&mut rig, ClaimPurpose::SelfConnection).await.claim;
    let mut net = Net::new(rig.router.clone()).await;
    ok(&net.call(request(ClaimPresentRequest::new(claim))).await);
    let catalog = json!({"op":"catalog.update","provides":[]});
    let declarations = operation_body("catalog.update_role_declarations");
    assert_eq!(
        code(&ask(&mut net, &catalog).await),
        CLAIM_OPERATION_NOT_ALLOWED
    );
    assert_eq!(
        code(&ask(&mut net, &declarations).await),
        CLAIM_OPERATION_NOT_ALLOWED
    );
    assert_eq!(
        code(&after_exit(&rig, &catalog).await),
        REGISTRATION_CHANNEL_CLOSED
    );
    assert_eq!(
        code(&after_exit(&rig, &declarations).await),
        REGISTRATION_CHANNEL_CLOSED
    );
}

/// A fixture whose handler keeps an activity ledger.
async fn activity_rig(label: &str) -> (Rig, std::path::PathBuf) {
    let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../target/scratch")
        .join(format!("channel-activity-{label}-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&root);
    std::fs::create_dir_all(&root).unwrap();
    let ledger = root.clone();
    let rig =
        rig_with(move |handler| handler.with_activity_ledger(&ledger, "activity".into())).await;
    (rig, root)
}

fn query(consumer_identity: Option<Value>) -> Value {
    let mut body = json!({"op":"activity.query","limit":10});
    if let Some(identity) = consumer_identity {
        body["consumer_identity"] = identity;
    }
    body
}

#[tokio::test]
async fn channel_operations_activity_reads_take_the_principal_from_the_binding() {
    let (mut rig, root) = activity_rig("principal").await;
    let (mut callosum, _spawn, _module, _) = network_primary(&rig, "callosum").await;
    let callosum_nonce = rig
        .handler
        .supervisor
        .spawn_launch_nonce_for("callosum")
        .expect("an ungated spawn also gets a launch nonce");
    let borrowed = json!({"module_id":"callosum","launch_nonce":callosum_nonce});
    let forbidden = subc_control::ACTIVITY_READ_FORBIDDEN;

    // A `self` connection of an ordinary module reads as that module, which is
    // not an activity reader, whatever its body claims to be.
    let own = mint(&mut rig, ClaimPurpose::SelfConnection).await.claim;
    let mut ordinary = Net::new(rig.router.clone()).await;
    ok(&ordinary.call(request(ClaimPresentRequest::new(own))).await);
    assert_eq!(code(&ask(&mut ordinary, query(None)).await), forbidden);
    assert_eq!(
        code(&ask(&mut ordinary, query(Some(borrowed.clone()))).await),
        forbidden,
        "a body identity cannot replace the binding"
    );

    // A channel that has not sent HELLO yet is still its spawn's binding.
    let (mut unregistered, _unregistered_spawn, _unregistered_module, _) =
        network_primary_with_hello(&rig, "roles-unregistered", false).await;
    assert_eq!(code(&ask(&mut unregistered, query(None)).await), forbidden);

    // Helper and relay connections act for someone else, so even an elevated
    // reader's are refused instead of being treated as `direct`.
    for purpose in [ClaimPurpose::Helper, ClaimPurpose::Relay] {
        let mut net = consumer(&rig, &mut callosum, purpose).await;
        assert_eq!(code(&ask(&mut net, query(None)).await), forbidden);
    }

    // The elevated reader keeps its standing on its live channel and on its
    // `self` connections, which ignore a body identity naming anyone else.
    let mut reader = consumer(&rig, &mut callosum, ClaimPurpose::SelfConnection).await;
    let stranger = json!({"module_id":"claims-module","launch_nonce":"not-a-nonce"});
    ok(&ask(&mut reader, query(Some(stranger))).await);
    ok(&ask(&mut callosum, query(None)).await);

    // A connection without a binding keeps the nonce-era rules: `direct`
    // reads, and so does a consumer proving the ungated reader's nonce.
    let mut plain = Net::new(rig.router.clone()).await;
    ok(&ask(&mut plain, query(None)).await);
    ok(&ask(&mut plain, query(Some(borrowed))).await);

    drop((ordinary, unregistered, reader, plain, callosum));
    drop(rig);
    let _ = std::fs::remove_dir_all(root);
}

#[tokio::test]
async fn channel_operations_claim_subscriptions_end_when_the_binding_is_revoked() {
    let (rig, root) = activity_rig("revoked").await;
    let (mut callosum, spawn, _module, _) = network_primary(&rig, "callosum").await;
    let claim = net_mint(&mut callosum, ClaimPurpose::SelfConnection)
        .await
        .claim;
    // An in-process connection: nothing here processes its close request, so
    // only the binding's revocation can end the subscription.
    let mut reader = Wire::new(&rig.router);
    ok(&reader
        .call(&rig.router, request(ClaimPresentRequest::new(claim)))
        .await);
    let subscribe = Frame::build(
        FrameType::Request,
        control_flags(),
        0,
        0,
        41,
        serde_json::to_vec(&json!({"op":"activity.subscribe"})).unwrap(),
    )
    .unwrap();
    rig.router
        .route_for_connection(&reader.ctx, subscribe)
        .await
        .unwrap();
    let key = (reader.ctx.connection_id, 41);
    let running = |handler: &ControlHandler| {
        handler
            .activity_streams
            .lock()
            .unwrap()
            .get(&key)
            .is_some_and(|task| !task.is_finished())
    };
    assert!(running(&rig.handler), "the subscription must be admitted");
    spawn.retire();
    tokio::time::timeout(Duration::from_secs(2), async {
        while running(&rig.handler) {
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
    })
    .await
    .expect("revoking the binding must end its subscription");
    drop((reader, callosum));
    drop(rig);
    let _ = std::fs::remove_dir_all(root);
}

#[tokio::test]
async fn channel_operations_prefix_registration_dies_with_its_owner() {
    let (mut rig, root) = activity_rig("prefix").await;
    let mut child = prefix_wire(&mut rig, "claims:roles-child").await;
    assert_eq!(
        child
            .call(&rig.router, prefix_hello("claims:roles-child"))
            .await
            .header
            .ty,
        FrameType::HelloAck
    );
    let child_id = child.ctx.connection_id;
    let brokered = [
        json!({"op":"catalog.update","provides":[],"ready":false}),
        operation_body("catalog.update_role_declarations"),
        json!({"op":"activity.record","kind":"claims:roles-child.settled","summary":"Settled"}),
        json!({"op":"scope.describe","owner":{"kind":"reserved","module_id":"claims-module"},"ref":"missing"}),
        json!({"op":"supervisor.live_roots"}),
    ];
    for body in &brokered {
        ok(&replay(&rig, child_id, body).await);
    }
    // The owner exits. Its prefix child's registry entry and binding remain
    // until the connection's cleanup runs, but a frame read before the claim
    // connection was fenced is refused.
    rig.spawn.retire();
    for body in &brokered {
        assert_eq!(
            code(&replay(&rig, child_id, body).await),
            REGISTRATION_CHANNEL_CLOSED,
            "{body}"
        );
    }
    drop(child);
    drop(rig);
    let _ = std::fs::remove_dir_all(root);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn channel_operations_busy_launch_record_is_retryable_for_the_serving_spawn() {
    let rig = rig().await;
    let (mut net, _spawn, module, _) = network_primary(&rig, "roles-busy").await;
    let agents = json!({"op":"supervisor.agent_commands","started":[],"finished":[]});
    let (locked, wait_locked) = std::sync::mpsc::channel();
    let (release, wait_release) = std::sync::mpsc::channel::<()>();
    let held = module.clone();
    let holder = std::thread::spawn(move || {
        held.with_resource_snapshot_locked_for_test(|| {
            locked.send(()).unwrap();
            let _ = wait_release.recv_timeout(Duration::from_secs(5));
        })
    });
    wait_locked.recv_timeout(Duration::from_secs(5)).unwrap();
    assert_eq!(code(&ask(&mut net, sync_request(1)).await), "busy");
    assert_eq!(code(&ask(&mut net, &agents).await), "busy");
    release.send(()).unwrap();
    holder.join().unwrap();
    ok(&ask(&mut net, sync_request(1)).await);
    ok(&ask(&mut net, &agents).await);
}

#[tokio::test]
async fn channel_operations_cutover_interval_is_retryable_for_the_promoted_spawn() {
    let rig = rig().await;
    let mut swap = swap_candidate(&rig, "roles-cutover").await;
    // Publish what a cutover publishes first, the forwarding endpoint and the
    // registry slot, and stop before the supervisor rewrites the launch
    // record, which still describes the incumbent's process.
    rig.handler
        .forwarding
        .cutover_candidate("roles-cutover")
        .unwrap()
        .expect("the candidate's endpoint takes over");
    rig.handler
        .registry
        .promote_candidate("roles-cutover")
        .unwrap()
        .expect("the registry names the candidate");
    let agents = json!({"op":"supervisor.agent_commands","started":[],"finished":[]});
    assert_eq!(
        code(&ask(&mut swap.candidate, sync_request(1)).await),
        "busy"
    );
    assert_eq!(code(&ask(&mut swap.candidate, &agents).await), "busy");
    assert_eq!(
        code(&ask(&mut swap.incumbent, sync_request(1)).await),
        error_codes::SCOPE_SYNC_NOT_AUTHORITY
    );
    assert_eq!(
        code(&ask(&mut swap.incumbent, &agents).await),
        "not_supervised"
    );
}

#[tokio::test]
async fn channel_operations_exited_reader_channel_and_claims_read_nothing() {
    let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../target/scratch")
        .join(format!("channel-activity-exited-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&root);
    std::fs::create_dir_all(&root).unwrap();
    let ledger = root.clone();
    let mut rig = rig_with_module("callosum", move |handler| {
        handler.with_activity_ledger(&ledger, "activity".into())
    })
    .await;
    let claim = mint(&mut rig, ClaimPurpose::SelfConnection).await.claim;
    let mut reader = Wire::new(&rig.router);
    ok(&reader
        .call(&rig.router, request(ClaimPresentRequest::new(claim)))
        .await);
    let readers = [rig.primary.ctx.connection_id, reader.ctx.connection_id];
    for connection in readers {
        ok(&replay(&rig, connection, query(None)).await);
    }
    rig.spawn.retire();
    for connection in readers {
        assert_eq!(
            code(&replay(&rig, connection, query(None)).await),
            subc_control::ACTIVITY_READ_FORBIDDEN
        );
    }
    drop(reader);
    drop(rig);
    let _ = std::fs::remove_dir_all(root);
}

/// A warming candidate serves no route before cutover, and `operator.confirm`
/// names a route its sender serves, so a candidate's confirm cannot succeed
/// end to end. Check the channel proof it would carry directly.
#[tokio::test]
async fn channel_operations_warming_candidate_channel_proves_operator_confirm() {
    let rig = rig().await;
    let swap = swap_candidate(&rig, "roles-confirm-candidate").await;
    for connection in [swap.candidate_id, swap.incumbent_id] {
        assert_eq!(
            rig.handler
                .confirm_channel_proven(connection, "roles-confirm-candidate"),
            Some(true)
        );
    }
    assert_eq!(
        rig.handler
            .confirm_channel_proven(swap.candidate_id, "another-module"),
        Some(false)
    );
    rig.spawn.retire();
    assert_eq!(
        rig.handler
            .confirm_channel_proven(rig.primary.ctx.connection_id, "claims-module"),
        Some(false)
    );
}

/// A supervised module's registration channel bound to an in-process
/// connection that has not sent HELLO, and the launch nonce its ungated spawn
/// also received.
async fn unregistered_channel(
    rig: &Rig,
    module_id: &str,
) -> (Wire, Arc<RegistrationSpawn>, SupervisedModule, String) {
    let handle = rig.handler.supervisor.clone();
    let supervisor =
        Supervisor::new_for_test(rig.handler.registry.clone(), RestartPolicy::default())
            .with_handle(handle.clone())
            .with_forwarding(rig.handler.forwarding.clone());
    let module = supervisor
        .spawn_async(ModuleSpec {
            script: None,
            module_id: module_id.into(),
            program: "/bin/sh".into(),
            args: vec!["-c".into(), "exec sleep 300".into()],
            env: Vec::new(),
            reserved: false,
            reserved_prefixes: Vec::new(),
            protocol: subc_control::ModuleProtocol::Subc,
            overlap: ModuleOverlap::Safe,
            carry_containment: false,
        })
        .await
        .unwrap();
    let spawn = handle.registration_for_test(module_id).unwrap();
    tokio::time::timeout(std::time::Duration::from_secs(6), spawn.wait_launch())
        .await
        .expect("registration launch must settle");
    let nonce = handle
        .spawn_launch_nonce_for(module_id)
        .expect("an ungated spawn also gets a launch nonce");
    let wire = Wire::new(&rig.router);
    spawn.bind(wire.ctx.connection_id);
    (wire, spawn, module, nonce)
}

fn hello_with_nonce(module_id: &str, nonce: &str) -> Frame {
    let body = subc_protocol::ModuleHelloBody {
        manifest: ModuleManifest::builder(module_id, "1.0.0").build(),
        protocol_ver: PROTOCOL_VERSION,
        control_ops: None,
        launch_nonce: Some(nonce.into()),
    };
    Frame::build(
        FrameType::Hello,
        control_flags(),
        0,
        0,
        CORR,
        serde_json::to_vec(&body).unwrap(),
    )
    .unwrap()
}

/// [`unregistered_channel`] after a HELLO that also presents the nonce.
async fn channel_with_nonce(
    rig: &Rig,
    module_id: &str,
) -> (Wire, Arc<RegistrationSpawn>, SupervisedModule, String) {
    let (mut wire, spawn, module, nonce) = unregistered_channel(rig, module_id).await;
    let ack = wire
        .call(&rig.router, hello_with_nonce(module_id, &nonce))
        .await;
    assert_eq!(ack.header.ty, FrameType::HelloAck, "{}", reply_text(&ack));
    (wire, spawn, module, nonce)
}

fn agent_commands() -> Value {
    json!({"op":"supervisor.agent_commands","started":[],"finished":[]})
}

#[tokio::test]
async fn channel_operations_channel_never_authenticates_through_a_presented_nonce() {
    let rig = rig().await;
    let (wire, _spawn, _module, nonce) = channel_with_nonce(&rig, "roles-nonce").await;
    let connection = wire.ctx.connection_id;
    assert_eq!(
        rig.handler
            .connection_is_current_launch("roles-nonce", connection, Some(&nonce)),
        LaunchStanding::Current
    );
    // Cleanup forgets the channel before it deregisters the connection. In
    // that interval the nonce the HELLO also carried must not stand in.
    rig.handler
        .supervisor
        .forget_registration_connection(connection);
    assert_eq!(
        rig.handler
            .connection_is_current_launch("roles-nonce", connection, Some(&nonce)),
        LaunchStanding::NotCurrent
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn channel_operations_declaration_running_during_cleanup_is_refused() {
    let rig = rig().await;
    let (wire, _spawn, _module, _nonce) = channel_with_nonce(&rig, "roles-queued").await;
    let connection = wire.ctx.connection_id;
    // Hold the declaration at its commit fence: authorized, with its fence
    // registered in the queue. Everything until its release happens well
    // inside the queue's validation window, so its own timeout cannot be what
    // refuses it. Its reply goes to a sink that retirement does not close.
    let (reached, resume) = rig.handler.supervisor.pause_resource_commit_for_test();
    let (sender, mut replies) = mpsc::channel(8);
    let ctx = RouteCtx {
        connection_id: connection,
        egress: crate::FrameSink::new(sender),
    };
    let declaration = Frame::build(
        FrameType::Request,
        control_flags(),
        0,
        0,
        51,
        serde_json::to_vec(&agent_commands()).unwrap(),
    )
    .unwrap();
    assert!(rig
        .handler
        .handle_control_frame(&ctx, declaration)
        .await
        .unwrap()
        .is_empty());
    tokio::time::timeout(Duration::from_secs(5), reached)
        .await
        .expect("the declaration must reach its commit fence")
        .unwrap();

    // Cleanup closes the declaration queue and is held there, before it
    // retires the spawn: the channel is still live and serving, so only the
    // cancelled fence can stop the commit.
    let (cleanup_reached_sender, cleanup_reached) = oneshot::channel();
    let (cleanup_release, cleanup_wait) = std::sync::mpsc::channel();
    *rig.handler.cleanup_pause.lock().unwrap() = Some((0, (cleanup_reached_sender, cleanup_wait)));
    let handler = rig.handler.clone();
    let cleanup =
        tokio::task::spawn_blocking(move || handler.cleanup_connection(connection).map(|_| ()));
    tokio::time::timeout(Duration::from_secs(5), cleanup_reached)
        .await
        .expect("cleanup must close the declaration queue")
        .unwrap();
    resume.send(()).unwrap();
    let reply = tokio::time::timeout(Duration::from_secs(5), replies.recv())
        .await
        .expect("the declaration must be answered")
        .unwrap()
        .frame;
    assert_eq!(reply.header.corr, 51);
    assert_eq!(code(&reply), "busy", "{}", reply_text(&reply));
    assert_eq!(
        serde_json::from_slice::<ErrorBody>(&reply.body)
            .unwrap()
            .message,
        "resource state is busy after revalidation; retry uncompleted finished and known entries",
        "the cancelled commit fence, not a timeout, must refuse the declaration"
    );
    cleanup_release.send(()).unwrap();
    cleanup.await.unwrap().unwrap();
    drop(wire);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn channel_operations_hello_outliving_cleanup_records_no_nonce_and_never_registers() {
    let rig = rig().await;
    let (wire, _spawn, _module, nonce) = unregistered_channel(&rig, "roles-late-hello").await;
    let connection = wire.ctx.connection_id;
    // The reader resolves the channel and is held before the blocking HELLO
    // handler starts, as when the HELLO outlives its reader.
    let (reached_sender, reached) = oneshot::channel();
    let (release, wait_release) = oneshot::channel();
    *rig.handler.hello_ingress_pause.lock().unwrap() = Some((reached_sender, wait_release));
    let router = rig.router.clone();
    let ctx = wire.ctx.clone();
    let hello = hello_with_nonce("roles-late-hello", &nonce);
    let hello = tokio::spawn(async move { router.route_for_connection(&ctx, hello).await });
    tokio::time::timeout(Duration::from_secs(5), reached)
        .await
        .expect("the HELLO must resolve its channel")
        .unwrap();
    // Cleanup retires the spawn and forgets the channel, and is held there.
    let (cleanup_reached_sender, cleanup_reached) = oneshot::channel();
    let (cleanup_release, cleanup_wait) = std::sync::mpsc::channel();
    *rig.handler.cleanup_pause.lock().unwrap() = Some((1, (cleanup_reached_sender, cleanup_wait)));
    let handler = rig.handler.clone();
    let cleanup =
        tokio::task::spawn_blocking(move || handler.cleanup_connection(connection).map(|_| ()));
    tokio::time::timeout(Duration::from_secs(5), cleanup_reached)
        .await
        .expect("cleanup must forget the channel")
        .unwrap();
    assert!(rig
        .handler
        .supervisor
        .registration_spawn(connection)
        .is_none());
    release.send(()).unwrap();
    let _ = tokio::time::timeout(Duration::from_secs(5), hello)
        .await
        .expect("the HELLO must settle");
    assert!(
        rig.handler
            .hello_launch_nonces
            .lock()
            .unwrap()
            .nonce(connection)
            .is_none(),
        "a channel HELLO recorded a launch nonce after teardown"
    );
    assert!(
        rig.handler
            .registry
            .get_module("roles-late-hello")
            .unwrap()
            .is_none(),
        "a channel HELLO registered after its spawn retired"
    );
    cleanup_release.send(()).unwrap();
    cleanup.await.unwrap().unwrap();
    drop(wire);
}

#[tokio::test]
async fn channel_operations_exited_predecessor_record_is_busy_not_final() {
    let rig = rig().await;
    let (mut net, _spawn, module, _) = network_primary(&rig, "roles-restarted").await;
    // After a restart the new spawn serves before its launch record is
    // rewritten, so the record still describes the exited predecessor. Pid 1
    // is never this spawn's process.
    module.set_launch_exited_predecessor_for_test(1);
    assert_eq!(code(&ask(&mut net, sync_request(1)).await), "busy");
    assert_eq!(code(&ask(&mut net, agent_commands()).await), "busy");
}

#[tokio::test]
async fn channel_operations_stopped_launch_is_not_current_rather_than_busy() {
    let rig = rig().await;
    let (mut net, _spawn, module, _) = network_primary(&rig, "roles-stopping").await;
    ok(&ask(&mut net, sync_request(1)).await);
    // The launch record shows the process gone before the spawn's exit has
    // been observed: the spawn will never be current again.
    module.set_launch_alive_for_test(false);
    assert_eq!(
        code(&ask(&mut net, sync_request(2)).await),
        error_codes::SCOPE_SYNC_NOT_AUTHORITY
    );
    assert_eq!(
        code(&ask(&mut net, agent_commands()).await),
        "not_supervised"
    );
    module.set_launch_alive_for_test(true);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn channel_operations_busy_launch_read_after_admission_is_retryable() {
    let rig = rig().await;
    let (mut net, _spawn, module, _) = network_primary(&rig, "roles-second-busy").await;
    // Hold the declaration after its current-launch check, then lock the
    // launch record only for the read that follows.
    let (reached_sender, reached) = oneshot::channel();
    let (release, wait_release) = std::sync::mpsc::channel();
    *rig.handler.agent_commands_pause.lock().unwrap() = Some((1, (reached_sender, wait_release)));
    net.send(request(agent_commands())).await;
    tokio::time::timeout(Duration::from_secs(5), reached)
        .await
        .expect("the declaration must pass its current-launch check")
        .unwrap();
    let (locked, wait_locked) = std::sync::mpsc::channel();
    let (unlock, wait_unlock) = std::sync::mpsc::channel::<()>();
    let held = module.clone();
    let holder = std::thread::spawn(move || {
        held.with_resource_snapshot_locked_for_test(|| {
            locked.send(()).unwrap();
            let _ = wait_unlock.recv_timeout(Duration::from_secs(5));
        })
    });
    wait_locked.recv_timeout(Duration::from_secs(5)).unwrap();
    release.send(()).unwrap();
    assert_eq!(code(&answer(&mut net).await), "busy");
    unlock.send(()).unwrap();
    holder.join().unwrap();
    ok(&ask(&mut net, agent_commands()).await);
}