replace tail_messages with TailMessages iterator
TailMessages is a proper iterator that yields (role, text, timestamp) newest-first. Owns the mmap internally. Caller decides when to stop. resolve_conversation collects up to 200KB, then reverses to chronological order. No compaction check needed — the byte budget naturally limits how far back we scan. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
6c41b50e04
commit
ecc2cb7b20
3 changed files with 108 additions and 66 deletions
|
|
@ -142,66 +142,97 @@ fn contains_bytes(haystack: &[u8], needle: &[u8]) -> bool {
|
|||
haystack.windows(needle.len()).any(|w| w == needle)
|
||||
}
|
||||
|
||||
/// Reverse-scan a transcript file, collecting user/assistant messages
|
||||
/// until `max_tokens` tokens (~4 chars each) are accumulated. Stops at
|
||||
/// the last compaction boundary. Returns messages in chronological order.
|
||||
pub fn tail_messages(path: &str, max_bytes: usize) -> Vec<(String, String, String)> {
|
||||
let (mmap, _file) = match mmap_transcript(path) {
|
||||
Some(v) => v,
|
||||
None => return Vec::new(),
|
||||
};
|
||||
/// Reverse iterator over user/assistant messages in a transcript file.
|
||||
/// Yields (role, text, timestamp) tuples newest-first. The caller decides
|
||||
/// when to stop (byte budget, count, etc).
|
||||
pub struct TailMessages {
|
||||
_file: fs::File,
|
||||
mmap: Mmap,
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
let mut messages: Vec<(String, String, String)> = Vec::new();
|
||||
let mut total_bytes = 0;
|
||||
|
||||
for obj_bytes in JsonlBackwardIter::new(&mmap) {
|
||||
if total_bytes >= max_bytes { 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) {
|
||||
Ok(v) => v,
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
let msg_type = obj.get("type").and_then(|v| v.as_str()).unwrap_or("");
|
||||
if msg_type != "user" && msg_type != "assistant" { continue; }
|
||||
|
||||
let msg = obj.get("message").unwrap_or(&obj);
|
||||
let text = match msg.get("content") {
|
||||
Some(Value::String(s)) => s.clone(),
|
||||
Some(Value::Array(arr)) => {
|
||||
arr.iter()
|
||||
.filter(|b| b.get("type").and_then(|v| v.as_str()) == Some("text"))
|
||||
.filter_map(|b| b.get("text").and_then(|v| v.as_str()))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
_ => 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")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
|
||||
total_bytes += text.len();
|
||||
messages.push((msg_type.to_string(), text, timestamp));
|
||||
impl TailMessages {
|
||||
pub fn open(path: &str) -> Option<Self> {
|
||||
let (mmap, file) = mmap_transcript(path)?;
|
||||
let pos = mmap.len();
|
||||
Some(Self { _file: file, mmap, pos })
|
||||
}
|
||||
}
|
||||
|
||||
messages.reverse();
|
||||
messages
|
||||
impl Iterator for TailMessages {
|
||||
type Item = (String, String, String);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
loop {
|
||||
// Find closing }
|
||||
let close = loop {
|
||||
let p = memrchr3(b'{', b'}', b'"', &self.mmap[..self.pos])?;
|
||||
self.pos = p;
|
||||
if self.mmap[p] == b'}' { break p; }
|
||||
};
|
||||
|
||||
// Track brace depth to find matching {
|
||||
let mut depth: usize = 1;
|
||||
let mut in_string = false;
|
||||
let open = loop {
|
||||
let p = memrchr3(b'{', b'}', b'"', &self.mmap[..self.pos])?;
|
||||
self.pos = p;
|
||||
let ch = self.mmap[p];
|
||||
|
||||
if in_string {
|
||||
if ch == b'"' {
|
||||
let mut bs = 0;
|
||||
while p > bs + 1 && self.mmap[p - 1 - bs] == b'\\' {
|
||||
bs += 1;
|
||||
}
|
||||
if bs % 2 == 0 { in_string = false; }
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
match ch {
|
||||
b'"' => { in_string = true; }
|
||||
b'}' => { depth += 1; }
|
||||
b'{' => {
|
||||
depth -= 1;
|
||||
if depth == 0 { break p; }
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
};
|
||||
|
||||
let obj_bytes = &self.mmap[open..=close];
|
||||
|
||||
let obj: Value = match serde_json::from_slice(obj_bytes) {
|
||||
Ok(v) => v,
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
let msg_type = obj.get("type").and_then(|v| v.as_str()).unwrap_or("");
|
||||
if msg_type != "user" && msg_type != "assistant" { continue; }
|
||||
|
||||
let msg = obj.get("message").unwrap_or(&obj);
|
||||
let text = match msg.get("content") {
|
||||
Some(Value::String(s)) => s.clone(),
|
||||
Some(Value::Array(arr)) => {
|
||||
arr.iter()
|
||||
.filter(|b| b.get("type").and_then(|v| v.as_str()) == Some("text"))
|
||||
.filter_map(|b| b.get("text").and_then(|v| v.as_str()))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
if text.is_empty() { continue; }
|
||||
|
||||
let timestamp = obj.get("timestamp")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
|
||||
return Some((msg_type.to_string(), text, timestamp));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the timestamp of the compaction message at a given byte offset.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue