Deepen context-window management into ContextWindow #116
No files matched your search
@@ -574,7 +574,7 @@ mod tests {
|
||||
Session::open(dir.path().join("s.jsonl")).unwrap(),
|
||||
cx,
|
||||
);
|
||||
a.set_compact_at(1_000_000); // masking only from ~333k tokens: never in this run
|
||||
a.set_compact_at(1_000_000); // capped at 60k tokens: this run stays below it
|
||||
a.run("go").await.unwrap();
|
||||
let elided = |r: &Request| {
|
||||
r.messages.iter().any(|m| {
|
||||
@@ -603,7 +603,7 @@ mod tests {
|
||||
Session::open(dir.path().join("s2.jsonl")).unwrap(),
|
||||
cx,
|
||||
);
|
||||
b.set_compact_at(3_000);
|
||||
b.set_compact_at(3_000); // a third = 1k tokens, below the cap
|
||||
b.run("go").await.unwrap();
|
||||
assert!(script.1.lock().unwrap().iter().any(elided));
|
||||
assert!(
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user