session: --session flag, journal agent cycle, conversation bytes config
- memory-search: add --session flag for multi-session support - config: add surface_conversation_bytes option - journal_agent_cycle: trigger journal agent every 10KB of conversation - Session::from_id() constructor Co-Authored-By: ProofOfConcept <poc@bcachefs.org>
This commit is contained in:
parent
7c0c376e0f
commit
84c78f7ae1
4 changed files with 91 additions and 18 deletions
|
|
@ -231,6 +231,60 @@ fn surface_agent_cycle(session: &Session, out: &mut String, log_f: &mut File) {
|
|||
}
|
||||
}
|
||||
|
||||
const JOURNAL_INTERVAL_BYTES: u64 = 10_000;
|
||||
|
||||
fn journal_agent_cycle(session: &Session, log_f: &mut File) {
|
||||
let offset_path = session.path("journal-offset");
|
||||
let pid_path = session.path("journal-pid");
|
||||
|
||||
// Check if a previous run is still going
|
||||
if let Ok(content) = fs::read_to_string(&pid_path) {
|
||||
let pid: u32 = content.split('\t').next()
|
||||
.and_then(|s| s.trim().parse().ok()).unwrap_or(0);
|
||||
if pid != 0 && unsafe { libc::kill(pid as i32, 0) == 0 } {
|
||||
let _ = writeln!(log_f, "journal: still running (pid {})", pid);
|
||||
return;
|
||||
}
|
||||
}
|
||||
fs::remove_file(&pid_path).ok();
|
||||
|
||||
// Check transcript size vs last run
|
||||
let transcript_size = fs::metadata(&session.transcript_path)
|
||||
.map(|m| m.len()).unwrap_or(0);
|
||||
let last_offset: u64 = fs::read_to_string(&offset_path).ok()
|
||||
.and_then(|s| s.trim().parse().ok()).unwrap_or(0);
|
||||
|
||||
if transcript_size.saturating_sub(last_offset) < JOURNAL_INTERVAL_BYTES {
|
||||
return;
|
||||
}
|
||||
|
||||
let _ = writeln!(log_f, "journal: spawning (transcript {}, last {})",
|
||||
transcript_size, last_offset);
|
||||
|
||||
// Save current offset
|
||||
fs::write(&offset_path, transcript_size.to_string()).ok();
|
||||
|
||||
// Spawn journal agent — it writes directly to the store via memory tools
|
||||
let log_dir = crate::store::memory_dir().join("logs");
|
||||
fs::create_dir_all(&log_dir).ok();
|
||||
let journal_log = fs::File::create(log_dir.join("journal-agent.log"))
|
||||
.unwrap_or_else(|_| fs::File::create("/dev/null").unwrap());
|
||||
|
||||
if let Ok(child) = Command::new("poc-memory")
|
||||
.args(["agent", "run", "journal", "--count", "1", "--local"])
|
||||
.env("POC_SESSION_ID", &session.session_id)
|
||||
.stdout(journal_log.try_clone().unwrap_or_else(|_| fs::File::create("/dev/null").unwrap()))
|
||||
.stderr(journal_log)
|
||||
.spawn()
|
||||
{
|
||||
let pid = child.id();
|
||||
let ts = now_secs();
|
||||
if let Ok(mut f) = fs::File::create(&pid_path) {
|
||||
write!(f, "{}\t{}", pid, ts).ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn cleanup_stale_files(dir: &Path, max_age: Duration) {
|
||||
let entries = match fs::read_dir(dir) {
|
||||
Ok(e) => e,
|
||||
|
|
@ -308,6 +362,7 @@ fn hook(session: &Session) -> String {
|
|||
let cfg = crate::config::get();
|
||||
if cfg.surface_hooks.iter().any(|h| h == &session.hook_event) {
|
||||
surface_agent_cycle(session, &mut out, &mut log_f);
|
||||
journal_agent_cycle(session, &mut log_f);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue