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
|
|
@ -1,7 +1,10 @@
|
|||
// session.rs — Session state for ambient hooks and agent interactions
|
||||
// session.rs — Session state and transcript info
|
||||
//
|
||||
// Tracks per-session state (seen set, state directory) across hook
|
||||
// Session: per-session state (seen set, state directory) across hook
|
||||
// invocations. Created from hook JSON input or POC_SESSION_ID env var.
|
||||
//
|
||||
// TranscriptInfo: cached metadata about the current session's transcript
|
||||
// file — size, path, compaction offset. Read once, used by all callers.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
|
|
@ -53,4 +56,51 @@ impl Session {
|
|||
pub fn seen(&self) -> HashSet<String> {
|
||||
super::hippocampus::memory_search::load_seen(&self.state_dir, &self.session_id)
|
||||
}
|
||||
|
||||
/// Get transcript metadata, resolving the path if needed.
|
||||
pub fn transcript(&self) -> TranscriptInfo {
|
||||
if !self.transcript_path.is_empty() {
|
||||
return TranscriptInfo::from_path(&self.transcript_path);
|
||||
}
|
||||
|
||||
// Find transcript by session ID in projects dir
|
||||
let projects = crate::config::get().projects_dir.clone();
|
||||
if let Ok(dirs) = fs::read_dir(&projects) {
|
||||
for dir in dirs.filter_map(|e| e.ok()) {
|
||||
let path = dir.path().join(format!("{}.jsonl", self.session_id));
|
||||
if path.exists() {
|
||||
return TranscriptInfo::from_path(&path.to_string_lossy());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TranscriptInfo::empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// Cached transcript metadata — read once, use everywhere.
|
||||
pub struct TranscriptInfo {
|
||||
pub path: String,
|
||||
pub size: u64,
|
||||
}
|
||||
|
||||
impl TranscriptInfo {
|
||||
pub fn from_path(path: &str) -> Self {
|
||||
let size = fs::metadata(path).map(|m| m.len()).unwrap_or(0);
|
||||
TranscriptInfo {
|
||||
path: path.to_string(),
|
||||
size,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn empty() -> Self {
|
||||
TranscriptInfo {
|
||||
path: String::new(),
|
||||
size: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exists(&self) -> bool {
|
||||
!self.path.is_empty() && self.size > 0
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue