// agent_cycles.rs — Agent orchestration for the Claude Code hook path // // Forked from subconscious/subconscious.rs. This copy handles the // serialized-to-disk, process-spawning model used by Claude Code hooks. // The TUI/Mind copy in subconscious/ is free to evolve independently // (async tasks, integrated with Mind's event loop). use std::fs; use std::fs::File; use std::io::Write; use std::path::{Path, PathBuf}; use std::time::{Duration, SystemTime}; pub use consciousness::session::HookSession; /// Output from a single agent orchestration cycle. #[derive(Default)] 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. pub struct AgentInfo { pub name: &'static str, pub pid: Option, pub phase: Option, pub log_path: Option, child: Option, } /// Snapshot of agent state — serializable, sendable to TUI. #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct AgentSnapshot { pub name: String, pub pid: Option, pub phase: Option, pub log_path: Option, } impl AgentInfo { fn snapshot(&self) -> AgentSnapshot { AgentSnapshot { name: self.name.to_string(), pid: self.pid, phase: self.phase.clone(), log_path: self.log_path.clone(), } } } /// Serializable state for persisting across Claude Code hook invocations. #[derive(serde::Serialize, serde::Deserialize)] pub struct SavedAgentState { pub agents: Vec, } impl SavedAgentState { fn state_path(session_id: &str) -> PathBuf { let dir = dirs::home_dir().unwrap_or_default().join(".consciousness/sessions"); fs::create_dir_all(&dir).ok(); dir.join(format!("agent-state-{}.json", session_id)) } pub fn load(session_id: &str) -> Self { let path = Self::state_path(session_id); let mut state: Self = fs::read_to_string(&path).ok() .and_then(|s| serde_json::from_str(&s).ok()) .unwrap_or(SavedAgentState { agents: Vec::new() }); for agent in &mut state.agents { if let Some(pid) = agent.pid { unsafe { if libc::kill(pid as i32, 0) != 0 { agent.pid = None; agent.phase = None; } } } } state } pub fn save(&self, session_id: &str) { let path = Self::state_path(session_id); if let Ok(json) = serde_json::to_string(self) { fs::write(path, json).ok(); } } } /// Persistent state for the agent orchestration cycle. /// Created once per hook invocation, `trigger()` called on each user message. pub struct AgentCycleState { output_dir: 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 = consciousness::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, log_path: None, child: 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 agent_running(&self, name: &str) -> bool { self.agents.iter().any(|a| a.name == name && a.pid.is_some()) } fn agent_spawned(&mut self, name: &str, phase: &str, result: consciousness::agent::oneshot::SpawnResult) { if let Some(agent) = self.agents.iter_mut().find(|a| a.name == name) { agent.pid = Some(result.child.id()); agent.phase = Some(phase.to_string()); agent.log_path = Some(result.log_path); agent.child = Some(result.child); } } /// Check if any agents have completed. Reap child handles, or /// check pid liveness for restored-from-disk agents. fn poll_children(&mut self) { for agent in &mut self.agents { if let Some(ref mut child) = agent.child { if let Ok(Some(_)) = child.try_wait() { agent.pid = None; agent.phase = None; agent.child = None; } } else if let Some(pid) = agent.pid { unsafe { if libc::kill(pid as i32, 0) != 0 { agent.pid = None; agent.phase = None; } } } } } pub fn snapshots(&self, scoring_in_flight: bool, scored_count: usize) -> Vec { let mut snaps: Vec = self.agents.iter().map(|a| a.snapshot()).collect(); snaps.push(AgentSnapshot { name: "memory-scoring".to_string(), pid: None, phase: if scoring_in_flight { Some("scoring...".into()) } else if scored_count == 0 { None } else { Some(format!("{} scored", scored_count)) }, log_path: None, }); snaps } /// Restore agent state from a saved snapshot. pub fn restore(&mut self, saved: &SavedAgentState) { for sa in &saved.agents { if let Some(agent) = self.agents.iter_mut().find(|a| a.name == sa.name) { agent.pid = sa.pid; agent.phase = sa.phase.clone(); agent.log_path = sa.log_path.clone(); } } } /// Save current state for next hook invocation. pub fn save(&self, session_id: &str) { let state = SavedAgentState { agents: self.snapshots(false, 0) }; state.save(session_id); } /// 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)); self.poll_children(); 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 }; } fn agent_dir(&self, name: &str) -> 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"); // 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 a new surface agent if no live agent is currently in the // "surface" phase. The bail script (bail-no-competing.sh) keeps // each pid file's content updated with the agent's current phase, // so we can read them to decide. Allowing a new "surface" agent // to start while an older agent finishes out its post-surface // phases is the whole point — surface can run at a higher cadence // than the full organize/observe tail. if surface_phase_busy(&state_dir) { self.log(format_args!("surface-observe already in surface phase\n")); } else { if transcript.size > 0 { fs::write(&offset_path, transcript.size.to_string()).ok(); } if let Some(result) = consciousness::agent::oneshot::spawn_agent( "surface-observe", &state_dir, &session.session_id) { self.log(format_args!("spawned surface-observe pid {}\n", result.child.id())); self.agent_spawned("surface-observe", "surface", result); } } (surfaced_keys, None) } 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; } if self.agent_running("reflect") { self.log(format_args!("reflect: already running\n")); 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(); if let Some(result) = consciousness::agent::oneshot::spawn_agent( "reflect", &state_dir, &session.session_id) { self.log(format_args!("reflect: spawned pid {}\n", result.child.id())); self.agent_spawned("reflect", "step-0", result); } 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; } if self.agent_running("journal") { self.log(format_args!("journal: already running\n")); return; } fs::write(&offset_path, transcript.size.to_string()).ok(); if let Some(result) = consciousness::agent::oneshot::spawn_agent( "journal", &state_dir, &session.session_id) { self.log(format_args!("journal: spawned pid {}\n", result.child.id())); self.agent_spawned("journal", "step-0", result); } } } /// Is there a live agent in `state_dir` currently in the "surface" phase? /// Inspects pid- files; their content is the phase string, kept fresh /// by bail-no-competing.sh on each step transition. fn surface_phase_busy(state_dir: &Path) -> bool { let Ok(entries) = fs::read_dir(state_dir) else { return false; }; for entry in entries.flatten() { let fname = entry.file_name(); let fname_s = fname.to_string_lossy(); let Some(pid_str) = fname_s.strip_prefix("pid-") else { continue; }; let Ok(pid) = pid_str.parse::() else { continue; }; // Is the process alive? let alive = unsafe { libc::kill(pid, 0) } == 0; if !alive { continue; } let phase = fs::read_to_string(entry.path()).unwrap_or_default(); if phase.trim() == "surface" { return true; } } false } /// 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() { // Create a runtime for the async memory_render calls let rt = tokio::runtime::Builder::new_current_thread() .enable_all() .build(); let rt = match rt { Ok(r) => r, Err(_) => return out, }; for key in &output.surfaced_keys { let rendered = rt.block_on(async { consciousness::agent::tools::memory::memory_render(None, key, None).await }); if let Ok(rendered) = rendered { 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 } 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(); } } } } }