MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / apply_budget

Function apply_budget

libatomic/src/daemon/services_agent.rs:594–624  ·  view source on GitHub ↗

Truncate bodies so their combined length fits `budget_chars`: an even allocation first, then unused capacity from short bodies goes to higher-ranked items instead of being discarded.

(items: &mut [ContextItem], budget_chars: usize)

Source from the content-addressed store, hash-verified

592 /// even allocation first, then unused capacity from short bodies goes
593 /// to higher-ranked items instead of being discarded.
594 fn apply_budget(items: &mut [ContextItem], budget_chars: usize) {
595 if items.is_empty() {
596 return;
597 }
598 let lengths: Vec<usize> = items
599 .iter()
600 .map(|item| item.body.as_deref().map_or(0, |body| body.chars().count()))
601 .collect();
602 let baseline = budget_chars / items.len();
603 let mut allocations: Vec<usize> = lengths
604 .iter()
605 .map(|length| (*length).min(baseline))
606 .collect();
607 let mut remaining = budget_chars.saturating_sub(allocations.iter().sum());
608 for (allocation, length) in allocations.iter_mut().zip(&lengths) {
609 let extra = length.saturating_sub(*allocation).min(remaining);
610 *allocation += extra;
611 remaining -= extra;
612 if remaining == 0 {
613 break;
614 }
615 }
616
617 for ((item, allocation), length) in items.iter_mut().zip(allocations).zip(lengths) {
618 if allocation < length {
619 let body = item.body.clone().unwrap_or_default();
620 item.body = Some(truncate_chars(&body, allocation));
621 item.truncated = true;
622 }
623 }
624 }
625
626 /// The gather→rank→budget pipeline for one context request: the ranked
627 /// ContextItems the CLI renders (md, candidates, and JSON forms).

Callers 1

context_itemsFunction · 0.70

Calls 8

iter_mutMethod · 0.80
truncate_charsFunction · 0.70
is_emptyMethod · 0.45
collectMethod · 0.45
iterMethod · 0.45
countMethod · 0.45
lenMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected