WIP: trim_entries dedup, context_window rename, compact simplification
Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
809679b6ce
commit
d419587c1b
3 changed files with 58 additions and 53 deletions
|
|
@ -909,23 +909,24 @@ impl Agent {
|
|||
self.do_compact();
|
||||
}
|
||||
|
||||
/// Internal compaction — rebuilds context window from current messages.
|
||||
fn do_compact(&mut self) {
|
||||
let conversation: Vec<Message> = self.context.entries.iter()
|
||||
.map(|e| e.api_message().clone()).collect();
|
||||
let messages = crate::thought::context::trim_conversation(
|
||||
/// Dedup memory entries, trim to fit, reload journal for new time range.
|
||||
fn trim_and_reload(&mut self, entries: &[ConversationEntry]) {
|
||||
self.context.entries = crate::thought::context::trim_entries(
|
||||
&self.context,
|
||||
&conversation,
|
||||
&self.client.model,
|
||||
entries,
|
||||
&self.tokenizer,
|
||||
);
|
||||
self.context.entries = messages.into_iter()
|
||||
.map(ConversationEntry::Message).collect();
|
||||
self.load_startup_journal();
|
||||
self.last_prompt_tokens = 0;
|
||||
|
||||
self.publish_context_state();
|
||||
}
|
||||
|
||||
/// Internal compaction — dedup memory entries and trim to fit.
|
||||
fn do_compact(&mut self) {
|
||||
let entries = self.context.entries.clone();
|
||||
self.trim_and_reload(&entries);
|
||||
}
|
||||
|
||||
/// Emergency compaction using stored config — called on context overflow.
|
||||
fn emergency_compact(&mut self) {
|
||||
self.do_compact();
|
||||
|
|
@ -964,32 +965,14 @@ impl Agent {
|
|||
}
|
||||
};
|
||||
|
||||
// Filter out system messages, keep everything else (including Memory entries)
|
||||
// Filter out system messages, dedup memory, trim to fit
|
||||
let entries: Vec<ConversationEntry> = entries
|
||||
.into_iter()
|
||||
.filter(|e| e.message().role != Role::System)
|
||||
.collect();
|
||||
|
||||
// Trim to fit context budget
|
||||
let n = entries.len();
|
||||
let conversation: Vec<Message> = entries.iter()
|
||||
.map(|e| e.api_message().clone()).collect();
|
||||
let trimmed = crate::thought::context::trim_conversation(
|
||||
&self.context,
|
||||
&conversation,
|
||||
&self.client.model,
|
||||
&self.tokenizer,
|
||||
);
|
||||
// Keep only the entries that survived trimming (by count from the end)
|
||||
let keep = trimmed.len();
|
||||
self.context.entries = entries.into_iter()
|
||||
.skip(n.saturating_sub(keep))
|
||||
.collect();
|
||||
|
||||
self.trim_and_reload(&entries);
|
||||
dbglog!("[restore] {} entries, journal: {} entries",
|
||||
self.context.entries.len(), self.context.journal.len());
|
||||
self.last_prompt_tokens = 0;
|
||||
self.publish_context_state();
|
||||
true
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue