cargo-context-core 0.2.0

Core engine for cargo-context: assembles, budgets, and scrubs Rust project context packs for LLMs.
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
//! Token budget allocation.
//!
//! The pack builder produces a list of *candidate* sections; this module
//! decides which survive within a token ceiling.

use crate::pack::Section;
use crate::tokenize::Tokenizer;

/// Strategy for reconciling candidate sections with the token limit.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum BudgetStrategy {
    /// Sort by priority, drop whole sections from the tail until the total
    /// fits. P0 sections (user prompt) are always retained even if they
    /// individually exceed the budget.
    #[default]
    Priority,

    /// Scale every competing section proportionally to its share of the
    /// total. Nothing is dropped unless its proportional slot rounds to
    /// zero tokens. Good when you want a balanced snapshot rather than a
    /// priority-biased triage.
    Proportional,

    /// Keep sections in priority order until one overflows; hard-truncate
    /// that section's content and stop.
    Truncate,
}

#[derive(Debug, Clone, Copy)]
pub struct Budget {
    pub max_tokens: usize,
    pub reserve_tokens: usize,
    pub strategy: BudgetStrategy,
}

impl Default for Budget {
    fn default() -> Self {
        Self {
            max_tokens: 8000,
            reserve_tokens: 2000,
            strategy: BudgetStrategy::default(),
        }
    }
}

impl Budget {
    /// Tokens available for pack content (i.e. `max - reserve`).
    pub fn effective(&self) -> usize {
        self.max_tokens.saturating_sub(self.reserve_tokens)
    }
}

/// Numeric priority for a candidate section. Lower = more important.
pub type Priority = u8;

/// Exempt from budget pressure. Used for the user prompt so the reader's
/// own question is never dropped.
pub const P_EXEMPT: Priority = 0;
pub const P_ERROR: Priority = 1;
pub const P_DIFF: Priority = 2;
pub const P_MAP: Priority = 3;
pub const P_ENTRY: Priority = 4;
pub const P_TESTS: Priority = 5;

/// Outcome of budget allocation: sections that fit + names of those dropped.
#[derive(Debug, Default)]
pub struct Allocation {
    pub kept: Vec<Section>,
    pub dropped: Vec<String>,
    pub tokens_used: usize,
    pub tokens_budget: usize,
}

/// Reconcile candidate sections with `budget` using its strategy.
pub fn allocate(
    candidates: Vec<(Priority, Section)>,
    budget: &Budget,
    tokenizer: &Tokenizer,
) -> Allocation {
    let limit = budget.effective();
    match budget.strategy {
        BudgetStrategy::Priority => apply_priority(candidates, limit),
        BudgetStrategy::Proportional => apply_proportional(candidates, limit, tokenizer),
        BudgetStrategy::Truncate => apply_truncate(candidates, limit, tokenizer),
    }
}

fn apply_priority(candidates: Vec<(Priority, Section)>, limit: usize) -> Allocation {
    // Split exempt sections off; they are unconditionally kept.
    let (exempt, mut competing): (Vec<Section>, Vec<(Priority, Section)>) =
        candidates.into_iter().partition_map(|(p, s)| {
            if p == P_EXEMPT {
                Left(s)
            } else {
                Right((p, s))
            }
        });
    competing.sort_by_key(|(p, _)| *p);

    let exempt_tokens: usize = exempt.iter().map(|s| s.token_estimate).sum();
    let remaining = limit.saturating_sub(exempt_tokens);

    let mut kept = exempt;
    let mut dropped: Vec<String> = Vec::new();
    let mut running: usize = 0;

    for (_p, s) in competing {
        if running + s.token_estimate <= remaining {
            running += s.token_estimate;
            kept.push(s);
        } else {
            dropped.push(s.name);
        }
    }

    let tokens_used = exempt_tokens + running;
    Allocation {
        kept,
        dropped,
        tokens_used,
        tokens_budget: limit,
    }
}

fn apply_truncate(
    candidates: Vec<(Priority, Section)>,
    limit: usize,
    tokenizer: &Tokenizer,
) -> Allocation {
    let (exempt, mut competing): (Vec<Section>, Vec<(Priority, Section)>) =
        candidates.into_iter().partition_map(|(p, s)| {
            if p == P_EXEMPT {
                Left(s)
            } else {
                Right((p, s))
            }
        });
    competing.sort_by_key(|(p, _)| *p);

    let exempt_tokens: usize = exempt.iter().map(|s| s.token_estimate).sum();
    let mut kept = exempt;
    let mut dropped: Vec<String> = Vec::new();
    let mut running: usize = 0;
    let remaining_after_exempt = limit.saturating_sub(exempt_tokens);

    for (_p, mut s) in competing {
        let budget_left = remaining_after_exempt.saturating_sub(running);
        if budget_left == 0 {
            dropped.push(s.name);
            continue;
        }
        if s.token_estimate <= budget_left {
            running += s.token_estimate;
            kept.push(s);
            continue;
        }
        // Oversized: hard-truncate content to roughly `budget_left` tokens.
        let orig_tokens = s.token_estimate.max(1);
        let ratio = (budget_left as f64 / orig_tokens as f64).clamp(0.0, 1.0);
        // Subtract ~5% for the truncation marker.
        let char_count = s.content.chars().count();
        let target_chars = ((char_count as f64) * ratio * 0.95) as usize;
        let mut new_content: String = s.content.chars().take(target_chars).collect();
        new_content.push_str("\n\n[... truncated by --budget-strategy=truncate ...]");
        s.content = new_content;
        s.token_estimate = tokenizer.count(&s.content);
        running += s.token_estimate;
        kept.push(s);
        // Remainder of competing sections get dropped.
        break;
    }

    // Any competing section we didn't process (because we broke out of the
    // loop above) was already fully consumed; nothing else to do.
    Allocation {
        kept,
        dropped,
        tokens_used: exempt_tokens + running,
        tokens_budget: limit,
    }
}

fn apply_proportional(
    candidates: Vec<(Priority, Section)>,
    limit: usize,
    tokenizer: &Tokenizer,
) -> Allocation {
    let (exempt, mut competing): (Vec<Section>, Vec<(Priority, Section)>) =
        candidates.into_iter().partition_map(|(p, s)| {
            if p == P_EXEMPT {
                Left(s)
            } else {
                Right((p, s))
            }
        });
    competing.sort_by_key(|(p, _)| *p);

    let exempt_tokens: usize = exempt.iter().map(|s| s.token_estimate).sum();
    let remaining = limit.saturating_sub(exempt_tokens);
    let total_competing: usize = competing.iter().map(|(_, s)| s.token_estimate).sum();

    let mut kept = exempt;
    let mut dropped: Vec<String> = Vec::new();
    let mut running: usize = 0;

    if total_competing == 0 {
        return Allocation {
            kept,
            dropped,
            tokens_used: exempt_tokens,
            tokens_budget: limit,
        };
    }

    // Fast path: everything fits, no truncation needed.
    if total_competing <= remaining {
        for (_, s) in competing {
            running += s.token_estimate;
            kept.push(s);
        }
        return Allocation {
            kept,
            dropped,
            tokens_used: exempt_tokens + running,
            tokens_budget: limit,
        };
    }

    // Scale every competing section by the same ratio.
    let ratio = remaining as f64 / total_competing as f64;
    let marker = "\n\n[... truncated by --budget-strategy=proportional ...]";
    let marker_tokens = tokenizer.count(marker);
    for (_, mut s) in competing {
        let target_tokens = ((s.token_estimate as f64) * ratio).floor() as usize;
        if target_tokens == 0 {
            // Section would be reduced to nothing; drop it rather than emit
            // an empty rendered section.
            dropped.push(s.name);
            continue;
        }
        if target_tokens >= s.token_estimate {
            running += s.token_estimate;
            kept.push(s);
            continue;
        }
        // Budget for body = target minus the marker's cost.
        let body_tokens = target_tokens.saturating_sub(marker_tokens);
        if body_tokens == 0 {
            dropped.push(s.name);
            continue;
        }
        let orig_chars = s.content.chars().count();
        let orig_tokens = s.token_estimate.max(1);
        let char_target =
            ((orig_chars as f64) * (body_tokens as f64) / (orig_tokens as f64)) as usize;
        let mut new_content: String = s.content.chars().take(char_target).collect();
        new_content.push_str(marker);
        s.content = new_content;
        s.token_estimate = tokenizer.count(&s.content);
        running += s.token_estimate;
        kept.push(s);
    }

    Allocation {
        kept,
        dropped,
        tokens_used: exempt_tokens + running,
        tokens_budget: limit,
    }
}

// Minimal partition_map to avoid pulling `itertools` just for this.
enum Either<L, R> {
    Left(L),
    Right(R),
}
use Either::{Left, Right};

trait PartitionMap<I> {
    fn partition_map<A, B, F>(self, f: F) -> (Vec<A>, Vec<B>)
    where
        F: FnMut(I) -> Either<A, B>;
}

impl<It, I> PartitionMap<I> for It
where
    It: Iterator<Item = I>,
{
    fn partition_map<A, B, F>(self, mut f: F) -> (Vec<A>, Vec<B>)
    where
        F: FnMut(I) -> Either<A, B>,
    {
        let mut a = Vec::new();
        let mut b = Vec::new();
        for item in self {
            match f(item) {
                Left(x) => a.push(x),
                Right(x) => b.push(x),
            }
        }
        (a, b)
    }
}

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

    fn mk(name: &str, tokens: usize) -> Section {
        Section {
            name: name.into(),
            content: "x".repeat(tokens * 4),
            token_estimate: tokens,
        }
    }

    #[test]
    fn effective_subtracts_reserve() {
        let b = Budget {
            max_tokens: 8000,
            reserve_tokens: 2000,
            strategy: BudgetStrategy::Priority,
        };
        assert_eq!(b.effective(), 6000);
    }

    #[test]
    fn effective_saturates_at_zero() {
        let b = Budget {
            max_tokens: 100,
            reserve_tokens: 500,
            strategy: BudgetStrategy::Priority,
        };
        assert_eq!(b.effective(), 0);
    }

    #[test]
    fn priority_drops_lowest_priority_when_over_budget() {
        let candidates = vec![
            (P_ERROR, mk("errors", 200)),
            (P_DIFF, mk("diff", 500)),
            (P_MAP, mk("map", 400)),
        ];
        let b = Budget {
            max_tokens: 800,
            reserve_tokens: 0,
            strategy: BudgetStrategy::Priority,
        };
        let a = allocate(candidates, &b, &Tokenizer::CharsDiv4);
        assert_eq!(a.kept.len(), 2);
        assert_eq!(a.kept[0].name, "errors");
        assert_eq!(a.kept[1].name, "diff");
        assert_eq!(a.dropped, vec!["map"]);
        assert_eq!(a.tokens_used, 700);
    }

    #[test]
    fn priority_keeps_prompt_even_when_oversized() {
        let candidates = vec![
            (P_EXEMPT, mk("📝 User Prompt", 5000)),
            (P_ERROR, mk("errors", 100)),
        ];
        let b = Budget {
            max_tokens: 1000,
            reserve_tokens: 0,
            strategy: BudgetStrategy::Priority,
        };
        let a = allocate(candidates, &b, &Tokenizer::CharsDiv4);
        assert!(a.kept.iter().any(|s| s.name.contains("Prompt")));
        assert_eq!(a.dropped, vec!["errors"]);
    }

    #[test]
    fn truncate_hard_cuts_overflowing_section() {
        let candidates = vec![(P_ERROR, mk("errors", 200)), (P_DIFF, mk("diff", 1500))];
        let b = Budget {
            max_tokens: 800,
            reserve_tokens: 0,
            strategy: BudgetStrategy::Truncate,
        };
        let a = allocate(candidates, &b, &Tokenizer::CharsDiv4);
        assert_eq!(a.kept.len(), 2);
        assert_eq!(a.kept[0].name, "errors");
        assert_eq!(a.kept[1].name, "diff");
        // Diff was truncated — its new content contains the marker.
        assert!(a.kept[1].content.contains("truncated"));
        assert!(a.tokens_used <= 800);
    }

    #[test]
    fn zero_budget_drops_everything_except_exempt() {
        let candidates = vec![
            (P_EXEMPT, mk("📝 User Prompt", 50)),
            (P_ERROR, mk("errors", 100)),
            (P_DIFF, mk("diff", 100)),
        ];
        let b = Budget {
            max_tokens: 0,
            reserve_tokens: 0,
            strategy: BudgetStrategy::Priority,
        };
        let a = allocate(candidates, &b, &Tokenizer::CharsDiv4);
        assert_eq!(a.kept.len(), 1);
        assert_eq!(a.kept[0].name, "📝 User Prompt");
        assert_eq!(a.dropped, vec!["errors", "diff"]);
    }

    #[test]
    fn empty_candidates_produce_empty_allocation() {
        let a = allocate(Vec::new(), &Budget::default(), &Tokenizer::CharsDiv4);
        assert!(a.kept.is_empty());
        assert!(a.dropped.is_empty());
        assert_eq!(a.tokens_used, 0);
    }

    #[test]
    fn proportional_scales_all_sections_when_over_budget() {
        let candidates = vec![(P_ERROR, mk("errors", 400)), (P_DIFF, mk("diff", 600))];
        let b = Budget {
            max_tokens: 500,
            reserve_tokens: 0,
            strategy: BudgetStrategy::Proportional,
        };
        let a = allocate(candidates, &b, &Tokenizer::CharsDiv4);
        // Both sections survive (the whole point of proportional: no drops).
        assert_eq!(a.kept.len(), 2);
        assert!(a.dropped.is_empty());
        // Both got truncated.
        assert!(
            a.kept.iter().all(|s| s.content.contains("truncated")),
            "all oversized sections should carry the truncation marker"
        );
        // Budget is honored.
        assert!(
            a.tokens_used <= b.max_tokens,
            "tokens_used {} exceeded budget {}",
            a.tokens_used,
            b.max_tokens
        );
    }

    #[test]
    fn proportional_keeps_whole_when_under_budget() {
        let candidates = vec![(P_ERROR, mk("errors", 100)), (P_DIFF, mk("diff", 200))];
        let b = Budget {
            max_tokens: 1000,
            reserve_tokens: 0,
            strategy: BudgetStrategy::Proportional,
        };
        let a = allocate(candidates, &b, &Tokenizer::CharsDiv4);
        assert_eq!(a.kept.len(), 2);
        assert_eq!(a.tokens_used, 300);
        assert!(a.dropped.is_empty());
        assert!(
            a.kept.iter().all(|s| !s.content.contains("truncated")),
            "under-budget allocation should not truncate"
        );
    }

    #[test]
    fn proportional_always_keeps_exempt() {
        let candidates = vec![
            (P_EXEMPT, mk("📝 User Prompt", 100)),
            (P_ERROR, mk("errors", 500)),
        ];
        let b = Budget {
            max_tokens: 200,
            reserve_tokens: 0,
            strategy: BudgetStrategy::Proportional,
        };
        let a = allocate(candidates, &b, &Tokenizer::CharsDiv4);
        // Prompt kept whole; errors proportionally scaled into remaining 100.
        assert!(a.kept.iter().any(|s| s.name.contains("Prompt")));
    }

    #[test]
    fn proportional_drops_zero_slot_sections() {
        // Tiny section + huge section with tight budget: tiny's share rounds
        // to zero. Preferable to emit an empty section? No — drop it and
        // report.
        let candidates = vec![(P_ERROR, mk("tiny", 1)), (P_DIFF, mk("huge", 10_000))];
        let b = Budget {
            max_tokens: 100,
            reserve_tokens: 0,
            strategy: BudgetStrategy::Proportional,
        };
        let a = allocate(candidates, &b, &Tokenizer::CharsDiv4);
        assert!(
            a.dropped.contains(&"tiny".to_string()),
            "zero-slot section should be dropped, got dropped={:?}",
            a.dropped
        );
    }
}