asupersync 0.3.4

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
//! Metamorphic tests for obligation system invariants.
//!
//! These tests verify properties that must hold regardless of execution order,
//! catching bugs that conventional unit tests miss by exploiting transformations
//! of the input space while checking relationships between outputs.

#[cfg(test)]
use crate::obligation::ledger::{LedgerStats, ObligationLedger, ObligationToken};
#[cfg(test)]
use crate::record::{ObligationAbortReason, ObligationKind};
#[cfg(test)]
use crate::types::{RegionId, TaskId, Time};

#[cfg(test)]
#[inline]
fn tid(n: u32) -> TaskId {
    // Helper: TaskId/RegionId expose `new_for_test(index, generation)`
    // (src/types/id.rs:297, 109) — there is no `new(u64)` constructor.
    // The metamorphic relations only need distinct IDs, so generation 0
    // suffices.
    TaskId::new_for_test(n, 0)
}

#[cfg(test)]
#[inline]
fn rid(n: u32) -> RegionId {
    RegionId::new_for_test(n, 0)
}

#[cfg(test)]
/// Represents a scheduled operation with its timing
#[derive(Debug, Clone)]
pub enum ScheduledOp {
    /// Acquire an obligation, returning a token
    Acquire {
        /// Obligation resource kind to acquire.
        kind: ObligationKind,
        /// Task that owns the acquired obligation.
        task: TaskId,
        /// Region that bounds the obligation lifecycle.
        region: RegionId,
        /// Logical acquisition time recorded in the ledger.
        time: Time,
    },
    /// Commit a previously acquired token (by acquire index)
    CommitByIndex {
        /// Index of the earlier acquire operation whose token should commit.
        acquire_index: usize,
        /// Logical commit time recorded in the ledger.
        time: Time,
    },
    /// Abort a previously acquired token (by acquire index)
    AbortByIndex {
        /// Index of the earlier acquire operation whose token should abort.
        acquire_index: usize,
        /// Abort reason recorded for the obligation.
        reason: ObligationAbortReason,
        /// Logical abort time recorded in the ledger.
        time: Time,
    },
    /// Mark a region as finalized (closed)
    FinalizeRegion {
        /// Region whose pending obligations should be finalized.
        region: RegionId,
    },
}

#[cfg(test)]
/// Execution context that executes a sequence of operations
#[derive(Debug)]
struct ExecutionContext {
    ledger: ObligationLedger,
    /// Tokens acquired by index in the operation sequence
    acquired_tokens: Vec<Option<ObligationToken>>,
}

#[cfg(test)]
impl ExecutionContext {
    fn new() -> Self {
        Self {
            ledger: ObligationLedger::new(),
            acquired_tokens: Vec::new(),
        }
    }

    fn execute(&mut self, ops: &[ScheduledOp]) -> LedgerStats {
        self.acquired_tokens.clear();
        // Work around ObligationToken not implementing Clone by storing indices instead
        self.acquired_tokens = (0..ops.len()).map(|_| None).collect();

        for (idx, op) in ops.iter().enumerate() {
            match op {
                ScheduledOp::Acquire {
                    kind,
                    task,
                    region,
                    time,
                } => {
                    let token = self.ledger.acquire(*kind, *task, *region, *time);
                    self.acquired_tokens[idx] = Some(token);
                }
                ScheduledOp::CommitByIndex {
                    acquire_index,
                    time,
                } => {
                    // ObligationToken is intentionally !Copy/!Clone (linear
                    // by design — see src/obligation/ledger.rs:108), so we
                    // must move the token out of the Option via `take()`
                    // rather than dereference it. After commit, the slot
                    // is left as None so a duplicate CommitByIndex /
                    // AbortByIndex on the same index becomes a no-op.
                    if let Some(slot) = self.acquired_tokens.get_mut(*acquire_index) {
                        if let Some(token) = slot.take() {
                            let _ = self.ledger.commit(token, *time);
                        }
                    }
                }
                ScheduledOp::AbortByIndex {
                    acquire_index,
                    reason,
                    time,
                } => {
                    if let Some(slot) = self.acquired_tokens.get_mut(*acquire_index) {
                        if let Some(token) = slot.take() {
                            let _ = self.ledger.abort(token, *time, *reason);
                        }
                    }
                }
                ScheduledOp::FinalizeRegion { region } => {
                    self.ledger.mark_region_finalized(*region);
                }
            }
        }

        self.ledger.stats()
    }
}

//
// METAMORPHIC RELATIONS
//

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

    /// MR1: Total Token Conservation (Additive)
    /// acquired = committed + aborted + leaked + pending, ALWAYS
    #[test]
    fn mr_total_token_conservation_simple() {
        let ops = vec![
            ScheduledOp::Acquire {
                kind: ObligationKind::SendPermit,
                task: tid(1),
                region: rid(1),
                time: Time::from_nanos(1000),
            },
            ScheduledOp::Acquire {
                kind: ObligationKind::Ack,
                task: tid(2),
                region: rid(1),
                time: Time::from_nanos(1500),
            },
            ScheduledOp::CommitByIndex {
                acquire_index: 0,
                time: Time::from_nanos(2000),
            },
        ];

        let mut ctx = ExecutionContext::new();
        let stats = ctx.execute(&ops);

        // Test the conservation law
        let total_resolved = stats.total_committed + stats.total_aborted + stats.total_leaked;
        let total_accounted = total_resolved + stats.pending;

        assert_eq!(
            stats.total_acquired,
            total_accounted,
            "CONSERVATION VIOLATION: acquired={}, committed={}, aborted={}, leaked={}, pending={}",
            stats.total_acquired,
            stats.total_committed,
            stats.total_aborted,
            stats.total_leaked,
            stats.pending
        );
    }

    /// MR2: Schedule Invariance (Permutative)
    /// Reordering independent operations should preserve conservation
    #[test]
    fn mr_schedule_invariance_simple() {
        let ops1 = vec![
            ScheduledOp::Acquire {
                kind: ObligationKind::SendPermit,
                task: tid(1),
                region: rid(1),
                time: Time::from_nanos(1000),
            },
            ScheduledOp::Acquire {
                kind: ObligationKind::Ack,
                task: tid(2),
                region: rid(2),
                time: Time::from_nanos(1500),
            },
        ];

        // Transformation: reorder the acquires
        let ops2 = vec![
            ScheduledOp::Acquire {
                kind: ObligationKind::Ack,
                task: tid(2),
                region: rid(2),
                time: Time::from_nanos(1500),
            },
            ScheduledOp::Acquire {
                kind: ObligationKind::SendPermit,
                task: tid(1),
                region: rid(1),
                time: Time::from_nanos(1000),
            },
        ];

        let mut ctx1 = ExecutionContext::new();
        let stats1 = ctx1.execute(&ops1);

        let mut ctx2 = ExecutionContext::new();
        let stats2 = ctx2.execute(&ops2);

        // Relation: final counts should be invariant under reordering
        assert_eq!(
            stats1.total_acquired, stats2.total_acquired,
            "Total acquired changed under reordering: {} -> {}",
            stats1.total_acquired, stats2.total_acquired
        );

        assert_eq!(
            stats1.pending, stats2.pending,
            "Pending count changed under reordering: {} -> {}",
            stats1.pending, stats2.pending
        );
    }

    /// MR3: Region Quiescence Conservation (Inclusive/Exclusive)
    /// Disjoint regions should compose independently
    #[test]
    fn mr_region_quiescence_conservation() {
        let region1_ops = vec![
            ScheduledOp::Acquire {
                kind: ObligationKind::SendPermit,
                task: tid(1),
                region: rid(1000),
                time: Time::from_nanos(1000),
            },
            ScheduledOp::CommitByIndex {
                acquire_index: 0,
                time: Time::from_nanos(2000),
            },
        ];

        let region2_ops = vec![ScheduledOp::Acquire {
            kind: ObligationKind::Ack,
            task: tid(2),
            region: rid(2000),
            time: Time::from_nanos(1500),
        }];

        // Execute regions separately
        let mut ctx1 = ExecutionContext::new();
        let stats1 = ctx1.execute(&region1_ops);

        let mut ctx2 = ExecutionContext::new();
        let stats2 = ctx2.execute(&region2_ops);

        // Execute regions combined
        let mut combined_ops = region1_ops.clone();
        combined_ops.extend(region2_ops.clone());

        let mut combined_ctx = ExecutionContext::new();
        let combined_stats = combined_ctx.execute(&combined_ops);

        // Relation: combined execution should equal sum of separate executions
        assert_eq!(
            combined_stats.total_acquired,
            stats1.total_acquired + stats2.total_acquired,
            "Region composition failed for acquired: {} ≠ {} + {}",
            combined_stats.total_acquired,
            stats1.total_acquired,
            stats2.total_acquired
        );

        assert_eq!(
            combined_stats.pending,
            stats1.pending + stats2.pending,
            "Region composition failed for pending: {} ≠ {} + {}",
            combined_stats.pending,
            stats1.pending,
            stats2.pending
        );
    }

    /// MR4: State Transition Uniqueness (Permutative)
    /// Each obligation transitions exactly once
    #[test]
    fn mr_state_transition_uniqueness() {
        let ops = vec![
            ScheduledOp::Acquire {
                kind: ObligationKind::SendPermit,
                task: tid(1),
                region: rid(1),
                time: Time::from_nanos(1000),
            },
            ScheduledOp::Acquire {
                kind: ObligationKind::Ack,
                task: tid(2),
                region: rid(1),
                time: Time::from_nanos(1500),
            },
            ScheduledOp::CommitByIndex {
                acquire_index: 0,
                time: Time::from_nanos(2000),
            },
            ScheduledOp::AbortByIndex {
                acquire_index: 1,
                reason: ObligationAbortReason::Cancel,
                time: Time::from_nanos(2500),
            },
        ];

        let mut ctx = ExecutionContext::new();
        let stats = ctx.execute(&ops);

        // Every acquired obligation should be in exactly one final state
        let total_final_states =
            stats.total_committed + stats.total_aborted + stats.total_leaked + stats.pending;

        assert_eq!(
            stats.total_acquired, total_final_states,
            "STATE TRANSITION UNIQUENESS VIOLATION: acquired={}, final_states={}",
            stats.total_acquired, total_final_states
        );
    }

    /// MR5: Lease Expiration Monotonicity (Multiplicative)
    /// Scaling all timestamps shouldn't affect obligation counts
    #[test]
    fn mr_lease_expiration_monotonicity() {
        let ops_original = vec![
            ScheduledOp::Acquire {
                kind: ObligationKind::Lease,
                task: tid(1),
                region: rid(1),
                time: Time::from_nanos(1000),
            },
            ScheduledOp::CommitByIndex {
                acquire_index: 0,
                time: Time::from_nanos(2000),
            },
        ];

        // Transformation: scale all timestamps by 10
        let ops_scaled = vec![
            ScheduledOp::Acquire {
                kind: ObligationKind::Lease,
                task: tid(1),
                region: rid(1),
                time: Time::from_nanos(10000),
            },
            ScheduledOp::CommitByIndex {
                acquire_index: 0,
                time: Time::from_nanos(20000),
            },
        ];

        let mut ctx_original = ExecutionContext::new();
        let stats_original = ctx_original.execute(&ops_original);

        let mut ctx_scaled = ExecutionContext::new();
        let stats_scaled = ctx_scaled.execute(&ops_scaled);

        // Relation: scaling time shouldn't affect final obligation counts
        assert_eq!(
            stats_original.total_acquired, stats_scaled.total_acquired,
            "Time scaling changed acquired count: {} -> {}",
            stats_original.total_acquired, stats_scaled.total_acquired
        );

        assert_eq!(
            stats_original.total_committed, stats_scaled.total_committed,
            "Time scaling changed commit count: {} -> {}",
            stats_original.total_committed, stats_scaled.total_committed
        );
    }

    /// MR6: Double-Resolve Determinism (Equivalence)
    /// Double commit/abort attempts should be rejected deterministically
    #[test]
    fn mr_double_resolve_determinism() {
        let ops_original = vec![
            ScheduledOp::Acquire {
                kind: ObligationKind::SendPermit,
                task: tid(1),
                region: rid(1),
                time: Time::from_nanos(1000),
            },
            ScheduledOp::CommitByIndex {
                acquire_index: 0,
                time: Time::from_nanos(2000),
            },
        ];

        // Transformation: add duplicate commit attempt
        let mut ops_doubled = ops_original.clone();
        ops_doubled.push(ScheduledOp::CommitByIndex {
            acquire_index: 0,
            time: Time::from_nanos(3000),
        });

        let mut ctx_original = ExecutionContext::new();
        let stats_original = ctx_original.execute(&ops_original);

        let mut ctx_doubled = ExecutionContext::new();
        let stats_doubled = ctx_doubled.execute(&ops_doubled);

        // Relation: duplicate resolves should not change final counts
        assert_eq!(
            stats_original.total_committed, stats_doubled.total_committed,
            "Double resolve changed commit count: {} -> {}",
            stats_original.total_committed, stats_doubled.total_committed
        );

        assert_eq!(
            stats_original.total_aborted, stats_doubled.total_aborted,
            "Double resolve changed abort count: {} -> {}",
            stats_original.total_aborted, stats_doubled.total_aborted
        );
    }

    /// Validation: basic smoke test that MRs can run
    #[test]
    fn metamorphic_suite_smoke_test() {
        let ops = vec![
            ScheduledOp::Acquire {
                kind: ObligationKind::SendPermit,
                task: tid(1),
                region: rid(1),
                time: Time::from_nanos(1000),
            },
            ScheduledOp::CommitByIndex {
                acquire_index: 0,
                time: Time::from_nanos(2000),
            },
        ];

        let mut ctx = ExecutionContext::new();
        let stats = ctx.execute(&ops);

        // Basic conservation check
        assert_eq!(stats.total_acquired, 1);
        assert_eq!(stats.total_committed, 1);
        assert_eq!(stats.pending, 0);

        // Conservation law
        let total =
            stats.total_committed + stats.total_aborted + stats.total_leaked + stats.pending;
        assert_eq!(stats.total_acquired, total);
    }
}