Session ops and ContextPolicy in hx-core
This commit is contained in:
1 parent
69d5ae4581
commit
be74dd9fec
7 files changed
+205
-105
No files matched your search
@@ -29,8 +29,19 @@ pub struct View {
|
||||
/// Context size above which old tool results start to be masked, at most.
|
||||
const MASK_FROM_MAX: usize = 60_000;
|
||||
|
||||
/// Where the compaction threshold comes from.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ContextPolicy {
|
||||
/// Compact above exactly this many tokens (config `compact_at`).
|
||||
Fixed(usize),
|
||||
/// Derived from the model's context window, leaving room for a reply.
|
||||
FromModel { window: u64 },
|
||||
}
|
||||
|
||||
pub struct ContextWindow {
|
||||
compact_at: usize,
|
||||
policy: ContextPolicy,
|
||||
/// Room kept free for the reply (the request's `max_tokens`).
|
||||
reserve: u32,
|
||||
/// `(real tokens, messages they covered)` from the last request.
|
||||
reading: Option<(usize, usize)>,
|
||||
/// Assistant turns whose big tool results are masked.
|
||||
@@ -41,18 +52,36 @@ impl ContextWindow {
|
||||
/// Compact once the context exceeds `compact_at` tokens.
|
||||
pub fn new(compact_at: usize) -> Self {
|
||||
Self {
|
||||
compact_at,
|
||||
policy: ContextPolicy::Fixed(compact_at),
|
||||
reserve: 0,
|
||||
reading: None,
|
||||
boundary: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compact_at(&self) -> usize {
|
||||
self.compact_at
|
||||
match self.policy {
|
||||
ContextPolicy::Fixed(n) => n,
|
||||
ContextPolicy::FromModel { window } => {
|
||||
hx_ai::models::compact_threshold(window, self.reserve)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_compact_at(&mut self, n: usize) {
|
||||
self.compact_at = n;
|
||||
pub fn set_policy(&mut self, p: ContextPolicy) {
|
||||
self.policy = p;
|
||||
}
|
||||
|
||||
/// Room for the reply; only matters for [`ContextPolicy::FromModel`], and any call order works.
|
||||
pub fn set_reserve(&mut self, tokens: u32) {
|
||||
self.reserve = tokens;
|
||||
}
|
||||
|
||||
/// The model changed: follow its window unless the threshold is fixed.
|
||||
pub fn set_model_window(&mut self, window: u64) {
|
||||
if let ContextPolicy::FromModel { window: w } = &mut self.policy {
|
||||
*w = window;
|
||||
}
|
||||
}
|
||||
|
||||
/// Context size of `msgs`: the last real reading plus an estimate of what was added since;
|
||||
@@ -66,7 +95,7 @@ impl ContextWindow {
|
||||
}
|
||||
|
||||
pub fn should_compact(&self, msgs: &[Message]) -> bool {
|
||||
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
|
||||
@@ -74,7 +103,7 @@ impl ContextWindow {
|
||||
/// 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)
|
||||
(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
|
||||
|
||||
Reference in new issue
Block a user