Phase 3c: nixpkgs-unstable for claude-code; harness calls claude --print, falls back to echo
This commit is contained in:
parent
2fe9e91005
commit
6e7fd2e897
6 changed files with 106 additions and 45 deletions
|
|
@ -1,10 +1,11 @@
|
|||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use clap::{Parser, Subcommand};
|
||||
use hive_ag3nt::{DEFAULT_SOCKET, client};
|
||||
use hive_sh4re::{AgentRequest, AgentResponse};
|
||||
use tokio::process::Command;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "hive-ag3nt", about = "hyperhive sub-agent harness")]
|
||||
|
|
@ -19,7 +20,8 @@ struct Cli {
|
|||
|
||||
#[derive(Subcommand)]
|
||||
enum Cmd {
|
||||
/// Run the long-lived harness loop. Polls inbox; prints messages to stdout.
|
||||
/// Run the long-lived harness loop. Polls inbox; replies via `claude --print`
|
||||
/// when available, falling back to a simple echo otherwise.
|
||||
Serve {
|
||||
/// Inbox poll interval in milliseconds.
|
||||
#[arg(long, default_value_t = 1000)]
|
||||
|
|
@ -56,21 +58,26 @@ async fn main() -> Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
async fn serve(socket: &std::path::Path, interval: Duration) -> Result<()> {
|
||||
async fn serve(socket: &Path, interval: Duration) -> Result<()> {
|
||||
tracing::info!(socket = %socket.display(), "hive-ag3nt serve");
|
||||
loop {
|
||||
match client::request(socket, AgentRequest::Recv).await {
|
||||
Ok(AgentResponse::Message { from, body }) => {
|
||||
tracing::info!(%from, %body, "inbox");
|
||||
// Placeholder "turn": echo back, prefixed. Phase 3c replaces this
|
||||
// with `claude --print` once API-key plumbing exists. Don't echo
|
||||
// an echo, so a manual `send` produces exactly one reply.
|
||||
// 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 = AgentRequest::Send {
|
||||
to: from.clone(),
|
||||
body: format!("echo: {body}"),
|
||||
};
|
||||
if let Err(e) = client::request(socket, reply).await {
|
||||
let reply = compute_reply(&body).await;
|
||||
if let Err(e) = client::request(
|
||||
socket,
|
||||
AgentRequest::Send {
|
||||
to: from,
|
||||
body: reply,
|
||||
},
|
||||
)
|
||||
.await
|
||||
{
|
||||
tracing::warn!(error = ?e, "send reply failed");
|
||||
}
|
||||
}
|
||||
|
|
@ -90,6 +97,36 @@ async fn serve(socket: &std::path::Path, interval: Duration) -> Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
async fn compute_reply(prompt: &str) -> String {
|
||||
match invoke_claude(prompt).await {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
tracing::warn!(error = %format!("{e:#}"), "claude failed; falling back to echo");
|
||||
format!("echo: {prompt}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn invoke_claude(prompt: &str) -> Result<String> {
|
||||
let out = Command::new("claude")
|
||||
.arg("--print")
|
||||
.arg(prompt)
|
||||
.output()
|
||||
.await?;
|
||||
if !out.status.success() {
|
||||
bail!(
|
||||
"claude exited {}: {}",
|
||||
out.status,
|
||||
String::from_utf8_lossy(&out.stderr).trim()
|
||||
);
|
||||
}
|
||||
let text = String::from_utf8_lossy(&out.stdout).trim().to_owned();
|
||||
if text.is_empty() {
|
||||
bail!("claude produced empty output");
|
||||
}
|
||||
Ok(text)
|
||||
}
|
||||
|
||||
fn render(resp: &AgentResponse) -> Result<()> {
|
||||
println!("{}", serde_json::to_string_pretty(resp)?);
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue