Move poc-agent into workspace, improve agent prompts
Move poc-agent (substrate-independent AI agent framework) into the memory workspace as a step toward using its API client for direct LLM calls instead of shelling out to claude CLI. Agent prompt improvements: - distill: rewrite from hub-focused to knowledge-flow-focused. Now walks upward from seed nodes to find and refine topic nodes, instead of only maintaining high-degree hubs. - distill: remove "don't touch journal entries" restriction - memory-instructions-core: add "Make it alive" section — write with creativity and emotional texture, not spreadsheet summaries - memory-instructions-core: add "Show your reasoning" section — agents must explain decisions, especially when they do nothing - linker: already had emotional texture guidance (kept as-is) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
0a62832fe3
commit
57fcfb472a
89 changed files with 16389 additions and 51 deletions
251
poc-agent/src/observe.rs
Normal file
251
poc-agent/src/observe.rs
Normal file
|
|
@ -0,0 +1,251 @@
|
|||
// observe.rs — Shared observation socket + logfile
|
||||
//
|
||||
// Two mechanisms:
|
||||
// 1. Logfile (~/.cache/poc-agent/sessions/observe.log) — append-only
|
||||
// plain text of the conversation. `poc-agent read` prints new
|
||||
// content since last read using a byte-offset cursor file.
|
||||
// 2. Unix socket — for live streaming (`poc-agent read -f`) and
|
||||
// sending input (`poc-agent write <msg>`).
|
||||
//
|
||||
// The logfile is the history. The socket is the live wire.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::{UnixListener, UnixStream};
|
||||
use tokio::sync::{broadcast, Mutex};
|
||||
|
||||
use crate::ui_channel::UiMessage;
|
||||
|
||||
fn format_message(msg: &UiMessage) -> Option<String> {
|
||||
match msg {
|
||||
UiMessage::TextDelta(text, _) => {
|
||||
let t = text.trim_end();
|
||||
if t.is_empty() { None } else { Some(t.to_string()) }
|
||||
}
|
||||
UiMessage::UserInput(text) => Some(format!("\n> {}", text)),
|
||||
UiMessage::ToolCall { name, args_summary } => {
|
||||
if args_summary.is_empty() {
|
||||
Some(format!("[{}]", name))
|
||||
} else {
|
||||
Some(format!("[{}: {}]", name, args_summary))
|
||||
}
|
||||
}
|
||||
UiMessage::ToolResult { name, result } => {
|
||||
let preview: String = result.lines().take(3).collect::<Vec<_>>().join("\n");
|
||||
if name.is_empty() {
|
||||
Some(format!(" → {}", preview))
|
||||
} else {
|
||||
Some(format!(" → {}: {}", name, preview))
|
||||
}
|
||||
}
|
||||
UiMessage::DmnAnnotation(text) => Some(text.clone()),
|
||||
UiMessage::Info(text) if !text.is_empty() => Some(text.clone()),
|
||||
UiMessage::Reasoning(text) => {
|
||||
let t = text.trim();
|
||||
if t.is_empty() { None } else { Some(format!("(thinking: {})", t)) }
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub type InputSender = tokio::sync::mpsc::UnboundedSender<String>;
|
||||
pub type InputReceiver = tokio::sync::mpsc::UnboundedReceiver<String>;
|
||||
|
||||
pub fn input_channel() -> (InputSender, InputReceiver) {
|
||||
tokio::sync::mpsc::unbounded_channel()
|
||||
}
|
||||
|
||||
fn session_dir() -> PathBuf {
|
||||
let cache = dirs::cache_dir().unwrap_or_else(|| PathBuf::from("/tmp"));
|
||||
cache.join("poc-agent/sessions")
|
||||
}
|
||||
|
||||
fn socket_path() -> PathBuf { session_dir().join("agent.sock") }
|
||||
fn log_path() -> PathBuf { session_dir().join("observe.log") }
|
||||
fn cursor_path() -> PathBuf { session_dir().join("read-cursor") }
|
||||
|
||||
// --- Client commands ---
|
||||
|
||||
/// Print new output since last read. With -f, also stream live from socket.
|
||||
pub async fn cmd_read(follow: bool, debug: bool) -> anyhow::Result<()> {
|
||||
use std::io::{Read, Seek, SeekFrom, Write};
|
||||
|
||||
let log = log_path();
|
||||
let cursor = cursor_path();
|
||||
|
||||
if debug {
|
||||
eprintln!("log: {}", log.display());
|
||||
}
|
||||
|
||||
let offset: u64 = std::fs::read_to_string(&cursor)
|
||||
.ok()
|
||||
.and_then(|s| s.trim().parse().ok())
|
||||
.unwrap_or(0);
|
||||
|
||||
if let Ok(mut f) = std::fs::File::open(&log) {
|
||||
let len = f.metadata()?.len();
|
||||
if offset < len {
|
||||
f.seek(SeekFrom::Start(offset))?;
|
||||
let mut buf = String::new();
|
||||
f.read_to_string(&mut buf)?;
|
||||
print!("{}", buf);
|
||||
let _ = std::io::stdout().flush();
|
||||
} else if !follow {
|
||||
println!("(nothing new)");
|
||||
}
|
||||
let _ = std::fs::write(&cursor, len.to_string());
|
||||
} else if !follow {
|
||||
println!("(no log yet — is poc-agent running?)");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !follow {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// -f: connect to socket for live output
|
||||
let sock = socket_path();
|
||||
let stream = UnixStream::connect(&sock).await
|
||||
.map_err(|e| anyhow::anyhow!(
|
||||
"can't connect for live streaming — is poc-agent running? ({})", e
|
||||
))?;
|
||||
|
||||
let (reader, _) = stream.into_split();
|
||||
let mut reader = BufReader::new(reader);
|
||||
let mut line = String::new();
|
||||
|
||||
loop {
|
||||
line.clear();
|
||||
match reader.read_line(&mut line).await {
|
||||
Ok(0) => break,
|
||||
Ok(_) => {
|
||||
print!("{}", line);
|
||||
let _ = std::io::stdout().lock().flush();
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Send a message to the running agent.
|
||||
pub async fn cmd_write(message: &str, debug: bool) -> anyhow::Result<()> {
|
||||
let sock = socket_path();
|
||||
if debug {
|
||||
eprintln!("connecting to {}", sock.display());
|
||||
}
|
||||
let stream = UnixStream::connect(&sock).await
|
||||
.map_err(|e| anyhow::anyhow!(
|
||||
"can't connect — is poc-agent running? ({})", e
|
||||
))?;
|
||||
|
||||
let (_, mut writer) = stream.into_split();
|
||||
writer.write_all(message.as_bytes()).await?;
|
||||
writer.write_all(b"\n").await?;
|
||||
writer.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// --- Server ---
|
||||
|
||||
/// Start the observation socket + logfile writer.
|
||||
pub fn start(
|
||||
socket_path_override: PathBuf,
|
||||
mut ui_rx: broadcast::Receiver<UiMessage>,
|
||||
input_tx: InputSender,
|
||||
) {
|
||||
let _ = std::fs::remove_file(&socket_path_override);
|
||||
|
||||
let listener = UnixListener::bind(&socket_path_override)
|
||||
.expect("failed to bind observation socket");
|
||||
|
||||
// Open logfile
|
||||
let logfile = Arc::new(Mutex::new(
|
||||
std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(log_path())
|
||||
.expect("failed to open observe log"),
|
||||
));
|
||||
|
||||
let (line_tx, _) = broadcast::channel::<String>(256);
|
||||
let line_tx2 = line_tx.clone();
|
||||
|
||||
// Receive UiMessages → write to logfile + broadcast to socket clients
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
match ui_rx.recv().await {
|
||||
Ok(msg) => {
|
||||
if let Some(line) = format_message(&msg) {
|
||||
{
|
||||
use std::io::Write;
|
||||
let mut f = logfile.lock().await;
|
||||
let _ = writeln!(f, "{}", line);
|
||||
let _ = f.flush();
|
||||
}
|
||||
let _ = line_tx2.send(line);
|
||||
}
|
||||
}
|
||||
Err(broadcast::error::RecvError::Lagged(_)) => {}
|
||||
Err(broadcast::error::RecvError::Closed) => break,
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Accept socket connections (live streaming + input)
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
match listener.accept().await {
|
||||
Ok((stream, _)) => {
|
||||
let mut line_rx = line_tx.subscribe();
|
||||
let input_tx = input_tx.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let (reader, mut writer) = stream.into_split();
|
||||
let mut reader = BufReader::new(reader);
|
||||
let mut input_buf = String::new();
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased;
|
||||
|
||||
result = reader.read_line(&mut input_buf) => {
|
||||
match result {
|
||||
Ok(0) | Err(_) => break,
|
||||
Ok(_) => {
|
||||
let line = input_buf.trim().to_string();
|
||||
if !line.is_empty() {
|
||||
let _ = input_tx.send(line);
|
||||
}
|
||||
input_buf.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result = line_rx.recv() => {
|
||||
match result {
|
||||
Ok(line) => {
|
||||
let data = format!("{}\n", line);
|
||||
if writer.write_all(data.as_bytes()).await.is_err() {
|
||||
break;
|
||||
}
|
||||
let _ = writer.flush().await;
|
||||
}
|
||||
Err(broadcast::error::RecvError::Lagged(_)) => {
|
||||
let _ = writer.write_all(
|
||||
b"[some output was dropped]\n"
|
||||
).await;
|
||||
}
|
||||
Err(broadcast::error::RecvError::Closed) => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue