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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! ATP daemon quota accounting and retention planning.

use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;

/// Resource bucket governed by the daemon quota ledger.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum QuotaBucket {
    /// Verified cache entries and seeded object graph metadata.
    Cache,
    /// Offline mailbox offers and pending mailbox payload metadata.
    Mailbox,
    /// Structured trace records.
    Traces,
    /// Proof bundles and replay artifacts.
    ProofArtifacts,
    /// Local inbox entries and receive plans.
    Inbox,
    /// Quarantined receive data and failed validation payload metadata.
    Quarantine,
    /// Partial journals needed for resume after shutdown.
    PartialJournals,
    /// Diagnostic bundles and daemon status snapshots.
    Diagnostics,
    /// Durable daemon settings.
    Settings,
    /// Peer identities and key handles.
    Identities,
    /// Capability grants and consent records.
    Grants,
    /// Active and completed transfer state.
    Transfers,
}

impl QuotaBucket {
    /// Stable lowercase bucket name.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Cache => "cache",
            Self::Mailbox => "mailbox",
            Self::Traces => "traces",
            Self::ProofArtifacts => "proof_artifacts",
            Self::Inbox => "inbox",
            Self::Quarantine => "quarantine",
            Self::PartialJournals => "partial_journals",
            Self::Diagnostics => "diagnostics",
            Self::Settings => "settings",
            Self::Identities => "identities",
            Self::Grants => "grants",
            Self::Transfers => "transfers",
        }
    }
}

impl fmt::Display for QuotaBucket {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Hard quota for one bucket.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct QuotaLimit {
    /// Maximum resident bytes.
    pub max_bytes: u64,
    /// Maximum resident records.
    pub max_records: u64,
}

impl QuotaLimit {
    /// Build a hard quota.
    #[must_use]
    pub const fn new(max_bytes: u64, max_records: u64) -> Self {
        Self {
            max_bytes,
            max_records,
        }
    }
}

/// Current quota usage for one bucket.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct QuotaUsage {
    /// Resident bytes.
    pub bytes: u64,
    /// Resident record count.
    pub records: u64,
}

impl QuotaUsage {
    /// Add usage with overflow checking.
    pub fn checked_add(self, allocation: QuotaAllocation) -> Result<Self, QuotaError> {
        let bytes =
            self.bytes
                .checked_add(allocation.bytes)
                .ok_or(QuotaError::CounterOverflow {
                    bucket: allocation.bucket,
                })?;
        let records =
            self.records
                .checked_add(allocation.records)
                .ok_or(QuotaError::CounterOverflow {
                    bucket: allocation.bucket,
                })?;
        Ok(Self { bytes, records })
    }

    /// Remove usage, saturating to zero so cleanup is idempotent.
    #[must_use]
    pub fn saturating_sub(self, allocation: QuotaAllocation) -> Self {
        Self {
            bytes: self.bytes.saturating_sub(allocation.bytes),
            records: self.records.saturating_sub(allocation.records),
        }
    }
}

/// Allocation charged to a quota bucket.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct QuotaAllocation {
    /// Bucket to charge.
    pub bucket: QuotaBucket,
    /// Bytes charged.
    pub bytes: u64,
    /// Records charged.
    pub records: u64,
}

impl QuotaAllocation {
    /// Build a one-record allocation.
    #[must_use]
    pub const fn one_record(bucket: QuotaBucket, bytes: u64) -> Self {
        Self {
            bucket,
            bytes,
            records: 1,
        }
    }
}

/// Per-record retention clock data.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct RetentionClock {
    /// Record creation time in seconds since Unix epoch.
    pub created_epoch_secs: u64,
    /// Last mutation time in seconds since Unix epoch.
    pub updated_epoch_secs: u64,
    /// Explicit expiry time in seconds since Unix epoch.
    pub expires_epoch_secs: Option<u64>,
}

impl RetentionClock {
    /// Build a clock with no explicit expiry.
    #[must_use]
    pub const fn new(created_epoch_secs: u64, updated_epoch_secs: u64) -> Self {
        Self {
            created_epoch_secs,
            updated_epoch_secs,
            expires_epoch_secs: None,
        }
    }

    /// Attach an explicit expiry.
    #[must_use]
    pub const fn with_expiry(mut self, expires_epoch_secs: u64) -> Self {
        self.expires_epoch_secs = Some(expires_epoch_secs);
        self
    }
}

/// Retention rule for one bucket.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct RetentionRule {
    /// Keep records for at most this many seconds after last update.
    pub max_age_secs: Option<u64>,
    /// Keep at most this many newest records.
    pub max_records: Option<u64>,
    /// Expire explicit `expires_epoch_secs` values when reached.
    pub honor_explicit_expiry: bool,
}

impl RetentionRule {
    /// Keep records indefinitely unless explicit expiry is set.
    #[must_use]
    pub const fn indefinite() -> Self {
        Self {
            max_age_secs: None,
            max_records: None,
            honor_explicit_expiry: true,
        }
    }

    /// Keep records for a bounded age.
    #[must_use]
    pub const fn max_age(max_age_secs: u64) -> Self {
        Self {
            max_age_secs: Some(max_age_secs),
            max_records: None,
            honor_explicit_expiry: true,
        }
    }

    /// Return true when this rule expires a record at `now_epoch_secs`.
    #[must_use]
    pub fn expires(self, clock: RetentionClock, now_epoch_secs: u64) -> bool {
        if self.honor_explicit_expiry
            && clock
                .expires_epoch_secs
                .is_some_and(|expires| now_epoch_secs >= expires)
        {
            return true;
        }
        self.max_age_secs.is_some_and(|max_age| {
            now_epoch_secs.saturating_sub(clock.updated_epoch_secs) > max_age
        })
    }
}

/// Retention policy keyed by bucket.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RetentionPolicy {
    rules: BTreeMap<QuotaBucket, RetentionRule>,
}

impl RetentionPolicy {
    /// Build the daemon default retention policy.
    #[must_use]
    pub fn daemon_defaults() -> Self {
        let mut rules = BTreeMap::new();
        rules.insert(QuotaBucket::Cache, RetentionRule::indefinite());
        rules.insert(
            QuotaBucket::Mailbox,
            RetentionRule::max_age(30 * 24 * 60 * 60),
        );
        rules.insert(
            QuotaBucket::Traces,
            RetentionRule::max_age(7 * 24 * 60 * 60),
        );
        rules.insert(
            QuotaBucket::ProofArtifacts,
            RetentionRule::max_age(30 * 24 * 60 * 60),
        );
        rules.insert(
            QuotaBucket::Inbox,
            RetentionRule::max_age(30 * 24 * 60 * 60),
        );
        rules.insert(
            QuotaBucket::Quarantine,
            RetentionRule::max_age(24 * 60 * 60),
        );
        rules.insert(
            QuotaBucket::PartialJournals,
            RetentionRule::max_age(7 * 24 * 60 * 60),
        );
        rules.insert(
            QuotaBucket::Diagnostics,
            RetentionRule::max_age(24 * 60 * 60),
        );
        rules.insert(QuotaBucket::Settings, RetentionRule::indefinite());
        rules.insert(QuotaBucket::Identities, RetentionRule::indefinite());
        rules.insert(QuotaBucket::Grants, RetentionRule::indefinite());
        rules.insert(
            QuotaBucket::Transfers,
            RetentionRule::max_age(30 * 24 * 60 * 60),
        );
        Self { rules }
    }

    /// Override one bucket rule.
    pub fn set_rule(&mut self, bucket: QuotaBucket, rule: RetentionRule) {
        self.rules.insert(bucket, rule);
    }

    /// Return the rule for a bucket.
    #[must_use]
    pub fn rule(&self, bucket: QuotaBucket) -> RetentionRule {
        self.rules
            .get(&bucket)
            .copied()
            .unwrap_or_else(RetentionRule::indefinite)
    }

    /// Select expired records in deterministic record-id order.
    #[must_use]
    pub fn expired_records(&self, records: &[RetentionRecord], now_epoch_secs: u64) -> Vec<String> {
        let mut expired = BTreeSet::new();
        for record in records {
            if self
                .rule(record.bucket)
                .expires(record.clock, now_epoch_secs)
            {
                expired.insert(record.record_id.clone());
            }
        }

        for (bucket, rule) in &self.rules {
            let Some(max_records) = rule.max_records else {
                continue;
            };
            let keep = usize::try_from(max_records).unwrap_or(usize::MAX);
            let mut bucket_records: Vec<_> = records
                .iter()
                .filter(|record| {
                    record.bucket == *bucket && !expired.contains(record.record_id.as_str())
                })
                .collect();
            bucket_records.sort_by(|left, right| {
                right
                    .clock
                    .updated_epoch_secs
                    .cmp(&left.clock.updated_epoch_secs)
                    .then_with(|| left.record_id.cmp(&right.record_id))
            });
            for record in bucket_records.into_iter().skip(keep) {
                expired.insert(record.record_id.clone());
            }
        }

        expired.into_iter().collect()
    }
}

/// Retention metadata for one persistent state record.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RetentionRecord {
    /// Stable record id.
    pub record_id: String,
    /// Bucket that owns the record.
    pub bucket: QuotaBucket,
    /// Record clock metadata.
    pub clock: RetentionClock,
}

/// In-memory quota ledger for deterministic daemon state accounting.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct QuotaLedger {
    limits: BTreeMap<QuotaBucket, QuotaLimit>,
    usage: BTreeMap<QuotaBucket, QuotaUsage>,
    allocations: BTreeMap<String, QuotaAllocation>,
}

impl QuotaLedger {
    /// Build a ledger with daemon defaults.
    #[must_use]
    pub fn daemon_defaults() -> Self {
        let mut ledger = Self::new();
        ledger.set_limit(
            QuotaBucket::Cache,
            QuotaLimit::new(4 * 1024 * 1024 * 1024, 500_000),
        );
        ledger.set_limit(
            QuotaBucket::Mailbox,
            QuotaLimit::new(512 * 1024 * 1024, 100_000),
        );
        ledger.set_limit(
            QuotaBucket::Traces,
            QuotaLimit::new(256 * 1024 * 1024, 250_000),
        );
        ledger.set_limit(
            QuotaBucket::ProofArtifacts,
            QuotaLimit::new(512 * 1024 * 1024, 100_000),
        );
        ledger.set_limit(
            QuotaBucket::Inbox,
            QuotaLimit::new(256 * 1024 * 1024, 100_000),
        );
        ledger.set_limit(
            QuotaBucket::Quarantine,
            QuotaLimit::new(512 * 1024 * 1024, 50_000),
        );
        ledger.set_limit(
            QuotaBucket::PartialJournals,
            QuotaLimit::new(1024 * 1024 * 1024, 250_000),
        );
        ledger.set_limit(
            QuotaBucket::Diagnostics,
            QuotaLimit::new(128 * 1024 * 1024, 50_000),
        );
        ledger.set_limit(
            QuotaBucket::Settings,
            QuotaLimit::new(16 * 1024 * 1024, 10_000),
        );
        ledger.set_limit(
            QuotaBucket::Identities,
            QuotaLimit::new(64 * 1024 * 1024, 100_000),
        );
        ledger.set_limit(
            QuotaBucket::Grants,
            QuotaLimit::new(64 * 1024 * 1024, 250_000),
        );
        ledger.set_limit(
            QuotaBucket::Transfers,
            QuotaLimit::new(512 * 1024 * 1024, 250_000),
        );
        ledger
    }

    /// Build an empty ledger with no configured limits.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            limits: BTreeMap::new(),
            usage: BTreeMap::new(),
            allocations: BTreeMap::new(),
        }
    }

    /// Set or replace one bucket limit.
    pub fn set_limit(&mut self, bucket: QuotaBucket, limit: QuotaLimit) {
        self.limits.insert(bucket, limit);
    }

    /// Return a bucket limit.
    #[must_use]
    pub fn limit(&self, bucket: QuotaBucket) -> Option<QuotaLimit> {
        self.limits.get(&bucket).copied()
    }

    /// Return current usage for a bucket.
    #[must_use]
    pub fn usage(&self, bucket: QuotaBucket) -> QuotaUsage {
        self.usage.get(&bucket).copied().unwrap_or_default()
    }

    /// Reserve an allocation for a stable record id.
    pub fn reserve(
        &mut self,
        record_id: impl Into<String>,
        allocation: QuotaAllocation,
    ) -> Result<(), QuotaError> {
        let record_id = record_id.into();
        if self.allocations.contains_key(&record_id) {
            return Err(QuotaError::DuplicateAllocation(record_id));
        }

        let next = self.usage(allocation.bucket).checked_add(allocation)?;
        if let Some(limit) = self.limit(allocation.bucket) {
            if next.bytes > limit.max_bytes || next.records > limit.max_records {
                return Err(QuotaError::Exhausted {
                    bucket: allocation.bucket,
                    requested: allocation,
                    current: self.usage(allocation.bucket),
                    limit,
                });
            }
        }

        self.usage.insert(allocation.bucket, next);
        self.allocations.insert(record_id, allocation);
        Ok(())
    }

    /// Release an allocation by record id.
    pub fn release(&mut self, record_id: &str) -> Result<QuotaAllocation, QuotaError> {
        let allocation = self
            .allocations
            .remove(record_id)
            .ok_or_else(|| QuotaError::UnknownAllocation(record_id.to_string()))?;
        let current = self.usage(allocation.bucket);
        self.usage
            .insert(allocation.bucket, current.saturating_sub(allocation));
        Ok(allocation)
    }

    /// Return deterministic ledger rows.
    #[must_use]
    pub fn rows(&self) -> Vec<QuotaRow> {
        self.limits
            .iter()
            .map(|(bucket, limit)| QuotaRow {
                bucket: *bucket,
                limit: *limit,
                usage: self.usage(*bucket),
            })
            .collect()
    }

    /// Return configured limits in deterministic bucket order.
    #[must_use]
    pub fn limits(&self) -> Vec<(QuotaBucket, QuotaLimit)> {
        self.limits
            .iter()
            .map(|(bucket, limit)| (*bucket, *limit))
            .collect()
    }

    /// Return the number of tracked record allocations.
    #[must_use]
    pub fn allocation_count(&self) -> usize {
        self.allocations.len()
    }
}

impl Default for QuotaLedger {
    fn default() -> Self {
        Self::new()
    }
}

/// Stable quota diagnostic row.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct QuotaRow {
    /// Bucket name.
    pub bucket: QuotaBucket,
    /// Configured limit.
    pub limit: QuotaLimit,
    /// Current usage.
    pub usage: QuotaUsage,
}

/// Quota accounting error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QuotaError {
    /// The record id already has an allocation.
    DuplicateAllocation(String),
    /// The record id has no allocation.
    UnknownAllocation(String),
    /// An accounting counter overflowed.
    CounterOverflow {
        /// Bucket whose counter overflowed.
        bucket: QuotaBucket,
    },
    /// Bucket quota would be exceeded.
    Exhausted {
        /// Bucket that refused the allocation.
        bucket: QuotaBucket,
        /// Requested allocation.
        requested: QuotaAllocation,
        /// Current usage before the allocation.
        current: QuotaUsage,
        /// Configured limit.
        limit: QuotaLimit,
    },
}

impl fmt::Display for QuotaError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::DuplicateAllocation(record_id) => {
                write!(f, "quota allocation already exists for `{record_id}`")
            }
            Self::UnknownAllocation(record_id) => {
                write!(f, "quota allocation `{record_id}` is unknown")
            }
            Self::CounterOverflow { bucket } => {
                write!(f, "quota counter overflow in bucket `{bucket}`")
            }
            Self::Exhausted {
                bucket,
                requested,
                current,
                limit,
            } => write!(
                f,
                "quota exhausted for `{bucket}`: requested {} bytes/{} records, current {} bytes/{} records, limit {} bytes/{} records",
                requested.bytes,
                requested.records,
                current.bytes,
                current.records,
                limit.max_bytes,
                limit.max_records
            ),
        }
    }
}

impl std::error::Error for QuotaError {}

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

    #[test]
    fn quota_reserve_and_release_are_accounted() {
        let mut ledger = QuotaLedger::new();
        ledger.set_limit(QuotaBucket::Cache, QuotaLimit::new(100, 2));

        ledger
            .reserve(
                "cache-a",
                QuotaAllocation::one_record(QuotaBucket::Cache, 40),
            )
            .unwrap(); // ubs:ignore - test oracle
        ledger
            .reserve(
                "cache-b",
                QuotaAllocation::one_record(QuotaBucket::Cache, 50),
            )
            .unwrap(); // ubs:ignore - test oracle

        assert_eq!(
            ledger.usage(QuotaBucket::Cache),
            QuotaUsage {
                bytes: 90,
                records: 2
            }
        );
        assert_eq!(
            ledger.release("cache-a").unwrap(),
            QuotaAllocation::one_record(QuotaBucket::Cache, 40)
        );
        assert_eq!(
            ledger.usage(QuotaBucket::Cache),
            QuotaUsage {
                bytes: 50,
                records: 1
            }
        );
    }

    #[test]
    fn quota_exhaustion_fails_before_mutating_usage() {
        let mut ledger = QuotaLedger::new();
        ledger.set_limit(QuotaBucket::Mailbox, QuotaLimit::new(10, 1));
        ledger
            .reserve(
                "mail-a",
                QuotaAllocation::one_record(QuotaBucket::Mailbox, 8),
            )
            .unwrap(); // ubs:ignore - test oracle

        let err = ledger
            .reserve(
                "mail-b",
                QuotaAllocation::one_record(QuotaBucket::Mailbox, 1),
            )
            .unwrap_err(); // ubs:ignore - test oracle

        assert!(matches!(
            err,
            QuotaError::Exhausted {
                bucket: QuotaBucket::Mailbox,
                ..
            }
        ));
        assert_eq!(
            ledger.usage(QuotaBucket::Mailbox),
            QuotaUsage {
                bytes: 8,
                records: 1
            }
        );
        assert_eq!(ledger.allocation_count(), 1);
    }

    #[test]
    fn retention_uses_explicit_expiry_and_max_age() {
        let mut policy = RetentionPolicy::daemon_defaults();
        policy.set_rule(QuotaBucket::Traces, RetentionRule::max_age(10));
        let records = vec![
            RetentionRecord {
                record_id: "new".to_string(),
                bucket: QuotaBucket::Traces,
                clock: RetentionClock::new(0, 95),
            },
            RetentionRecord {
                record_id: "old".to_string(),
                bucket: QuotaBucket::Traces,
                clock: RetentionClock::new(0, 80),
            },
            RetentionRecord {
                record_id: "explicit".to_string(),
                bucket: QuotaBucket::Mailbox,
                clock: RetentionClock::new(0, 99).with_expiry(100),
            },
        ];

        assert_eq!(
            policy.expired_records(&records, 100),
            vec!["explicit".to_string(), "old".to_string()]
        );
    }

    #[test]
    fn retention_enforces_max_records_by_oldest_update() {
        let mut policy = RetentionPolicy::daemon_defaults();
        policy.set_rule(
            QuotaBucket::Diagnostics,
            RetentionRule {
                max_age_secs: None,
                max_records: Some(2),
                honor_explicit_expiry: true,
            },
        );
        let records = vec![
            RetentionRecord {
                record_id: "middle".to_string(),
                bucket: QuotaBucket::Diagnostics,
                clock: RetentionClock::new(0, 20),
            },
            RetentionRecord {
                record_id: "oldest".to_string(),
                bucket: QuotaBucket::Diagnostics,
                clock: RetentionClock::new(0, 10),
            },
            RetentionRecord {
                record_id: "newest".to_string(),
                bucket: QuotaBucket::Diagnostics,
                clock: RetentionClock::new(0, 30),
            },
        ];

        assert_eq!(
            policy.expired_records(&records, 100),
            vec!["oldest".to_string()]
        );
    }

    #[test]
    fn retention_max_records_ignores_already_expired_records() {
        let mut policy = RetentionPolicy::daemon_defaults();
        policy.set_rule(
            QuotaBucket::Mailbox,
            RetentionRule {
                max_age_secs: None,
                max_records: Some(1),
                honor_explicit_expiry: true,
            },
        );
        let records = vec![
            RetentionRecord {
                record_id: "live-older".to_string(),
                bucket: QuotaBucket::Mailbox,
                clock: RetentionClock::new(0, 10),
            },
            RetentionRecord {
                record_id: "expired-newer".to_string(),
                bucket: QuotaBucket::Mailbox,
                clock: RetentionClock::new(0, 30).with_expiry(100),
            },
        ];

        assert_eq!(
            policy.expired_records(&records, 100),
            vec!["expired-newer".to_string()]
        );
    }

    #[test]
    fn quota_rows_are_deterministic() {
        let mut ledger = QuotaLedger::new();
        ledger.set_limit(QuotaBucket::Traces, QuotaLimit::new(10, 10));
        ledger.set_limit(QuotaBucket::Cache, QuotaLimit::new(20, 20));

        let rows = ledger.rows();
        assert_eq!(rows[0].bucket, QuotaBucket::Cache);
        assert_eq!(rows[1].bucket, QuotaBucket::Traces);
    }
}