Merge pull request 'Masking: start at min(compact_at/3, 60k) tokens' (#117) from mask-abs-threshold into main
ci / check (push) Canceled after 0s
ci / check (push) Canceled after 0s
This commit was merged in pull request #117.
This commit is contained in:
commit
1b63c27a52
1 file changed
+29
-4
@@ -23,6 +23,9 @@ pub enum Error {
|
||||
MaxTurns(u32),
|
||||
}
|
||||
|
||||
/// Context size above which old tool results start to be masked, at most.
|
||||
const MASK_FROM_MAX: usize = 60_000;
|
||||
|
||||
pub struct Agent {
|
||||
provider: Arc<dyn Provider>,
|
||||
pub tools: Registry,
|
||||
@@ -198,9 +201,12 @@ impl Agent {
|
||||
self.mask_boundary = 0;
|
||||
}
|
||||
|
||||
/// Masking starts once the context passes this share of the compaction threshold.
|
||||
/// 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
|
||||
(self.compact_at / 3).min(MASK_FROM_MAX)
|
||||
}
|
||||
|
||||
fn push(&mut self, m: Message) -> Result<(), Error> {
|
||||
@@ -590,7 +596,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| {
|
||||
@@ -619,7 +625,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!(
|
||||
@@ -633,6 +639,25 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mask_threshold_is_capped() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let cx = ToolCtx {
|
||||
cwd: dir.path().into(),
|
||||
state: Default::default(),
|
||||
};
|
||||
let (mut a, _h) = Agent::new(
|
||||
Arc::new(Script(Mutex::new(vec![]), Mutex::new(vec![]))),
|
||||
Registry::default(),
|
||||
Session::open(dir.path().join("s.jsonl")).unwrap(),
|
||||
cx,
|
||||
);
|
||||
a.set_compact_at(900_000);
|
||||
assert_eq!(a.mask_from(), MASK_FROM_MAX);
|
||||
a.set_compact_at(90_000);
|
||||
assert_eq!(a.mask_from(), 30_000);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn compaction_inside_tool_loop() {
|
||||
let text = |t: &str| asst(vec![ContentBlock::Text { text: t.into() }]);
|
||||
|
||||
Reference in new issue
Block a user