active-rpc 1.1.9

Active RPC — capability-based, promise-pipelined, multi-lane RPC for Active Infinity payloads and multi-agent systems
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
//! Integration tests: domain operations, transports and the cross-process peer.
//!
//! Each transport named in the proposal is exercised with a real payload: a
//! validated Infinity module plus a capability table. Malformed frames must come
//! back as errors, never panics.

use std::collections::BTreeMap;
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};

use active_rpc::cap::CapAttrs;
use active_rpc::domain::ModuleProvider;
use active_rpc::transport::connect::{BridgeServer, ConnectClient};
use active_rpc::transport::shm::{SharedMemoryTransport, SharedSegment};
use active_rpc::transport::{InProcessTransport, TcpTransport, Transport};
use active_rpc::value::{Arg, Value};
use active_rpc::wire::{CapDescriptor, Envelope, Kind};
use active_rpc::{Agent, Budget};

const SOURCE: &str = r#"
# @intent: "integration fixture"
[app]
name    = "integration"
port    = 8080
ratio   = 25%
routes  = ["/a", "/b"]

let timeout = 1500ms

[limits]
read_timeout = timeout * 2
token        = secret("integration-canary")
"#;

fn module() -> active_infinity::InfinityModule {
    active_rpc::domain::module_from_source(SOURCE, "integration.active").expect("module")
}

fn setup() -> (
    Agent,
    active_rpc::ProviderId,
    active_rpc::CapHandle,
    active_rpc::LaneId,
) {
    let mut provider = ModuleProvider::new();
    let object = provider.install(module());
    let agent = Agent::with_secret([21u8; 32]);
    let provider_id = agent.register(Box::new(provider));
    let cap = agent
        .mint(
            provider_id,
            object,
            CapAttrs::new(active_rpc::MODULE_METHODS),
        )
        .expect("mint");
    let lane = agent.create_lane("main", Budget::default()).expect("lane");
    (agent, provider_id, cap, lane)
}

#[test]
fn evaluate_runs_the_active_engine_over_the_binary_module() {
    let (agent, _provider, cap, lane) = setup();
    let promise = agent
        .call(
            lane,
            &cap,
            "evaluate",
            vec![Arg::Value(Value::Path("app".into()))],
        )
        .expect("call");
    let value = promise.value().expect("evaluate");
    let map = match value {
        Value::Map(map) => map,
        other => panic!("expected a map, got {other:?}"),
    };
    assert_eq!(map.get("name"), Some(&Value::text("integration")));
    assert_eq!(map.get("port"), Some(&Value::Int(8080)));
    let routes = match map.get("routes") {
        Some(Value::List(items)) => items.clone(),
        other => panic!("expected a list, got {other:?}"),
    };
    assert_eq!(routes.len(), 2);
}

#[test]
fn stats_hash_and_metadata_are_served_through_capabilities() {
    let (agent, _provider, cap, lane) = setup();
    let stats = agent
        .call(lane, &cap, "stats", vec![])
        .expect("call")
        .value()
        .expect("stats");
    let map = match stats {
        Value::Map(map) => map,
        other => panic!("expected map, got {other:?}"),
    };
    let nodes = match map.get("nodes") {
        Some(Value::Int(nodes)) => *nodes,
        other => panic!("expected nodes, got {other:?}"),
    };
    assert!(nodes > 10);
    assert_eq!(map.get("secrets"), Some(&Value::Int(1)));

    let hash = agent
        .call(lane, &cap, "content_hash", vec![])
        .expect("call")
        .value()
        .expect("hash");
    let hash = hash.as_text().expect("text").to_string();
    assert!(hash.starts_with("blake3:"), "{hash}");
    let again = agent
        .call(lane, &cap, "content_hash", vec![])
        .expect("call")
        .value()
        .expect("hash");
    assert_eq!(again.as_text().unwrap(), hash, "content address is stable");

    let provenance = agent
        .call(lane, &cap, "provenance", vec![])
        .expect("call")
        .value()
        .expect("provenance");
    assert_eq!(provenance, Value::Null);
}

#[test]
fn why_and_impact_answer_with_paths() {
    let (agent, _provider, cap, lane) = setup();
    let why = agent
        .call(
            lane,
            &cap,
            "why",
            vec![Arg::Value(Value::Path("limits.read_timeout".into()))],
        )
        .expect("call")
        .value()
        .expect("why");
    let labels = label_list(&why);
    assert!(
        labels.iter().any(|label| label == "timeout"),
        "why must include the binding: {labels:?}"
    );

    let impact = agent
        .call(
            lane,
            &cap,
            "impact",
            vec![Arg::Value(Value::Path("app.port".into()))],
        )
        .expect("call")
        .value()
        .expect("impact");
    let labels = label_list(&impact);
    assert!(
        labels.iter().any(|label| label == "app"),
        "impact must include the table: {labels:?}"
    );
}

fn label_list(value: &Value) -> Vec<String> {
    match value {
        Value::List(items) => items
            .iter()
            .filter_map(|item| item.as_text().map(|text| text.to_string()))
            .collect(),
        other => panic!("expected labels, got {other:?}"),
    }
}

#[test]
fn redaction_hides_the_secret_cone() {
    let (agent, _provider, cap, lane) = setup();
    let redacted = agent
        .attenuate(&cap, CapAttrs::new(&["why", "impact"]).redacted())
        .expect("attenuate");
    let value = agent
        .call(
            lane,
            &redacted,
            "why",
            vec![Arg::Value(Value::Path("limits.token".into()))],
        )
        .expect("call")
        .value()
        .expect("why");
    let labels = label_list(&value);
    assert_eq!(labels, vec!["***SECRET***".to_string()]);

    // Without redaction the paths are visible — values never are.
    let value = agent
        .call(
            lane,
            &cap,
            "why",
            vec![Arg::Value(Value::Path("limits.token".into()))],
        )
        .expect("call")
        .value()
        .expect("why");
    let labels = label_list(&value);
    assert!(!labels.is_empty(), "why must answer with dependencies");
    assert!(
        !labels.iter().any(|label| label.contains("canary")),
        "no label may carry secret material: {labels:?}"
    );
}

#[test]
fn subgraph_returns_a_valid_module() {
    let (agent, _provider, cap, lane) = setup();
    let value = agent
        .call(
            lane,
            &cap,
            "subgraph",
            vec![Arg::Value(Value::Path("app".into()))],
        )
        .expect("call")
        .value()
        .expect("subgraph");
    let bytes = match value {
        Value::Module(bytes) => bytes,
        other => panic!("expected module bytes, got {other:?}"),
    };
    let meta = active_infinity::validate(&bytes).expect("subgraph is a valid image");
    assert!(meta.nodes > 0);
    let decoded = active_infinity::decode(&bytes).expect("decode");
    assert!(decoded.root().is_some());
}

#[test]
fn patch_creates_a_new_module_version() {
    let (agent, _provider, cap, lane) = setup();
    let before = agent
        .call(lane, &cap, "content_hash", vec![])
        .unwrap()
        .value()
        .unwrap();
    let value = agent
        .call(
            lane,
            &cap,
            "patch",
            vec![
                Arg::Value(Value::Path("app.port".into())),
                Arg::value(Value::Int(9090)),
            ],
        )
        .expect("call")
        .value()
        .expect("patch");
    let bytes = match value {
        Value::Module(bytes) => bytes,
        other => panic!("expected module bytes, got {other:?}"),
    };
    let patched = active_infinity::decode(&bytes).expect("decode");
    assert!(patched
        .nodes()
        .iter()
        .any(|node| matches!(node, active_infinity::Inode::Int(9090))));
    let after = agent
        .call(lane, &cap, "content_hash", vec![])
        .unwrap()
        .value()
        .unwrap();
    assert_ne!(before, after, "the module capability advanced");
}

#[test]
fn evaluate_honors_the_capability_budget() {
    let (agent, _provider, cap, lane) = setup();
    let tight = agent
        .attenuate(
            &cap,
            CapAttrs::new(&["evaluate"]).with_budget(Budget {
                max_nodes: 1,
                ..Budget::default()
            }),
        )
        .expect("attenuate");
    let error = agent
        .call(lane, &tight, "evaluate", vec![])
        .expect("call")
        .value()
        .expect_err("evaluation must trip the node budget");
    assert_eq!(error.code(), "rpc/domain");
    assert!(error.to_string().contains("X001"), "{error}");
}

#[test]
fn in_process_transport_moves_envelopes_without_bytes() {
    let (mut left, mut right) = InProcessTransport::pair();
    let envelope = sample_envelope();
    left.send(&envelope).expect("send");
    let received = right.recv().expect("recv").expect("envelope");
    assert_eq!(received, envelope);
    assert_eq!(left.bytes_written(), 0, "no serialization in-process");
}

#[test]
fn shared_memory_transport_is_zero_copy_and_rejects_malformed_frames() {
    let segment = SharedSegment::new(1 << 20);
    let mut transport = SharedMemoryTransport::new(segment.clone());
    let envelope = sample_envelope();
    transport.send(&envelope).expect("send");

    // Zero copy: the borrowed frame points inside the segment's buffer.
    let inside = segment
        .with_frame(|frame| {
            let first = frame.as_ptr() as usize;
            first > 0 && !frame.is_empty()
        })
        .expect("frame")
        .expect("borrowed");
    assert!(inside);

    let received = transport.recv_zero_copy().expect("recv").expect("envelope");
    assert_eq!(received, envelope);
    assert_eq!(segment.available(), 0);

    // A frame that is not an envelope is refused.
    segment.write(b"not an envelope").expect("write");
    let error = transport.recv().unwrap_err();
    assert_eq!(error.code(), "rpc/wire");
}

#[test]
fn connect_bridge_moves_a_module_and_rejects_bad_frames() {
    let server = BridgeServer::bind_loopback().expect("bind");
    let address = server.address().to_string();
    let handle = std::thread::spawn(move || {
        server
            .serve(3, |envelope| match envelope.method.as_str() {
                "evaluate" => Envelope::response(envelope.request_id, Value::text("bridged")),
                _ => Envelope::error(
                    envelope.request_id,
                    &active_rpc::Error::MethodNotGranted {
                        method: envelope.method,
                    },
                ),
            })
            .expect("serve")
    });

    let mut client = ConnectClient::connect(&address).expect("connect");
    let envelope = sample_envelope();
    let response = client.call(&envelope).expect("call");
    assert_eq!(response.kind, Kind::Response);
    assert_eq!(response.first_value(), Some(&Value::text("bridged")));

    // Malformed gRPC frame.
    let (status, body) = client.post_raw(b"\x00\x00\x00").expect("post");
    assert_eq!(status, 400);
    assert!(String::from_utf8_lossy(&body).starts_with("rpc/"));

    // Wrong content type.
    let (status, _body) = client.post_typed(&[0u8; 8], "text/plain").expect("post");
    assert_eq!(status, 415);

    let _served = handle.join().expect("join");
    // The well-formed call and the malformed frame are both "served" (the\n    // malformed one with an error); the bad content type is rejected before\n    // dispatch and does not count.\n    assert_eq!(served, 2, "served calls");
}

#[cfg(feature = "quic")]
#[test]
fn quic_transport_moves_a_module_and_rejects_malformed_frames() {
    let server = active_rpc::transport::quic::QuicServer::start(2, |envelope| {
        Envelope::response(envelope.request_id, Value::text("quic"))
    })
    .expect("start quic server");
    let address = server.address().to_string();
    let certificate = server.certificate().clone();

    let client =
        active_rpc::transport::quic::QuicClient::connect(&address, &certificate).expect("connect");
    let envelope = sample_envelope();
    let response = client.call(&envelope).expect("call");
    assert_eq!(response.kind, Kind::Response);
    assert_eq!(response.first_value(), Some(&Value::text("quic")));

    // A raw, unframed payload is rejected with an error envelope.
    let raw = client.exchange(&[0xff; 8]).expect("raw exchange");
    let mut cursor = std::io::Cursor::new(raw.as_slice());
    let body = active_rpc::transport::read_frame(&mut cursor, &active_rpc::WireLimits::default())
        .expect("frame ok")
        .expect("frame");
    let decoded = Envelope::decode(&body).expect("decode");
    assert_eq!(decoded.kind, Kind::Error);
    assert!(decoded.method.starts_with("rpc/"));
}

fn sample_envelope() -> Envelope {
    let module = active_infinity::encode(&module()).expect("encode");
    Envelope::request(9, "evaluate", vec![Arg::Value(Value::Path("app".into()))])
        .with_payload(module)
        .with_caps(vec![CapDescriptor {
            id: active_rpc::CapId([5u8; 16]),
            methods: ["evaluate".to_string()].into_iter().collect(),
            budget: Budget::default(),
            read_only: false,
            redact: false,
            path_scope: None,
        }])
}

#[test]
fn cross_process_peer_pipelines_in_one_round_trip() {
    let mut child = Command::new(env!("CARGO_BIN_EXE_active-rpc-peer"))
        .args(["--listen", "127.0.0.1:0", "--calls", "16"])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn peer");
    let stdout = child.stdout.take().expect("stdout");
    let mut reader = BufReader::new(stdout);
    let mut line = String::new();
    reader.read_line(&mut line).expect("read listen line");
    let address = line
        .trim()
        .strip_prefix("listening ")
        .expect("peer announces its address")
        .to_string();

    let mut transport = TcpTransport::connect(&address).expect("connect");

    let hello = Envelope::request(1, "hello", vec![]);
    transport.send(&hello).expect("send hello");
    let response = transport.recv().expect("recv").expect("response");
    assert_eq!(response.kind, Kind::Response);
    let capabilities = match response.first_value() {
        Some(Value::Map(map)) => map.clone(),
        other => panic!("expected hello map, got {other:?}"),
    };
    assert_eq!(
        capabilities.get("peer"),
        Some(&Value::text("active-rpc-peer"))
    );

    let ping = Envelope::request(2, "ping", vec![]);
    transport.send(&ping).expect("send ping");
    let response = transport.recv().expect("recv").expect("response");
    assert_eq!(response.first_value(), Some(&Value::text("pong")));

    let pipeline = Envelope::request(3, "pipeline", vec![]);
    transport.send(&pipeline).expect("send pipeline");
    let response = transport.recv().expect("recv").expect("response");
    let pipelined_trips = round_trips(&response);

    let sequential = Envelope::request(4, "sequential", vec![]);
    transport.send(&sequential).expect("send sequential");
    let response = transport.recv().expect("recv").expect("response");
    let sequential_trips = round_trips(&response);

    println!(
        "cross-process round trips: pipelined={pipelined_trips} sequential={sequential_trips}"
    );
    assert_eq!(pipelined_trips, 1);
    assert_eq!(sequential_trips, 2);
    assert!(pipelined_trips < sequential_trips);

    // A domain call over the same cross-process connection.
    let stats = Envelope::request(5, "stats", vec![]);
    transport.send(&stats).expect("send stats");
    let response = transport.recv().expect("recv").expect("response");
    let map = match response.first_value() {
        Some(Value::Map(map)) => map.clone(),
        other => panic!("expected stats, got {other:?}"),
    };
    assert!(matches!(map.get("nodes"), Some(Value::Int(nodes)) if *nodes > 5));

    drop(transport);
    let _ = child.kill();
    let _ = child.wait();
}

fn round_trips(response: &Envelope) -> i64 {
    match response.first_value() {
        Some(Value::Map(map)) => match map.get("round_trips") {
            Some(Value::Int(trips)) => *trips,
            other => panic!("expected round_trips, got {other:?}"),
        },
        other => panic!("expected a map, got {other:?}"),
    }
}

#[test]
fn peer_reports_an_error_envelope_for_unknown_methods() {
    let mut child = Command::new(env!("CARGO_BIN_EXE_active-rpc-peer"))
        .args(["--listen", "127.0.0.1:0", "--calls", "4"])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn peer");
    let stdout = child.stdout.take().expect("stdout");
    let mut reader = BufReader::new(stdout);
    let mut line = String::new();
    reader.read_line(&mut line).expect("read listen line");
    let address = line
        .trim()
        .strip_prefix("listening ")
        .expect("address")
        .to_string();
    let mut transport = TcpTransport::connect(&address).expect("connect");
    transport
        .send(&Envelope::request(1, "not_a_method", vec![]))
        .expect("send");
    let response = transport.recv().expect("recv").expect("response");
    assert_eq!(response.kind, Kind::Error);
    assert_eq!(response.method, "rpc/method-not-granted");
    drop(transport);
    let _ = child.kill();
    let _ = child.wait();
}

#[test]
fn connect_bridge_survives_a_request_without_content_length() {
    use std::io::Write;
    let server = BridgeServer::bind_loopback().expect("bind");
    let address = server.address().to_string();
    let handle = std::thread::spawn(move || {
        server
            .serve(2, |envelope| {
                Envelope::response(envelope.request_id, Value::text("ok"))
            })
            .expect("serve")
    });

    // A request with no content-length is a 400; the server keeps accepting.
    let mut raw = std::net::TcpStream::connect(&address).expect("connect");
    raw.write_all(b"POST /active.rpc.v1.Agent/Call HTTP/1.1\r\nhost: x\r\ncontent-type: application/connect+active-infinity\r\n\r\n")
        .expect("write");
    let mut reply = Vec::new();
    use std::io::Read;
    raw.read_to_end(&mut reply).expect("read");
    let text = String::from_utf8_lossy(&reply);
    assert!(text.starts_with("HTTP/1.1 400"), "{text}");

    let mut client = ConnectClient::connect(&address).expect("client");
    let response = client
        .call(&Envelope::request(1, "ping", vec![]))
        .expect("call after the malformed request");
    assert_eq!(response.first_value(), Some(&Value::text("ok")));
    let served = handle.join().expect("join");
    assert_eq!(served, 2);
}

#[test]
fn tcp_transport_rejects_a_malformed_envelope() {
    let (listener, address) = active_rpc::transport::bind_loopback().expect("bind");
    let handle = std::thread::spawn(move || {
        let (stream, _) = listener.accept().expect("accept");
        let mut transport = TcpTransport::new(stream).expect("wrap");
        transport.recv()
    });
    // A valid frame header with a body that is not an envelope.
    let mut stream = std::net::TcpStream::connect(&address).expect("connect");
    active_rpc::transport::write_frame(&mut stream, b"garbage").expect("write frame");
    let outcome = handle.join().expect("join");
    let error = outcome.expect_err("malformed envelope must be refused");
    assert_eq!(error.code(), "rpc/wire");
    let _ = BTreeMap::<u8, u8>::new();
}