asupersync 0.3.6

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
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
//! Explicit multi-source ATP fetch planning.
//!
//! Multi-source transfer starts from a receiver-owned list of candidate peers
//! that can serve the same object. This module keeps that protocol decision
//! transport-agnostic: validate the explicit source list, select a deterministic
//! subset, assign complementary source/repair emphasis to reduce waste, and
//! produce the stop fanout once any union of symbols decodes.

use std::collections::{BTreeMap, BTreeSet};

/// Stable object identity used to prove all selected peers serve the same data.
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct MultiSourceObjectRef {
    /// Transfer or object identifier shared by the peers.
    pub object_id: String,
    /// Expected Merkle root for the complete logical object.
    pub merkle_root_hex: String,
}

impl MultiSourceObjectRef {
    /// Create an object reference.
    #[must_use]
    pub fn new(object_id: impl Into<String>, merkle_root_hex: impl Into<String>) -> Self {
        Self {
            object_id: object_id.into(),
            merkle_root_hex: merkle_root_hex.into(),
        }
    }

    fn validate(&self) -> Result<(), MultiSourcePlanError> {
        if self.object_id.trim().is_empty() {
            return Err(MultiSourcePlanError::EmptyObjectId);
        }
        if self.merkle_root_hex.trim().is_empty() {
            return Err(MultiSourcePlanError::EmptyMerkleRoot);
        }
        Ok(())
    }
}

/// Per-peer authentication posture for a multi-source candidate.
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub enum MultiSourceAuth {
    /// The peer must authenticate symbols with the named per-transfer key scope.
    SymbolAuth { key_id: String },
    /// Lab-only escape hatch. Production plans reject this unless explicitly allowed.
    UnauthenticatedLab,
}

impl MultiSourceAuth {
    /// Return a stable lower-case identifier for logs and plan artifacts.
    #[must_use]
    pub fn mode_id(&self) -> &'static str {
        match self {
            Self::SymbolAuth { .. } => "symbol_auth",
            Self::UnauthenticatedLab => "unauthenticated_lab",
        }
    }

    fn is_symbol_auth(&self) -> bool {
        matches!(self, Self::SymbolAuth { .. })
    }

    fn validate(&self) -> Result<(), MultiSourcePlanError> {
        match self {
            Self::SymbolAuth { key_id } if key_id.trim().is_empty() => {
                Err(MultiSourcePlanError::EmptyAuthKeyId)
            }
            _ => Ok(()),
        }
    }
}

/// One explicit peer that can serve the object.
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct MultiSourcePeer {
    /// Stable peer label or identity digest.
    pub peer_id: String,
    /// Transport endpoint, for example `host:port`.
    pub endpoint: String,
    /// Lower values are selected first.
    pub priority: u32,
    /// Authentication posture required for this source.
    pub auth: MultiSourceAuth,
}

impl MultiSourcePeer {
    /// Create a candidate source peer.
    #[must_use]
    pub fn new(
        peer_id: impl Into<String>,
        endpoint: impl Into<String>,
        priority: u32,
        auth: MultiSourceAuth,
    ) -> Self {
        Self {
            peer_id: peer_id.into(),
            endpoint: endpoint.into(),
            priority,
            auth,
        }
    }

    fn validate(&self) -> Result<(), MultiSourcePlanError> {
        if self.peer_id.trim().is_empty() {
            return Err(MultiSourcePlanError::EmptyPeerId);
        }
        if self.endpoint.trim().is_empty() {
            return Err(MultiSourcePlanError::EmptyEndpoint {
                peer_id: self.peer_id.clone(),
            });
        }
        self.auth.validate()
    }
}

/// How a selected source should bias its first spray.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub enum MultiSourceSymbolBias {
    /// Prefer systematic/source symbols first.
    SourceFirst,
    /// Prefer repair symbols earlier to complement a source-first peer.
    RepairFirst,
    /// Balanced fallback for additional sources.
    Balanced,
}

impl MultiSourceSymbolBias {
    /// Stable lower-case identifier for logs and plan artifacts.
    #[must_use]
    pub const fn bias_id(self) -> &'static str {
        match self {
            Self::SourceFirst => "source_first",
            Self::RepairFirst => "repair_first",
            Self::Balanced => "balanced",
        }
    }
}

/// Config for deterministic explicit-source selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct MultiSourceSelectionConfig {
    /// Minimum eligible sources needed before the plan is usable.
    pub min_sources: usize,
    /// Maximum selected sources to ask for the object.
    pub max_sources: usize,
    /// Whether `UnauthenticatedLab` peers are admitted.
    pub allow_unauthenticated_lab: bool,
}

impl MultiSourceSelectionConfig {
    /// Production default: at least two authenticated sources, select up to four.
    #[must_use]
    pub const fn production_default() -> Self {
        Self {
            min_sources: 2,
            max_sources: 4,
            allow_unauthenticated_lab: false,
        }
    }

    fn validate(self) -> Result<(), MultiSourcePlanError> {
        if self.min_sources == 0 {
            return Err(MultiSourcePlanError::ZeroMinSources);
        }
        if self.max_sources < self.min_sources {
            return Err(MultiSourcePlanError::MaxSourcesBelowMin {
                min_sources: self.min_sources,
                max_sources: self.max_sources,
            });
        }
        Ok(())
    }
}

impl Default for MultiSourceSelectionConfig {
    fn default() -> Self {
        Self::production_default()
    }
}

/// One selected peer with its deterministic role in the multi-source fetch.
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct MultiSourceSourcePlan {
    /// Selected source peer.
    pub peer: MultiSourcePeer,
    /// Complementary source/repair bias for this peer.
    pub symbol_bias: MultiSourceSymbolBias,
    /// Stable zero-based order after deterministic selection.
    pub selection_order: u32,
}

/// Stop command emitted for every selected source once the receiver decodes.
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct MultiSourceStopCommand {
    /// Peer to stop.
    pub peer_id: String,
    /// Endpoint to send the stop/proof signal to.
    pub endpoint: String,
    /// Object the stop applies to.
    pub object_id: String,
    /// Stable reason identifier.
    pub reason: MultiSourceStopReason,
}

/// Why the receiver asks selected sources to stop.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub enum MultiSourceStopReason {
    /// Decode, SHA, and Merkle verification completed.
    DecodedAndVerified,
    /// Receiver cancelled the fetch before completion.
    Cancelled,
    /// Receiver failed closed.
    FailedClosed,
}

impl MultiSourceStopReason {
    /// Stable lower-case identifier for logs and plan artifacts.
    #[must_use]
    pub const fn reason_id(self) -> &'static str {
        match self {
            Self::DecodedAndVerified => "decoded_and_verified",
            Self::Cancelled => "cancelled",
            Self::FailedClosed => "failed_closed",
        }
    }
}

/// Complete receiver-side plan for fetching one object from several sources.
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct MultiSourceFetchPlan {
    /// Object all selected sources must serve.
    pub object: MultiSourceObjectRef,
    /// Deterministically selected sources.
    pub selected_sources: Vec<MultiSourceSourcePlan>,
}

impl MultiSourceFetchPlan {
    /// Number of selected sources.
    #[must_use]
    pub fn source_count(&self) -> usize {
        self.selected_sources.len()
    }

    /// Build one stop command per selected source in selection order.
    #[must_use]
    pub fn stop_commands(&self, reason: MultiSourceStopReason) -> Vec<MultiSourceStopCommand> {
        self.selected_sources
            .iter()
            .map(|source| MultiSourceStopCommand {
                peer_id: source.peer.peer_id.clone(),
                endpoint: source.peer.endpoint.clone(),
                object_id: self.object.object_id.clone(),
                reason,
            })
            .collect()
    }
}

