Files
codex/codex-rs/guardian-context/src/retention_tests.rs
T
felixxia-oai 8209978616 Share Guardian user-message retention logic (#42031)
## What changed

- Add a shared `select_user_messages` helper in `guardian-context` that always
  anchors the first user message and fills the remaining token budget from
  newest to oldest.
- Use the helper for both core Guardian prompt rendering and the Guardian V2
  async scorer so they apply the same retention policy.

## Testing

- Cover empty and single-message transcripts, over-budget anchors, and
  newest-first selection across multiple token budgets.

GitOrigin-RevId: 7db246013875263ceb5806d2a6a19b4e2a3db1c4
2026-09-01 10:40:33 +00:00

49 lines
1.1 KiB
Rust

use super::*;
use pretty_assertions::assert_eq;
#[test]
fn user_selection_preserves_anchors_and_fills_from_newest() {
let messages = [
UserMessageCost {
index: 2,
tokens: 4,
},
UserMessageCost {
index: 5,
tokens: 3,
},
UserMessageCost {
index: 8,
tokens: 6,
},
UserMessageCost {
index: 10,
tokens: 2,
},
];
for (budget, indices, tokens) in [
(0, vec![2], 4),
(4, vec![2], 4),
(6, vec![2, 10], 6),
(9, vec![2, 10, 5], 9),
(12, vec![2, 10, 8], 12),
(15, vec![2, 10, 8, 5], 15),
] {
assert_eq!(
select_user_messages(&messages, budget),
UserMessageSelection { indices, tokens },
);
}
assert_eq!(
select_user_messages(&[], /*max_message_tokens*/ 10),
UserMessageSelection::default(),
);
assert_eq!(
select_user_messages(&messages[..1], /*max_message_tokens*/ 10),
UserMessageSelection {
indices: vec![2],
tokens: 4
},
);
}