hive-ag3nt: serve loop + send/recv CLI; template runs serve
This commit is contained in:
parent
d79b5a39a1
commit
61407f41c9
6 changed files with 139 additions and 4 deletions
|
|
@ -3,6 +3,16 @@ name = "hive-ag3nt"
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
version.workspace = true
|
version.workspace = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow.workspace = true
|
||||||
|
clap.workspace = true
|
||||||
|
hive-sh4re.workspace = true
|
||||||
|
serde.workspace = true
|
||||||
|
serde_json.workspace = true
|
||||||
|
tokio.workspace = true
|
||||||
|
tracing.workspace = true
|
||||||
|
tracing-subscriber.workspace = true
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "hive-ag3nt"
|
name = "hive-ag3nt"
|
||||||
path = "src/bin/hive-ag3nt.rs"
|
path = "src/bin/hive-ag3nt.rs"
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,91 @@
|
||||||
fn main() {
|
use std::path::PathBuf;
|
||||||
println!("hive-ag3nt placeholder");
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use anyhow::{Result, bail};
|
||||||
|
use clap::{Parser, Subcommand};
|
||||||
|
use hive_ag3nt::{DEFAULT_SOCKET, client};
|
||||||
|
use hive_sh4re::{AgentRequest, AgentResponse};
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
#[command(name = "hive-ag3nt", about = "hyperhive sub-agent harness")]
|
||||||
|
struct Cli {
|
||||||
|
/// Path to the per-agent MCP socket (bind-mounted from the host).
|
||||||
|
#[arg(long, global = true, default_value = DEFAULT_SOCKET)]
|
||||||
|
socket: PathBuf,
|
||||||
|
|
||||||
|
#[command(subcommand)]
|
||||||
|
cmd: Cmd,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
enum Cmd {
|
||||||
|
/// Run the long-lived harness loop. Polls inbox; prints messages to stdout.
|
||||||
|
Serve {
|
||||||
|
/// Inbox poll interval in milliseconds.
|
||||||
|
#[arg(long, default_value_t = 1000)]
|
||||||
|
poll_ms: u64,
|
||||||
|
},
|
||||||
|
/// Send a message to another agent.
|
||||||
|
Send { to: String, body: String },
|
||||||
|
/// Pop one message from the inbox.
|
||||||
|
Recv,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<()> {
|
||||||
|
tracing_subscriber::fmt()
|
||||||
|
.with_env_filter(
|
||||||
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||||
|
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
|
||||||
|
)
|
||||||
|
.init();
|
||||||
|
|
||||||
|
let cli = Cli::parse();
|
||||||
|
match cli.cmd {
|
||||||
|
Cmd::Serve { poll_ms } => serve(&cli.socket, Duration::from_millis(poll_ms)).await,
|
||||||
|
Cmd::Send { to, body } => {
|
||||||
|
let resp = client::request(&cli.socket, AgentRequest::Send { to, body }).await?;
|
||||||
|
render(&resp)?;
|
||||||
|
check(&resp)
|
||||||
|
}
|
||||||
|
Cmd::Recv => {
|
||||||
|
let resp = client::request(&cli.socket, AgentRequest::Recv).await?;
|
||||||
|
render(&resp)?;
|
||||||
|
check(&resp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn serve(socket: &std::path::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");
|
||||||
|
}
|
||||||
|
Ok(AgentResponse::Empty) => {}
|
||||||
|
Ok(AgentResponse::Ok) => {
|
||||||
|
tracing::warn!("recv produced Ok (unexpected)");
|
||||||
|
}
|
||||||
|
Ok(AgentResponse::Err { message }) => {
|
||||||
|
tracing::warn!(%message, "recv error");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!(error = ?e, "recv failed; retrying");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tokio::time::sleep(interval).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render(resp: &AgentResponse) -> Result<()> {
|
||||||
|
println!("{}", serde_json::to_string_pretty(resp)?);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check(resp: &AgentResponse) -> Result<()> {
|
||||||
|
if let AgentResponse::Err { message } = resp {
|
||||||
|
bail!("{message}");
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// Phase 4 — manager tool surface. For now, a placeholder so the binary
|
||||||
|
// exists and can be referenced from the manager nixos-container template.
|
||||||
println!("hive-m1nd placeholder");
|
println!("hive-m1nd placeholder");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
27
hive-ag3nt/src/client.rs
Normal file
27
hive-ag3nt/src/client.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result, bail};
|
||||||
|
use hive_sh4re::{AgentRequest, AgentResponse};
|
||||||
|
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||||
|
use tokio::net::UnixStream;
|
||||||
|
|
||||||
|
pub async fn request(socket: &Path, req: AgentRequest) -> Result<AgentResponse> {
|
||||||
|
let stream = UnixStream::connect(socket)
|
||||||
|
.await
|
||||||
|
.with_context(|| format!("connect to {}", socket.display()))?;
|
||||||
|
let (read, mut write) = stream.into_split();
|
||||||
|
|
||||||
|
let mut payload = serde_json::to_string(&req)?;
|
||||||
|
payload.push('\n');
|
||||||
|
write.write_all(payload.as_bytes()).await?;
|
||||||
|
write.flush().await?;
|
||||||
|
|
||||||
|
let mut reader = BufReader::new(read);
|
||||||
|
let mut line = String::new();
|
||||||
|
reader.read_line(&mut line).await?;
|
||||||
|
if line.is_empty() {
|
||||||
|
bail!("server closed connection without responding");
|
||||||
|
}
|
||||||
|
let resp: AgentResponse = serde_json::from_str(line.trim())?;
|
||||||
|
Ok(resp)
|
||||||
|
}
|
||||||
|
|
@ -1 +1,7 @@
|
||||||
|
//! Shared in-container harness code used by both `hive-ag3nt` (agent) and
|
||||||
|
//! `hive-m1nd` (manager) binaries.
|
||||||
|
|
||||||
|
pub mod client;
|
||||||
|
|
||||||
|
/// Default socket path inside the container — bind-mounted by `hive-c0re`.
|
||||||
|
pub const DEFAULT_SOCKET: &str = "/run/hive/mcp.sock";
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,11 @@
|
||||||
systemd.services.hive-ag3nt = {
|
systemd.services.hive-ag3nt = {
|
||||||
description = "hive-ag3nt harness";
|
description = "hive-ag3nt harness";
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
|
after = [ "network.target" ];
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
ExecStart = "${pkgs.hyperhive}/bin/hive-ag3nt";
|
ExecStart = "${pkgs.hyperhive}/bin/hive-ag3nt serve";
|
||||||
Type = "oneshot";
|
Restart = "on-failure";
|
||||||
|
RestartSec = 2;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue