subc-client-rs 0.41.0

Shared serve + consume client for Rust subc modules.
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
use super::*;
use serde_json::{json, Value};
use subc_protocol::consent::{ConsentDecision, ConsentRequest, ConsentResponse, CONSENT_V1};
use tokio::{io::DuplexStream, sync::Notify};

const WAIT: Duration = Duration::from_secs(2);

struct Handler {
    entered: mpsc::UnboundedSender<(String, Value, CancellationToken)>,
    release: Arc<Notify>,
    declarations: Arc<AtomicU64>,
    config: bool,
}

#[async_trait]
impl ModuleHandler for Handler {
    async fn handle(&self, _: RequestCtx, body: Vec<u8>) -> HandlerOutcome {
        HandlerOutcome::Response(body)
    }

    fn control_ops(&self) -> Vec<String> {
        self.declarations.fetch_add(1, Ordering::SeqCst);
        vec!["sample.check".into(), CONSENT_V1.into()]
    }

    fn config_declaration(&self) -> Option<config::ConfigDeclareRequest> {
        self.config.then(|| {
            config::ConfigDeclareRequest::new(
                json!({"type":"object"}),
                "/isolated/sample.json",
                vec![],
            )
        })
    }

    async fn config_review(&self, _: config::ConfigReviewRequest) -> config::ConfigReviewReply {
        config::ConfigReviewReply::new(vec![], vec![])
    }

    async fn on_control_request(
        &self,
        op: &str,
        body: Value,
        cancel: CancellationToken,
    ) -> Result<Value, ErrorBody> {
        self.entered
            .send((op.into(), body.clone(), cancel))
            .unwrap();
        if body["wait"] == true {
            self.release.notified().await;
        }
        if body["fail"] == true {
            return Err(ErrorBody::new(
                "approval_surface_unavailable",
                "phone is offline",
            ));
        }
        if op == CONSENT_V1 {
            let request: ConsentRequest = serde_json::from_value(body).unwrap();
            assert!(request.is_consistent());
            return Ok(serde_json::to_value(ConsentResponse::new(
                &request,
                ConsentDecision::Approved,
            ))
            .unwrap());
        }
        Ok(json!({"op":op,"answer":42}))
    }
}

struct Rig {
    wire: DuplexStream,
    serving: tokio::task::JoinHandle<Result<(), SubcModuleError>>,
    entered: mpsc::UnboundedReceiver<(String, Value, CancellationToken)>,
    release: Arc<Notify>,
    declarations: Arc<AtomicU64>,
    hello: ModuleHelloBody,
}

impl Rig {
    async fn new() -> Self {
        Self::with_config(false).await
    }

    async fn with_config(config: bool) -> Self {
        let (module, mut wire) = tokio::io::duplex(65536);
        let (entered, rx) = mpsc::unbounded_channel();
        let release = Arc::new(Notify::new());
        let declarations = Arc::new(AtomicU64::new(0));
        let register = tokio::spawn(register_connection(
            module,
            ModuleManifest::builder("sample", "1.0.0").build(),
            Handler {
                entered,
                release: release.clone(),
                declarations: declarations.clone(),
                config,
            },
            ServeOptions::default(),
        ));
        let hello = recv(&mut wire).await;
        let hello_body = serde_json::from_slice(&hello.body).unwrap();
        send(
            &mut wire,
            Frame::build(
                FrameType::HelloAck,
                control_flags(),
                0,
                0,
                hello.header.corr,
                serde_json::to_vec(&ModuleHelloAckBody {
                    negotiated_ver: PROTOCOL_VERSION,
                    subc_ops: vec![],
                    subc_capabilities: if config {
                        vec![config::CONFIG_GATE_CAPABILITY.into()]
                    } else {
                        vec![]
                    },
                    storage: None,
                    machine_id: None,
                })
                .unwrap(),
            )
            .unwrap(),
        )
        .await;
        let (_handle, serving) = register.await.unwrap().unwrap().unwrap();
        let mut rig = Self {
            wire,
            serving: tokio::spawn(serving),
            entered: rx,
            release,
            declarations,
            hello: hello_body,
        };
        if config {
            let declare = recv(&mut rig.wire).await;
            assert_eq!(
                serde_json::from_slice::<Value>(&declare.body).unwrap()["op"],
                "config.declare"
            );
            // The declaration refusal stops background fetches; review itself
            // remains available for the snapshots supplied by the daemon.
            send(
                &mut rig.wire,
                control(
                    FrameType::Error,
                    declare.header.corr,
                    serde_json::to_vec(&ErrorBody::new("unavailable", "isolated test")).unwrap(),
                ),
            )
            .await;
        }
        rig
    }

