asupersync 0.3.1

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! Delivery-class taxonomy for the native FABRIC lane.

#![allow(clippy::struct_field_names)]

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use thiserror::Error;

/// Named delivery classes reserved by the FABRIC design.
///
/// The enum order is intentional: moving upward makes stronger guarantees
/// explicit and never silently downgrades the common-case packet plane.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
#[serde(rename_all = "snake_case")]
pub enum DeliveryClass {
    /// Hot ephemeral pub/sub. No durability or obligation tracking.
    #[default]
    EphemeralInteractive,
    /// Durable ordered stream semantics with authority-plane commit.
    DurableOrdered,
    /// Request/reply and service flows backed by explicit obligations.
    ObligationBacked,
    /// Safe for stewardship change and cut-certified mobility operations.
    MobilitySafe,
    /// Replay-heavy reasoning tier with explicit evidence retention.
    ForensicReplayable,
}

impl DeliveryClass {
    /// All delivery classes in ascending cost/strength order.
    pub const ALL: [Self; 5] = [
        Self::EphemeralInteractive,
        Self::DurableOrdered,
        Self::ObligationBacked,
        Self::MobilitySafe,
        Self::ForensicReplayable,
    ];

    /// Return the operator-facing cost vector for this class.
    #[inline]
    #[must_use]
    pub const fn cost_vector(self) -> DeliveryCostVector {
        match self {
            Self::EphemeralInteractive => DeliveryCostVector::new(0, 0, 0, 0),
            Self::DurableOrdered => DeliveryCostVector::new(1, 1, 1, 1),
            Self::ObligationBacked => DeliveryCostVector::new(2, 2, 2, 2),
            Self::MobilitySafe => DeliveryCostVector::new(3, 2, 3, 3),
            Self::ForensicReplayable => DeliveryCostVector::new(4, 3, 4, 4),
        }
    }

    /// Minimum acknowledgement boundary this class can honestly claim.
    #[inline]
    #[must_use]
    pub const fn minimum_ack(self) -> AckKind {
        match self {
            Self::EphemeralInteractive => AckKind::Accepted,
            Self::DurableOrdered => AckKind::Recoverable,
            Self::ObligationBacked => AckKind::Served,
            Self::MobilitySafe | Self::ForensicReplayable => AckKind::Received,
        }
    }
}

impl fmt::Display for DeliveryClass {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            Self::EphemeralInteractive => "ephemeral-interactive",
            Self::DurableOrdered => "durable-ordered",
            Self::ObligationBacked => "obligation-backed",
            Self::MobilitySafe => "mobility-safe",
            Self::ForensicReplayable => "forensic-replayable",
        };
        write!(f, "{name}")
    }
}

/// Distinct acknowledgement boundaries for delivery classes and policies.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AckKind {
    /// Packet plane accepted custody for forwarding.
    Accepted,
    /// Authority plane committed the control entry or obligation.
    Committed,
    /// Declared durability class has been met.
    Recoverable,
    /// Service obligation completed by the callee.
    Served,
    /// Configured delivery or receipt boundary was crossed.
    Received,
}

impl fmt::Display for AckKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            Self::Accepted => "accepted",
            Self::Committed => "committed",
            Self::Recoverable => "recoverable",
            Self::Served => "served",
            Self::Received => "received",
        };
        write!(f, "{name}")
    }
}

/// Relative cost envelope for a delivery class.
///
/// These are tier numbers, not calibrated performance claims.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct DeliveryCostVector {
    /// Relative latency cost of the class.
    pub latency_tier: u8,
    /// Relative storage cost of the class.
    pub storage_tier: u8,
    /// Relative CPU cost of the class.
    pub cpu_tier: u8,
    /// Relative evidence or replay overhead of the class.
    pub evidence_tier: u8,
}

impl DeliveryCostVector {
    /// Construct a new cost vector.
    #[must_use]
    pub const fn new(latency_tier: u8, storage_tier: u8, cpu_tier: u8, evidence_tier: u8) -> Self {
        Self {
            latency_tier,
            storage_tier,
            cpu_tier,
            evidence_tier,
        }
    }
}

/// Provider-declared admissible classes plus the common-case default.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeliveryClassPolicy {
    /// Default class applied when the caller does not explicitly request one.
    pub default_class: DeliveryClass,
    admissible_classes: Vec<DeliveryClass>,
}

impl DeliveryClassPolicy {
    /// Build a canonical provider policy.
    pub fn new<I>(
        default_class: DeliveryClass,
        admissible_classes: I,
    ) -> Result<Self, DeliveryClassPolicyError>
    where
        I: IntoIterator<Item = DeliveryClass>,
    {
        let mut admissible_classes = admissible_classes.into_iter().collect::<Vec<_>>();
        admissible_classes.sort_unstable();
        admissible_classes.dedup();

        if admissible_classes.is_empty() {
            return Err(DeliveryClassPolicyError::EmptyProviderPolicy);
        }
        if admissible_classes.binary_search(&default_class).is_err() {
            return Err(DeliveryClassPolicyError::DefaultClassNotAdmissible { default_class });
        }

        Ok(Self {
            default_class,
            admissible_classes,
        })
    }

