hyperhive/hivectl/src/client.rs
atlas be3411e180 feat(#3245): gate rustdoc in nix flake check, and clear the workspace
Nothing in the gate read doc-comments: clippy doesn't check intra-doc
links, cargo test doesn't, and no check built docs. So a [`Foo`] pointing
at a renamed, moved or deleted item rendered as plain text and had no
discoverer but a human happening to read the comment.

That matters here more than in most repos, because the convention is to
put a thing's authoritative description in one doc-comment and point at
it from everywhere else -- the design leans on the pointers being real,
and a dangling link is worse than no link since it names something and
sends the reader looking.

Adds `docs-rustdoc` to nix/checks.nix: craneLib.cargoDoc over
--workspace --no-deps --document-private-items, denying six rustdoc
lints. Listed explicitly rather than -D warnings so a new lint appearing
upstream cannot red the build on a class nobody has triaged.

--document-private-items is load-bearing rather than thoroughness for
its own sake: most of this workspace's doc-comments live on private
items and //! module headers, so without it rustdoc checks a small
fraction of the links and the gate sits green while the rot continues.

Then fixes every error it reports, 40 to 0 across nine crates. The
classes differ and so do the fixes:

- public item, wrong scope -> qualify. Node and Node::parent are both
  public; the link failed only because scheduler.rs does not import
  Node. Six sites become [`crate::Node::parent`].
- private item -> downgrade to backticks. Nothing was made public to
  satisfy a lint; changing API surface to appease a doc check would be
  the tail wagging the dog.
- genuinely dead -> [`JobBuilder::insert_into`] names a method that does
  not exist. Insertion is Scheduler::insert_job.
- prose that looks like markup -> argv[0] parsed as a link, and
  <args>/<hex>/<name> parsed as HTML tags.

Note for future fixes: pub(crate) resolves in an intra-doc link, a plain
private fn in a binary crate does not (wait_for_nodes resolved,
connect_hint did not, same crate, same shape).

The check does not ride the clippy/test artifact cache. It takes
cargoArtifacts, but rustdoc needs its own flavour of dependency
metadata, which cargo build does not produce, so a --no-deps docs build
still compiles dependencies it never documents. Measured at 6m47s cold;
that reasoning is recorded in the check's own comment so the next reader
does not re-derive it.

Verified by running the check's exact command against the pre-cleanup
tree first: 40 errors, build failed. A gate that cannot fail is not
evidence, and building it before the cleanup makes that proof free.
2026-08-14 02:30:55 +02:00

126 lines
5 KiB
Rust

//! Host admin socket client: one request/response round trip over
//! `/run/hyperhive/host.sock`.
//!
//! Connect failures are classified into an actionable message before they
//! reach the operator (see `connect_hint`) — the three ways this fails
//! (not in `hive-admin`, no socket, nobody listening) need three different
//! fixes, and the raw `Permission denied (os error 13)` names none of them.
use std::io::ErrorKind;
use std::path::Path;
use anyhow::{Result, bail};
use hive_host_sock::{HostRequest, HostResponse};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::UnixStream;
/// Turn a socket-connect `io::ErrorKind` into a message that names the fix.
///
/// The interesting one is `PermissionDenied`: the socket is `0660
/// root:hive-admin` behind a `0751` runtime dir, so a non-member gets EACCES
/// on connect — and so does a *member* whose shell predates the group being
/// granted, because secondary group membership is only applied at login. That
/// second case is the one that reads as "the daemon is down" and isn't.
fn connect_hint(kind: ErrorKind, socket: &Path) -> String {
let path = socket.display();
match kind {
ErrorKind::PermissionDenied => format!(
"permission denied opening the host admin socket {path} — reaching it requires \
membership in the `hive-admin` group. Add your user to \
`services.hyperhive.adminUsers`, then log out and back in: secondary group \
membership is applied at login, so a shell opened before the change still \
cannot connect. `id -nG` shows what the running shell actually has; \
`newgrp hive-admin` picks the group up without a full re-login"
),
ErrorKind::NotFound => format!(
"no socket at {path} — hive-c0re is not running, or it binds somewhere else \
(`--socket`). Check `systemctl status hive-c0re.socket hive-c0re.service`"
),
ErrorKind::ConnectionRefused => format!(
"nothing is listening on {path} — the socket exists but the daemon behind it \
is down. Check `systemctl status hive-c0re.service` and \
`journalctl -u hive-c0re`"
),
_ => format!("could not connect to the hive-c0re host admin socket {path}"),
}
}
pub async fn request(socket: &Path, req: HostRequest) -> Result<HostResponse> {
let stream = match UnixStream::connect(socket).await {
Ok(stream) => stream,
// Keep the io error as the cause and put the actionable line on top,
// so `Error: <hint>` / `Caused by: <errno>` reads fix-first.
Err(e) => {
let hint = connect_hint(e.kind(), socket);
return Err(anyhow::Error::new(e).context(hint));
}
};
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: HostResponse = serde_json::from_str(line.trim())?;
Ok(resp)
}
#[cfg(test)]
mod tests {
use super::{ErrorKind, Path, connect_hint};
fn hint(kind: ErrorKind) -> String {
connect_hint(kind, Path::new("/run/hyperhive/host.sock"))
}
/// EACCES is an operator-side fix, and the non-obvious half is the
/// re-login — so both the group and the login requirement must appear,
/// and it must not send the operator off checking a daemon that is fine.
#[test]
fn permission_denied_names_the_group_and_the_relogin() {
let h = hint(ErrorKind::PermissionDenied);
assert!(h.contains("hive-admin"), "{h}");
assert!(h.contains("adminUsers"), "{h}");
assert!(h.contains("log out"), "{h}");
assert!(
!h.contains("not running"),
"EACCES must not blame the daemon: {h}"
);
}
/// The inverse: a missing socket is a daemon-side problem, so it must not
/// send the operator chasing group membership.
#[test]
fn missing_socket_blames_the_daemon_not_the_operator() {
let h = hint(ErrorKind::NotFound);
assert!(h.contains("not running"), "{h}");
assert!(!h.contains("hive-admin"), "{h}");
}
#[test]
fn refused_socket_points_at_the_service_not_the_group() {
let h = hint(ErrorKind::ConnectionRefused);
assert!(h.contains("nothing is listening"), "{h}");
assert!(!h.contains("hive-admin"), "{h}");
}
#[test]
fn every_hint_names_the_socket_path() {
for kind in [
ErrorKind::PermissionDenied,
ErrorKind::NotFound,
ErrorKind::ConnectionRefused,
ErrorKind::BrokenPipe,
] {
let h = hint(kind);
assert!(h.contains("/run/hyperhive/host.sock"), "{kind:?}: {h}");
}
}
}