cupel 1.2.0

Context window management pipeline for LLM applications
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! Count-quota slicer that enforces absolute item-count requirements and caps per [`ContextKind`].
//!
//! [`CountQuotaSlice`] is a decorator slicer implementing the two-phase COUNT-DISTRIBUTE-BUDGET
//! algorithm:
//!
//! - **Phase 1 (Count-Satisfy):** For each kind with `require_count > 0`, commits the top-N
//!   candidates by score descending. Shortfalls (when fewer candidates exist than required) are
//!   recorded in `count_requirement_shortfalls` on the pipeline's [`SelectionReport`].
//!
//! - **Phase 2 (Budget-Distribute):** The remaining candidates are passed to the inner slicer
//!   with the residual token budget (`target_tokens - pre_allocated_tokens`). Items exceeding
//!   `cap_count` for their kind are excluded from the final result.
//!
//! # Design references
//!
//! - `.planning/design/count-quota-design.md` — pseudocode and all DI rulings

use std::collections::HashMap;

use crate::CupelError;
use crate::diagnostics::CountRequirementShortfall;
use crate::model::{ContextBudget, ContextItem, ContextKind, ScoredItem};
use crate::slicer::{QuotaConstraint, QuotaConstraintMode, QuotaPolicy, Slicer};

// ── ScarcityBehavior ──────────────────────────────────────────────────────────

/// Controls how [`CountQuotaSlice`] responds when the candidate pool has fewer
/// items of a kind than the configured `require_count`.
///
/// # Default
///
/// [`ScarcityBehavior::Degrade`] — the slicer continues with all available
/// candidates and records the shortfall in `count_requirement_shortfalls`.
#[derive(Debug, Clone, Copy, Default)]
pub enum ScarcityBehavior {
    /// Include all available candidates (even if fewer than `require_count`)
    /// and record the shortfall in `SelectionReport::count_requirement_shortfalls`.
    /// Pipeline execution continues normally.
    #[default]
    Degrade,

    /// Return a [`CupelError::SlicerConfig`] when a kind's candidate pool is
    /// exhausted before satisfying `require_count`. Use this when count requirements
    /// are hard guarantees (e.g., required disclaimer text that must always appear).
    Throw,
}

// ── CountQuotaEntry ───────────────────────────────────────────────────────────

/// A single count-quota entry specifying require and cap item counts for a kind.
///
/// # Validation
///
/// - `require_count` must be `<= cap_count`.
/// - `cap_count == 0` with `require_count > 0` is rejected (guaranteed violation).
///
/// # Examples
///
/// ```
/// use cupel::{ContextKind, CountQuotaEntry};
///
/// // Require at least 2 tool items, cap at 4.
/// let entry = CountQuotaEntry::new(ContextKind::new("tool")?, 2, 4)?;
/// assert_eq!(entry.require_count(), 2);
/// assert_eq!(entry.cap_count(), 4);
/// # Ok::<(), cupel::CupelError>(())
/// ```
#[derive(Debug, Clone)]
pub struct CountQuotaEntry {
    kind: ContextKind,
    require_count: usize,
    cap_count: usize,
}

impl CountQuotaEntry {
    /// Creates a new `CountQuotaEntry` with validated require and cap counts.
    ///
    /// # Errors
    ///
    /// Returns [`CupelError::SlicerConfig`] if:
    /// - `require_count > cap_count`
    /// - `cap_count == 0 && require_count > 0`
    pub fn new(
        kind: ContextKind,
        require_count: usize,
        cap_count: usize,
    ) -> Result<Self, CupelError> {
        if cap_count == 0 && require_count > 0 {
            return Err(CupelError::SlicerConfig(format!(
                "kind {:?}: cap_count is 0 but require_count is {require_count}; \
                 a zero cap with a positive requirement can never be satisfied",
                kind.as_str(),
            )));
        }
        if require_count > cap_count {
            return Err(CupelError::SlicerConfig(format!(
                "kind {:?}: require_count ({require_count}) must be <= cap_count ({cap_count})",
                kind.as_str(),
            )));
        }
        Ok(Self {
            kind,
            require_count,
            cap_count,
        })
    }

    /// The context kind this entry applies to.
    pub fn kind(&self) -> &ContextKind {
        &self.kind
    }

    /// Minimum number of items of this kind to commit in Phase 1.
    pub fn require_count(&self) -> usize {
        self.require_count
    }

    /// Maximum number of items of this kind to include in the final result.
    pub fn cap_count(&self) -> usize {
        self.cap_count
    }
}

// ── CountQuotaSlice ───────────────────────────────────────────────────────────

/// A decorator slicer that enforces absolute item-count requirements and caps
/// per [`ContextKind`] using the two-phase COUNT-DISTRIBUTE-BUDGET algorithm.
///
/// # Algorithm
///
/// **Phase 1 — Count-Satisfy:** For each [`CountQuotaEntry`] with `require_count > 0`,
/// selects the top-N candidates of that kind by score descending and pre-commits them.
/// Their token cost is subtracted from the budget before Phase 2.
///
/// **Phase 2 — Budget-Distribute:** The inner slicer runs on the remaining candidates
/// with the residual budget. Items exceeding `cap_count` for their kind are then
/// filtered from the inner slicer's output.
///
/// # Scarcity
///
/// When the candidate pool has fewer items than `require_count`, behavior is
/// controlled by [`ScarcityBehavior`]:
/// - [`Degrade`](ScarcityBehavior::Degrade) (default): select all available; record shortfall.
/// - [`Throw`](ScarcityBehavior::Throw): return [`CupelError::SlicerConfig`].
///
/// # Compatibility
///
/// `CountQuotaSlice` does not support [`crate::KnapsackSlice`] as the inner slicer.
/// Construction fails with a [`CupelError::SlicerConfig`] if `inner.is_knapsack()` returns `true`.
/// Use [`crate::GreedySlice`] as the inner slicer instead.
///
/// # Examples
///
/// ```
/// use std::collections::HashMap;
/// use cupel::{
///     ContextItemBuilder, ContextBudget, ContextKind,
///     CountQuotaEntry, CountQuotaSlice, GreedySlice, ScoredItem, Slicer,
/// };
///
/// let entries = vec![
///     CountQuotaEntry::new(ContextKind::new("tool")?, 2, 4)?,
/// ];
/// let slicer = CountQuotaSlice::new(entries, Box::new(GreedySlice), Default::default())?;
///
/// let items = vec![
///     ScoredItem {
///         item: ContextItemBuilder::new("tool-a", 100)
///             .kind(ContextKind::new("tool")?)
///             .build()?,
///         score: 0.9,
///     },
///     ScoredItem {
///         item: ContextItemBuilder::new("tool-b", 100)
///             .kind(ContextKind::new("tool")?)
///             .build()?,
///         score: 0.7,
///     },
///     ScoredItem {
///         item: ContextItemBuilder::new("tool-c", 100)
///             .kind(ContextKind::new("tool")?)
///             .build()?,
///         score: 0.5,
///     },
/// ];
///
/// let budget = ContextBudget::new(1000, 1000, 0, HashMap::new(), 0.0)?;
/// let selected = slicer.slice(&items, &budget)?;
/// // Phase 1 commits tool-a (0.9) and tool-b (0.7).
/// // Phase 2 inner slicer receives tool-c with residual budget.
/// assert_eq!(selected.len(), 3);
/// # Ok::<(), cupel::CupelError>(())
/// ```
pub struct CountQuotaSlice {
    entries: Vec<CountQuotaEntry>,
    inner: Box<dyn Slicer>,
    scarcity: ScarcityBehavior,
}

impl std::fmt::Debug for CountQuotaSlice {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CountQuotaSlice")
            .field("entries", &self.entries)
            .field("inner", &"<dyn Slicer>")
            .field("scarcity", &self.scarcity)
            .finish()
    }
}

impl CountQuotaSlice {
    /// Creates a new `CountQuotaSlice`.
    ///
    /// # Errors
    ///
    /// Returns [`CupelError::SlicerConfig`] if:
    /// - `inner.is_knapsack()` returns `true` (not supported in v1)
    /// - Any entry fails validation (see [`CountQuotaEntry::new`])
    pub fn new(
        entries: Vec<CountQuotaEntry>,
        inner: Box<dyn Slicer>,
        scarcity: ScarcityBehavior,
    ) -> Result<Self, CupelError> {
        if inner.is_knapsack() {
            return Err(CupelError::SlicerConfig(
                "CountQuotaSlice does not support KnapsackSlice as the inner slicer in this \
                 version. Use GreedySlice as the inner slicer. A CountConstrainedKnapsackSlice \
                 will be provided in a future release."
                    .to_owned(),
            ));
        }
        Ok(Self {
            entries,
            inner,
            scarcity,
        })
    }

    /// Returns the configured quota entries.
    pub fn entries(&self) -> &[CountQuotaEntry] {
        &self.entries
    }

    /// Returns the configured scarcity behavior.
    pub fn scarcity(&self) -> ScarcityBehavior {
        self.scarcity
    }

    /// Builds lookup maps for require_count and cap_count keyed by kind.
    fn build_policy_maps(&self) -> (HashMap<ContextKind, usize>, HashMap<ContextKind, usize>) {
        let mut require_map: HashMap<ContextKind, usize> = HashMap::new();
        let mut cap_map: HashMap<ContextKind, usize> = HashMap::new();
        for entry in &self.entries {
            require_map.insert(entry.kind().clone(), entry.require_count());
            cap_map.insert(entry.kind().clone(), entry.cap_count());
        }
        (require_map, cap_map)
    }
}

impl Slicer for CountQuotaSlice {
    /// Runs the two-phase COUNT-DISTRIBUTE-BUDGET algorithm.
    ///
    /// Returns the union of Phase 1 committed items and Phase 2 inner slicer results
    /// (with cap enforcement applied to Phase 2 output).
    fn slice(
        &self,
        sorted: &[ScoredItem],
        budget: &ContextBudget,
    ) -> Result<Vec<ContextItem>, CupelError> {
        if sorted.is_empty() || budget.target_tokens() <= 0 {
            return Ok(Vec::new());
        }

        let (require_map, cap_map) = self.build_policy_maps();
        let target_tokens = budget.target_tokens();

        // ── Phase 1: Count-Satisfy ────────────────────────────────────────────
        //
        // For each kind with require_count > 0, partition candidates by kind,
        // sort descending by score, commit the top-N, and accumulate token cost.

        // Partition all sorted items by kind, preserving score order.
        let mut partitions: HashMap<ContextKind, Vec<&ScoredItem>> = HashMap::new();
        for si in sorted {
            partitions
                .entry(si.item.kind().clone())
                .or_default()
                .push(si);
        }
        // Each partition is already in the caller's score-descending order (sorted input),
        // but to be safe we sort explicitly.
        for items in partitions.values_mut() {
            items.sort_by(|a, b| b.score.total_cmp(&a.score));
        }

        // Track committed items, counts, and pre-allocated token cost.
        let mut committed: Vec<ContextItem> = Vec::new();
        let mut selected_count: HashMap<ContextKind, usize> = HashMap::new();
        let mut pre_alloc_tokens: i64 = 0;

        // Track which ScoredItems were committed so we can exclude them from Phase 2.
        let mut committed_ids: std::collections::HashSet<*const ScoredItem> =
            std::collections::HashSet::new();

        // Record shortfalls for reporting.
        let mut shortfalls: Vec<CountRequirementShortfall> = Vec::new();

        // Sort kinds for deterministic iteration.
        let mut sorted_kinds: Vec<&ContextKind> = partitions.keys().collect();
        sorted_kinds.sort_by_key(|k| k.as_str().to_ascii_lowercase());

        for kind in &sorted_kinds {
            let req_count = require_map.get(*kind).copied().unwrap_or(0);
            if req_count == 0 {
                continue;
            }

            let candidates = &partitions[*kind];
            let mut satisfied = 0usize;

            for &si in candidates.iter() {
                if satisfied >= req_count {
                    break;
                }
                committed.push(si.item.clone());
                committed_ids.insert(si as *const ScoredItem);
                pre_alloc_tokens += si.item.tokens();
                satisfied += 1;
            }

            selected_count.insert((*kind).clone(), satisfied);

            if satisfied < req_count {
                // Scarcity: fewer candidates than required.
                match self.scarcity {
                    ScarcityBehavior::Degrade => {
                        shortfalls.push(CountRequirementShortfall {
                            kind: kind.as_str().to_owned(),
                            required_count: req_count,
                            satisfied_count: satisfied,
                        });
                    }
                    ScarcityBehavior::Throw => {
                        return Err(CupelError::SlicerConfig(format!(
                            "CountQuotaSlice: kind {:?} requires {req_count} items but only \
                             {satisfied} candidates are available",
                            kind.as_str(),
                        )));
                    }
                }
            }
        }

        // ── Phase 2: Budget-Distribute ────────────────────────────────────────
        //
        // Build residual candidate pool (omit committed items) and run the inner
        // slicer with the residual budget.

        let residual_budget_tokens = (target_tokens - pre_alloc_tokens).max(0);

        // Build remaining candidates (not committed in Phase 1), preserving original order.
        let remaining: Vec<ScoredItem> = sorted
            .iter()
            .filter(|si| !committed_ids.contains(&(*si as *const ScoredItem)))
            .cloned()
            .collect();

        let mut phase2_selected: Vec<ContextItem> =
            if residual_budget_tokens > 0 && !remaining.is_empty() {
                // Create a sub-budget for the inner slicer.
                // total_tokens = residual_budget_tokens (generous upper bound),
                // target_tokens = residual_budget_tokens.
                let sub_budget = ContextBudget::new(
                    residual_budget_tokens,
                    residual_budget_tokens,
                    0,
                    HashMap::new(),
                    0.0,
                )
                .expect("residual budget is non-negative");

                self.inner.slice(&remaining, &sub_budget)?
            } else {
                Vec::new()
            };

        // ── Phase 3: Cap Enforcement ──────────────────────────────────────────
        //
        // Filter Phase 2 output: items of a kind exceeding cap_count are excluded.
        // selected_count already reflects Phase 1 committed items.

        let mut filtered_phase2: Vec<ContextItem> = Vec::new();
        for item in phase2_selected.drain(..) {
            let kind = item.kind();
            let cap = cap_map.get(kind).copied();
            let current = selected_count.entry(kind.clone()).or_insert(0);

            match cap {
                Some(cap_count) if *current >= cap_count => {
                    // Exceeds cap — exclude (cap reason is not surfaced at slicer level;
                    // it is observable at the pipeline level via SelectionReport.excluded).
                    // No action needed here; item simply is not added.
                }
                _ => {
                    filtered_phase2.push(item);
                    *current += 1;
                }
            }
        }

        // Final result: Phase 1 committed + Phase 2 cap-enforced.
        let mut result = committed;
        result.extend(filtered_phase2);
        Ok(result)
    }

    fn is_count_quota(&self) -> bool {
        true
    }

    fn count_cap_map(&self) -> std::collections::HashMap<ContextKind, usize> {
        self.entries
            .iter()
            .map(|e| (e.kind().clone(), e.cap_count()))
            .collect()
    }
}

impl QuotaPolicy for CountQuotaSlice {
    fn quota_constraints(&self) -> Vec<QuotaConstraint> {
        self.entries
            .iter()
            .map(|e| QuotaConstraint {
                kind: e.kind().clone(),
                mode: QuotaConstraintMode::Count,
                require: e.require_count() as f64,
                cap: e.cap_count() as f64,
            })
            .collect()
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;
    use crate::model::ContextBudget;
    use crate::{ContextItemBuilder, ContextKind, GreedySlice, KnapsackSlice, ScoredItem};

    fn make_item(content: &str, tokens: i64, kind: &str, score: f64) -> ScoredItem {
        ScoredItem {
            item: ContextItemBuilder::new(content, tokens)
                .kind(ContextKind::new(kind).unwrap())
                .build()
                .unwrap(),
            score,
        }
    }

    fn make_budget(target: i64) -> ContextBudget {
        ContextBudget::new(target, target, 0, HashMap::new(), 0.0).unwrap()
    }

    // ── Construction guards ───────────────────────────────────────────────────

    #[test]
    fn count_quota_construction_rejects_knapsack_inner() {
        let entries = vec![CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 1, 2).unwrap()];
        let inner = Box::new(KnapsackSlice::with_default_bucket_size());
        let result = CountQuotaSlice::new(entries, inner, ScarcityBehavior::Degrade);
        match result {
            Err(CupelError::SlicerConfig(msg)) => {
                assert!(
                    msg.contains("CountQuotaSlice"),
                    "expected message to name CountQuotaSlice"
                );
                assert!(
                    msg.contains("KnapsackSlice"),
                    "expected message to name KnapsackSlice"
                );
                assert!(
                    msg.contains("GreedySlice"),
                    "expected message to name GreedySlice"
                );
            }
            other => panic!("expected Err(SlicerConfig), got {other:?}"),
        }
    }

    #[test]
    fn entry_rejects_require_greater_than_cap() {
        let result = CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 5, 3);
        assert!(matches!(result, Err(CupelError::SlicerConfig(_))));
    }

    #[test]
    fn entry_rejects_zero_cap_with_positive_require() {
        let result = CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 1, 0);
        assert!(matches!(result, Err(CupelError::SlicerConfig(_))));
    }

    #[test]
    fn entry_allows_zero_require_with_positive_cap() {
        let result = CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 0, 3);
        assert!(result.is_ok());
    }

    #[test]
    fn entry_allows_zero_require_and_zero_cap() {
        let result = CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 0, 0);
        assert!(result.is_ok());
    }

    // ── Vector 1: Baseline Count Satisfaction ─────────────────────────────────
    // Policy: RequireCount("tool", 2). Candidates: 3 tool items with scores 0.9, 0.7, 0.5.
    // Expected: Phase 1 commits top-2 (0.9 and 0.7). Phase 2 receives 0.5 item.

    #[test]
    fn count_quota_v1_baseline_count_satisfaction() {
        let entries = vec![CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 2, 4).unwrap()];
        let slicer =
            CountQuotaSlice::new(entries, Box::new(GreedySlice), ScarcityBehavior::Degrade)
                .unwrap();

        let items = vec![
            make_item("tool-a", 100, "tool", 0.9),
            make_item("tool-b", 100, "tool", 0.7),
            make_item("tool-c", 100, "tool", 0.5),
        ];
        let budget = make_budget(1000);
        let selected = slicer.slice(&items, &budget).unwrap();

        // All 3 should be selected: 2 committed in Phase 1, 1 via Phase 2.
        assert_eq!(selected.len(), 3, "all 3 tool items should be selected");

        // Verify Phase 1 committed items are in result (by content).
        let contents: Vec<&str> = selected.iter().map(|i| i.content()).collect();
        assert!(
            contents.contains(&"tool-a"),
            "tool-a (score 0.9) must be committed"
        );
        assert!(
            contents.contains(&"tool-b"),
            "tool-b (score 0.7) must be committed"
        );
    }

    // ── Vector 2: Count-Cap Exclusion ────────────────────────────────────────
    // Policy: CapCount("tool", 1). Candidates: 3 tool items.
    // Expected: Only 1 tool item in result; others excluded by cap.

    #[test]
    fn count_quota_v2_cap_exclusion() {
        let entries = vec![CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 0, 1).unwrap()];
        let slicer =
            CountQuotaSlice::new(entries, Box::new(GreedySlice), ScarcityBehavior::Degrade)
                .unwrap();

        let items = vec![
            make_item("tool-a", 100, "tool", 0.9),
            make_item("tool-b", 100, "tool", 0.7),
            make_item("tool-c", 100, "tool", 0.5),
        ];
        let budget = make_budget(1000);
        let selected = slicer.slice(&items, &budget).unwrap();

        // Only 1 tool item should survive the cap.
        let tool_items: Vec<_> = selected
            .iter()
            .filter(|i| i.kind().as_str().eq_ignore_ascii_case("tool"))
            .collect();
        assert_eq!(
            tool_items.len(),
            1,
            "cap of 1 should exclude 2 tool items; got {}",
            tool_items.len()
        );
    }

    // ── Vector 4: Scarcity Degrade ───────────────────────────────────────────
    // Policy: RequireCount("tool", 3), only 1 candidate.
    // Expected: 1 item selected; shortfall recorded.

    #[test]
    fn count_quota_v4_scarcity_degrade() {
        let entries = vec![CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 3, 5).unwrap()];
        let slicer =
            CountQuotaSlice::new(entries, Box::new(GreedySlice), ScarcityBehavior::Degrade)
                .unwrap();

        let items = vec![make_item("tool-a", 100, "tool", 0.9)];
        let budget = make_budget(1000);
        let selected = slicer.slice(&items, &budget).unwrap();

        assert_eq!(selected.len(), 1, "should select the 1 available item");
        assert_eq!(selected[0].content(), "tool-a");
    }

    #[test]
    fn count_quota_v4_scarcity_throw() {
        let entries = vec![CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 3, 5).unwrap()];
        let slicer =
            CountQuotaSlice::new(entries, Box::new(GreedySlice), ScarcityBehavior::Throw).unwrap();

        let items = vec![make_item("tool-a", 100, "tool", 0.9)];
        let budget = make_budget(1000);

        let result = slicer.slice(&items, &budget);
        assert!(
            matches!(result, Err(CupelError::SlicerConfig(_))),
            "Throw mode should return error on scarcity"
        );
    }

    // ── Vector 5: Tag Non-Exclusivity ─────────────────────────────────────────
    // Multi-tag item satisfies 2 require constraints simultaneously.
    // NOTE: CountQuotaSlice operates per-kind (ContextKind), not per-tag.
    // Non-exclusivity between tag-based constraints is a pipeline-level concern.
    // This test verifies the single-kind behavior correctly.

    #[test]
    fn count_quota_empty_input_returns_empty() {
        let entries = vec![CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 2, 4).unwrap()];
        let slicer =
            CountQuotaSlice::new(entries, Box::new(GreedySlice), ScarcityBehavior::Degrade)
                .unwrap();
        let selected = slicer.slice(&[], &make_budget(1000)).unwrap();
        assert!(selected.is_empty());
    }

    #[test]
    fn count_quota_zero_budget_returns_empty() {
        let entries = vec![CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 2, 4).unwrap()];
        let slicer =
            CountQuotaSlice::new(entries, Box::new(GreedySlice), ScarcityBehavior::Degrade)
                .unwrap();
        let items = vec![make_item("tool-a", 100, "tool", 0.9)];
        // ContextBudget with target=0 is invalid; target must be > 0.
        // We test with a budget that provides exactly 0 target_tokens via a negative pre-allocation.
        // Instead, test the empty check: budget.target_tokens() == 0 is the guard.
        // ContextBudget::new requires total_tokens >= target_tokens >= 0, so we skip this variant.
        let _ = (items, slicer);
    }

    #[test]
    fn count_quota_no_require_only_cap() {
        // Only cap configured; no require. Phase 1 commits nothing; Phase 2 enforces cap.
        let entries = vec![CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 0, 2).unwrap()];
        let slicer =
            CountQuotaSlice::new(entries, Box::new(GreedySlice), ScarcityBehavior::Degrade)
                .unwrap();

        let items = vec![
            make_item("tool-a", 100, "tool", 0.9),
            make_item("tool-b", 100, "tool", 0.7),
            make_item("tool-c", 100, "tool", 0.5),
        ];
        let budget = make_budget(1000);
        let selected = slicer.slice(&items, &budget).unwrap();

        let tool_count = selected
            .iter()
            .filter(|i| i.kind().as_str().eq_ignore_ascii_case("tool"))
            .count();
        assert_eq!(
            tool_count, 2,
            "cap of 2 should allow exactly 2 tool items; got {tool_count}"
        );
    }

    #[test]
    fn count_quota_require_equals_cap_fills_exactly() {
        // require == cap: Phase 1 commits exactly cap items; Phase 2 cannot add more.
        let entries = vec![CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 2, 2).unwrap()];
        let slicer =
            CountQuotaSlice::new(entries, Box::new(GreedySlice), ScarcityBehavior::Degrade)
                .unwrap();

        let items = vec![
            make_item("tool-a", 100, "tool", 0.9),
            make_item("tool-b", 100, "tool", 0.7),
            make_item("tool-c", 100, "tool", 0.5),
        ];
        let budget = make_budget(1000);
        let selected = slicer.slice(&items, &budget).unwrap();

        let tool_count = selected
            .iter()
            .filter(|i| i.kind().as_str().eq_ignore_ascii_case("tool"))
            .count();
        assert_eq!(
            tool_count, 2,
            "require==cap should yield exactly 2 tool items; got {tool_count}"
        );
        let contents: Vec<&str> = selected.iter().map(|i| i.content()).collect();
        assert!(contents.contains(&"tool-a"));
        assert!(contents.contains(&"tool-b"));
        assert!(
            !contents.contains(&"tool-c"),
            "tool-c must be excluded by cap"
        );
    }

    #[test]
    fn count_quota_mixed_kinds_independent() {
        // Two kinds, each with their own require/cap. Verify independence.
        let entries = vec![
            CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 1, 2).unwrap(),
            CountQuotaEntry::new(ContextKind::new("system").unwrap(), 1, 1).unwrap(),
        ];
        let slicer =
            CountQuotaSlice::new(entries, Box::new(GreedySlice), ScarcityBehavior::Degrade)
                .unwrap();

        let items = vec![
            make_item("tool-a", 100, "tool", 0.9),
            make_item("tool-b", 100, "tool", 0.7),
            make_item("sys-a", 100, "system", 0.8),
            make_item("sys-b", 100, "system", 0.6),
        ];
        let budget = make_budget(1000);
        let selected = slicer.slice(&items, &budget).unwrap();

        let tool_count = selected
            .iter()
            .filter(|i| i.kind().as_str().eq_ignore_ascii_case("tool"))
            .count();
        let sys_count = selected
            .iter()
            .filter(|i| i.kind().as_str().eq_ignore_ascii_case("system"))
            .count();

        // tool: require 1, cap 2 → up to 2 selected
        assert!(
            tool_count <= 2,
            "tool count must not exceed cap of 2; got {tool_count}"
        );
        assert!(
            tool_count >= 1,
            "tool count must satisfy require of 1; got {tool_count}"
        );

        // system: require 1, cap 1 → exactly 1
        assert_eq!(
            sys_count, 1,
            "system cap of 1 must yield exactly 1; got {sys_count}"
        );
    }

    // ── is_knapsack integration ───────────────────────────────────────────────

    #[test]
    fn is_knapsack_false_for_count_quota_slice() {
        let entries = vec![CountQuotaEntry::new(ContextKind::new("tool").unwrap(), 1, 2).unwrap()];
        let slicer =
            CountQuotaSlice::new(entries, Box::new(GreedySlice), ScarcityBehavior::Degrade)
                .unwrap();
        assert!(
            !slicer.is_knapsack(),
            "CountQuotaSlice.is_knapsack() must return false"
        );
    }

    #[test]
    fn is_knapsack_true_for_knapsack_slice() {
        let slicer = KnapsackSlice::with_default_bucket_size();
        assert!(
            slicer.is_knapsack(),
            "KnapsackSlice.is_knapsack() must return true"
        );
    }

    #[test]
    fn is_knapsack_false_for_greedy_slice() {
        assert!(
            !GreedySlice.is_knapsack(),
            "GreedySlice.is_knapsack() must return false"
        );
    }
}