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