From 8dd6ce833b2003c00ba3e669dd2e2298aa4009cb Mon Sep 17 00:00:00 2001 From: hermes Date: Fri, 2 Oct 2026 14:59:18 +0000 Subject: [PATCH] mask: start masking at min(compact_at/3, 60k) tokens --- crates/hx-core/src/agent.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/hx-core/src/agent.rs b/crates/hx-core/src/agent.rs index 7996b84..97d373a 100644 --- a/crates/hx-core/src/agent.rs +++ b/crates/hx-core/src/agent.rs @@ -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, 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() }]); -- 2.54.1