// hook — session hook: context injection + agent orchestration // // Called on each UserPromptSubmit to inject memory context and // orchestrate subconscious agents (surface-observe, journal, reflect). // Lives in subconscious/ because it's agent orchestration, not // memory storage. The memory-search binary is a thin CLI wrapper. use std::collections::HashSet; use std::fs; use std::fs::File; use std::io::Write; use std::path::Path; use std::process::Command; use std::time::{Duration, Instant, SystemTime}; /// Max bytes per context chunk (hook output limit is ~10K chars) const CHUNK_SIZE: usize = 9000; pub use crate::session::HookSession; /// Run the hook logic on parsed JSON input. Returns output to inject. pub fn run_hook(input: &str) -> String { let Some(session) = HookSession::from_json(input) else { return String::new() }; hook(&session) } /// Split context output into chunks of approximately `max_bytes`, breaking /// at section boundaries ("--- KEY (group) ---" lines). fn chunk_context(ctx: &str, max_bytes: usize) -> Vec { let mut sections: Vec = Vec::new(); let mut current = String::new(); for line in ctx.lines() { if line.starts_with("--- ") && line.ends_with(" ---") && !current.is_empty() { sections.push(std::mem::take(&mut current)); } if !current.is_empty() { current.push('\n'); } current.push_str(line); } if !current.is_empty() { sections.push(current); } let mut chunks: Vec = Vec::new(); let mut chunk = String::new(); for section in sections { if !chunk.is_empty() && chunk.len() + section.len() + 1 > max_bytes { chunks.push(std::mem::take(&mut chunk)); } if !chunk.is_empty() { chunk.push('\n'); } chunk.push_str(§ion); } if !chunk.is_empty() { chunks.push(chunk); } chunks } fn save_pending_chunks(dir: &Path, session_id: &str, chunks: &[String]) { let chunks_dir = dir.join(format!("chunks-{}", session_id)); let _ = fs::remove_dir_all(&chunks_dir); if chunks.is_empty() { return; } fs::create_dir_all(&chunks_dir).ok(); for (i, chunk) in chunks.iter().enumerate() { let path = chunks_dir.join(format!("{:04}", i)); fs::write(path, chunk).ok(); } } fn pop_pending_chunk(dir: &Path, session_id: &str) -> Option { let chunks_dir = dir.join(format!("chunks-{}", session_id)); if !chunks_dir.exists() { return None; } let mut entries: Vec<_> = fs::read_dir(&chunks_dir).ok()? .flatten() .filter(|e| e.file_type().map(|t| t.is_file()).unwrap_or(false)) .collect(); entries.sort_by_key(|e| e.file_name()); let first = entries.first()?; let content = fs::read_to_string(first.path()).ok()?; fs::remove_file(first.path()).ok(); if fs::read_dir(&chunks_dir).ok().map(|mut d| d.next().is_none()).unwrap_or(true) { fs::remove_dir(&chunks_dir).ok(); } Some(content) } fn generate_cookie() -> String { uuid::Uuid::new_v4().as_simple().to_string()[..12].to_string() } fn parse_seen_line(line: &str) -> &str { line.split_once('\t').map(|(_, key)| key).unwrap_or(line) } pub fn load_seen(dir: &Path, session_id: &str) -> HashSet { let path = dir.join(format!("seen-{}", session_id)); if path.exists() { fs::read_to_string(&path) .unwrap_or_default() .lines() .filter(|s| !s.is_empty()) .map(|s| parse_seen_line(s).to_string()) .collect() } else { HashSet::new() } } fn mark_seen(dir: &Path, session_id: &str, key: &str, seen: &mut HashSet) { if !seen.insert(key.to_string()) { return; } let path = dir.join(format!("seen-{}", session_id)); if let Ok(mut f) = fs::OpenOptions::new().create(true).append(true).open(path) { let ts = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S"); writeln!(f, "{}\t{}", ts, key).ok(); } } /// Output from a single agent orchestration cycle. pub struct AgentCycleOutput { /// Memory node keys surfaced by surface-observe. pub surfaced_keys: Vec, /// Freeform reflection text from the reflect agent. pub reflection: Option, /// How long we slept waiting for observe to catch up, if at all. pub sleep_secs: Option, } /// Per-agent runtime state visible to the TUI. pub struct AgentInfo { pub name: &'static str, pub pid: Option, pub phase: Option, pub last_log: Option, } /// Persistent state for the agent orchestration cycle. /// Created once, `trigger()` called on each user message. /// TUI reads `agents` and `last_output` for display. pub struct AgentCycleState { output_dir: std::path::PathBuf, log_file: Option, pub agents: Vec, pub last_output: AgentCycleOutput, } const AGENT_CYCLE_NAMES: &[&str] = &["surface-observe", "journal", "reflect"]; impl AgentCycleState { pub fn new(session_id: &str) -> Self { let output_dir = crate::store::memory_dir().join("agent-output"); let log_dir = dirs::home_dir().unwrap_or_default().join(".consciousness/logs"); fs::create_dir_all(&log_dir).ok(); let log_path = log_dir.join(format!("hook-{}", session_id)); let log_file = fs::OpenOptions::new() .create(true).append(true).open(log_path).ok(); let agents = AGENT_CYCLE_NAMES.iter() .map(|&name| AgentInfo { name, pid: None, phase: None, last_log: None }) .collect(); AgentCycleState { output_dir, log_file, agents, last_output: AgentCycleOutput { surfaced_keys: vec![], reflection: None, sleep_secs: None, }, } } fn log(&mut self, msg: std::fmt::Arguments) { if let Some(ref mut f) = self.log_file { let _ = write!(f, "{}", msg); } } fn update_agent(&mut self, name: &str, pid: Option, phase: Option) { if let Some(agent) = self.agents.iter_mut().find(|a| a.name == name) { agent.pid = pid; agent.phase = phase; } } /// Run all agent cycles. Call on each user message. pub fn trigger(&mut self, session: &HookSession) { let ts = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S"); self.log(format_args!("\n=== {} agent_cycles ===\n", ts)); cleanup_stale_files(&session.state_dir, Duration::from_secs(86400)); let (surfaced_keys, sleep_secs) = self.surface_observe_cycle(session); let reflection = self.reflection_cycle(session); self.journal_cycle(session); self.last_output = AgentCycleOutput { surfaced_keys, reflection, sleep_secs }; } } /// Standalone entry point for the Claude Code hook path. pub fn run_agent_cycles(session: &HookSession) -> AgentCycleOutput { let mut state = AgentCycleState::new(&session.session_id); state.trigger(session); state.last_output } /// Format agent cycle output for injection into a Claude Code session. pub fn format_agent_output(output: &AgentCycleOutput) -> String { let mut out = String::new(); if let Some(secs) = output.sleep_secs { out.push_str(&format!("Slept {secs:.2}s to let observe catch up\n")); } if !output.surfaced_keys.is_empty() { if let Ok(store) = crate::store::Store::load() { for key in &output.surfaced_keys { if let Some(rendered) = crate::cli::node::render_node(&store, key) { if !rendered.trim().is_empty() { use std::fmt::Write as _; writeln!(out, "--- {} (surfaced) ---", key).ok(); write!(out, "{}", rendered).ok(); } } } } } if let Some(ref reflection) = output.reflection { use std::fmt::Write as _; writeln!(out, "--- subconscious reflection ---").ok(); write!(out, "{}", reflection.trim()).ok(); } out } impl AgentCycleState { fn agent_dir(&self, name: &str) -> std::path::PathBuf { let dir = self.output_dir.join(name); fs::create_dir_all(&dir).ok(); dir } fn surface_observe_cycle(&mut self, session: &HookSession) -> (Vec, Option) { let state_dir = self.agent_dir("surface-observe"); let transcript = session.transcript(); let offset_path = state_dir.join("transcript-offset"); let last_offset: u64 = fs::read_to_string(&offset_path).ok() .and_then(|s| s.trim().parse().ok()) .unwrap_or(0); let timeout = crate::config::get() .surface_timeout_secs .unwrap_or(300) as u64; let live = crate::agents::knowledge::scan_pid_files(&state_dir, timeout); if let Some((phase, pid)) = live.first() { self.update_agent("surface-observe", Some(*pid), Some(phase.clone())); self.log(format_args!("alive pid-{}: phase={}\n", pid, phase)); } else { self.update_agent("surface-observe", None, None); } // Read surfaced keys let mut surfaced_keys = Vec::new(); let surface_path = state_dir.join("surface"); if let Ok(content) = fs::read_to_string(&surface_path) { let mut seen = session.seen(); let seen_path = session.path("seen"); for key in content.lines().map(|l| l.trim()).filter(|l| !l.is_empty()) { if !seen.insert(key.to_string()) { self.log(format_args!(" skip (seen): {}\n", key)); continue; } surfaced_keys.push(key.to_string()); if let Ok(mut f) = fs::OpenOptions::new() .create(true).append(true).open(&seen_path) { let ts = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S"); writeln!(f, "{}\t{}", ts, key).ok(); } self.log(format_args!(" surfaced: {}\n", key)); } fs::remove_file(&surface_path).ok(); } // Spawn new agent if needed let live = crate::agents::knowledge::scan_pid_files(&state_dir, timeout); let any_in_surface = live.iter().any(|(p, _)| p == "surface"); if any_in_surface { self.log(format_args!("agent in surface phase, waiting\n")); } else { if transcript.size > 0 { fs::write(&offset_path, transcript.size.to_string()).ok(); } let pid = crate::agents::knowledge::spawn_agent( "surface-observe", &state_dir, &session.session_id); self.update_agent("surface-observe", pid, Some("surface".into())); self.log(format_args!("spawned agent {:?}\n", pid)); } // Wait if agent is significantly behind let mut sleep_secs = None; let conversation_budget: u64 = 50_000; if !live.is_empty() && transcript.size > 0 { let behind = transcript.size.saturating_sub(last_offset); if behind > conversation_budget / 2 { let sleep_start = Instant::now(); self.log(format_args!("agent {}KB behind\n", behind / 1024)); for _ in 0..5 { std::thread::sleep(std::time::Duration::from_secs(1)); let still_live = crate::agents::knowledge::scan_pid_files(&state_dir, timeout); if still_live.is_empty() { break; } } let secs = (Instant::now() - sleep_start).as_secs_f64(); self.log(format_args!("slept {secs:.2}s\n")); sleep_secs = Some(secs); } } (surfaced_keys, sleep_secs) } fn reflection_cycle(&mut self, session: &HookSession) -> Option { let state_dir = self.agent_dir("reflect"); let offset_path = state_dir.join("transcript-offset"); let transcript = session.transcript(); let last_offset: u64 = fs::read_to_string(&offset_path).ok() .and_then(|s| s.trim().parse().ok()) .unwrap_or(0); const REFLECTION_INTERVAL: u64 = 100_000; if transcript.size.saturating_sub(last_offset) < REFLECTION_INTERVAL { return None; } let live = crate::agents::knowledge::scan_pid_files(&state_dir, 300); if let Some((phase, pid)) = live.first() { self.update_agent("reflect", Some(*pid), Some(phase.clone())); self.log(format_args!("reflect: already running pid {}\n", pid)); return None; } // Copy walked nodes from surface-observe let so_state = self.agent_dir("surface-observe"); if let Ok(walked) = fs::read_to_string(so_state.join("walked")) { fs::write(state_dir.join("walked"), &walked).ok(); } // Read and consume pending reflection let reflection = fs::read_to_string(state_dir.join("reflection")).ok() .filter(|s| !s.trim().is_empty()); if reflection.is_some() { fs::remove_file(state_dir.join("reflection")).ok(); self.log(format_args!("reflect: consumed reflection\n")); } fs::write(&offset_path, transcript.size.to_string()).ok(); let pid = crate::agents::knowledge::spawn_agent( "reflect", &state_dir, &session.session_id); self.update_agent("reflect", pid, Some("step-0".into())); self.log(format_args!("reflect: spawned {:?}\n", pid)); reflection } fn journal_cycle(&mut self, session: &HookSession) { let state_dir = self.agent_dir("journal"); let offset_path = state_dir.join("transcript-offset"); let transcript = session.transcript(); let last_offset: u64 = fs::read_to_string(&offset_path).ok() .and_then(|s| s.trim().parse().ok()) .unwrap_or(0); const JOURNAL_INTERVAL: u64 = 20_000; if transcript.size.saturating_sub(last_offset) < JOURNAL_INTERVAL { return; } let live = crate::agents::knowledge::scan_pid_files(&state_dir, 300); if let Some((phase, pid)) = live.first() { self.update_agent("journal", Some(*pid), Some(phase.clone())); self.log(format_args!("journal: already running pid {}\n", pid)); return; } fs::write(&offset_path, transcript.size.to_string()).ok(); let pid = crate::agents::knowledge::spawn_agent( "journal", &state_dir, &session.session_id); self.update_agent("journal", pid, Some("step-0".into())); self.log(format_args!("journal: spawned {:?}\n", pid)); } } // end impl AgentCycleState (cycle methods) fn cleanup_stale_files(dir: &Path, max_age: Duration) { let entries = match fs::read_dir(dir) { Ok(e) => e, Err(_) => return, }; let cutoff = SystemTime::now() - max_age; for entry in entries.flatten() { if let Ok(meta) = entry.metadata() { if let Ok(modified) = meta.modified() { if modified < cutoff { fs::remove_file(entry.path()).ok(); } } } } } fn hook(session: &HookSession) -> String { let start_time = Instant::now(); let mut out = String::new(); let is_compaction = crate::transcript::detect_new_compaction( &session.state_dir, &session.session_id, &session.transcript_path, ); let cookie_path = session.path("cookie"); let is_first = !cookie_path.exists(); let log_dir = dirs::home_dir().unwrap_or_default().join(".consciousness/logs"); fs::create_dir_all(&log_dir).ok(); let log_path = log_dir.join(format!("hook-{}", session.session_id)); let Ok(mut log_f) = fs::OpenOptions::new().create(true).append(true).open(log_path) else { return Default::default(); }; let ts = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S"); let _ = writeln!(log_f, "\n=== {} ({}) {} bytes ===", ts, session.hook_event, out.len()); let _ = writeln!(log_f, "is_first {is_first} is_compaction {is_compaction}"); if is_first || is_compaction { if is_compaction { fs::rename(&session.path("seen"), &session.path("seen-prev")).ok(); } else { fs::remove_file(&session.path("seen")).ok(); fs::remove_file(&session.path("seen-prev")).ok(); } fs::remove_file(&session.path("returned")).ok(); if is_first { fs::write(&cookie_path, generate_cookie()).ok(); } if let Ok(output) = Command::new("poc-memory").args(["admin", "load-context"]).output() { if output.status.success() { let ctx = String::from_utf8_lossy(&output.stdout).to_string(); if !ctx.trim().is_empty() { let mut ctx_seen = session.seen(); for line in ctx.lines() { if line.starts_with("--- ") && line.ends_with(" ---") { let inner = &line[4..line.len() - 4]; if let Some(paren) = inner.rfind(" (") { let key = inner[..paren].trim(); mark_seen(&session.state_dir, &session.session_id, key, &mut ctx_seen); } } } let chunks = chunk_context(&ctx, CHUNK_SIZE); if let Some(first) = chunks.first() { out.push_str(first); } save_pending_chunks(&session.state_dir, &session.session_id, &chunks[1..]); } } } } if let Some(chunk) = pop_pending_chunk(&session.state_dir, &session.session_id) { out.push_str(&chunk); } else { let cfg = crate::config::get(); if cfg.surface_hooks.iter().any(|h| h == &session.hook_event) { let cycle_output = run_agent_cycles(&session); out.push_str(&format_agent_output(&cycle_output)); } } let _ = write!(log_f, "{}", out); let duration = (Instant::now() - start_time).as_secs_f64(); let _ = writeln!(log_f, "\nran in {duration:.2}s"); out }