agents: phase tracking, pid files, pipelining, unified cycle
- AgentStep with phase labels (=== PROMPT phase:name ===)
- PID files in state dir (pid-{PID} with JSON phase/timestamp)
- Built-in bail check: between steps, bail if other pid files exist
- surface_observe_cycle replaces surface_agent_cycle + journal_agent_cycle
- Reads surface output from state dir instead of parsing stdout
- Pipelining: starts new agent if running one is past surface phase
- link_set upserts (creates link if missing)
- Better error message for context window overflow
Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
parent
11289667f5
commit
e20aeeeabe
8 changed files with 256 additions and 178 deletions
|
|
@ -129,159 +129,121 @@ fn mark_seen(dir: &Path, session_id: &str, key: &str, seen: &mut HashSet<String>
|
|||
}
|
||||
}
|
||||
|
||||
fn surface_agent_cycle(session: &Session, out: &mut String, log_f: &mut File) {
|
||||
let result_path = session.state_dir.join(format!("surface-result-{}", session.session_id));
|
||||
let pid_path = session.state_dir.join(format!("surface-pid-{}", session.session_id));
|
||||
/// Unified agent cycle — runs surface-observe agent with state dir.
|
||||
/// Reads output files for surface results, spawns new agent when ready.
|
||||
///
|
||||
/// Pipelining: if a running agent is past the surface phase, start
|
||||
/// a new one so surface stays fresh.
|
||||
fn surface_observe_cycle(session: &Session, out: &mut String, log_f: &mut File) {
|
||||
let state_dir = crate::store::memory_dir()
|
||||
.join("agent-output")
|
||||
.join("surface-observe");
|
||||
fs::create_dir_all(&state_dir).ok();
|
||||
|
||||
let surface_timeout = crate::config::get()
|
||||
let timeout = crate::config::get()
|
||||
.surface_timeout_secs
|
||||
.unwrap_or(120) as u64;
|
||||
.unwrap_or(300) as u64;
|
||||
|
||||
let agent_done = match fs::read_to_string(&pid_path) {
|
||||
Ok(content) => {
|
||||
let parts: Vec<&str> = content.split('\t').collect();
|
||||
let pid: u32 = parts.first().and_then(|s| s.trim().parse().ok()).unwrap_or(0);
|
||||
let start_ts: u64 = parts.get(1).and_then(|s| s.trim().parse().ok()).unwrap_or(0);
|
||||
if pid == 0 { true }
|
||||
else {
|
||||
let alive = unsafe { libc::kill(pid as i32, 0) == 0 };
|
||||
if !alive { true }
|
||||
else if now_secs().saturating_sub(start_ts) > surface_timeout {
|
||||
unsafe { libc::kill(pid as i32, libc::SIGTERM); }
|
||||
true
|
||||
} else { false }
|
||||
// Scan pid files — find live agents and their phases
|
||||
let mut any_in_surface = false;
|
||||
let mut any_alive = false;
|
||||
if let Ok(entries) = fs::read_dir(&state_dir) {
|
||||
for entry in entries.flatten() {
|
||||
let name = entry.file_name();
|
||||
let name_str = name.to_string_lossy();
|
||||
if !name_str.starts_with("pid-") { continue; }
|
||||
let pid: u32 = name_str.strip_prefix("pid-")
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(0);
|
||||
if pid == 0 { continue; }
|
||||
|
||||
let alive = unsafe { libc::kill(pid as i32, 0) == 0 };
|
||||
if !alive {
|
||||
let _ = writeln!(log_f, "cleanup stale pid-{}", pid);
|
||||
fs::remove_file(entry.path()).ok();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for timeout
|
||||
let phase_json = fs::read_to_string(entry.path()).unwrap_or_default();
|
||||
let started: u64 = phase_json.split("\"started\":")
|
||||
.nth(1)
|
||||
.and_then(|s| s.trim_start().split(|c: char| !c.is_ascii_digit()).next())
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(0);
|
||||
if started > 0 && now_secs().saturating_sub(started) > timeout {
|
||||
let _ = writeln!(log_f, "killing timed-out pid-{} ({}s)", pid, timeout);
|
||||
unsafe { libc::kill(pid as i32, libc::SIGTERM); }
|
||||
fs::remove_file(entry.path()).ok();
|
||||
continue;
|
||||
}
|
||||
|
||||
any_alive = true;
|
||||
|
||||
let in_surface = phase_json.contains("\"phase\":\"surface\"")
|
||||
|| phase_json.contains("\"phase\":\"step-0\"");
|
||||
if in_surface {
|
||||
any_in_surface = true;
|
||||
}
|
||||
let _ = writeln!(log_f, "alive pid-{}: {}", pid, phase_json.trim());
|
||||
}
|
||||
Err(_) => true,
|
||||
};
|
||||
}
|
||||
|
||||
let _ = writeln!(log_f, "agent_done {agent_done}");
|
||||
|
||||
if !agent_done { return; }
|
||||
|
||||
if let Ok(result) = fs::read_to_string(&result_path) {
|
||||
if !result.trim().is_empty() {
|
||||
let tail_lines: Vec<&str> = result.lines().rev()
|
||||
.filter(|l| !l.trim().is_empty()).take(8).collect();
|
||||
let has_new = tail_lines.iter().any(|l| l.starts_with("NEW RELEVANT MEMORIES:"));
|
||||
let has_none = tail_lines.iter().any(|l| l.starts_with("NO NEW RELEVANT MEMORIES"));
|
||||
|
||||
let _ = writeln!(log_f, "has_new {has_new} has_none {has_none}");
|
||||
|
||||
if has_new {
|
||||
let after_marker = result.rsplit_once("NEW RELEVANT MEMORIES:")
|
||||
.map(|(_, rest)| rest).unwrap_or("");
|
||||
let keys: Vec<String> = after_marker.lines()
|
||||
.map(|l| l.trim().trim_start_matches("- ").trim().to_string())
|
||||
.filter(|l| !l.is_empty() && !l.starts_with("```")).collect();
|
||||
|
||||
let _ = writeln!(log_f, "keys {:?}", keys);
|
||||
|
||||
let Ok(store) = crate::store::Store::load() else { return; };
|
||||
let mut seen = session.seen();
|
||||
let seen_path = session.path("seen");
|
||||
for key in &keys {
|
||||
if !seen.insert(key.clone()) {
|
||||
let _ = writeln!(log_f, " skip (seen): {}", key);
|
||||
continue;
|
||||
}
|
||||
if let Some(content) = crate::cli::node::render_node(&store, key) {
|
||||
if !content.trim().is_empty() {
|
||||
use std::fmt::Write as _;
|
||||
writeln!(out, "--- {} (surfaced) ---", key).ok();
|
||||
write!(out, "{}", content).ok();
|
||||
let _ = writeln!(log_f, " rendered {}: {} bytes, out now {} bytes", key, content.len(), out.len());
|
||||
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();
|
||||
}
|
||||
}
|
||||
// Read surface output and inject into context
|
||||
let surface_path = state_dir.join("surface");
|
||||
if let Ok(content) = fs::read_to_string(&surface_path) {
|
||||
let Ok(store) = crate::store::Store::load() else { return; };
|
||||
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()) {
|
||||
let _ = writeln!(log_f, " skip (seen): {}", key);
|
||||
continue;
|
||||
}
|
||||
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();
|
||||
let _ = writeln!(log_f, " rendered {}: {} bytes", key, rendered.len());
|
||||
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();
|
||||
}
|
||||
}
|
||||
} else if !has_none {
|
||||
let log_dir = crate::store::memory_dir().join("logs");
|
||||
fs::create_dir_all(&log_dir).ok();
|
||||
let log_path = log_dir.join("surface-errors.log");
|
||||
if let Ok(mut f) = fs::OpenOptions::new().create(true).append(true).open(&log_path) {
|
||||
let ts = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S");
|
||||
let last = tail_lines.first().unwrap_or(&"");
|
||||
let _ = writeln!(f, "[{}] unexpected surface output: {}", ts, last);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Clear surface output after consuming
|
||||
fs::remove_file(&surface_path).ok();
|
||||
}
|
||||
fs::remove_file(&result_path).ok();
|
||||
fs::remove_file(&pid_path).ok();
|
||||
|
||||
if let Ok(output_file) = fs::File::create(&result_path) {
|
||||
if let Ok(child) = Command::new("poc-memory")
|
||||
.args(["agent", "run", "surface", "--count", "1", "--local"])
|
||||
.env("POC_SESSION_ID", &session.session_id)
|
||||
.stdout(output_file)
|
||||
.stderr(std::process::Stdio::null())
|
||||
.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
// Start a new agent if:
|
||||
// - nothing running, OR
|
||||
// - something running but past surface phase (pipelining)
|
||||
if any_in_surface {
|
||||
let _ = writeln!(log_f, "agent in surface phase, waiting");
|
||||
return;
|
||||
}
|
||||
|
||||
let _ = writeln!(log_f, "journal: spawning (transcript {}, last {})",
|
||||
transcript_size, last_offset);
|
||||
if any_alive {
|
||||
let _ = writeln!(log_f, "agent past surface, starting new (pipeline)");
|
||||
}
|
||||
|
||||
// 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"))
|
||||
let agent_log = fs::File::create(log_dir.join("surface-observe.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"])
|
||||
.args(["agent", "run", "surface-observe", "--count", "1", "--local",
|
||||
"--state-dir", &state_dir.to_string_lossy()])
|
||||
.env("POC_SESSION_ID", &session.session_id)
|
||||
.stdout(journal_log.try_clone().unwrap_or_else(|_| fs::File::create("/dev/null").unwrap()))
|
||||
.stderr(journal_log)
|
||||
.stdout(agent_log.try_clone().unwrap_or_else(|_| fs::File::create("/dev/null").unwrap()))
|
||||
.stderr(agent_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();
|
||||
}
|
||||
let _ = writeln!(log_f, "spawned pid {}", child.id());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -361,8 +323,7 @@ fn hook(session: &Session) -> String {
|
|||
} else {
|
||||
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);
|
||||
surface_observe_cycle(session, &mut out, &mut log_f);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue