Fast startup: mmap backward scan instead of reading full log

Uses JsonlBackwardIter (SIMD memrchr3) to scan the conversation log
newest-first without reading/parsing the whole file. Stops as soon
as the conversation budget is full. Only the kept nodes get
retokenized and pushed into context.

18MB log → only tokenize the ~50 nodes that fit in the budget.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
Kent Overstreet 2026-04-09 13:09:26 -04:00
parent 7da3efc5df
commit 949dacd861
2 changed files with 35 additions and 32 deletions

View file

@ -568,18 +568,17 @@ impl Agent {
}
pub async fn restore_from_log(&self) -> bool {
let all_nodes = {
let tail = {
let ctx = self.context.lock().await;
match &ctx.conversation_log {
Some(log) => match log.read_nodes(64 * 1024 * 1024) {
Ok(nodes) if !nodes.is_empty() => nodes,
_ => return false,
Some(log) => match log.read_tail() {
Ok(t) => t,
Err(_) => return false,
},
None => return false,
}
};
// Walk backwards from the tail, retokenize, stop at budget
let budget = context::context_budget_tokens();
let fixed = {
let ctx = self.context.lock().await;
@ -588,9 +587,10 @@ impl Agent {
};
let conv_budget = budget.saturating_sub(fixed);
// Walk backwards (newest first), retokenize, stop at budget
let mut kept = Vec::new();
let mut total = 0;
for node in all_nodes.into_iter().rev() {
for node in tail.iter() {
let node = node.retokenize();
let tok = node.tokens();
if total + tok > conv_budget && !kept.is_empty() { break; }