budgetkernel 0.1.2

A small, auditable, deterministic budget accounting kernel with zero heap allocation on the hot path.
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
//! The budget itself.
//!
//! A [`Budget`] is constructed via [`Budget::builder`] and mutated by
//! repeated [`charge`](Budget::charge) calls. Each charge returns a
//! [`Verdict`] describing the outcome.
//!
//! Internally, a `Budget` holds three internal fixed-capacity maps: one
//! for configured limits, one for configured warn thresholds, and one
//! for running spent totals. Presence in the limits map is the
//! canonical indicator that a dimension is declared in this budget.

use crate::fixed_map::FixedMap;
use crate::{BuilderError, ChargeError, Dim, Verdict};

/// A declared budget with running spent counters.
///
/// Construct via [`Budget::builder`]. See module docs for the full
/// lifecycle.
#[derive(Debug)]
pub struct Budget {
    /// Per-dimension limits. Presence == dimension is declared.
    pub(crate) limits: FixedMap,

    /// Per-dimension warn thresholds. Presence == this dimension has a
    /// warn threshold configured (not all declared dimensions must).
    pub(crate) warn_thresholds: FixedMap,

    /// Per-dimension running totals. Presence == this dimension has
    /// been charged at least once. Absence means "never charged";
    /// `get_or(dim, 0)` gives the effective value on the hot path.
    pub(crate) spent: FixedMap,
}

/// Builder for a [`Budget`].
///
/// Accumulates per-dimension configuration and validates it in
/// [`BudgetBuilder::build`]. Duplicate dimensions, zero limits, warn
/// thresholds that do not fire strictly before exhaustion, and empty
/// budgets are all rejected structurally.
///
/// If the same dimension is declared more than once, the builder
/// records the duplicate and [`build`](Self::build) will return
/// [`BuilderError::DuplicateDimension`]. The internal state of the
/// builder after a duplicate declaration is unspecified - callers
/// must not rely on which declaration's values are stored, because
/// `build` will not produce a valid budget from a duplicate
/// declaration regardless.
#[derive(Debug)]
pub struct BudgetBuilder {
    limits: FixedMap,
    warn_thresholds: FixedMap,
    /// Set on any attempt to declare a dimension that was already
    /// declared. Reported by `build` if present. We defer the error
    /// until `build` so the builder type can remain a simple
    /// fluent-chain shape without `Result` at every step.
    duplicate: Option<Dim>,
}

impl BudgetBuilder {
    /// Internal constructor. Use [`Budget::builder`].
    const fn new() -> Self {
        Self {
            limits: FixedMap::new(),
            warn_thresholds: FixedMap::new(),
            duplicate: None,
        }
    }

    /// Declare a dimension with a limit and no warn threshold.
    ///
    /// Charging the dimension will return [`Verdict::Continue`] for
    /// every charge where the running total stays at or below `limit`,
    /// and [`Verdict::Exhausted`] once the total exceeds it.
    ///
    /// Declaring the same dimension twice is a structural error
    /// reported by [`build`](Self::build).
    #[must_use]
    pub fn limit(mut self, dim: Dim, limit: u64) -> Self {
        if self.limits.contains(dim) && self.duplicate.is_none() {
            self.duplicate = Some(dim);
        }
        // Discard: duplicate tracking is handled explicitly above; the
        // previous stored value is not part of the builder's public contract.
        let _ = self.limits.insert(dim, limit);
        self
    }

    /// Declare a dimension with a limit and an absolute warn threshold.
    ///
    /// `warn` must be strictly less than `limit` or [`build`](Self::build)
    /// will return [`BuilderError::WarnNotBelowLimit`]. Charging the
    /// dimension returns [`Verdict::Warn`] on any charge where the
    /// running total exceeds `warn` but has not yet exceeded `limit`.
    ///
    /// A `warn` threshold of zero is valid: it causes [`Verdict::Warn`]
    /// to be returned on any charge where `spent` becomes positive.
    /// A zero-amount charge against a fresh `warn = 0` budget returns
    /// [`Verdict::Continue`] because the comparison is strict
    /// (`spent > warn`). After any positive spend, a zero-amount charge
    /// reports [`Verdict::Warn`] because the current spent value remains
    /// above the threshold.
    ///
    /// Declaring the same dimension twice is a structural error
    /// reported by [`build`](Self::build).
    #[must_use]
    pub fn limit_with_warn(mut self, dim: Dim, limit: u64, warn: u64) -> Self {
        if self.limits.contains(dim) && self.duplicate.is_none() {
            self.duplicate = Some(dim);
        }
        // Discard: duplicate tracking is handled explicitly above; the
        // previous stored value is not part of the builder's public contract.
        let _ = self.limits.insert(dim, limit);
        // Discard: duplicate tracking is handled by the limit declaration;
        // a duplicate builder cannot produce a valid budget.
        let _ = self.warn_thresholds.insert(dim, warn);
        self
    }

    /// Finalize the builder into a [`Budget`].
    ///
    /// Validation order is fixed and documented: duplicate declarations
    /// first, then the no-dimensions check, then per-dimension checks
    /// in [`Dim::ALL`] order (zero-limit before warn-not-below-limit).
    ///
    /// # Errors
    ///
    /// Returns [`BuilderError`] on any structural problem with the
    /// declaration. All errors are non-recoverable; they indicate a
    /// bug in the caller's declaration code.
    pub fn build(self) -> Result<Budget, BuilderError> {
        if let Some(dim) = self.duplicate {
            return Err(BuilderError::DuplicateDimension(dim));
        }
        if self.limits.is_empty() {
            return Err(BuilderError::NoDimensions);
        }
        debug_assert!(
            Dim::ALL
                .iter()
                .all(|d| !self.warn_thresholds.contains(*d) || self.limits.contains(*d)),
            "warn_thresholds invariant violated: a warn threshold exists for an undeclared dimension"
        );
        for dim in Dim::ALL {
            if let Some(limit) = self.limits.get(dim) {
                if limit == 0 {
                    return Err(BuilderError::ZeroLimit(dim));
                }
                if let Some(warn) = self.warn_thresholds.get(dim) {
                    if warn >= limit {
                        return Err(BuilderError::WarnNotBelowLimit(dim));
                    }
                }
            }
        }
        Ok(Budget {
            limits: self.limits,
            warn_thresholds: self.warn_thresholds,
            spent: FixedMap::new(),
        })
    }
}

impl Budget {
    /// Begin constructing a [`Budget`].
    #[must_use]
    pub const fn builder() -> BudgetBuilder {
        BudgetBuilder::new()
    }

    /// Charge `amount` against `dim` and return the resulting verdict.
    ///
    /// # Semantics
    ///
    /// - **Inclusive limits.** A charge that brings `spent` exactly to
    ///   `limit` returns [`Verdict::Continue`] (or [`Verdict::Warn`] if
    ///   a warn threshold is configured and crossed). A charge that
    ///   brings `spent` to `limit + 1` or beyond returns
    ///   [`Verdict::Exhausted`].
    /// - **Saturating arithmetic.** If `current_spent + amount` would
    ///   overflow `u64`, the running total saturates at `u64::MAX`.
    ///   Saturation never panics.
    /// - **Exhaustion wins over warn.** If a single charge crosses both
    ///   the warn threshold and the limit, [`Verdict::Exhausted`] is
    ///   returned; `Warn` is not reported for that charge.
    /// - **Spent is always updated.** Even on exhaustion, `spent` is
    ///   updated to reflect the attempted charge (saturated). This lets
    ///   callers query [`remaining`](Self::remaining) or a snapshot to
    ///   diagnose overruns.
    ///
    /// ## Zero-amount charges
    ///
    /// Charging zero units is valid. It returns the verdict for the
    /// current accounting state without increasing the reported spent
    /// value. This is useful as a state poll: `charge(dim, 0)` tells
    /// callers whether the budget is currently continuing, warning, or
    /// exhausted without consuming additional budget.
    ///
    /// # Errors
    ///
    /// Returns [`ChargeError::UnknownDimension`] if `dim` was not
    /// declared in this budget.
    pub fn charge(&mut self, dim: Dim, amount: u64) -> Result<Verdict, ChargeError> {
        let Some(limit) = self.limits.get(dim) else {
            return Err(ChargeError::UnknownDimension(dim));
        };
        let current = self.spent.get_or(dim, 0);
        let new_spent = current.saturating_add(amount);
        // Discard: the previous spent value was already read above via
        // `get_or`; this write commits the new saturated total.
        let _ = self.spent.insert(dim, new_spent);

        if new_spent > limit {
            return Ok(Verdict::Exhausted(dim));
        }
        if let Some(warn) = self.warn_thresholds.get(dim) {
            if new_spent > warn {
                return Ok(Verdict::Warn(dim));
            }
        }
        Ok(Verdict::Continue)
    }

    /// Return the remaining headroom for `dim`, i.e. `limit - spent`
    /// saturating at zero. Returns `None` if the dimension was not
    /// declared.
    #[must_use]
    pub fn remaining(&self, dim: Dim) -> Option<u64> {
        let limit = self.limits.get(dim)?;
        let spent = self.spent.get_or(dim, 0);
        Some(limit.saturating_sub(spent))
    }

    /// Return the amount already charged against `dim`. Returns `None`
    /// if the dimension was not declared, and `Some(0)` if the dimension
    /// was declared but never charged.
    #[must_use]
    pub fn spent(&self, dim: Dim) -> Option<u64> {
        if self.limits.contains(dim) {
            Some(self.spent.get_or(dim, 0))
        } else {
            None
        }
    }

    /// Zero all spent counters, preserving the declared limits and warn
    /// thresholds. Useful for per-request budget reuse across a long-
    /// lived connection or worker.
    pub fn reset(&mut self) {
        self.spent = FixedMap::new();
    }
}

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

    fn build_ok(builder: BudgetBuilder) -> Option<Budget> {
        let result = builder.build();
        assert!(result.is_ok());
        result.ok()
    }

    fn build_err(builder: BudgetBuilder) -> Option<BuilderError> {
        let result = builder.build();
        assert!(result.is_err());
        result.err()
    }

    #[test]
    fn build_rejects_empty_budget() {
        assert_eq!(
            build_err(Budget::builder()),
            Some(BuilderError::NoDimensions)
        );
    }

    #[test]
    fn build_rejects_zero_limit() {
        assert_eq!(
            build_err(Budget::builder().limit(Dim::Tokens, 0)),
            Some(BuilderError::ZeroLimit(Dim::Tokens))
        );
    }

    #[test]
    fn build_rejects_warn_equal_to_limit() {
        assert_eq!(
            build_err(Budget::builder().limit_with_warn(Dim::Tokens, 100, 100)),
            Some(BuilderError::WarnNotBelowLimit(Dim::Tokens))
        );
    }

    #[test]
    fn build_rejects_warn_above_limit() {
        assert_eq!(
            build_err(Budget::builder().limit_with_warn(Dim::Tokens, 100, 101)),
            Some(BuilderError::WarnNotBelowLimit(Dim::Tokens))
        );
    }

    #[test]
    fn build_rejects_duplicate_declaration() {
        assert_eq!(
            build_err(
                Budget::builder()
                    .limit(Dim::Tokens, 100)
                    .limit(Dim::Tokens, 200),
            ),
            Some(BuilderError::DuplicateDimension(Dim::Tokens))
        );
    }

    #[test]
    fn build_accepts_single_dimension() {
        let Some(budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        assert_eq!(budget.remaining(Dim::Tokens), Some(100));
    }

    #[test]
    fn build_accepts_all_dimensions() {
        let mut builder = Budget::builder();
        for dim in Dim::ALL {
            builder = builder.limit(dim, 1);
        }
        let Some(mut budget) = build_ok(builder) else {
            return;
        };
        for dim in Dim::ALL {
            assert_eq!(budget.charge(dim, 1), Ok(Verdict::Continue));
        }
    }

    #[test]
    fn charge_unknown_dim_returns_error() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        assert_eq!(
            budget.charge(Dim::Bytes, 1),
            Err(ChargeError::UnknownDimension(Dim::Bytes))
        );
    }

    #[test]
    fn charge_below_limit_returns_continue() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        assert_eq!(budget.charge(Dim::Tokens, 50), Ok(Verdict::Continue));
    }

    #[test]
    fn charge_exactly_to_limit_returns_continue() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        assert_eq!(budget.charge(Dim::Tokens, 100), Ok(Verdict::Continue));
    }

    #[test]
    fn charge_one_past_limit_returns_exhausted() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        assert_eq!(
            budget.charge(Dim::Tokens, 101),
            Ok(Verdict::Exhausted(Dim::Tokens))
        );
    }

    #[test]
    fn charge_incremental_to_exact_limit_returns_continue() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        assert_eq!(budget.charge(Dim::Tokens, 40), Ok(Verdict::Continue));
        assert_eq!(budget.charge(Dim::Tokens, 60), Ok(Verdict::Continue));
    }

    #[test]
    fn charge_incremental_past_limit_returns_exhausted() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        assert_eq!(budget.charge(Dim::Tokens, 40), Ok(Verdict::Continue));
        assert_eq!(
            budget.charge(Dim::Tokens, 61),
            Ok(Verdict::Exhausted(Dim::Tokens))
        );
    }

    #[test]
    fn charge_crosses_warn_returns_warn() {
        let Some(mut budget) = build_ok(Budget::builder().limit_with_warn(Dim::Tokens, 100, 80))
        else {
            return;
        };
        assert_eq!(
            budget.charge(Dim::Tokens, 81),
            Ok(Verdict::Warn(Dim::Tokens))
        );
    }

    #[test]
    fn warn_fires_every_call_above_threshold() {
        let Some(mut budget) = build_ok(Budget::builder().limit_with_warn(Dim::Tokens, 100, 80))
        else {
            return;
        };
        assert_eq!(
            budget.charge(Dim::Tokens, 81),
            Ok(Verdict::Warn(Dim::Tokens))
        );
        assert_eq!(
            budget.charge(Dim::Tokens, 1),
            Ok(Verdict::Warn(Dim::Tokens))
        );
    }

    #[test]
    fn charge_past_limit_reports_exhausted_not_warn() {
        let Some(mut budget) = build_ok(Budget::builder().limit_with_warn(Dim::Tokens, 100, 80))
        else {
            return;
        };
        assert_eq!(
            budget.charge(Dim::Tokens, 101),
            Ok(Verdict::Exhausted(Dim::Tokens))
        );
    }

    #[test]
    fn charge_saturates_at_u64_max() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, u64::MAX - 1)) else {
            return;
        };
        assert_eq!(
            budget.charge(Dim::Tokens, u64::MAX - 1),
            Ok(Verdict::Continue)
        );
        assert_eq!(
            budget.charge(Dim::Tokens, 10),
            Ok(Verdict::Exhausted(Dim::Tokens))
        );
        assert_eq!(budget.spent(Dim::Tokens), Some(u64::MAX));
    }

    #[test]
    fn charge_independent_across_dimensions() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100).limit_with_warn(
            Dim::Bytes,
            1000,
            500,
        )) else {
            return;
        };
        assert_eq!(budget.charge(Dim::Tokens, 100), Ok(Verdict::Continue));
        assert_eq!(
            budget.charge(Dim::Bytes, 600),
            Ok(Verdict::Warn(Dim::Bytes))
        );
        assert_eq!(budget.spent(Dim::Tokens), Some(100));
        assert_eq!(budget.spent(Dim::Bytes), Some(600));
    }

    #[test]
    fn remaining_reports_headroom() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        assert_eq!(budget.charge(Dim::Tokens, 30), Ok(Verdict::Continue));
        assert_eq!(budget.remaining(Dim::Tokens), Some(70));
    }

    #[test]
    fn remaining_saturates_at_zero_on_exhaustion() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        assert_eq!(
            budget.charge(Dim::Tokens, 200),
            Ok(Verdict::Exhausted(Dim::Tokens))
        );
        assert_eq!(budget.remaining(Dim::Tokens), Some(0));
    }

    #[test]
    fn remaining_none_for_undeclared_dim() {
        let Some(budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        assert_eq!(budget.remaining(Dim::Bytes), None);
    }

    #[test]
    fn spent_reports_zero_before_first_charge() {
        let Some(budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        assert_eq!(budget.spent(Dim::Tokens), Some(0));
    }

    #[test]
    fn spent_none_for_undeclared_dim() {
        let Some(budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        assert_eq!(budget.spent(Dim::Bytes), None);
    }

    #[test]
    fn reset_zeros_spent_preserves_limits() {
        let Some(mut budget) = build_ok(Budget::builder().limit_with_warn(Dim::Tokens, 100, 80))
        else {
            return;
        };
        assert_eq!(
            budget.charge(Dim::Tokens, 90),
            Ok(Verdict::Warn(Dim::Tokens))
        );
        budget.reset();
        assert_eq!(budget.spent(Dim::Tokens), Some(0));
        assert_eq!(budget.remaining(Dim::Tokens), Some(100));
        assert_eq!(
            budget.charge(Dim::Tokens, 100),
            Ok(Verdict::Warn(Dim::Tokens))
        );
    }

    #[test]
    fn reset_on_fresh_budget_is_noop() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };
        budget.reset();
        assert_eq!(budget.spent(Dim::Tokens), Some(0));
        assert_eq!(budget.remaining(Dim::Tokens), Some(100));
    }

    #[test]
    fn charge_zero_polls_state_without_consuming_budget() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };

        assert_eq!(budget.charge(Dim::Tokens, 50), Ok(Verdict::Continue));
        assert_eq!(budget.charge(Dim::Tokens, 0), Ok(Verdict::Continue));
        assert_eq!(budget.spent(Dim::Tokens), Some(50));
    }

    #[test]
    fn charge_zero_reports_exhausted_when_already_exhausted() {
        let Some(mut budget) = build_ok(Budget::builder().limit(Dim::Tokens, 100)) else {
            return;
        };

        assert_eq!(
            budget.charge(Dim::Tokens, 200),
            Ok(Verdict::Exhausted(Dim::Tokens))
        );
        assert_eq!(
            budget.charge(Dim::Tokens, 0),
            Ok(Verdict::Exhausted(Dim::Tokens))
        );
        assert_eq!(budget.spent(Dim::Tokens), Some(200));
    }

    #[test]
    fn warn_zero_fires_on_positive_charge_and_zero_poll_afterward() {
        let Some(mut budget) = build_ok(Budget::builder().limit_with_warn(Dim::Tokens, 100, 0))
        else {
            return;
        };

        assert_eq!(budget.charge(Dim::Tokens, 0), Ok(Verdict::Continue));
        assert_eq!(
            budget.charge(Dim::Tokens, 1),
            Ok(Verdict::Warn(Dim::Tokens))
        );
        assert_eq!(
            budget.charge(Dim::Tokens, 0),
            Ok(Verdict::Warn(Dim::Tokens))
        );
    }
}