Merge main into context-window: mask cap (#117) moves into ContextWindow
ci / check (push) Canceled after 0s
ci / check (pull_request) Canceled after 0s

This commit is contained in:
hermes committed 2026-10-02 15:04:41 +00:00
commit cd52d409b2
2 files changed
+22 -5

No files matched your search

+20 -3
View File
@@ -11,8 +11,8 @@
//! drops both; call it whenever the history is replaced or rewritten (new session, fork, clear,
//! compaction). Until the next [`record`](ContextWindow::record) the count is a pure estimate.
//! - The boundary only moves forward, and only once the context passes a third of the compaction
//! threshold: every move rewrites the prompt cache from the first changed result on, so the
//! saving has to beat that. While it stays put the request prefix is byte-stable.
//! threshold (at most [`MASK_FROM_MAX`] tokens): every move rewrites the prompt cache from the
//! first changed result on, so the saving has to beat that. While it stays put the request prefix is byte-stable.
use crate::mask;
use hx_ai::Message;
@@ -26,6 +26,9 @@ pub struct View {
pub mask: usize,
}
/// Context size above which old tool results start to be masked, at most.
const MASK_FROM_MAX: usize = 60_000;
pub struct ContextWindow {
compact_at: usize,
/// `(real tokens, messages they covered)` from the last request.
@@ -66,11 +69,19 @@ impl ContextWindow {
self.tokens(msgs) > self.compact_at
}
/// Masking starts once the context passes a third of the compaction threshold, but never later
/// than `MASK_FROM_MAX` tokens: with a 1M window a third is ~300k, far beyond where old results
/// already cost more to resend than one cache rewrite.
// ponytail: 60k comes from two real sessions (contexts of 72k and 105k); tune with `hx stats`.
fn mask_from(&self) -> usize {
(self.compact_at / 3).min(MASK_FROM_MAX)
}
/// What to send for the next request. Advances the masking boundary if it is due, so call it
/// once per request, after any compaction.
pub fn view(&mut self, msgs: Vec<Message>) -> View {
let target = mask::target_boundary(&msgs);
if target > self.boundary && self.tokens(&msgs) > self.compact_at / 3 {
if target > self.boundary && self.tokens(&msgs) > self.mask_from() {
self.boundary = target;
}
View {
@@ -191,6 +202,12 @@ mod tests {
assert_eq!(w.view(history(2)).mask, 8);
}
#[test]
fn mask_threshold_is_capped() {
assert_eq!(ContextWindow::new(900_000).mask_from(), MASK_FROM_MAX);
assert_eq!(ContextWindow::new(90_000).mask_from(), 30_000);
}
#[test]
fn reset_forgets_the_reading_and_the_boundary() {
let m = history(16);