add tail_messages() for fast reverse transcript scanning
Reverse-scans the mmap'd transcript using JsonlBackwardIter, collecting user/assistant messages up to a token budget, stopping at the compaction boundary. Returns messages in chronological order. resolve_conversation() now uses this instead of parsing the entire file through extract_conversation + split_on_compaction. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
a03bf390a8
commit
e39096b787
2 changed files with 71 additions and 20 deletions
|
|
@ -455,19 +455,13 @@ fn resolve_conversation() -> String {
|
|||
|
||||
let Some(path) = transcript else { return String::new() };
|
||||
let path_str = path.to_string_lossy();
|
||||
let messages = match super::enrich::extract_conversation(&path_str) {
|
||||
Ok(m) => m,
|
||||
Err(_) => return String::new(),
|
||||
};
|
||||
|
||||
// Take the last segment (post-compaction)
|
||||
let segments = super::enrich::split_on_compaction(messages);
|
||||
let Some(segment) = segments.last() else { return String::new() };
|
||||
let messages = crate::transcript::tail_messages(&path_str, 25_000);
|
||||
if messages.is_empty() { return String::new(); }
|
||||
|
||||
// Format and take the tail
|
||||
let cfg = crate::config::get();
|
||||
let mut text = String::new();
|
||||
for (_, role, content, ts) in segment {
|
||||
for (role, content, ts) in &messages {
|
||||
let name = if role == "user" { &cfg.user_name } else { &cfg.assistant_name };
|
||||
if !ts.is_empty() {
|
||||
text.push_str(&format!("**{}** {}: {}\n\n", name, &ts[..ts.len().min(19)], content));
|
||||
|
|
@ -475,17 +469,7 @@ fn resolve_conversation() -> String {
|
|||
text.push_str(&format!("**{}:** {}\n\n", name, content));
|
||||
}
|
||||
}
|
||||
|
||||
// Tail: keep last ~100K chars
|
||||
const MAX_CHARS: usize = 100_000;
|
||||
if text.len() > MAX_CHARS {
|
||||
// Find a clean line break near the cut point
|
||||
let start = text.len() - MAX_CHARS;
|
||||
let start = text[start..].find('\n').map(|i| start + i + 1).unwrap_or(start);
|
||||
text[start..].to_string()
|
||||
} else {
|
||||
text
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
/// Get recently surfaced memory keys for the current session.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue