remove the agent's live terminal page and the endpoints only it used
This commit is contained in:
parent
802151820c
commit
6289b8d64b
1 changed files with 127 additions and 4053 deletions
|
|
@ -9,7 +9,6 @@
|
|||
//! shared [`AppState`], the listener + router wiring in [`serve`], and a couple
|
||||
//! of small shared helpers ([`error_response`], [`SOCKET_FETCH_TIMEOUT`]).
|
||||
|
||||
mod actions;
|
||||
mod auth;
|
||||
pub(crate) use auth::force_logout;
|
||||
mod proxy;
|
||||
|
|
@ -25,26 +24,13 @@ use std::sync::{Arc, Mutex};
|
|||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use axum::{
|
||||
Router,
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
routing::{get, post},
|
||||
};
|
||||
use axum::{Router, response::Redirect, routing::get};
|
||||
use backon::{ExponentialBuilder, Retryable};
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
use crate::events::Bus;
|
||||
use crate::login::LoginState;
|
||||
|
||||
/// Deadline for broker-backed fetches on web-UI request paths. The
|
||||
/// page's critical fields (status, turn state, usage) are all
|
||||
/// in-memory; a busy or stalled hive-c0re must degrade the
|
||||
/// socket-backed extras (inbox rows, loose ends, reminder stats)
|
||||
/// instead of hanging the whole response — an unbounded await here is
|
||||
/// what let `/api/state` stall long enough to bork the terminal.
|
||||
const SOCKET_FETCH_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(3);
|
||||
|
||||
/// Live login state for the web UI. The harness updates this in place as it
|
||||
/// transitions between `NeedsLogin` and `Online`; the UI reads on each
|
||||
/// render.
|
||||
|
|
@ -53,33 +39,19 @@ pub type LoginStateCell = Arc<Mutex<LoginState>>;
|
|||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
label: String,
|
||||
login: LoginStateCell,
|
||||
bus: Bus,
|
||||
socket: PathBuf,
|
||||
/// VNC port from the `HIVE_GUI_VNC_PORT` env var at startup.
|
||||
/// `None` when unset (gui not enabled for this agent).
|
||||
gui_vnc_port: Option<u16>,
|
||||
/// Set by `post_cancel_turn` on a successful SIGINT; read-and-cleared
|
||||
/// by the serve loop's next `handle_turn` to prepend
|
||||
/// `hive_sh4re::inbox::INTERRUPTED_HINT` to that turn's wake prompt. Shared
|
||||
/// with the serve loop via the same `Arc` (see `serve_main`) — an
|
||||
/// in-memory flag, not a marker file, since a `/cancel` from a prior
|
||||
/// process lifetime isn't meaningful once the harness restarts.
|
||||
interrupted: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
/// The API routes, without the extra proxies and the static fallback.
|
||||
fn routes() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/", get(|| async { Redirect::temporary("stats") }))
|
||||
.route("/api/state", get(state::api_state))
|
||||
.route("/api/dashboard-state", get(state::api_dashboard_state))
|
||||
.route("/events/stream", get(stream::events_stream))
|
||||
.route("/events/history", get(stream::events_history))
|
||||
.route("/api/cancel", post(actions::post_cancel_turn))
|
||||
.route("/api/new-session", post(actions::post_new_session))
|
||||
.route("/api/logout", post(auth::post_logout))
|
||||
.route("/api/todos", get(stats::api_todos))
|
||||
.route("/api/todos/mark-done", post(actions::post_mark_todos_done))
|
||||
.route("/api/stats", get(stats::api_stats))
|
||||
.route("/screen/ws", get(screen::screen_ws))
|
||||
.route("/icon", get(screen::serve_icon))
|
||||
|
|
@ -97,14 +69,7 @@ fn routes() -> Router<AppState> {
|
|||
/// Returns an error if neither the TCP listener (default) nor the
|
||||
/// unix-socket bind (`HIVE_WEB_SOCKET`, if set) can be acquired, or
|
||||
/// if `HIVE_STATIC_DIR` is missing.
|
||||
pub async fn serve(
|
||||
label: String,
|
||||
port: u16,
|
||||
login: LoginStateCell,
|
||||
bus: Bus,
|
||||
socket: PathBuf,
|
||||
interrupted: Arc<AtomicBool>,
|
||||
) -> Result<()> {
|
||||
pub async fn serve(label: String, port: u16, bus: Bus) -> Result<()> {
|
||||
let gui_vnc_port = read_gui_vnc_port();
|
||||
let static_dir: PathBuf = std::env::var_os("HIVE_STATIC_DIR")
|
||||
.map(PathBuf::from)
|
||||
|
|
@ -121,11 +86,8 @@ pub async fn serve(
|
|||
tracing::info!(static_dir = %static_dir.display(), "web UI static dir resolved");
|
||||
let state = AppState {
|
||||
label,
|
||||
login,
|
||||
bus,
|
||||
socket,
|
||||
gui_vnc_port,
|
||||
interrupted,
|
||||
};
|
||||
let app = routes();
|
||||
// Mount any `services.hyperhive.agent.extraWebProxies` under `/extra/<name>/` before the
|
||||
|
|
@ -401,59 +363,11 @@ fn sigint_turn(pid: u32) -> SigintOutcome {
|
|||
}
|
||||
}
|
||||
|
||||
fn error_response(status: StatusCode, message: &str) -> Response {
|
||||
// Plain text — JS app surfaces in `alert()`, HTML wrapping would just
|
||||
// be noise. Status is per-caller: 400 for bad input, 409 for a
|
||||
// retryable state conflict (turn in flight / hive-c0re busy), 500 only
|
||||
// for a genuine server/transport failure — the frontend shows the code
|
||||
// in its alert, so a benign "busy, retry" must not read as a 500.
|
||||
(status, message.to_owned()).into_response()
|
||||
}
|
||||
|
||||
/// Why a deadline-bounded broker request via the per-agent socket didn't
|
||||
/// yield a response. Callers `.ok()` both today; the variants stay distinct so a
|
||||
/// handler that must tell a busy hive-c0re from a broken socket can.
|
||||
enum BrokerError {
|
||||
/// Outran [`SOCKET_FETCH_TIMEOUT`] — hive-c0re is busy or stalled, not a
|
||||
/// broken socket.
|
||||
Timeout,
|
||||
/// The socket transport itself failed (connect / encode / decode).
|
||||
Transport,
|
||||
}
|
||||
|
||||
/// Issue a broker request over the per-agent socket, bounded by
|
||||
/// [`SOCKET_FETCH_TIMEOUT`] so a busy or stalled hive-c0re degrades the
|
||||
/// response instead of hanging it. Callers match the returned [`Response`]
|
||||
/// variant themselves; the error side distinguishes a retryable timeout from
|
||||
/// a transport failure. This is the one shared broker-call scaffold — every
|
||||
/// web-UI handler that talks to the broker goes through it.
|
||||
async fn broker_request(
|
||||
socket: &Path,
|
||||
req: &hive_core_agent_sock::Request,
|
||||
) -> std::result::Result<hive_core_agent_sock::Response, BrokerError> {
|
||||
match tokio::time::timeout(
|
||||
SOCKET_FETCH_TIMEOUT,
|
||||
hive_sock_client::request::<_, hive_core_agent_sock::Response>(
|
||||
socket,
|
||||
req,
|
||||
crate::CONTROL_SOCKET_RETRY,
|
||||
),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(Ok(resp)) => Ok(resp),
|
||||
Ok(Err(_)) => Err(BrokerError::Transport),
|
||||
Err(_) => Err(BrokerError::Timeout),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::os::unix::process::CommandExt as _;
|
||||
use std::path::Path;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
use axum::body::Body;
|
||||
|
|
@ -462,7 +376,6 @@ mod tests {
|
|||
|
||||
use super::{AppState, SigintOutcome, bind_with_retry, routes, sigint_turn, try_bind};
|
||||
use crate::events::Bus;
|
||||
use crate::login::LoginState;
|
||||
|
||||
/// Hold a loopback port just below the kernel's ephemeral range. Once the
|
||||
/// holder drops, nothing that binds `:0` or connects outward can take the
|
||||
|
|
@ -534,11 +447,8 @@ mod tests {
|
|||
let (model, effort, existing) = crate::paths::with_harness_dir(|| {
|
||||
let state = AppState {
|
||||
label: "test".to_owned(),
|
||||
login: Arc::new(Mutex::new(LoginState::Online)),
|
||||
bus: Bus::new(),
|
||||
socket: crate::paths::harness_dir().join("mcp.sock"),
|
||||
gui_vnc_port: None,
|
||||
interrupted: Arc::new(AtomicBool::new(false)),
|
||||
};
|
||||
let app = routes().with_state(state);
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
|
|
@ -549,7 +459,8 @@ mod tests {
|
|||
let effort = app.clone().oneshot(form("/api/effort", "effort=high"));
|
||||
// A route the router does serve, so the 404s above are not
|
||||
// the router refusing everything.
|
||||
let existing = app.oneshot(form("/api/new-session", ""));
|
||||
let existing =
|
||||
app.oneshot(Request::get("/api/state").body(Body::empty()).unwrap());
|
||||
(
|
||||
model.await.unwrap(),
|
||||
effort.await.unwrap(),
|
||||
|
|
|
|||
Loading…
Reference in a new issue