/// Errors from [`plan_multi_source_fetch`].
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum MultiSourcePlanError {
    /// Object id is empty.
    #[error("multi-source object id must be non-empty")]
    EmptyObjectId,
    /// Merkle root is empty.
    #[error("multi-source merkle root must be non-empty")]
    EmptyMerkleRoot,
    /// Peer id is empty.
    #[error("multi-source peer id must be non-empty")]
    EmptyPeerId,
    /// Endpoint is empty.
    #[error("multi-source endpoint for peer {peer_id} must be non-empty")]
    EmptyEndpoint {
        /// Peer with the empty endpoint.
        peer_id: String,
    },
    /// Symbol-auth key id is empty.
    #[error("multi-source symbol-auth key id must be non-empty")]
    EmptyAuthKeyId,
    /// Duplicate peer id in the explicit source list.
    #[error("multi-source duplicate peer id: {peer_id}")]
    DuplicatePeerId {
        /// Duplicate peer id.
        peer_id: String,
    },
    /// Duplicate endpoint in the explicit source list.
    #[error("multi-source duplicate endpoint: {endpoint}")]
    DuplicateEndpoint {
        /// Duplicate endpoint.
        endpoint: String,
    },
    /// No selected source can be unauthenticated unless lab mode is explicit.
    #[error("multi-source peer {peer_id} is unauthenticated but lab mode is not allowed")]
    UnauthenticatedPeerRejected {
        /// Rejected peer id.
        peer_id: String,
    },
    /// Minimum source count must be positive.
    #[error("multi-source min_sources must be greater than zero")]
    ZeroMinSources,
    /// max_sources must be at least min_sources.
    #[error("multi-source max_sources {max_sources} is below min_sources {min_sources}")]
    MaxSourcesBelowMin {
        /// Required minimum.
        min_sources: usize,
        /// Configured maximum.
        max_sources: usize,
    },
    /// Not enough eligible sources after validation/auth checks.
    #[error("multi-source needs at least {required} eligible sources, got {available}")]
    NotEnoughEligibleSources {
        /// Required eligible source count.
        required: usize,
        /// Available eligible source count.
        available: usize,
    },
    /// More selected sources than can be represented in stable plan artifacts.
    #[error("multi-source selected source count exceeds u32::MAX")]
    TooManySelectedSources,
}

/// Plan an explicit multi-source fetch for one object.
///
/// Selection is deterministic: candidates are first validated and deduplicated,
/// then sorted by `(priority, peer_id, endpoint)`, capped by `max_sources`, and
/// assigned complementary symbol bias by selection order. This is intentionally
/// policy-only; transports map each selected peer into a connection and feed
/// authenticated symbols into the multipath aggregator.
pub fn plan_multi_source_fetch(
    object: MultiSourceObjectRef,
    peers: impl IntoIterator<Item = MultiSourcePeer>,
    config: MultiSourceSelectionConfig,
) -> Result<MultiSourceFetchPlan, MultiSourcePlanError> {
    object.validate()?;
    config.validate()?;

    let mut peer_ids = BTreeSet::new();
    let mut endpoints = BTreeSet::new();
    let mut by_key = BTreeMap::new();

    for peer in peers {
        peer.validate()?;
        if !peer_ids.insert(peer.peer_id.clone()) {
            return Err(MultiSourcePlanError::DuplicatePeerId {
                peer_id: peer.peer_id,
            });
        }
        if !endpoints.insert(peer.endpoint.clone()) {
            return Err(MultiSourcePlanError::DuplicateEndpoint {
                endpoint: peer.endpoint,
            });
        }
        if !config.allow_unauthenticated_lab && !peer.auth.is_symbol_auth() {
            return Err(MultiSourcePlanError::UnauthenticatedPeerRejected {
                peer_id: peer.peer_id,
            });
        }
        by_key.insert(
            (peer.priority, peer.peer_id.clone(), peer.endpoint.clone()),
            peer,
        );
    }

    if by_key.len() < config.min_sources {
        return Err(MultiSourcePlanError::NotEnoughEligibleSources {
            required: config.min_sources,
            available: by_key.len(),
        });
    }

    let mut selected_sources = Vec::new();
    for (idx, peer) in by_key.into_values().take(config.max_sources).enumerate() {
        let selection_order =
            u32::try_from(idx).map_err(|_| MultiSourcePlanError::TooManySelectedSources)?;
        selected_sources.push(MultiSourceSourcePlan {
            peer,
            symbol_bias: symbol_bias_for_order(idx),
            selection_order,
        });
    }

    Ok(MultiSourceFetchPlan {
        object,
        selected_sources,
    })
}

fn symbol_bias_for_order(order: usize) -> MultiSourceSymbolBias {
    match order {
        0 => MultiSourceSymbolBias::SourceFirst,
        1 => MultiSourceSymbolBias::RepairFirst,
        _ => MultiSourceSymbolBias::Balanced,
    }
}

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

    fn object_ref() -> MultiSourceObjectRef {
        MultiSourceObjectRef::new("object-01", "abc123")
    }

    fn auth(key_id: &str) -> MultiSourceAuth {
        MultiSourceAuth::SymbolAuth {
            key_id: key_id.to_string(),
        }
    }

    fn peer(id: &str, endpoint: &str, priority: u32) -> MultiSourcePeer {
        MultiSourcePeer::new(id, endpoint, priority, auth("key-a"))
    }

    #[test]
    fn production_default_requires_two_authenticated_sources() {
        assert_eq!(
            MultiSourceSelectionConfig::production_default(),
            MultiSourceSelectionConfig {
                min_sources: 2,
                max_sources: 4,
                allow_unauthenticated_lab: false,
            }
        );
    }

    #[test]
    fn selection_is_priority_then_peer_then_endpoint_deterministic() {
        let plan = plan_multi_source_fetch(
            object_ref(),
            [
                peer("peer-c", "10.0.0.3:8472", 10),
                peer("peer-b", "10.0.0.2:8472", 5),
                peer("peer-a", "10.0.0.1:8472", 5),
            ],
            MultiSourceSelectionConfig {
                min_sources: 2,
                max_sources: 2,
                allow_unauthenticated_lab: false,
            },
        )
        .unwrap();

        assert_eq!(plan.source_count(), 2);
        assert_eq!(plan.selected_sources[0].peer.peer_id, "peer-a");
        assert_eq!(plan.selected_sources[1].peer.peer_id, "peer-b");
        assert_eq!(
            plan.selected_sources
                .iter()
                .map(|source| source.selection_order)
                .collect::<Vec<_>>(),
            vec![0, 1]
        );
    }

    #[test]
    fn selected_sources_get_complementary_symbol_biases() {
        let plan = plan_multi_source_fetch(
            object_ref(),
            [
                peer("peer-a", "10.0.0.1:8472", 1),
                peer("peer-b", "10.0.0.2:8472", 2),
                peer("peer-c", "10.0.0.3:8472", 3),
            ],
            MultiSourceSelectionConfig {
                min_sources: 2,
                max_sources: 3,
                allow_unauthenticated_lab: false,
            },
        )
        .unwrap();

        assert_eq!(
            plan.selected_sources
                .iter()
                .map(|source| source.symbol_bias)
                .collect::<Vec<_>>(),
            vec![
                MultiSourceSymbolBias::SourceFirst,
                MultiSourceSymbolBias::RepairFirst,
                MultiSourceSymbolBias::Balanced,
            ]
        );
        assert_eq!(
            plan.selected_sources[0].symbol_bias.bias_id(),
            "source_first"
        );
    }

    #[test]
    fn stop_commands_cover_every_selected_source_in_selection_order() {
        let plan = plan_multi_source_fetch(
            object_ref(),
            [
                peer("peer-b", "10.0.0.2:8472", 1),
                peer("peer-a", "10.0.0.1:8472", 0),
            ],
            MultiSourceSelectionConfig::production_default(),
        )
        .unwrap();

        let stops = plan.stop_commands(MultiSourceStopReason::DecodedAndVerified);
        assert_eq!(stops.len(), 2);
        assert_eq!(stops[0].peer_id, "peer-a");
        assert_eq!(stops[1].peer_id, "peer-b");
        assert!(stops.iter().all(|stop| {
            stop.object_id == "object-01" && stop.reason.reason_id() == "decoded_and_verified"
        }));
    }

    #[test]
    fn unauthenticated_sources_fail_closed_outside_lab_mode() {
        let err = plan_multi_source_fetch(
            object_ref(),
            [
                peer("peer-a", "10.0.0.1:8472", 0),
                MultiSourcePeer::new(
                    "peer-b",
                    "10.0.0.2:8472",
                    1,
                    MultiSourceAuth::UnauthenticatedLab,
                ),
            ],
            MultiSourceSelectionConfig::production_default(),
        )
        .unwrap_err();

        assert!(matches!(
            err,
            MultiSourcePlanError::UnauthenticatedPeerRejected { peer_id }
                if peer_id == "peer-b"
        ));
    }

    #[test]
    fn lab_mode_must_still_meet_minimum_source_count() {
        let plan = plan_multi_source_fetch(
            object_ref(),
            [
                MultiSourcePeer::new(
                    "peer-a",
                    "10.0.0.1:8472",
                    0,
                    MultiSourceAuth::UnauthenticatedLab,
                ),
                MultiSourcePeer::new(
                    "peer-b",
                    "10.0.0.2:8472",
                    1,
                    MultiSourceAuth::UnauthenticatedLab,
                ),
            ],
            MultiSourceSelectionConfig {
                min_sources: 2,
                max_sources: 2,
                allow_unauthenticated_lab: true,
            },
        )
        .unwrap();

        assert_eq!(plan.source_count(), 2);
        assert_eq!(
            plan.selected_sources[0].peer.auth.mode_id(),
            "unauthenticated_lab"
        );
    }

    #[test]
    fn duplicate_peer_ids_and_endpoints_fail_closed() {
        assert!(matches!(
            plan_multi_source_fetch(
                object_ref(),
                [
                    peer("peer-a", "10.0.0.1:8472", 0),
                    peer("peer-a", "10.0.0.2:8472", 1),
                ],
                MultiSourceSelectionConfig::production_default(),
            ),
            Err(MultiSourcePlanError::DuplicatePeerId { .. })
        ));

        assert!(matches!(
            plan_multi_source_fetch(
                object_ref(),
                [
                    peer("peer-a", "10.0.0.1:8472", 0),
                    peer("peer-b", "10.0.0.1:8472", 1),
                ],
                MultiSourceSelectionConfig::production_default(),
            ),
            Err(MultiSourcePlanError::DuplicateEndpoint { .. })
        ));
    }

    #[test]
    fn invalid_config_and_object_identity_fail_closed() {
        assert!(matches!(
            plan_multi_source_fetch(
                MultiSourceObjectRef::new("", "abc"),
                [peer("peer-a", "10.0.0.1:8472", 0)],
                MultiSourceSelectionConfig::production_default(),
            ),
            Err(MultiSourcePlanError::EmptyObjectId)
        ));
        assert!(matches!(
            plan_multi_source_fetch(
                object_ref(),
                [
                    peer("peer-a", "10.0.0.1:8472", 0),
                    peer("peer-b", "10.0.0.2:8472", 1),
                ],
                MultiSourceSelectionConfig {
                    min_sources: 3,
                    max_sources: 2,
                    allow_unauthenticated_lab: false,
                },
            ),
            Err(MultiSourcePlanError::MaxSourcesBelowMin { .. })
        ));
    }
}