    async fn request(&mut self, corr: u64, body: Value) {
        send(
            &mut self.wire,
            control(FrameType::Request, corr, serde_json::to_vec(&body).unwrap()),
        )
        .await;
    }

    async fn entered(&mut self) -> (String, Value, CancellationToken) {
        timeout(WAIT, self.entered.recv())
            .await
            .expect("control handler did not start")
            .unwrap()
    }

    async fn finish(self) {
        self.release.notify_waiters();
        drop(self.wire);
        timeout(WAIT, self.serving).await.unwrap().unwrap().unwrap();
    }
}

fn control(ty: FrameType, corr: u64, body: Vec<u8>) -> Frame {
    Frame::build(ty, control_flags(), 0, 0, corr, body).unwrap()
}

async fn send(wire: &mut DuplexStream, frame: Frame) {
    write_frame(wire, &frame).await.unwrap();
}

async fn recv(wire: &mut DuplexStream) -> Frame {
    timeout(WAIT, read_frame(wire))
        .await
        .expect("no reply from module")
        .unwrap()
        .unwrap()
}

#[tokio::test]
async fn declared_op_is_advertised_once_in_hello() {
    let rig = Rig::new().await;
    assert_eq!(
        rig.hello.control_ops.as_ref().unwrap(),
        &["health.check", "sample.check", CONSENT_V1]
    );
    assert_eq!(rig.declarations.load(Ordering::SeqCst), 1);
    rig.finish().await;
}

#[tokio::test]
async fn declared_request_reaches_handler_and_reply_keeps_correlation() {
    let mut rig = Rig::new().await;
    let body = json!({"op":"sample.check","input":{"number":7}});
    rig.request(120, body.clone()).await;
    let (op, received, _) = rig.entered().await;
    assert_eq!(op, "sample.check");
    assert_eq!(received, body);
    let frame = recv(&mut rig.wire).await;
    assert_eq!(
        (frame.header.ty, frame.header.channel, frame.header.corr),
        (FrameType::Response, 0, 120)
    );
    assert_eq!(
        serde_json::from_slice::<Value>(&frame.body).unwrap(),
        json!({"op":"sample.check","answer":42})
    );
    assert_eq!(rig.declarations.load(Ordering::SeqCst), 1);
    rig.finish().await;
}

#[tokio::test]
async fn consent_request_uses_exact_daemon_serialization_without_op() {
    let request: ConsentRequest = serde_json::from_str(include_str!(
        "../../../subc-protocol/tests/golden/config_gate/consent_v1_request.json"
    ))
    .unwrap();
    let bytes = serde_json::to_vec(&request).unwrap();
    assert!(serde_json::from_slice::<Value>(&bytes)
        .unwrap()
        .get("op")
        .is_none());
    let mut rig = Rig::new().await;
    send(&mut rig.wire, control(FrameType::Request, 121, bytes)).await;
    let (op, _, _) = rig.entered().await;
    assert_eq!(op, CONSENT_V1);
    let frame = recv(&mut rig.wire).await;
    assert_eq!(
        (frame.header.ty, frame.header.corr),
        (FrameType::Response, 121)
    );
    assert!(serde_json::from_slice::<ConsentResponse>(&frame.body)
        .unwrap()
        .approves(&request));
    rig.finish().await;
}

#[tokio::test]
async fn typed_control_error_reaches_daemon_with_code() {
    let mut rig = Rig::new().await;
    rig.request(122, json!({"op":"sample.check","fail":true}))
        .await;
    rig.entered().await;
    let frame = recv(&mut rig.wire).await;
    assert_eq!(
        (frame.header.ty, frame.header.channel, frame.header.corr),
        (FrameType::Error, 0, 122)
    );
    let error: ErrorBody = serde_json::from_slice(&frame.body).unwrap();
    assert_eq!(
        (error.code.as_str(), error.message.as_str()),
        ("approval_surface_unavailable", "phone is offline")
    );
    rig.finish().await;
}

#[tokio::test]
async fn long_control_handler_does_not_block_health_route_or_config_review() {
    let mut rig = Rig::with_config(true).await;
    rig.request(130, json!({"op":"sample.check","wait":true}))
        .await;
    rig.entered().await;
    rig.request(131, json!({"op":"health.check"})).await;
    let health = recv(&mut rig.wire).await;
    assert_eq!(
        (health.header.ty, health.header.corr),
        (FrameType::Response, 131)
    );
    rig.request(
        132,
        serde_json::to_value(ModuleControlRequest::RouteBind {
            route_channel: 1,
            epoch: 1,
            target: RouteTarget::ToolProvider {
                module_id: "sample".into(),
            },
            identity: BindIdentity::new("test", "bind"),
            principal: None,
            consumer_capabilities: None,
            role_versions: None,
            admission_facts: None,
            scope: None,
            call_mode: None,
        })
        .unwrap(),
    )
    .await;
    let bind = recv(&mut rig.wire).await;
    assert_eq!(
        (bind.header.ty, bind.header.corr),
        (FrameType::Response, 132)
    );
    send(
        &mut rig.wire,
        Frame::build(
            FrameType::Request,
            data_flags(),
            1,
            1,
            133,
            b"route request".to_vec(),
        )
        .unwrap(),
    )
    .await;
    let route = recv(&mut rig.wire).await;
    assert_eq!(
        (route.header.ty, route.header.channel, route.header.corr),
        (FrameType::Response, 1, 133)
    );
    assert_eq!(route.body, b"route request");
    rig.request(
        134,
        serde_json::to_value(config::ConfigReviewRequest::new(0, vec![], None)).unwrap(),
    )
    .await;
    let review = recv(&mut rig.wire).await;
    assert_eq!(
        (review.header.ty, review.header.corr),
        (FrameType::Response, 134)
    );
    assert!(
        serde_json::from_slice::<config::ConfigReviewReply>(&review.body)
            .unwrap()
            .refusals
            .is_empty()
    );
    rig.release.notify_one();
    assert_eq!(recv(&mut rig.wire).await.header.corr, 130);
    rig.finish().await;
}

#[tokio::test]
async fn cancel_signals_handler_and_drops_its_late_reply() {
    let (entered, mut rx) = mpsc::unbounded_channel();
    let release = Arc::new(Notify::new());
    let handler = Arc::new(Handler {
        entered,
        release: release.clone(),
        declarations: Arc::new(AtomicU64::new(0)),
        config: false,
    });
    let (writer, mut outbound) = mpsc::channel(1);
    let handle = ModuleHandle::new(
        &ModuleHelloAckBody {
            negotiated_ver: PROTOCOL_VERSION,
            subc_ops: vec![],
            subc_capabilities: vec![],
            storage: None,
            machine_id: None,
        },
        writer,
        1,
        CancellationToken::new(),
        tokio::runtime::Handle::current(),
    );
    handle
        .shared
        .served_control_ops
        .set(vec!["sample.check".into()])
        .unwrap();
    let (egress, mut replies) = mpsc::channel(1);
    let dispatcher = RequestDispatcher::new();
    handle_frame(
        control(
            FrameType::Request,
            140,
            br#"{"op":"sample.check","wait":true}"#.to_vec(),
        ),
        &egress,
        handler.clone(),
        dispatcher.clone(),
        handle.clone(),
    )
    .await
    .unwrap();
    let (_, _, cancel) = timeout(WAIT, rx.recv())
        .await
        .expect("control handler did not start")
        .unwrap();
    handle_frame(
        control(FrameType::Cancel, 140, vec![]),
        &egress,
        handler,
        dispatcher.clone(),
        handle,
    )
    .await
    .unwrap();
    timeout(WAIT, cancel.cancelled())
        .await
        .expect("CANCEL did not signal handler");
    release.notify_one();
    dispatcher.tasks.close();
    timeout(WAIT, dispatcher.tasks.wait())
        .await
        .expect("late handler did not finish");
    assert!(matches!(
        replies.try_recv(),
        Err(mpsc::error::TryRecvError::Empty)
    ));
    assert!(outbound.try_recv().is_err());
    assert!(lock_in_flight(&dispatcher.in_flight).unwrap().is_empty());
}

#[tokio::test]
async fn disconnect_signals_every_running_control_handler() {
    let mut rig = Rig::new().await;
    let mut tokens = Vec::new();
    for corr in 150..153 {
        rig.request(corr, json!({"op":"sample.check","wait":true}))
            .await;
        tokens.push(rig.entered().await.2);
    }
    drop(rig.wire);
    for token in tokens {
        timeout(WAIT, token.cancelled())
            .await
            .expect("disconnect did not signal handler");
    }
    rig.release.notify_waiters();
    timeout(WAIT, rig.serving).await.unwrap().unwrap().unwrap();
}

#[tokio::test]
async fn control_concurrency_is_bounded_and_overload_does_not_block_health() {
    let mut rig = Rig::new().await;
    for corr in 200..216 {
        rig.request(corr, json!({"op":"sample.check","wait":true}))
            .await;
        rig.entered().await;
    }
    rig.request(216, json!({"op":"sample.check"})).await;
    let refused = recv(&mut rig.wire).await;
    assert_eq!(
        (refused.header.ty, refused.header.corr),
        (FrameType::Error, 216)
    );
    assert_eq!(
        serde_json::from_slice::<ErrorBody>(&refused.body)
            .unwrap()
            .code,
        "resource_exhausted"
    );
    assert!(rig.entered.try_recv().is_err());
    rig.request(217, json!({"op":"health.check"})).await;
    assert_eq!(recv(&mut rig.wire).await.header.corr, 217);
    rig.release.notify_waiters();
    for _ in 0..16 {
        assert!((200..216).contains(&recv(&mut rig.wire).await.header.corr));
    }
    rig.request(218, json!({"op":"sample.check"})).await;
    assert_eq!(recv(&mut rig.wire).await.header.corr, 218);
    rig.finish().await;
}

#[tokio::test]
async fn op_takes_precedence_and_nonstring_op_does_not_fall_back_to_protocol() {
    let mut rig = Rig::new().await;
    for (corr, op) in [(230, json!("unknown.op")), (231, Value::Null)] {
        rig.request(corr, json!({"op":op,"protocol":"sample.check"}))
            .await;
        let refusal = recv(&mut rig.wire).await;
        assert_eq!(
            (refusal.header.ty, refusal.header.corr),
            (FrameType::Error, corr)
        );
        assert_eq!(
            serde_json::from_slice::<ErrorBody>(&refusal.body)
                .unwrap()
                .code,
            "invalid_request"
        );
    }
    assert!(rig.entered.try_recv().is_err());
    rig.finish().await;
}

#[test]
fn invalid_reserved_and_duplicate_control_names_are_refused() {
    for op in [
        "",
        "health.check",
        "config.review",
        "route.bind",
        "has space",
        "bad\nname",
        "é",
        "a..b",
        "/a",
        "a/",
        "a:b",
    ] {
        assert!(
            matches!(
                validate(vec![op.into()]),
                Err(SubcModuleError::InvalidControlOp(_))
            ),
            "accepted {op:?}"
        );
    }
    assert!(validate(vec!["sample.check".into(), "sample.check".into()]).is_err());
    assert!(validate(vec![
        "sample.check".into(),
        "consent/v1".into(),
        "custom_op-v2".into()
    ])
    .is_ok());
}

#[tokio::test]
async fn invalid_declaration_fails_before_any_hello_is_written() {
    struct Invalid;
    #[async_trait]
    impl ModuleHandler for Invalid {
        async fn handle(&self, _: RequestCtx, _: Vec<u8>) -> HandlerOutcome {
            unreachable!()
        }
        fn control_ops(&self) -> Vec<String> {
            vec!["config.review".into()]
        }
    }
    let (module, mut wire) = tokio::io::duplex(1024);
    let result = register_connection(
        module,
        ModuleManifest::builder("sample", "1").build(),
        Invalid,
        ServeOptions::default(),
    )
    .await;
    assert!(matches!(result, Err(SubcModuleError::InvalidControlOp(op)) if op == "config.review"));
    assert!(read_frame(&mut wire).await.unwrap().is_none());
}