    /// Return the canonical admissible class set in ascending order.
    #[must_use]
    pub fn admissible_classes(&self) -> &[DeliveryClass] {
        &self.admissible_classes
    }

    /// Return true when the provider admits the requested class.
    #[must_use]
    pub fn allows(&self, requested: DeliveryClass) -> bool {
        self.admissible_classes.binary_search(&requested).is_ok()
    }

    /// Select the effective class for a caller request.
    ///
    /// `None` means "use the provider default".
    pub fn select_for_caller(
        &self,
        requested: Option<DeliveryClass>,
    ) -> Result<DeliveryClass, DeliveryClassPolicyError> {
        let requested = requested.unwrap_or(self.default_class);
        if self.allows(requested) {
            Ok(requested)
        } else {
            Err(DeliveryClassPolicyError::RequestedClassNotAdmissible {
                requested,
                default_class: self.default_class,
            })
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct DeliveryClassPolicyRepr {
    default_class: DeliveryClass,
    admissible_classes: Vec<DeliveryClass>,
}

impl Serialize for DeliveryClassPolicy {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        DeliveryClassPolicyRepr {
            default_class: self.default_class,
            admissible_classes: self.admissible_classes.clone(),
        }
        .serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for DeliveryClassPolicy {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let repr = DeliveryClassPolicyRepr::deserialize(deserializer)?;
        Self::new(repr.default_class, repr.admissible_classes).map_err(serde::de::Error::custom)
    }
}

/// Validation failures for provider/caller class selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
pub enum DeliveryClassPolicyError {
    /// Provider policy must declare at least one admissible class.
    #[error("provider policy must declare at least one admissible delivery class")]
    EmptyProviderPolicy,
    /// Provider default must be a member of the admissible set.
    #[error("provider default class {default_class} is not in the admissible set")]
    DefaultClassNotAdmissible {
        /// Default class that was not admitted by the provider.
        default_class: DeliveryClass,
    },
    /// Caller requested a class outside the provider envelope.
    #[error(
        "caller requested class {requested}, but the provider only admits classes rooted at default {default_class}"
    )]
    RequestedClassNotAdmissible {
        /// Class requested by the caller.
        requested: DeliveryClass,
        /// Provider default used for the service surface.
        default_class: DeliveryClass,
    },
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn delivery_class_default_is_ephemeral_interactive() {
        assert_eq!(
            DeliveryClass::default(),
            DeliveryClass::EphemeralInteractive
        );
    }

    #[test]
    fn delivery_class_order_tracks_non_decreasing_cost() {
        for pair in DeliveryClass::ALL.windows(2) {
            let left = pair[0].cost_vector();
            let right = pair[1].cost_vector();
            assert!(
                left <= right,
                "expected {:?} cost {:?} <= {:?} cost {:?}",
                pair[0],
                left,
                pair[1],
                right
            );
        }
    }

    #[test]
    fn delivery_class_minimum_ack_is_monotonic() {
        for pair in DeliveryClass::ALL.windows(2) {
            assert!(
                pair[0].minimum_ack() <= pair[1].minimum_ack(),
                "expected {:?} minimum ack {} <= {:?} minimum ack {}",
                pair[0],
                pair[0].minimum_ack(),
                pair[1],
                pair[1].minimum_ack()
            );
        }
    }

    #[test]
    fn ack_kind_order_is_progressive() {
        assert!(AckKind::Accepted < AckKind::Committed);
        assert!(AckKind::Committed < AckKind::Recoverable);
        assert!(AckKind::Recoverable < AckKind::Served);
        assert!(AckKind::Served < AckKind::Received);
    }

    #[test]
    fn serde_round_trip_preserves_taxonomy_values() {
        let class = DeliveryClass::MobilitySafe;
        let ack = AckKind::Recoverable;

        let class_json = serde_json::to_string(&class).expect("serialize delivery class");
        let ack_json = serde_json::to_string(&ack).expect("serialize ack kind");

        assert_eq!(
            serde_json::from_str::<DeliveryClass>(&class_json).expect("deserialize delivery class"),
            class
        );
        assert_eq!(
            serde_json::from_str::<AckKind>(&ack_json).expect("deserialize ack kind"),
            ack
        );
    }

    #[test]
    fn provider_policy_requires_non_empty_admissible_set() {
        let err = DeliveryClassPolicy::new(DeliveryClass::EphemeralInteractive, [])
            .expect_err("empty provider policy must fail");
        assert_eq!(err, DeliveryClassPolicyError::EmptyProviderPolicy);
    }

    #[test]
    fn provider_policy_requires_default_to_be_admissible() {
        let err = DeliveryClassPolicy::new(
            DeliveryClass::DurableOrdered,
            [DeliveryClass::EphemeralInteractive],
        )
        .expect_err("default outside admissible set must fail");
        assert_eq!(
            err,
            DeliveryClassPolicyError::DefaultClassNotAdmissible {
                default_class: DeliveryClass::DurableOrdered,
            }
        );
    }

    #[test]
    fn provider_policy_deduplicates_and_sorts_classes() {
        let policy = DeliveryClassPolicy::new(
            DeliveryClass::DurableOrdered,
            [
                DeliveryClass::DurableOrdered,
                DeliveryClass::EphemeralInteractive,
                DeliveryClass::DurableOrdered,
            ],
        )
        .expect("provider policy should canonicalize duplicates");

        assert_eq!(
            policy.admissible_classes(),
            &[
                DeliveryClass::EphemeralInteractive,
                DeliveryClass::DurableOrdered,
            ]
        );
    }

    #[test]
    fn provider_policy_uses_default_when_caller_omits_request() {
        let policy = DeliveryClassPolicy::new(
            DeliveryClass::DurableOrdered,
            [
                DeliveryClass::EphemeralInteractive,
                DeliveryClass::DurableOrdered,
            ],
        )
        .expect("valid provider policy");

        assert_eq!(
            policy
                .select_for_caller(None)
                .expect("use provider default"),
            DeliveryClass::DurableOrdered
        );
    }

    #[test]
    fn provider_policy_rejects_unavailable_requested_class() {
        let policy = DeliveryClassPolicy::new(
            DeliveryClass::EphemeralInteractive,
            [DeliveryClass::EphemeralInteractive],
        )
        .expect("valid provider policy");

        let err = policy
            .select_for_caller(Some(DeliveryClass::ForensicReplayable))
            .expect_err("caller request outside provider envelope must fail");
        assert_eq!(
            err,
            DeliveryClassPolicyError::RequestedClassNotAdmissible {
                requested: DeliveryClass::ForensicReplayable,
                default_class: DeliveryClass::EphemeralInteractive,
            }
        );
    }

    #[test]
    fn provider_policy_deserialization_revalidates_invariants() {
        let invalid =
            r#"{"default_class":"durable_ordered","admissible_classes":["ephemeral_interactive"]}"#;
        let err = serde_json::from_str::<DeliveryClassPolicy>(invalid)
            .expect_err("invalid serialized policy must be rejected");
        let err = err.to_string();
        assert!(
            err.contains("default class durable-ordered"),
            "expected validation error to mention the invalid default, got {err}"
        );
    }

    #[test]
    fn delivery_class_display_and_minimum_ack_cover_every_variant() {
        let cases = [
            (
                DeliveryClass::EphemeralInteractive,
                "ephemeral-interactive",
                AckKind::Accepted,
            ),
            (
                DeliveryClass::DurableOrdered,
                "durable-ordered",
                AckKind::Recoverable,
            ),
            (
                DeliveryClass::ObligationBacked,
                "obligation-backed",
                AckKind::Served,
            ),
            (
                DeliveryClass::MobilitySafe,
                "mobility-safe",
                AckKind::Received,
            ),
            (
                DeliveryClass::ForensicReplayable,
                "forensic-replayable",
                AckKind::Received,
            ),
        ];

        for (class, expected_display, expected_ack) in cases {
            assert_eq!(class.to_string(), expected_display);
            assert_eq!(class.minimum_ack(), expected_ack);
        }
    }

    #[test]
    fn provider_policy_allows_valid_requested_classes_and_round_trips() {
        let policy = DeliveryClassPolicy::new(
            DeliveryClass::DurableOrdered,
            [
                DeliveryClass::MobilitySafe,
                DeliveryClass::DurableOrdered,
                DeliveryClass::ObligationBacked,
            ],
        )
        .expect("valid provider policy");

        assert!(policy.allows(DeliveryClass::DurableOrdered));
        assert!(policy.allows(DeliveryClass::ObligationBacked));
        assert!(policy.allows(DeliveryClass::MobilitySafe));
        assert!(!policy.allows(DeliveryClass::ForensicReplayable));

        assert_eq!(
            policy
                .select_for_caller(Some(DeliveryClass::ObligationBacked))
                .expect("caller request within provider envelope"),
            DeliveryClass::ObligationBacked
        );
        assert_eq!(
            policy
                .select_for_caller(Some(DeliveryClass::MobilitySafe))
                .expect("caller request within provider envelope"),
            DeliveryClass::MobilitySafe
        );

        let json = serde_json::to_string(&policy).expect("serialize provider policy");
        let decoded: DeliveryClassPolicy =
            serde_json::from_str(&json).expect("deserialize provider policy");
        assert_eq!(decoded, policy);
        assert_eq!(
            decoded.admissible_classes(),
            &[
                DeliveryClass::DurableOrdered,
                DeliveryClass::ObligationBacked,
                DeliveryClass::MobilitySafe,
            ]
        );
    }
}