Merge main into context-window: mask cap (#117) moves into ContextWindow
This commit is contained in:
commit
cd52d409b2
2 files changed
+22
-5
No files matched your search
@@ -574,7 +574,7 @@ mod tests {
|
|||||||
Session::open(dir.path().join("s.jsonl")).unwrap(),
|
Session::open(dir.path().join("s.jsonl")).unwrap(),
|
||||||
cx,
|
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();
|
a.run("go").await.unwrap();
|
||||||
let elided = |r: &Request| {
|
let elided = |r: &Request| {
|
||||||
r.messages.iter().any(|m| {
|
r.messages.iter().any(|m| {
|
||||||
@@ -603,7 +603,7 @@ mod tests {
|
|||||||
Session::open(dir.path().join("s2.jsonl")).unwrap(),
|
Session::open(dir.path().join("s2.jsonl")).unwrap(),
|
||||||
cx,
|
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();
|
b.run("go").await.unwrap();
|
||||||
assert!(script.1.lock().unwrap().iter().any(elided));
|
assert!(script.1.lock().unwrap().iter().any(elided));
|
||||||
assert!(
|
assert!(
|
||||||
|
|||||||
@@ -11,8 +11,8 @@
|
|||||||
//! drops both; call it whenever the history is replaced or rewritten (new session, fork, clear,
|
//! 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.
|
//! 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
|
//! - 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
|
//! threshold (at most [`MASK_FROM_MAX`] tokens): every move rewrites the prompt cache from the
|
||||||
//! saving has to beat that. While it stays put the request prefix is byte-stable.
|
//! 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 crate::mask;
|
||||||
use hx_ai::Message;
|
use hx_ai::Message;
|
||||||
@@ -26,6 +26,9 @@ pub struct View {
|
|||||||
pub mask: usize,
|
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 {
|
pub struct ContextWindow {
|
||||||
compact_at: usize,
|
compact_at: usize,
|
||||||
/// `(real tokens, messages they covered)` from the last request.
|
/// `(real tokens, messages they covered)` from the last request.
|
||||||
@@ -66,11 +69,19 @@ impl ContextWindow {
|
|||||||
self.tokens(msgs) > self.compact_at
|
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
|
/// What to send for the next request. Advances the masking boundary if it is due, so call it
|
||||||
/// once per request, after any compaction.
|
/// once per request, after any compaction.
|
||||||
pub fn view(&mut self, msgs: Vec<Message>) -> View {
|
pub fn view(&mut self, msgs: Vec<Message>) -> View {
|
||||||
let target = mask::target_boundary(&msgs);
|
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;
|
self.boundary = target;
|
||||||
}
|
}
|
||||||
View {
|
View {
|
||||||
@@ -191,6 +202,12 @@ mod tests {
|
|||||||
assert_eq!(w.view(history(2)).mask, 8);
|
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]
|
#[test]
|
||||||
fn reset_forgets_the_reading_and_the_boundary() {
|
fn reset_forgets_the_reading_and_the_boundary() {
|
||||||
let m = history(16);
|
let m = history(16);
|
||||||
|
|||||||
Reference in new issue
Block a user