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
|
|
@ -1,51 +1,27 @@
|
|||
//! `/api/state` + `/api/dashboard-state` snapshot builders.
|
||||
//! `/api/state` (the agent's name, for the stats and screen pages) and
|
||||
//! `/api/dashboard-state` (what the hive dashboard's card reads).
|
||||
|
||||
use axum::extract::State;
|
||||
use serde::Serialize;
|
||||
use swarm_wire::agent_inference::Source;
|
||||
|
||||
use crate::login::LoginState;
|
||||
|
||||
use super::AppState;
|
||||
|
||||
pub(super) async fn api_state(State(state): State<AppState>) -> axum::Json<StateSnapshot> {
|
||||
let login = *state.login.lock().unwrap();
|
||||
let status = status_of(login, state.bus.is_rate_limited());
|
||||
/// What the stats and screen pages read of the agent: its name, for the title
|
||||
/// and the screen's login, and the port of the hive dashboard for the way back.
|
||||
#[derive(Serialize)]
|
||||
pub(super) struct Identity {
|
||||
label: String,
|
||||
dashboard_port: u16,
|
||||
}
|
||||
|
||||
pub(super) async fn api_state(State(state): State<AppState>) -> axum::Json<Identity> {
|
||||
let dashboard_port = std::env::var("HIVE_DASHBOARD_PORT")
|
||||
.ok()
|
||||
.and_then(|s| s.parse::<u16>().ok())
|
||||
.unwrap_or(7000);
|
||||
let inbox = recent_inbox(&state.socket).await;
|
||||
let (turn_state, turn_state_since) = state.bus.state_snapshot();
|
||||
// The alias the operator selected (e.g. "sonnet") vs. what the last
|
||||
// turn actually ran on (e.g. "claude-sonnet-4-5-20260805") — mara:
|
||||
// "i cannot see if sonnet became claude-sonnet-5 or something else".
|
||||
// `None` until the first turn completes (no assistant event to
|
||||
// resolve from yet); same source `serve_common.rs`'s turn-stats
|
||||
// rollup already prefers over the requested alias.
|
||||
let resolved_model = state.bus.last_resolved_model();
|
||||
let ctx_usage = state.bus.last_ctx_usage();
|
||||
let cost_usage = state.bus.last_cost_usage();
|
||||
let shown = shown(&state.bus);
|
||||
axum::Json(StateSnapshot {
|
||||
axum::Json(Identity {
|
||||
label: state.label.clone(),
|
||||
qualified_label: crate::identity::qualify(&state.label),
|
||||
dashboard_port,
|
||||
status,
|
||||
inbox,
|
||||
turn_state,
|
||||
turn_state_since,
|
||||
model: shown.model,
|
||||
model_source: state.bus.model_source(),
|
||||
resolved_model,
|
||||
ctx_usage,
|
||||
cost_usage,
|
||||
links: agent_links(state.gui_vnc_port.is_some()),
|
||||
hive_name: crate::identity::hive_name(),
|
||||
swarm_name: crate::identity::swarm_name(),
|
||||
effort: shown.effort,
|
||||
effort_source: state.bus.effort_source(),
|
||||
paused: crate::paths::paused_marker().exists(),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -71,83 +47,8 @@ pub(super) async fn api_dashboard_state(
|
|||
})
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(super) struct StateSnapshot {
|
||||
label: String,
|
||||
/// Hive-qualified long name (`${label}@${hyperhive.domain}`) when
|
||||
/// the host has been configured for a multi-hive swarm; falls back
|
||||
/// to the short label when the hive domain env var is unset.
|
||||
/// The frontend uses this for the page title / agent self-introduction;
|
||||
/// when it equals `label`, the page renders the short form unchanged.
|
||||
qualified_label: String,
|
||||
dashboard_port: u16,
|
||||
/// `"online"` | `"rate_limited"` | `"needs_login_idle"`.
|
||||
status: &'static str,
|
||||
/// Last N messages addressed to this agent, newest-first. Pulled
|
||||
/// from the broker via the per-agent socket on each render.
|
||||
/// Empty on transport failure.
|
||||
inbox: Vec<hive_sh4re::inbox::InboxRow>,
|
||||
/// Authoritative turn-loop state from the harness and the unix
|
||||
/// timestamp the state was entered. The JS computes the age
|
||||
/// client-side off this rather than tracking it from SSE events.
|
||||
turn_state: crate::events::TurnState,
|
||||
turn_state_since: i64,
|
||||
/// Currently-active claude model name, shown on the page. The swarm
|
||||
/// sets it (`crate::swarm_inference`); the page cannot.
|
||||
model: String,
|
||||
/// Where `model` comes from.
|
||||
model_source: Source,
|
||||
/// The concrete model id the most recent completed turn actually
|
||||
/// ran on (e.g. `"claude-sonnet-4-5-20260805"`), resolved from that
|
||||
/// turn's assistant events — `model` above is the alias the
|
||||
/// operator picked, which claude itself maps to a specific dated
|
||||
/// snapshot. `None` before any turn has completed.
|
||||
resolved_model: Option<String>,
|
||||
/// Last-inference token usage from the most recent completed
|
||||
/// turn — represents the current context-window size at turn-end.
|
||||
/// `null` until the first turn finishes.
|
||||
ctx_usage: Option<hive_claude::TokenUsage>,
|
||||
/// Cumulative token usage across the most recent turn's inferences
|
||||
/// (cost signal). `null` until the first turn finishes.
|
||||
cost_usage: Option<hive_claude::TokenUsage>,
|
||||
/// Navigation links for this agent page. Also served via
|
||||
/// `DashboardState.links` (`GET /api/dashboard-state`) for the
|
||||
/// dashboard card's icon strip. Both are produced by `agent_links()`
|
||||
/// — single source of truth. See [`docs/4-implementation/web-ui/dashboard.md::Container row`]
|
||||
/// for the frontend resolver + which links appear in which conditions.
|
||||
links: Vec<AgentLink>,
|
||||
/// Human name of this hive instance (e.g. `"pr1ma"`). Sourced
|
||||
/// from `HYPERHIVE_HIVE_NAME`; `None` when unset. The frontend
|
||||
/// uses this for the page `<title>` and header label so browser
|
||||
/// tabs disambiguate when multiple hives are open in parallel.
|
||||
hive_name: Option<String>,
|
||||
/// Human name of the swarm (e.g. `"constellat1on"`). Sourced from
|
||||
/// `HYPERHIVE_SWARM_NAME`; `None` when unset.
|
||||
swarm_name: Option<String>,
|
||||
/// Currently-active claude effort level, shown on the page; `None` per
|
||||
/// [`Shown`]. The swarm sets it (`crate::swarm_inference`); the page
|
||||
/// cannot.
|
||||
effort: Option<String>,
|
||||
/// Where `effort` comes from.
|
||||
effort_source: Source,
|
||||
/// Whether this agent's turn loop is currently parked (the harness
|
||||
/// keeps serving this page + its MCP daemons but drives no turns).
|
||||
/// Same on-disk marker hive-c0re's `Coordinator::is_paused` checks
|
||||
/// (`crate::paths::paused_marker`) — read directly here rather than
|
||||
/// asking hive-c0re over the socket, since the harness already has
|
||||
/// the file locally. hive-c0re owns the actual pause/resume *writes*
|
||||
/// (via hive-priv, this process runs unprivileged) — the per-agent
|
||||
/// page's own `⋯` menu POSTs to hive-c0re's existing
|
||||
/// `/api/pause/<name>` / `/api/resume/<name>` (same endpoints
|
||||
/// `<hive-agent-menu>` on the dashboard already uses), this field
|
||||
/// only tells the frontend which of the two to show.
|
||||
paused: bool,
|
||||
}
|
||||
|
||||
/// One navigation link in the agent page header row. The same JSON
|
||||
/// shape appears in both `StateSnapshot.links` (`GET /api/state`,
|
||||
/// per-agent page) and `DashboardState.links` (`GET /api/dashboard-state`,
|
||||
/// dashboard card icon strip). `agent_links()` is the single source
|
||||
/// One navigation link in the dashboard card's icon strip
|
||||
/// (`DashboardState.links`, `GET /api/dashboard-state`). `agent_links()` is the single source
|
||||
/// of truth for what links an agent exposes.
|
||||
#[derive(Serialize)]
|
||||
struct AgentLink {
|
||||
|
|
@ -286,225 +187,23 @@ struct ExtraLink {
|
|||
url: String,
|
||||
}
|
||||
|
||||
/// Best-effort: pull the last 30 messages addressed to us via the
|
||||
/// per-agent socket. Empty list on any transport / decode
|
||||
/// failure — the inbox section is decorative, not authoritative.
|
||||
async fn recent_inbox(socket: &std::path::Path) -> Vec<hive_sh4re::inbox::InboxRow> {
|
||||
const LIMIT: u64 = 30;
|
||||
// Deadline-bounded (via `broker_request`): `/api/state` must render even
|
||||
// when hive-c0re is busy — an empty inbox section beats a hung snapshot.
|
||||
match super::broker_request(
|
||||
socket,
|
||||
&hive_core_agent_sock::Request::Recent { limit: LIMIT },
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(hive_core_agent_sock::Response::Recent { rows }) => rows,
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The model and effort the page shows. No effort on an ACP agent whose
|
||||
/// session offers no effort level now.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub(super) struct Shown {
|
||||
pub(super) model: String,
|
||||
pub(super) effort: Option<String>,
|
||||
}
|
||||
|
||||
/// On claude, the requested model and effort. On ACP, see [`offered_shown`].
|
||||
pub(super) fn shown(bus: &crate::events::Bus) -> Shown {
|
||||
let (model, effort) = (bus.model(), bus.effort());
|
||||
match bus.session_choices() {
|
||||
Some(choices) => offered_shown(model, effort, choices.get()),
|
||||
None => Shown {
|
||||
model,
|
||||
effort: Some(effort),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// What an ACP agent shows. The runtime sets a requested value on the next
|
||||
/// turn, and only if the session offers it, so the model and the effort each
|
||||
/// show the requested value when offered and not refused by the agent, and
|
||||
/// the session's own otherwise. The effort levels on offer come with the
|
||||
/// model, so while a new model waits for the next turn no effort shows.
|
||||
fn offered_shown(model: String, effort: String, offered: hive_runtime::SessionChoices) -> Shown {
|
||||
let settable = |wanted: &String, c: &hive_runtime::Choice| {
|
||||
c.values.contains(wanted) && c.refused.as_ref() != Some(wanted)
|
||||
};
|
||||
let pick = |wanted: String, choice: Option<&hive_runtime::Choice>| match choice {
|
||||
Some(c) if !settable(&wanted, c) => c.current.clone(),
|
||||
_ => wanted,
|
||||
};
|
||||
let model_pending = offered
|
||||
.model
|
||||
.as_ref()
|
||||
.is_some_and(|c| c.current != model && settable(&model, c));
|
||||
let effort_choice = offered.effort.filter(|_| !model_pending);
|
||||
Shown {
|
||||
model: pick(model, offered.model.as_ref()),
|
||||
effort: effort_choice.map(|c| pick(effort, Some(&c))),
|
||||
}
|
||||
}
|
||||
|
||||
/// The `status` string of the state snapshot: credentials missing beats
|
||||
/// everything, then a rate limit, else online.
|
||||
fn status_of(login: LoginState, rate_limited: bool) -> &'static str {
|
||||
match login {
|
||||
LoginState::Online if rate_limited => "rate_limited",
|
||||
LoginState::Online => "online",
|
||||
LoginState::NeedsLogin => "needs_login_idle",
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use hive_runtime::{Choice, SessionChoices};
|
||||
|
||||
use super::{LoginState, Shown, offered_shown, status_of};
|
||||
|
||||
#[test]
|
||||
fn status_follows_the_credentials_then_the_rate_limit() {
|
||||
assert_eq!(status_of(LoginState::Online, false), "online");
|
||||
assert_eq!(status_of(LoginState::Online, true), "rate_limited");
|
||||
assert_eq!(status_of(LoginState::NeedsLogin, false), "needs_login_idle");
|
||||
assert_eq!(
|
||||
status_of(LoginState::NeedsLogin, true),
|
||||
"needs_login_idle",
|
||||
"missing credentials outrank a rate limit"
|
||||
);
|
||||
}
|
||||
|
||||
fn choice(current: &str, values: &[&str]) -> Choice {
|
||||
Choice {
|
||||
current: current.to_owned(),
|
||||
values: values.iter().map(|v| (*v).to_owned()).collect(),
|
||||
refused: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn shown(model: &str, effort: Option<&str>) -> Shown {
|
||||
Shown {
|
||||
model: model.to_owned(),
|
||||
effort: effort.map(str::to_owned),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acp_shows_the_requested_values_the_session_offers() {
|
||||
let offered = SessionChoices {
|
||||
model: Some(choice("m/think", &["m/think", "m/plain"])),
|
||||
effort: Some(choice("low", &["low", "high"])),
|
||||
};
|
||||
// `haiku` and `max` are not on offer: the session's own values show.
|
||||
assert_eq!(
|
||||
offered_shown("haiku".into(), "max".into(), offered.clone()),
|
||||
shown("m/think", Some("low"))
|
||||
);
|
||||
assert_eq!(
|
||||
offered_shown("m/think".into(), "high".into(), offered),
|
||||
shown("m/think", Some("high"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acp_shows_no_effort_without_one_on_offer_or_while_a_model_is_pending() {
|
||||
let think = SessionChoices {
|
||||
model: Some(choice("m/think", &["m/think", "m/plain"])),
|
||||
effort: Some(choice("low", &["low", "high"])),
|
||||
};
|
||||
assert_eq!(
|
||||
offered_shown("m/plain".into(), "max".into(), think),
|
||||
shown("m/plain", None)
|
||||
);
|
||||
let plain = SessionChoices {
|
||||
model: Some(choice("m/plain", &["m/think", "m/plain"])),
|
||||
effort: None,
|
||||
};
|
||||
assert_eq!(
|
||||
offered_shown("m/plain".into(), "high".into(), plain),
|
||||
shown("m/plain", None)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acp_shows_the_session_s_model_once_the_agent_refuses_the_wanted_one() {
|
||||
let refused = SessionChoices {
|
||||
model: Some(Choice {
|
||||
refused: Some("m/plain".into()),
|
||||
..choice("m/think", &["m/think", "m/plain"])
|
||||
}),
|
||||
effort: Some(choice("low", &["low", "high"])),
|
||||
};
|
||||
assert_eq!(
|
||||
offered_shown("m/plain".into(), "high".into(), refused),
|
||||
shown("m/think", Some("high"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acp_shows_the_requested_model_and_no_effort_before_a_session_offers_anything() {
|
||||
assert_eq!(
|
||||
offered_shown("haiku".into(), "high".into(), SessionChoices::default()),
|
||||
shown("haiku", None)
|
||||
);
|
||||
}
|
||||
|
||||
fn sorted_keys(v: &serde_json::Value) -> Vec<&str> {
|
||||
let mut keys: Vec<&str> = v.as_object().unwrap().keys().map(String::as_str).collect();
|
||||
keys.sort_unstable();
|
||||
keys
|
||||
}
|
||||
|
||||
/// `GET /api/state` carries exactly the fields the agent page and stats
|
||||
/// page read; a field added back needs a reader.
|
||||
/// `GET /api/state` carries exactly what the stats and screen pages read.
|
||||
#[test]
|
||||
fn the_state_snapshot_serialises_exactly_its_read_fields() {
|
||||
let snap = super::StateSnapshot {
|
||||
fn the_identity_serialises_exactly_its_read_fields() {
|
||||
let identity = super::Identity {
|
||||
label: "a".to_owned(),
|
||||
qualified_label: "a".to_owned(),
|
||||
dashboard_port: 7000,
|
||||
status: "online",
|
||||
inbox: Vec::new(),
|
||||
turn_state: crate::events::TurnState::Idle,
|
||||
turn_state_since: 0,
|
||||
model: "haiku".to_owned(),
|
||||
model_source: swarm_wire::agent_inference::Source::Default,
|
||||
resolved_model: None,
|
||||
ctx_usage: None,
|
||||
cost_usage: None,
|
||||
links: Vec::new(),
|
||||
hive_name: None,
|
||||
swarm_name: None,
|
||||
effort: None,
|
||||
effort_source: swarm_wire::agent_inference::Source::Default,
|
||||
paused: false,
|
||||
};
|
||||
let value = serde_json::to_value(&snap).unwrap();
|
||||
assert_eq!(
|
||||
sorted_keys(&value),
|
||||
[
|
||||
"cost_usage",
|
||||
"ctx_usage",
|
||||
"dashboard_port",
|
||||
"effort",
|
||||
"effort_source",
|
||||
"hive_name",
|
||||
"inbox",
|
||||
"label",
|
||||
"links",
|
||||
"model",
|
||||
"model_source",
|
||||
"paused",
|
||||
"qualified_label",
|
||||
"resolved_model",
|
||||
"status",
|
||||
"swarm_name",
|
||||
"turn_state",
|
||||
"turn_state_since",
|
||||
]
|
||||
);
|
||||
let value = serde_json::to_value(&identity).unwrap();
|
||||
assert_eq!(sorted_keys(&value), ["dashboard_port", "label"]);
|
||||
}
|
||||
|
||||
/// `GET /api/dashboard-state` carries exactly what the dashboard card reads.
|
||||
|
|
|
|||
Loading…
Reference in a new issue