session: add TranscriptInfo struct, consolidate transcript lookups
TranscriptInfo provides cached transcript metadata (path, size) with a single read. Replaces scattered fs::metadata calls in surface_observe_cycle, reflection_cycle, resolve_conversation, and resolve_memory_ratio. Session::transcript() resolves the path from transcript_path or by searching projects dir, returning a TranscriptInfo. Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
parent
8ccc30d97e
commit
bb2e3b9fbb
3 changed files with 79 additions and 45 deletions
|
|
@ -572,26 +572,14 @@ fn resolve(
|
|||
/// Reads POC_SESSION_ID to find the transcript, extracts the last
|
||||
/// segment (post-compaction), returns the tail (~100K chars).
|
||||
fn resolve_conversation(budget: Option<usize>) -> String {
|
||||
let session_id = std::env::var("POC_SESSION_ID").unwrap_or_default();
|
||||
if session_id.is_empty() { return String::new(); }
|
||||
let session = crate::session::Session::from_env();
|
||||
let transcript = session.as_ref()
|
||||
.map(|s| s.transcript())
|
||||
.unwrap_or_else(crate::session::TranscriptInfo::empty);
|
||||
|
||||
let projects = crate::config::get().projects_dir.clone();
|
||||
// Find the transcript file matching this session
|
||||
let mut transcript = None;
|
||||
if let Ok(dirs) = std::fs::read_dir(&projects) {
|
||||
for dir in dirs.filter_map(|e| e.ok()) {
|
||||
let path = dir.path().join(format!("{}.jsonl", session_id));
|
||||
if path.exists() {
|
||||
transcript = Some(path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !transcript.exists() { return String::new(); }
|
||||
|
||||
let Some(path) = transcript else { return String::new() };
|
||||
let path_str = path.to_string_lossy();
|
||||
|
||||
let Some(iter) = crate::transcript::TailMessages::open(&path_str) else {
|
||||
let Some(iter) = crate::transcript::TailMessages::open(&transcript.path) else {
|
||||
return String::new();
|
||||
};
|
||||
|
||||
|
|
@ -680,22 +668,14 @@ fn resolve_memory_ratio() -> String {
|
|||
let state_dir = std::path::PathBuf::from("/tmp/claude-memory-search");
|
||||
|
||||
// Get post-compaction transcript size
|
||||
let projects = crate::config::get().projects_dir.clone();
|
||||
let transcript_size: u64 = std::fs::read_dir(&projects).ok()
|
||||
.and_then(|dirs| {
|
||||
for dir in dirs.filter_map(|e| e.ok()) {
|
||||
let path = dir.path().join(format!("{}.jsonl", session_id));
|
||||
if path.exists() {
|
||||
let file_len = path.metadata().map(|m| m.len()).unwrap_or(0);
|
||||
let compaction_offset: u64 = std::fs::read_to_string(
|
||||
state_dir.join(format!("compaction-{}", session_id))
|
||||
).ok().and_then(|s| s.trim().parse().ok()).unwrap_or(0);
|
||||
return Some(file_len.saturating_sub(compaction_offset));
|
||||
}
|
||||
}
|
||||
None
|
||||
})
|
||||
.unwrap_or(0);
|
||||
let session = crate::session::Session::from_env();
|
||||
let transcript = session.as_ref()
|
||||
.map(|s| s.transcript())
|
||||
.unwrap_or_else(crate::session::TranscriptInfo::empty);
|
||||
let compaction_offset: u64 = std::fs::read_to_string(
|
||||
state_dir.join(format!("compaction-{}", session_id))
|
||||
).ok().and_then(|s| s.trim().parse().ok()).unwrap_or(0);
|
||||
let transcript_size = transcript.size.saturating_sub(compaction_offset);
|
||||
|
||||
if transcript_size == 0 {
|
||||
return "0% of context is recalled memories (new session)".to_string();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue