From d7d631d77d7293e311738ef50abd440709345fd1 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Sun, 22 Mar 2026 03:05:04 -0400 Subject: [PATCH] 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) --- poc-memory/src/transcript.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/poc-memory/src/transcript.rs b/poc-memory/src/transcript.rs index 3fe3679..3855768 100644 --- a/poc-memory/src/transcript.rs +++ b/poc-memory/src/transcript.rs @@ -158,20 +158,11 @@ pub fn tail_messages(path: &str, max_tokens: usize) -> Vec<(String, String, Stri for obj_bytes in JsonlBackwardIter::new(&mmap) { if token_count >= max_tokens { break; } - // Stop at compaction boundary - if contains_bytes(obj_bytes, compaction_marker) { - let obj: Value = match serde_json::from_slice(obj_bytes) { - Ok(v) => v, - Err(_) => 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; - } - } + // Quick byte check: skip objects that aren't user/assistant + // (avoids parsing large tool_result / system objects) + if !contains_bytes(obj_bytes, b"\"user\"") + && !contains_bytes(obj_bytes, b"\"assistant\"") { + continue; } 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; } + // Stop at compaction boundary + if msg_type == "user" && text.starts_with("This session is being continued") { + break; + } + let timestamp = obj.get("timestamp") .and_then(|v| v.as_str()) .unwrap_or("")