loopd: compaction threshold and the fallback summary

Co-Authored-By: ornith-1.5-35b-a3b <agent@wntrmute.dev>
This commit is contained in:
2026-10-03 00:04:03 -07:00
co-authored by ornith-1.5-35b-a3b
parent 3716595950
commit 32e81b51fb
5 changed files with 121 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
//! Compaction: when a session starts a new epoch, the summary call, and writing the next files
//! (M5d spec, section 3). Later tasks fill the call and the write.
use bxcore::{LogRecord, Usage};
/// True when `cache_n + prompt_n` is at or above `threshold * n_ctx`.
pub fn due(usage: &Usage, threshold: f64, n_ctx: u64) -> bool {
let total = usage.cache_n.saturating_add(usage.prompt_n) as f64;
total >= threshold * (n_ctx as f64)
}
/// The epoch's `User` texts, newest last, joined with `"\n\n"`, then `cap_chars`.
pub fn fallback_summary(records: &[LogRecord], summary_chars: usize) -> String {
let contents: Vec<&str> = records
.iter()
.filter_map(|r| match r {
LogRecord::User { content, .. } => Some(content.as_str()),
_ => None,
})
.collect();
let joined = contents
.iter()
.rev()
.cloned()
.collect::<Vec<_>>()
.join("\n\n");
cap_chars(&joined, summary_chars)
}
pub(crate) fn cap_chars(s: &str, n: usize) -> String {
s.chars().take(n).collect()
}
+1
View File
@@ -3,6 +3,7 @@
pub mod baseline;
pub mod broker_port;
pub mod channel;
pub mod compact;
pub mod config;
pub mod http;
pub mod llama;