tail_messages: parse each object once, skip non-message types early

Was parsing every object twice (compaction check + message extract)
and running contains_bytes on every object for the compaction marker.
Now: quick byte pre-filter for "user"/"assistant", parse once, check
compaction after text extraction.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kent Overstreet 2026-03-22 03:05:04 -04:00
parent e39096b787
commit d7d631d77d

View file

@ -158,20 +158,11 @@ pub fn tail_messages(path: &str, max_tokens: usize) -> Vec<(String, String, Stri
for obj_bytes in JsonlBackwardIter::new(&mmap) { for obj_bytes in JsonlBackwardIter::new(&mmap) {
if token_count >= max_tokens { break; } if token_count >= max_tokens { break; }
// Stop at compaction boundary // Quick byte check: skip objects that aren't user/assistant
if contains_bytes(obj_bytes, compaction_marker) { // (avoids parsing large tool_result / system objects)
let obj: Value = match serde_json::from_slice(obj_bytes) { if !contains_bytes(obj_bytes, b"\"user\"")
Ok(v) => v, && !contains_bytes(obj_bytes, b"\"assistant\"") {
Err(_) => continue, continue;
};
if obj.get("type").and_then(|v| v.as_str()) == Some("user") {
if let Some(c) = obj.get("message")
.and_then(|m| m.get("content"))
.and_then(|c| c.as_str())
&& c.starts_with("This session is being continued") {
break;
}
}
} }
let obj: Value = match serde_json::from_slice(obj_bytes) { let obj: Value = match serde_json::from_slice(obj_bytes) {
@ -196,6 +187,11 @@ pub fn tail_messages(path: &str, max_tokens: usize) -> Vec<(String, String, Stri
}; };
if text.is_empty() { continue; } if text.is_empty() { continue; }
// Stop at compaction boundary
if msg_type == "user" && text.starts_with("This session is being continued") {
break;
}
let timestamp = obj.get("timestamp") let timestamp = obj.get("timestamp")
.and_then(|v| v.as_str()) .and_then(|v| v.as_str())
.unwrap_or("") .unwrap_or("")