agent loop: claude drives; tool envelope (log/run/status/log)

This commit is contained in:
müde 2026-05-15 14:54:10 +02:00
parent a061f83cfa
commit 3c9d42b2a7
6 changed files with 147 additions and 47 deletions

View file

@ -123,32 +123,21 @@ async fn serve(socket: &Path, interval: Duration, state: Arc<Mutex<LoginState>>)
tracing::info!(socket = %socket.display(), "hive-ag3nt serve");
let _ = state; // reserved for future state transitions (turn-loop -> needs-login)
let mcp_config = write_mcp_config(socket).await?;
let label = std::env::var("HIVE_LABEL").unwrap_or_else(|_| "hive-ag3nt".into());
loop {
let recv: Result<AgentResponse> = client::request(socket, &AgentRequest::Recv).await;
match recv {
Ok(AgentResponse::Message { from, body }) => {
tracing::info!(%from, %body, "inbox");
// Don't auto-reply to echoes — prevents infinite ping-pong when
// both ends are falling back to echo. Real loop control is the
// manager's job (Phase 4+).
if !body.starts_with("echo: ") {
let reply = compute_reply(&body, &mcp_config).await;
let send: Result<AgentResponse> = client::request(
socket,
&AgentRequest::Send {
to: from,
body: reply,
},
)
.await;
if let Err(e) = send {
tracing::warn!(error = ?e, "send reply failed");
}
let prompt = format_wake_prompt(&label, &from, &body);
match invoke_claude(&prompt, &mcp_config).await {
Ok(out) => tracing::info!(stdout = %out.trim(), "claude turn finished"),
Err(e) => tracing::warn!(error = %format!("{e:#}"), "claude turn failed"),
}
}
Ok(AgentResponse::Empty) => {}
Ok(AgentResponse::Ok) => {
tracing::warn!("recv produced Ok (unexpected)");
Ok(AgentResponse::Ok | AgentResponse::Status { .. }) => {
tracing::warn!("recv produced unexpected response kind");
}
Ok(AgentResponse::Err { message }) => {
tracing::warn!(%message, "recv error");
@ -161,14 +150,27 @@ async fn serve(socket: &Path, interval: Duration, state: Arc<Mutex<LoginState>>)
}
}
async fn compute_reply(prompt: &str, mcp_config: &Path) -> String {
match invoke_claude(prompt, mcp_config).await {
Ok(s) => s,
Err(e) => {
tracing::warn!(error = %format!("{e:#}"), "claude failed; falling back to echo");
format!("echo: {prompt}")
}
}
/// System prompt handed to claude on each turn. The harness has already
/// popped one message off the inbox (the wake signal); claude is told
/// about it and the MCP tools, and is expected to drive any further
/// recv/send itself.
fn format_wake_prompt(label: &str, from: &str, body: &str) -> String {
format!(
"You are hyperhive agent `{label}` in a multi-agent system.\n\
\n\
Incoming message from `{from}`:\n\
---\n\
{body}\n\
---\n\
\n\
Tools:\n\
- `mcp__hyperhive__recv()` drain one more message from your inbox \
(returns `(empty)` if nothing pending).\n\
- `mcp__hyperhive__send(to, body)` message a peer (by their name) \
or the operator (recipient `operator`, surfaces in the dashboard).\n\
\n\
Handle the inbox, then stop. Don't narrate intent act."
)
}
async fn invoke_claude(prompt: &str, mcp_config: &Path) -> Result<String> {