refactor(sock): one socket client, retry as a policy value
Six places in the tree hand-rolled the same connect / write one JSON line / read one JSON line back. Two of them — the harness serve loop's client and the MCP server's — were byte-identical apart from a six-line wrapper, ~145 lines of literal copy-paste. The other four each reimplemented a subset, and the subsets had drifted: some named the socket path in their errors and some did not, one classified transient against fatal failures and the rest retried nothing at all, two drained the response and two decoded it. That duplication was defended when the daemons were split out, on the grounds that a daemon's socket etiquette should stay visible in the crate that depends on it. The etiquette genuinely does differ. The code does not, and five copies is where "each daemon documents its own etiquette" stops paying for itself. `hive-sock-client` now owns the transport once, generic over the request and response types so it is protocol-agnostic: the host-served control socket and the harness's in-agent socket both use it with their own wire-type crates. The two real differences become values instead of forks. Retry is `Retry::RideOutRestart` (2/4/8/16/30s, sized to ride out a service restart) for callers with no natural retry of their own, or `Retry::None` for callers already inside a poll loop where the poll interval is the retry — and the reason each caller picked one is a comment at the call site rather than a reimplementation. The response is either decoded (`request`) or half-closed and drained (`notify`, where the drain exists so the server's write-back doesn't land on a closed socket). Whether a failure propagates or is logged and swallowed stays at the call site, because that is the caller's choice and not a property of the transport. Errors always name the socket path now, everywhere. That detail is load-bearing: a permission problem on a socket that reads as "is the daemon running?" sends the operator to fix the wrong thing. The transient-against-fatal enum is gone rather than moved. Serialising happens before the retry loop and deserialising after it, so only connect, I/O and short-read failures can reach the loop at all — a deterministic failure is now unretryable by construction instead of by classification. It is deliberately a new crate and not part of `hive-agent-sock`. The `*-sock` crates are pure wire types by convention — `hive-agent-sock` depends on serde and nothing else — and the two largest copies talk to the host socket, whose types live in a different crate entirely. A transport in either wire-type crate would drag tokio into it and point the wrong way besides. No wire-format change: same JSON line in, same line out.
This commit is contained in:
parent
bd14cc5c46
commit
7a826f9ee2
23 changed files with 498 additions and 501 deletions
|
|
@ -9,7 +9,6 @@
|
|||
//! Single bin crate: the module tree below (formerly this crate's `lib.rs`,
|
||||
//! before lib + bin were collapsed into one) plus the serve loop.
|
||||
|
||||
mod client;
|
||||
mod db_migrate;
|
||||
mod disk_watch;
|
||||
mod events;
|
||||
|
|
@ -37,6 +36,12 @@ mod web_ui;
|
|||
/// Default socket path inside the container — bind-mounted by `hive-c0re`.
|
||||
const DEFAULT_SOCKET: &str = "/run/hive/mcp.sock";
|
||||
|
||||
/// Retry policy for every request to the host-served control socket.
|
||||
/// Nothing on this side of the socket has a natural retry — the serve loop
|
||||
/// and the web UI both hand a failure straight to a human or to claude —
|
||||
/// so a hive-c0re restart is worth waiting out rather than surfacing.
|
||||
const CONTROL_SOCKET_RETRY: Retry = Retry::RideOutRestart;
|
||||
|
||||
/// Default web UI port — used when `HIVE_PORT` env is unset.
|
||||
const DEFAULT_WEB_PORT: u16 = 8042;
|
||||
|
||||
|
|
@ -57,6 +62,7 @@ use anyhow::Result;
|
|||
use clap::Parser;
|
||||
use hive_core_agent_sock::{Request, Response};
|
||||
use hive_sh4re::{HelperEvent, SYSTEM_SENDER};
|
||||
use hive_sock_client::Retry;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "hive-agent", about = "hyperhive harness serve loop")]
|
||||
|
|
@ -358,7 +364,7 @@ struct AgentSurface;
|
|||
/// the `Surface` methods that don't need the reply (`ack_turn`,
|
||||
/// `requeue_inflight`, `graceful_stop_complete`).
|
||||
async fn fire_and_forget(socket: &Path, req: Request, label: &str) {
|
||||
match client::request::<_, Response>(socket, &req).await {
|
||||
match hive_sock_client::request::<_, Response>(socket, &req, CONTROL_SOCKET_RETRY).await {
|
||||
Ok(Response::Ok) => {}
|
||||
Ok(Response::Err { message }) => {
|
||||
tracing::warn!(%message, "{label} rejected by broker");
|
||||
|
|
@ -387,20 +393,29 @@ impl Surface for AgentSurface {
|
|||
}
|
||||
|
||||
async fn inbox_unread(socket: &Path) -> u64 {
|
||||
match client::request::<_, Response>(socket, &Request::Status).await {
|
||||
match hive_sock_client::request::<_, Response>(
|
||||
socket,
|
||||
&Request::Status,
|
||||
CONTROL_SOCKET_RETRY,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(Response::Status { unread }) => unread,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
async fn post_turn_counts(socket: &Path) -> (Option<u64>, Option<u64>) {
|
||||
let threads =
|
||||
match client::request::<_, Response>(socket, &Request::GetLooseEnds { agent: None })
|
||||
.await
|
||||
{
|
||||
Ok(Response::LooseEnds { loose_ends }) => u64::try_from(loose_ends.len()).ok(),
|
||||
_ => None,
|
||||
};
|
||||
let threads = match hive_sock_client::request::<_, Response>(
|
||||
socket,
|
||||
&Request::GetLooseEnds { agent: None },
|
||||
CONTROL_SOCKET_RETRY,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(Response::LooseEnds { loose_ends }) => u64::try_from(loose_ends.len()).ok(),
|
||||
_ => None,
|
||||
};
|
||||
// Reminders are harness-local — dial the in-agent socket directly
|
||||
// instead of the broker.
|
||||
let reminders =
|
||||
|
|
@ -412,13 +427,14 @@ impl Surface for AgentSurface {
|
|||
}
|
||||
|
||||
async fn send_to_parent(socket: &Path, body: String) {
|
||||
let res = client::request::<_, Response>(
|
||||
let res = hive_sock_client::request::<_, Response>(
|
||||
socket,
|
||||
&Request::Send {
|
||||
to: hive_sh4re::PARENT_RECIPIENT.into(),
|
||||
body,
|
||||
in_reply_to: None,
|
||||
},
|
||||
CONTROL_SOCKET_RETRY,
|
||||
)
|
||||
.await;
|
||||
if let Err(e) = res {
|
||||
|
|
@ -427,12 +443,13 @@ impl Surface for AgentSurface {
|
|||
}
|
||||
|
||||
async fn recv_next(socket: &Path) -> RecvOutcome {
|
||||
let recv: Result<Response> = client::request(
|
||||
let recv: Result<Response> = hive_sock_client::request(
|
||||
socket,
|
||||
&Request::Recv {
|
||||
wait_seconds: Some(180),
|
||||
max: None,
|
||||
},
|
||||
CONTROL_SOCKET_RETRY,
|
||||
)
|
||||
.await;
|
||||
match recv {
|
||||
|
|
|
|||
Loading…
Reference in a new issue