codewhale_runtime/context_budget.rs
1//! Unified context-budget math for the TUI.
2//!
3//! Given a model's context window, the current input token estimate, and a
4//! configured output cap, [`ContextBudget`] derives the four numbers the rest
5//! of the app needs to reason about a turn:
6//!
7//! * **available input budget** — how many input tokens may still be spent
8//! after reserving room for the model's output;
9//! * **output token cap** — the output reservation actually used to compute
10//! that budget (clamped so it never starves the window);
11//! * **compaction trigger** — the input-token level at which compaction
12//! should be suggested (default: ~75% of the full window, clamped to the
13//! spendable input ceiling);
14//! * **[`PressureLevel`]** — a coarse Low/Medium/High/Critical signal the UI
15//! can render without re-deriving thresholds.
16//!
17//! This module is the budget-math *foundation*. It is intentionally pure (no
18//! I/O, no clock, no engine/config types) so it can be unit-tested in isolation.
19//! Its consumers live outside it and call in, never the reverse: `route_budget`
20//! and `core::engine::context` for [`ContextBudget`], `context_report` for
21//! [`PressureLevel`].
22//!
23//! ### Route ceilings stay independent
24//!
25//! A route can publish a total context window, an output ceiling, and a
26//! separate input ceiling. [`ContextBudget::new_with_input_limit`] intersects
27//! all three without treating one as a substitute for another: the output
28//! reservation is the route-effective wire request (a quarter of the window, at
29//! most 64K and never more than that request, on a route that shares one window
30//! between input and output), the total-window
31//! arithmetic remains saturating, and a concrete input limit clamps the final
32//! spendable ceiling and compaction trigger.
33
34pub const CONTEXT_WARNING_THRESHOLD_PERCENT: f64 = 85.0;
35pub const CONTEXT_CRITICAL_THRESHOLD_PERCENT: f64 = 95.0;
36
37// This module IS wired. `ContextBudget` is consumed by `route_budget.rs` and
38// `core/engine/context.rs`; `PressureLevel` by `context_report.rs`. It sits on
39// the do-not-delete list in AGENTS.md because a blanket `allow(dead_code)` here,
40// plus a comment that used to claim the module was "not yet referenced," taught
41// several dead-code audits to propose deleting a live file. The suppression is
42// now `#[cfg_attr(not(test), expect(dead_code))]` on the three methods unused
43// outside tests, so a suppression that stops matching the lint fails the
44// build instead of hiding behind a module-wide waiver. Tests call them, so
45// a bare `#[expect(dead_code)]` would be unfulfilled under `cfg(test)`.
46
47/// Fraction of the window, expressed as a percentage, at or above which
48/// compaction should be suggested. Mirrors the "high" pressure boundary the
49/// existing context report uses for its diagnostic label, rounded up to the
50/// conventional three-quarters-full trigger.
51pub const DEFAULT_COMPACTION_TRIGGER_PERCENT: f64 = 75.0;
52
53/// Percentage of the window at or above which pressure is [`PressureLevel::Critical`].
54pub const CRITICAL_PRESSURE_PERCENT: f64 = 90.0;
55
56/// Percentage of the window at or above which pressure is [`PressureLevel::High`].
57/// Pressure and the requested compaction trigger are window-relative. The
58/// trigger is clamped to the spendable input ceiling, so it can fire before
59/// this UI boundary when output reservation consumes substantial window space.
60pub const HIGH_PRESSURE_PERCENT: f64 = DEFAULT_COMPACTION_TRIGGER_PERCENT;
61
62/// Percentage of the window at or above which pressure is [`PressureLevel::Medium`].
63/// Matches the "moderate" boundary of the existing diagnostic report.
64pub const MEDIUM_PRESSURE_PERCENT: f64 = 40.0;
65
66/// Safety headroom (tokens) subtracted from the window in addition to the
67/// reserved output, to avoid bumping a provider's hard limit. Matches the
68/// engine's `CONTEXT_HEADROOM_TOKENS`.
69pub const CONTEXT_HEADROOM_TOKENS: u64 = 1_024;
70
71/// Smallest input budget (tokens) [`ContextBudget`] will report for any window
72/// large enough to hold it. The output cap is clamped down as needed to
73/// preserve this much input room, so a generous configured output cap can never
74/// drive the available input budget to zero on a usable window.
75pub const MIN_INPUT_BUDGET_TOKENS: u64 = 1_024;
76
77/// Coarse, UI-facing description of how full the context window is.
78///
79/// Ordered from least to most pressure so the variants can be compared
80/// (`level >= PressureLevel::High`) and so the derived `Ord` matches intuition.
81#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
82pub enum PressureLevel {
83 /// Plenty of room; nothing to surface.
84 Low,
85 /// Noticeably filling up; informational.
86 Medium,
87 /// At or past the compaction trigger; suggest compaction.
88 High,
89 /// Near the window limit; compaction/clear is urgent.
90 Critical,
91}
92
93impl PressureLevel {
94 /// Classify a window-usage percentage (0.0..=100.0) into a level.
95 ///
96 /// Inputs outside the range are clamped, so callers may pass a raw
97 /// percentage without pre-validating it.
98 #[must_use]
99 pub fn from_usage_percent(percent: f64) -> Self {
100 let percent = percent.clamp(0.0, 100.0);
101 if percent >= CRITICAL_PRESSURE_PERCENT {
102 PressureLevel::Critical
103 } else if percent >= HIGH_PRESSURE_PERCENT {
104 PressureLevel::High
105 } else if percent >= MEDIUM_PRESSURE_PERCENT {
106 PressureLevel::Medium
107 } else {
108 PressureLevel::Low
109 }
110 }
111
112 /// Lowercase, stable label suitable for status lines and logs.
113 ///
114 /// Kept aligned with the existing context-report vocabulary
115 /// (`low`/`moderate`/`high`/`critical`).
116 #[must_use]
117 pub const fn label(self) -> &'static str {
118 match self {
119 PressureLevel::Low => "low",
120 PressureLevel::Medium => "moderate",
121 PressureLevel::High => "high",
122 PressureLevel::Critical => "critical",
123 }
124 }
125
126 /// Whether this level is at or past the point where compaction should be
127 /// suggested to the user.
128 ///
129 /// Unused by the engine today; kept as the pressure-level counterpart of
130 /// `ContextBudget::should_compact` so both live next to their thresholds.
131 #[must_use]
132 pub const fn suggests_compaction(self) -> bool {
133 matches!(self, PressureLevel::High | PressureLevel::Critical)
134 }
135}
136
137/// A computed snapshot of how a turn's input sits against a model's context
138/// window, plus the derived output cap, compaction trigger, and pressure level.
139///
140/// Construct via [`ContextBudget::new`]. All fields are token counts unless the
141/// name says otherwise. The struct is `Copy` and holds no borrowed data so it
142/// can be cached on UI state cheaply.
143#[derive(Debug, Clone, Copy, PartialEq, Eq)]
144pub struct ContextBudget {
145 /// Total context window for the active route (input + output), in tokens.
146 pub window_tokens: u64,
147 /// Current estimated input tokens already committed to the turn.
148 pub input_tokens: u64,
149 /// Output tokens reserved (and thus the effective output cap) for the turn.
150 /// Derived from the configured cap, clamped to fit the window while leaving
151 /// at least [`MIN_INPUT_BUDGET_TOKENS`] of input room.
152 pub output_cap_tokens: u64,
153 /// Spendable input ceiling for the turn (`window - output_cap - headroom`,
154 /// saturating at 0). Compaction percentages use the full window and clamp
155 /// their resulting token threshold to this ceiling.
156 pub input_budget_ceiling: u64,
157 /// Input tokens still available before hitting the reserved boundary
158 /// (`input_budget_ceiling - input`, saturating at 0).
159 pub available_input_tokens: u64,
160 /// Input-token level at or above which compaction should be suggested
161 /// (`DEFAULT_COMPACTION_TRIGGER_PERCENT` of the window, clamped to the
162 /// input budget ceiling).
163 pub compaction_trigger_tokens: u64,
164 /// Coarse pressure level derived from window usage.
165 pub pressure: PressureLevel,
166}
167
168impl ContextBudget {
169 /// Build a budget snapshot for a route.
170 ///
171 /// * `window_tokens` — the route-effective context window (input + output).
172 /// * `input_tokens` — current estimated input tokens for the turn.
173 /// * `configured_output_cap` — the route-effective output request the
174 /// caller needs to reserve. It is clamped down so it never consumes the
175 /// headroom or the minimum input budget; on a window too small to hold
176 /// even the minimum input budget plus headroom, the cap collapses to
177 /// whatever is left (possibly zero).
178 ///
179 /// Never panics and never underflows: all arithmetic saturates.
180 #[must_use]
181 pub fn new(window_tokens: u64, input_tokens: u64, configured_output_cap: u64) -> Self {
182 Self::new_with_input_limit(window_tokens, input_tokens, configured_output_cap, None)
183 }
184
185 /// Build a budget snapshot and intersect it with a provider's independent
186 /// hard input ceiling when one is published by the resolved route.
187 #[must_use]
188 pub fn new_with_input_limit(
189 window_tokens: u64,
190 input_tokens: u64,
191 configured_output_cap: u64,
192 input_limit_tokens: Option<u64>,
193 ) -> Self {
194 let output_cap_tokens = clamp_output_cap(window_tokens, configured_output_cap);
195
196 // Reserve output + safety headroom; whatever remains is spendable input.
197 let reserved = output_cap_tokens.saturating_add(CONTEXT_HEADROOM_TOKENS);
198 let window_input_ceiling = window_tokens.saturating_sub(reserved);
199 let input_budget_ceiling = input_limit_tokens
200 .filter(|limit| *limit > 0)
201 .map_or(window_input_ceiling, |limit| {
202 window_input_ceiling.min(limit)
203 });
204 let available_input_tokens = input_budget_ceiling.saturating_sub(input_tokens);
205
206 let compaction_trigger_tokens =
207 percent_of(window_tokens, DEFAULT_COMPACTION_TRIGGER_PERCENT).min(input_budget_ceiling);
208
209 let pressure =
210 PressureLevel::from_usage_percent(usage_percent(window_tokens, input_tokens));
211
212 ContextBudget {
213 window_tokens,
214 input_tokens,
215 output_cap_tokens,
216 input_budget_ceiling,
217 available_input_tokens,
218 compaction_trigger_tokens,
219 pressure,
220 }
221 }
222
223 /// Derive a compaction trigger from a window percentage.
224 ///
225 /// The percentage means what the user-facing context meter says it means:
226 /// a fraction of the full route window (`80` on a 1M window is 800K input
227 /// tokens before the route ceiling clamp). One internal clamp applies:
228 ///
229 /// ```text
230 /// trigger = min(window × percent, window − output reservation − headroom)
231 /// ```
232 ///
233 /// so a late percentage can never push the trigger past the spendable
234 /// input ceiling and overflow the provider window. Previously the
235 /// percentage was applied to the ceiling itself, which silently pulled an
236 /// "80%" setting down to ~60% of the window on routes with large output
237 /// reservations; UI surfaces should disclose the clamp when it engages
238 /// instead of redefining what the percentage means.
239 #[must_use]
240 pub fn compaction_trigger_for_percent(&self, percent: f64) -> u64 {
241 percent_of(self.window_tokens, percent).min(self.input_budget_ceiling)
242 }
243
244 /// Fraction of the window currently consumed by input, as a percentage in
245 /// `0.0..=100.0`. A zero window reports `0.0` rather than dividing by zero.
246 #[must_use]
247 pub fn usage_percent(&self) -> f64 {
248 usage_percent(self.window_tokens, self.input_tokens)
249 }
250
251 /// Whether current input has reached the compaction trigger and compaction
252 /// should be suggested.
253 #[must_use]
254 pub fn should_compact(&self) -> bool {
255 self.window_tokens > 0 && self.input_tokens >= self.compaction_trigger_tokens
256 }
257
258 /// Whether another `additional_input_tokens` of input would fit within the
259 /// available budget (i.e. not exceed the reserved boundary).
260 #[must_use]
261 pub fn fits_additional(&self, additional_input_tokens: u64) -> bool {
262 additional_input_tokens <= self.available_input_tokens
263 }
264}
265
266/// Clamp a desired output cap so it fits the window while preserving at least
267/// [`MIN_INPUT_BUDGET_TOKENS`] of input room plus [`CONTEXT_HEADROOM_TOKENS`].
268///
269/// On a window too small to hold even that floor, returns whatever room is left
270/// after the headroom (possibly zero) rather than underflowing.
271fn clamp_output_cap(window_tokens: u64, configured_output_cap: u64) -> u64 {
272 // The most output we can reserve and still keep the input floor + headroom.
273 let reserved_floor = MIN_INPUT_BUDGET_TOKENS.saturating_add(CONTEXT_HEADROOM_TOKENS);
274 let max_output = window_tokens.saturating_sub(reserved_floor);
275 configured_output_cap.min(max_output)
276}
277
278/// Window usage as a percentage in `0.0..=100.0`. Zero window -> `0.0`.
279pub fn usage_percent(window_tokens: u64, input_tokens: u64) -> f64 {
280 if window_tokens == 0 {
281 return 0.0;
282 }
283 ((input_tokens as f64 / window_tokens as f64) * 100.0).clamp(0.0, 100.0)
284}
285
286/// `percent`% of `window_tokens`, rounded to the nearest token. Saturates at
287/// `u64::MAX` and treats out-of-range percentages by clamping to `0.0..=100.0`.
288fn percent_of(window_tokens: u64, percent: f64) -> u64 {
289 let percent = percent.clamp(0.0, 100.0);
290 let value = (window_tokens as f64) * (percent / 100.0);
291 // `as u64` saturates on overflow and floors; add 0.5 to round to nearest.
292 (value + 0.5) as u64
293}
294
295#[cfg(test)]
296mod tests {
297 use super::*;
298
299 /// A representative spread of real-world windows: a tight self-hosted
300 /// deployment, common provider sizes, and a V4-class 1M window.
301 const WINDOWS: &[u64] = &[8_192, 32_768, 131_072, 262_144, 1_048_576];
302
303 // -- PressureLevel boundaries ------------------------------------------
304
305 #[test]
306 fn pressure_level_boundaries_are_inclusive_lower_bounds() {
307 assert_eq!(PressureLevel::from_usage_percent(0.0), PressureLevel::Low);
308 assert_eq!(PressureLevel::from_usage_percent(39.9), PressureLevel::Low);
309 // 40% is the moderate boundary.
310 assert_eq!(
311 PressureLevel::from_usage_percent(40.0),
312 PressureLevel::Medium
313 );
314 assert_eq!(
315 PressureLevel::from_usage_percent(74.9),
316 PressureLevel::Medium
317 );
318 // 75% is the high / compaction boundary.
319 assert_eq!(PressureLevel::from_usage_percent(75.0), PressureLevel::High);
320 assert_eq!(PressureLevel::from_usage_percent(89.9), PressureLevel::High);
321 // 90% is the critical boundary.
322 assert_eq!(
323 PressureLevel::from_usage_percent(90.0),
324 PressureLevel::Critical
325 );
326 assert_eq!(
327 PressureLevel::from_usage_percent(100.0),
328 PressureLevel::Critical
329 );
330 }
331
332 #[test]
333 fn pressure_level_clamps_out_of_range_inputs() {
334 assert_eq!(PressureLevel::from_usage_percent(-10.0), PressureLevel::Low);
335 assert_eq!(
336 PressureLevel::from_usage_percent(150.0),
337 PressureLevel::Critical
338 );
339 assert_eq!(
340 PressureLevel::from_usage_percent(f64::INFINITY),
341 PressureLevel::Critical
342 );
343 }
344
345 #[test]
346 fn pressure_level_ordering_and_helpers() {
347 assert!(PressureLevel::Low < PressureLevel::Medium);
348 assert!(PressureLevel::Medium < PressureLevel::High);
349 assert!(PressureLevel::High < PressureLevel::Critical);
350
351 assert!(!PressureLevel::Low.suggests_compaction());
352 assert!(!PressureLevel::Medium.suggests_compaction());
353 assert!(PressureLevel::High.suggests_compaction());
354 assert!(PressureLevel::Critical.suggests_compaction());
355
356 assert_eq!(PressureLevel::Low.label(), "low");
357 assert_eq!(PressureLevel::Medium.label(), "moderate");
358 assert_eq!(PressureLevel::High.label(), "high");
359 assert_eq!(PressureLevel::Critical.label(), "critical");
360 }
361
362 // -- Compaction trigger -------------------------------------------------
363
364 #[test]
365 fn compaction_trigger_is_window_percent_clamped_to_input_ceiling() {
366 for &window in WINDOWS {
367 let budget = ContextBudget::new(window, 0, 64_000);
368 let expected = percent_of(window, DEFAULT_COMPACTION_TRIGGER_PERCENT)
369 .min(budget.input_budget_ceiling);
370 assert_eq!(
371 budget.compaction_trigger_tokens, expected,
372 "window {window}: trigger should be 75% of the window, clamped to the ceiling"
373 );
374 assert!(budget.compaction_trigger_tokens <= budget.input_budget_ceiling);
375 }
376 }
377
378 #[test]
379 fn should_compact_flips_at_the_trigger() {
380 let window = 1_048_576;
381 let cap = 262_144;
382 let trigger = ContextBudget::new(window, 0, cap).compaction_trigger_tokens;
383 assert!(trigger > 0);
384
385 let below = ContextBudget::new(window, trigger - 1, cap);
386 assert!(!below.should_compact());
387
388 let at = ContextBudget::new(window, trigger, cap);
389 assert!(at.should_compact());
390
391 let above = ContextBudget::new(window, trigger + 1, cap);
392 assert!(above.should_compact());
393 }
394
395 #[test]
396 fn trigger_never_exceeds_input_ceiling() {
397 const WINDOWS: &[u64] = &[
398 0, 512, 8_192, 65_536, 131_072, 262_144, 500_000, 1_048_576, 2_000_000,
399 ];
400 const CAPS: &[u64] = &[0, 1_024, 64_000, 131_072, 262_144, 1_000_000];
401 const INPUTS: &[u64] = &[0, 1_024, 200_000, 2_000_000];
402 const PERCENTS: &[f64] = &[10.0, 50.0, 75.0, 80.0, 95.0, 100.0];
403
404 for &window in WINDOWS {
405 for &cap in CAPS {
406 for &input in INPUTS {
407 let budget = ContextBudget::new(window, input, cap);
408 assert!(budget.compaction_trigger_tokens <= budget.input_budget_ceiling);
409 for &percent in PERCENTS {
410 assert!(
411 budget.compaction_trigger_for_percent(percent)
412 <= budget.input_budget_ceiling,
413 "window={window} cap={cap} input={input} percent={percent}"
414 );
415 }
416 }
417 }
418 }
419 }
420
421 #[test]
422 fn zero_window_never_suggests_compaction() {
423 let budget = ContextBudget::new(0, 0, 64_000);
424 assert_eq!(budget.compaction_trigger_tokens, 0);
425 assert_eq!(budget.input_budget_ceiling, 0);
426 assert!(!budget.should_compact());
427 assert_eq!(budget.pressure, PressureLevel::Low);
428 assert_eq!(budget.available_input_tokens, 0);
429 assert_eq!(budget.usage_percent(), 0.0);
430 }
431
432 // -- Output cap clamping & available budget ----------------------------
433
434 #[test]
435 fn output_cap_is_preserved_when_window_is_roomy() {
436 // 1M window, 64K configured cap: cap fits comfortably.
437 let budget = ContextBudget::new(1_048_576, 0, 64_000);
438 assert_eq!(budget.output_cap_tokens, 64_000);
439 // available = window - cap - headroom - input
440 let expected = 1_048_576 - 64_000 - CONTEXT_HEADROOM_TOKENS;
441 assert_eq!(budget.available_input_tokens, expected);
442 }
443
444 #[test]
445 fn output_cap_is_clamped_to_protect_input_floor_on_small_window() {
446 // This is the engine's hard-won lesson: a generous output reservation
447 // on a small window must not underflow the input budget. An 8,192-token
448 // window with a 262,144-token desired cap must still leave the input
449 // floor available rather than collapsing to zero or wrapping.
450 let window = 8_192u64;
451 let budget = ContextBudget::new(window, 0, 262_144);
452
453 let reserved_floor = MIN_INPUT_BUDGET_TOKENS + CONTEXT_HEADROOM_TOKENS;
454 let expected_cap = window - reserved_floor;
455 assert_eq!(budget.output_cap_tokens, expected_cap);
456 // With zero input committed, the whole remaining budget is available
457 // and is at least the protected floor.
458 assert!(budget.available_input_tokens >= MIN_INPUT_BUDGET_TOKENS);
459 assert_eq!(
460 budget.available_input_tokens,
461 window - budget.output_cap_tokens - CONTEXT_HEADROOM_TOKENS
462 );
463 }
464
465 #[test]
466 fn tiny_window_below_floor_saturates_without_panic() {
467 // Window smaller than the protected floor: cap collapses to 0 and the
468 // available budget saturates at 0 instead of underflowing.
469 let window = 512u64; // < MIN_INPUT_BUDGET_TOKENS + headroom
470 let budget = ContextBudget::new(window, 100, 262_144);
471 assert_eq!(budget.output_cap_tokens, 0);
472 assert_eq!(budget.available_input_tokens, 0);
473 // Usage still computes a sane percentage.
474 assert!((budget.usage_percent() - (100.0 / 512.0 * 100.0)).abs() < 1e-9);
475 }
476
477 #[test]
478 fn available_budget_saturates_when_input_exceeds_ceiling() {
479 let window = 131_072u64;
480 let cap = 32_000u64;
481 // Commit far more input than the window holds.
482 let budget = ContextBudget::new(window, window * 2, cap);
483 assert_eq!(budget.available_input_tokens, 0);
484 assert!(!budget.fits_additional(1));
485 assert!(budget.fits_additional(0));
486 // Usage is clamped to 100%.
487 assert_eq!(budget.usage_percent(), 100.0);
488 assert_eq!(budget.pressure, PressureLevel::Critical);
489 }
490
491 #[test]
492 fn fits_additional_respects_the_reserved_boundary() {
493 let window = 262_144u64;
494 let cap = 64_000u64;
495 let budget = ContextBudget::new(window, 100_000, cap);
496 let room = budget.available_input_tokens;
497 assert!(budget.fits_additional(room));
498 assert!(!budget.fits_additional(room + 1));
499 }
500
501 // -- Usage percent & pressure across window sizes ----------------------
502
503 #[test]
504 fn usage_percent_is_proportional_across_window_sizes() {
505 for &window in WINDOWS {
506 // Half-full should read ~50% and classify as Medium regardless of
507 // absolute window size.
508 let half = window / 2;
509 let budget = ContextBudget::new(window, half, 64_000);
510 let pct = budget.usage_percent();
511 assert!(
512 (pct - 50.0).abs() < 0.5,
513 "window {window}: half-full should be ~50%, got {pct}"
514 );
515 assert_eq!(budget.pressure, PressureLevel::Medium);
516 }
517 }
518
519 #[test]
520 fn pressure_tracks_input_growth_on_a_1m_window() {
521 let window = 1_048_576u64;
522 let cap = 262_144u64;
523
524 let low = ContextBudget::new(window, percent_of(window, 10.0), cap);
525 assert_eq!(low.pressure, PressureLevel::Low);
526
527 let medium = ContextBudget::new(window, percent_of(window, 50.0), cap);
528 assert_eq!(medium.pressure, PressureLevel::Medium);
529
530 let high = ContextBudget::new(window, percent_of(window, 80.0), cap);
531 assert_eq!(high.pressure, PressureLevel::High);
532 assert!(high.should_compact());
533
534 let critical = ContextBudget::new(window, percent_of(window, 95.0), cap);
535 assert_eq!(critical.pressure, PressureLevel::Critical);
536 }
537
538 #[test]
539 fn snapshot_fields_are_internally_consistent() {
540 for &window in WINDOWS {
541 for &input in &[0u64, window / 4, window / 2, window] {
542 let budget = ContextBudget::new(window, input, 64_000);
543 // Field mirrors the constructor arguments.
544 assert_eq!(budget.window_tokens, window);
545 assert_eq!(budget.input_tokens, input);
546 // available + input never claims more than the window minus
547 // reserved output and headroom.
548 let ceiling = window
549 .saturating_sub(budget.output_cap_tokens)
550 .saturating_sub(CONTEXT_HEADROOM_TOKENS);
551 assert!(budget.available_input_tokens <= ceiling);
552 // Pressure agrees with the standalone usage percentage.
553 assert_eq!(
554 budget.pressure,
555 PressureLevel::from_usage_percent(budget.usage_percent())
556 );
557 }
558 }
559 }
560
561 #[test]
562 fn percent_of_rounds_to_nearest_token() {
563 assert_eq!(percent_of(0, 75.0), 0);
564 assert_eq!(percent_of(100, 0.0), 0);
565 assert_eq!(percent_of(100, 100.0), 100);
566 assert_eq!(percent_of(100, 75.0), 75);
567 // 3 * 0.75 = 2.25 -> rounds to 2.
568 assert_eq!(percent_of(3, 75.0), 2);
569 // 2 * 0.75 = 1.5 -> rounds to 2.
570 assert_eq!(percent_of(2, 75.0), 2);
571 }
572
573 #[test]
574 fn budget_is_copy_and_comparable() {
575 let a = ContextBudget::new(131_072, 1_000, 32_000);
576 let b = a; // Copy, not move.
577 assert_eq!(a, b);
578 assert_eq!(a.window_tokens, b.window_tokens);
579 }
580}