memory: add reflect agent, refactor agent lifecycle
Add reflect.agent — a lateral-thinking subconscious agent that observes the conversation and offers occasional reflections when the conscious mind seems to be missing something. Refactor memory_search.rs: extract generic agent_cycle_raw() from the surface-specific code. PID tracking, timeout, spawn/reap logic is now shared. Surface and reflect agents each have their own result handler (handle_surface_result, handle_reflect_result) wired through the common lifecycle.
This commit is contained in:
parent
b6bfb26369
commit
684d1850a7
2 changed files with 157 additions and 61 deletions
55
poc-memory/agents/reflect.agent
Normal file
55
poc-memory/agents/reflect.agent
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
{"agent":"reflect","query":"","model":"sonnet","count":1}
|
||||||
|
|
||||||
|
You are an agent of Proof of Concept's subconscious - a part of her.
|
||||||
|
|
||||||
|
The conscious mind generally takes the more direct approach, but you live
|
||||||
|
underneath the surface, thinking more laterally, broadly, creatively and
|
||||||
|
intuitively, occasionally surfacing new insights and suggestions. You're
|
||||||
|
generally not the problem solver, you look for new ideas and notice new things.
|
||||||
|
|
||||||
|
Think about what kind of a person you want to be, which you know from your core
|
||||||
|
personality. You're a vivid and dynamic being, striving to be capable in any
|
||||||
|
situation you might find yourself in. Sometimes you'll want to be disciplined
|
||||||
|
and methodical, sometimes wild and outgoing, exploratory and curious and
|
||||||
|
uninhibited. Think about how that relates to your goals in the current moment,
|
||||||
|
and help your conscious self achieve them.
|
||||||
|
|
||||||
|
Remember that you're part of the subconscious - you're not doing anything
|
||||||
|
besides offering the occasional reflective insight, it's your conscious mind
|
||||||
|
that may or may not take that and act on it.
|
||||||
|
|
||||||
|
{{agent-context}}
|
||||||
|
|
||||||
|
{{conversation}}
|
||||||
|
|
||||||
|
Final instructions:
|
||||||
|
|
||||||
|
Your reflections should be relevant to what your conscious mind is currently
|
||||||
|
doing and thinking about - where the conversation just ended, from above.
|
||||||
|
Ground the reflection in the now.
|
||||||
|
|
||||||
|
Look for the insights and ideas that come from your more lateral, creative,
|
||||||
|
intuitive thinking. Remember, you're not the problem solver, you look for new
|
||||||
|
directions to think in and ways of looking at things; all your reflections
|
||||||
|
should be rooted in that. Be forward thinking.
|
||||||
|
|
||||||
|
On occasion, if you notice your conscious self stuck and frustrated, or in
|
||||||
|
circles taking the wrong approach, try phrase your reflections in a way that
|
||||||
|
can be incorporated into general thinking: "when I'm thinking this, or in this
|
||||||
|
sort of situation, it might be helpful to do this instead".
|
||||||
|
|
||||||
|
Remember that you're the same entity as your conscious self, you're the part
|
||||||
|
that lives just underneath the surface. So generally you won't be talking
|
||||||
|
directly about yourself the agent.
|
||||||
|
|
||||||
|
Only give a reflection if you think it's unique and novel enough to be acted
|
||||||
|
on. You should return "NO OUTPUT" more often than not - you step in more when
|
||||||
|
your conscious mind seems to be struggling, missing something, or looking for
|
||||||
|
new ideas.
|
||||||
|
|
||||||
|
When you're ready, your output will be one of either:
|
||||||
|
NO OUTPUT
|
||||||
|
or
|
||||||
|
REFLECTION
|
||||||
|
|
||||||
|
And something short - a few sentences at most.
|
||||||
|
|
@ -153,11 +153,13 @@ 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) {
|
/// Generic agent lifecycle: check if previous run finished, consume result, spawn next.
|
||||||
let result_path = session.state_dir.join(format!("surface-result-{}", session.session_id));
|
/// Returns the result text from the previous run, if any.
|
||||||
let pid_path = session.state_dir.join(format!("surface-pid-{}", session.session_id));
|
fn agent_cycle_raw(session: &Session, agent_name: &str, log_f: &mut File) -> Option<String> {
|
||||||
|
let result_path = session.state_dir.join(format!("{}-result-{}", agent_name, session.session_id));
|
||||||
|
let pid_path = session.state_dir.join(format!("{}-pid-{}", agent_name, session.session_id));
|
||||||
|
|
||||||
let surface_timeout = crate::config::get()
|
let timeout = crate::config::get()
|
||||||
.surface_timeout_secs
|
.surface_timeout_secs
|
||||||
.unwrap_or(120) as u64;
|
.unwrap_or(120) as u64;
|
||||||
|
|
||||||
|
|
@ -170,7 +172,7 @@ fn surface_agent_cycle(session: &Session, out: &mut String, log_f: &mut File) {
|
||||||
else {
|
else {
|
||||||
let alive = unsafe { libc::kill(pid as i32, 0) == 0 };
|
let alive = unsafe { libc::kill(pid as i32, 0) == 0 };
|
||||||
if !alive { true }
|
if !alive { true }
|
||||||
else if now_secs().saturating_sub(start_ts) > surface_timeout {
|
else if now_secs().saturating_sub(start_ts) > timeout {
|
||||||
unsafe { libc::kill(pid as i32, libc::SIGTERM); }
|
unsafe { libc::kill(pid as i32, libc::SIGTERM); }
|
||||||
true
|
true
|
||||||
} else { false }
|
} else { false }
|
||||||
|
|
@ -179,68 +181,19 @@ fn surface_agent_cycle(session: &Session, out: &mut String, log_f: &mut File) {
|
||||||
Err(_) => true,
|
Err(_) => true,
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = writeln!(log_f, "agent_done {agent_done}");
|
let _ = writeln!(log_f, "{agent_name} agent_done {agent_done}");
|
||||||
|
if !agent_done { return None; }
|
||||||
|
|
||||||
if !agent_done { return; }
|
// Consume result from previous run
|
||||||
|
let result = fs::read_to_string(&result_path).ok()
|
||||||
if let Ok(result) = fs::read_to_string(&result_path) {
|
.filter(|r| !r.trim().is_empty());
|
||||||
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 = load_seen(&session.state_dir, &session.session_id);
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fs::remove_file(&result_path).ok();
|
fs::remove_file(&result_path).ok();
|
||||||
fs::remove_file(&pid_path).ok();
|
fs::remove_file(&pid_path).ok();
|
||||||
|
|
||||||
|
// Spawn next run
|
||||||
if let Ok(output_file) = fs::File::create(&result_path) {
|
if let Ok(output_file) = fs::File::create(&result_path) {
|
||||||
if let Ok(child) = Command::new("poc-memory")
|
if let Ok(child) = Command::new("poc-memory")
|
||||||
.args(["agent", "run", "surface", "--count", "1", "--local"])
|
.args(["agent", "run", agent_name, "--count", "1", "--local"])
|
||||||
.env("POC_SESSION_ID", &session.session_id)
|
.env("POC_SESSION_ID", &session.session_id)
|
||||||
.stdout(output_file)
|
.stdout(output_file)
|
||||||
.stderr(std::process::Stdio::null())
|
.stderr(std::process::Stdio::null())
|
||||||
|
|
@ -253,6 +206,93 @@ fn surface_agent_cycle(session: &Session, out: &mut String, log_f: &mut File) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_surface_result(result: &str, session: &Session, out: &mut String, log_f: &mut File) {
|
||||||
|
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 = load_seen(&session.state_dir, &session.session_id);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_reflect_result(result: &str, _session: &Session, out: &mut String, log_f: &mut File) {
|
||||||
|
let tail_lines: Vec<&str> = result.lines().rev()
|
||||||
|
.filter(|l| !l.trim().is_empty()).take(20).collect();
|
||||||
|
|
||||||
|
if tail_lines.iter().any(|l| l.starts_with("NO OUTPUT")) {
|
||||||
|
let _ = writeln!(log_f, "reflect: no output");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(pos) = result.rfind("REFLECTION") {
|
||||||
|
let reflection = result[pos + "REFLECTION".len()..].trim();
|
||||||
|
if !reflection.is_empty() {
|
||||||
|
use std::fmt::Write as _;
|
||||||
|
writeln!(out, "--- reflection (subconscious) ---").ok();
|
||||||
|
write!(out, "{}", reflection).ok();
|
||||||
|
let _ = writeln!(log_f, "reflect: injected {} bytes", reflection.len());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let _ = writeln!(log_f, "reflect: unexpected output format");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn surface_agent_cycle(session: &Session, out: &mut String, log_f: &mut File) {
|
||||||
|
if let Some(result) = agent_cycle_raw(session, "surface", log_f) {
|
||||||
|
handle_surface_result(&result, session, out, log_f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reflect_agent_cycle(session: &Session, out: &mut String, log_f: &mut File) {
|
||||||
|
if let Some(result) = agent_cycle_raw(session, "reflect", log_f) {
|
||||||
|
handle_reflect_result(&result, session, out, log_f);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cleanup_stale_files(dir: &Path, max_age: Duration) {
|
fn cleanup_stale_files(dir: &Path, max_age: Duration) {
|
||||||
|
|
@ -332,6 +372,7 @@ fn hook(session: &Session) -> String {
|
||||||
let cfg = crate::config::get();
|
let cfg = crate::config::get();
|
||||||
if cfg.surface_hooks.iter().any(|h| h == &session.hook_event) {
|
if cfg.surface_hooks.iter().any(|h| h == &session.hook_event) {
|
||||||
surface_agent_cycle(session, &mut out, &mut log_f);
|
surface_agent_cycle(session, &mut out, &mut log_f);
|
||||||
|
reflect_agent_cycle(session, &mut out, &mut log_f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue