Watch
0
0
Fork
You've already forked hyperhive
0

remove the agent's stats page and its endpoint

This commit is contained in:
iris 2026-11-16 19:00:52 +01:00
commit 6ed714a0a7
21 changed files with 49 additions and 1744 deletions

View file

@ -98,8 +98,7 @@ box it gets:
at `/knowledge`, plus hyperhive's own docs in the container. → at `/knowledge`, plus hyperhive's own docs in the container. →
[knowledge](docs/2-handbook/swarm/knowledge.md) [knowledge](docs/2-handbook/swarm/knowledge.md)
- **web UI** — the swarm UI's terminal for each agent: the live turn stream and a - **web UI** — the swarm UI's terminal for each agent: the live turn stream and a
message box, with `/compact` and `/clear`; a per-agent stats page behind the message box, with `/compact` and `/clear`.
gateway.
- **GUI** (opt-in, `services.hyperhive.agent.gui.enable`) — a Wayland - **GUI** (opt-in, `services.hyperhive.agent.gui.enable`) — a Wayland
desktop with screenshot / keyboard / mouse tools, viewable over VNC in desktop with screenshot / keyboard / mouse tools, viewable over VNC in
the web UI. the web UI.

View file

@ -8,11 +8,9 @@ watch an agent work or write to it, use its terminal in the
[swarm UI](../swarm/ui.md), and `hivectl watch` follows it from the command [swarm UI](../swarm/ui.md), and `hivectl watch` follows it from the command
line. What the server offers: line. What the server offers:
- **Stats page**: the agent's turn statistics. The same figures are in
Grafana's `agents` board.
- **Screen page**: a live view of the agent's desktop, for an agent with a GUI. - **Screen page**: a live view of the agent's desktop, for an agent with a GUI.
- **Dashboard card data**: the status text and the links the hive dashboard shows - **Dashboard card data**: the status text and the links the hive dashboard shows
for the agent. for the agent.
The endpoints and the stats page are in The endpoints are in
[the per-agent web server reference](../../4-implementation/web-ui/agent-page.md). [the per-agent web server reference](../../4-implementation/web-ui/agent-page.md).

View file

@ -60,14 +60,15 @@ The `hive-bash-daemon` (not the harness) writes a sibling
file: one row per executed bash task recording the normalised command head - file: one row per executed bash task recording the normalised command head -
the basename of the first real command, looking past `cd repo &&` the basename of the first real command, looking past `cd repo &&`
prefixes, env-assignments, and prefix-runners like `sudo`/`env`. It prefixes, env-assignments, and prefix-runners like `sudo`/`env`. It
backs the "favorite tools" view on the /stats page. Best-effort and created on first write feeds the bash-command counter in the agent's OTEL metrics. Best-effort and created on first write
(`CREATE TABLE IF NOT EXISTS`), so it's absent until a bash (`CREATE TABLE IF NOT EXISTS`), so it's absent until a bash
task runs. task runs.
turn-stats.sqlite has **no vacuum** — it's one small row per turn turn-stats.sqlite has **no vacuum** — it's one small row per turn
(~hundreds of KB even over months), read directly by the `/stats` page, (~hundreds of KB even over months), so pruning it would only lose trend
so pruning it would only lose trend history for no space gain. The agent has no page that charts it; the
history for no space gain. [legacy stats viewer](https://forge.constellation.darkest.space/hyperhive/legacy-stats-viewer)
turns the file into the old stats page.
### `/state/hyperhive-harness.json` (per agent) ### `/state/hyperhive-harness.json` (per agent)

View file

@ -379,8 +379,7 @@ checking file existence):
| request | resolved | outcome | | request | resolved | outcome |
| --- | --- | --- | | --- | --- | --- |
| `/agent/iris/` | no file match → `@iris_dynamic` | proxied to the agent daemon, which redirects to `stats` | | `/agent/iris/` | no file match → `@iris_dynamic` | proxied to the agent daemon, which has no page there |
| `/agent/iris/stats` | `<frontend>/stats.html` | stats page |
| `/agent/iris/screen` | `<frontend>/screen.html` | screen page | | `/agent/iris/screen` | `<frontend>/screen.html` | screen page |
| `/agent/iris/static/app.js` | caught by `^~` block first | served with immutable cache | | `/agent/iris/static/app.js` | caught by `^~` block first | served with immutable cache |
| `/agent/iris/api/state` | no file match → `@iris_dynamic` | proxied to agent daemon | | `/agent/iris/api/state` | no file match → `@iris_dynamic` | proxied to agent daemon |

View file

@ -68,7 +68,6 @@ container needs a stable numeric id across a full destroy + recreate.
```nix ```nix
services.hyperhive.agent.dashboardLinks = [ services.hyperhive.agent.dashboardLinks = [
{ label = "Stats"; icon = "📊"; url = "http://localhost:9001/stats"; }
{ label = "Scratchpad"; url = "http://localhost:8080"; } { label = "Scratchpad"; url = "http://localhost:8080"; }
]; ];
``` ```

View file

@ -1,8 +1,7 @@
# Per-agent web server: stats, screen and endpoints # Per-agent web server: screen and endpoints
What each agent's harness serves on its own port: the stats page, the GUI What each agent's harness serves on its own port: the GUI screen page, the
screen page, the stream `hivectl watch` reads, the dashboard card's state and stream `hivectl watch` reads, the dashboard card's state and the extra proxies. The operator's overview is
the extra proxies. The operator's overview is
[the agent web server](../../2-handbook/agents/web-page.md). The agent has no [the agent web server](../../2-handbook/agents/web-page.md). The agent has no
terminal page of its own: the live terminal and its message box are in the swarm terminal page of its own: the live terminal and its message box are in the swarm
UI. UI.
@ -13,18 +12,14 @@ The agent has no login flow of its own. An agent without credentials
reports `needs_login_idle`, the operator logs in from the swarm UI, and the reports `needs_login_idle`, the operator logs in from the swarm UI, and the
credentials arrive from the swarm secret store. credentials arrive from the swarm secret store.
- `GET /api/state` — `{label, dashboard_port}`: the agent's name, for the stats - `GET /api/state` — `{label, dashboard_port}`: the agent's name, for the screen
and screen pages' titles and the screen's login, and the hive dashboard's port page's title and login, and the hive dashboard's port for its way back.
for their way back.
- `GET /api/dashboard-state` — lean snapshot of agent-owned fields - `GET /api/dashboard-state` — lean snapshot of agent-owned fields
fetched once per running agent by the dashboard's container row to fetched once per running agent by the dashboard's container row to
get fresh values without relying on hive-c0re's periodic file-reads. get fresh values without relying on hive-c0re's periodic file-reads.
Returns `{ status_text?, status_set_at?, rate_limited, links }`. Only called when the container is running; Returns `{ status_text?, status_set_at?, rate_limited, links }`. Only called when the container is running;
skipped (muted badges) when stopped. Also accessible via the gateway skipped (muted badges) when stopped. Also accessible via the gateway
at `/agent/<name>/api/dashboard-state`. at `/agent/<name>/api/dashboard-state`.
- `GET /api/stats?window=24h|7d|30d|all` — time-bucketed turn analytics
`Snapshot` consumed by the `/stats` page. `all` ranges from the
earliest recorded turn with an adaptive bucket width.
- `GET /icon` — agent's icon as `image/svg+xml`. Returns - `GET /icon` — agent's icon as `image/svg+xml`. Returns
`/etc/hyperhive/icon.svg` (set via `services.hyperhive.agent.icon` in `agent.nix`) `/etc/hyperhive/icon.svg` (set via `services.hyperhive.agent.icon` in `agent.nix`)
when present, otherwise **404** — there is no server-side default. when present, otherwise **404** — there is no server-side default.
@ -74,7 +69,6 @@ credentials arrive from the swarm secret store.
no UDS transport. Implemented in `web_ui/proxy.rs`. no UDS transport. Implemented in `web_ui/proxy.rs`.
- `GET /events/stream` — the live SSE stream of classified terminal rows, which - `GET /events/stream` — the live SSE stream of classified terminal rows, which
`hivectl watch` reads. The swarm UI shows the same rows from the swarm queue. `hivectl watch` reads. The swarm UI shows the same rows from the swarm queue.
- `GET /` redirects to `/stats`.
Bus events on `/events/stream`: Bus events on `/events/stream`:
@ -94,45 +88,3 @@ Bus events on `/events/stream`:
totals). totals).
- `turn_state_changed { state, since_unix }` — drives the - `turn_state_changed { state, since_unix }` — drives the
state badge (`idle`/`thinking`/`compacting`). state badge (`idle`/`thinking`/`compacting`).
## Stats page
`GET /stats` is a per-agent page (served by the
harness, linked from each dashboard container row). Turn analytics, read-only, from
`/state/hyperhive-turn-stats.sqlite`. `GET /api/stats?window=
24h|7d|30d|all` returns a time-bucketed `Snapshot`; the page renders
it with Chart.js (bundled into `stats.js` via esbuild — no CDN
dependency). Charts: turns, duration (p50 · p95 · avg), context
tokens, token cost per bucket, a **turns-by-model** stacked bar
(model choice drives token cost, so it sits directly under the
cost chart), doughnuts for tool / wake-source / result mix, and a
**result-trend** stacked bar — per-bucket `result_counts` so
error / rate-limit / compaction outcomes are visible over time
(the doughnut shows only the window total).
A **favorite tools** doughnut shows the most-run shell commands —
normalised `bash_commands` heads written per bash task by the
hive-bash-daemon capture: the basename of the _first real command_,
looking past `cd repo &&` prefixes, env-assignments, and
prefix-runners like `sudo` / `env` (so `cd /repo && cargo build`
records `cargo`, not `cd`). Read via `bash_breakdown`; the card
stays hidden until the agent has run a bash command (a missing
`bash_commands` table degrades to an empty list), so it never
renders an empty chart.
A summary chip row carries window totals, plus two
token-efficiency chips derived from the bucket sums: **cache
hit-rate** (`cache_read` over all input-side tokens) and
**tokens/turn**. When `reminder_stats` is present (fetched via
`ReminderRollup` RPC and merged into the snapshot in
`web_ui/stats.rs::api_stats`) three more chips appear: **reminders
scheduled / delivered / pending** for the window. When the
per-session capture has data, a **first-turn ctx** chip shows the
input tokens of the most recent fresh claude session's first turn —
a proxy for system-prompt + AGENTS.md sprawl (a fresh session's first
turn pays the full static prefix uncached). It's derived in `stats.rs`
(`first_turn_ctx`: the first turn, `ORDER BY started_at LIMIT 1`, of
the latest `sessions` row in the window) and stays out of the JSON
until the `sessions` / `turn_stats.session_id` capture has rows — so
the chip stays hidden on older dbs (inert-until-capture).
`stats.rs` opens the sqlite db read-only and degrades to an
empty snapshot on any error — the page is decorative, never
authoritative.

View file

@ -283,10 +283,10 @@ That makes the page work under any prefix the agent ends up mounted
at without rebuilding the dist. The cases that matter: at without rebuilding the dist. The cases that matter:
| served at | `api/state` resolves to | | served at | `api/state` resolves to |
| ------------------------------------------------ | ----------------------- | | ------------------------------------------------- | ----------------------- |
| `/` (own port, today's shape) | `/api/state` | | `/` (own port, today's shape) | `/api/state` |
| `/agent/iris/` (gateway-prefixed) | `/agent/iris/api/state` | | `/agent/iris/` (gateway-prefixed) | `/agent/iris/api/state` |
| `/agent/iris/stats` (subpage, no trailing slash) | `/agent/iris/api/state` | | `/agent/iris/screen` (subpage, no trailing slash) | `/agent/iris/api/state` |
The gateway upstream config strips the prefix before forwarding to The gateway upstream config strips the prefix before forwarding to
the per-agent server, so the agent's Rust routes (`api/state`, the per-agent server, so the agent's Rust routes (`api/state`,
@ -294,9 +294,9 @@ the per-agent server, so the agent's Rust routes (`api/state`,
paths server-side. Only the browser-facing URLs key off the paths server-side. Only the browser-facing URLs key off the
mount prefix. mount prefix.
The router serves subpages (`stats`, `screen`) without a trailing slash so The router serves subpages (`screen`) without a trailing slash so
the relative-path resolution stays correct: `static/stats.js` from the relative-path resolution stays correct: `static/agent.css` from
`/stats` becomes `/static/stats.js` (last segment `stats` gets `/screen` becomes `/static/agent.css` (last segment `screen` gets
replaced), not `/stats/static/stats.js`. Adding a trailing slash to replaced), not `/screen/static/agent.css`. Adding a trailing slash to
those routes would break the resolution; either keep them those routes would break the resolution; either keep them
slash-less or use `<base href>` injection at serve time. slash-less or use `<base href>` injection at serve time.

View file

@ -1 +1 @@
sha256-Hzu21wcDxdn32JJPpj/3sJSkEDH32mqrm4YsFvHfni0= sha256-EuIAdR+yd+/qxmQTg1LFjiGdFuZUlZK30hMmxBSmisg=

View file

@ -475,12 +475,6 @@
"resolved": "packages/swarm-ui", "resolved": "packages/swarm-ui",
"link": true "link": true
}, },
"node_modules/@kurkle/color": {
"version": "0.3.4",
"resolved": "https://registry.npmjs.org/@kurkle/color/-/color-0.3.4.tgz",
"integrity": "sha512-M5UknZPHRu3DEDWoipU6sE8PdkZ6Z/S+v4dD+Ke8IaNlpdSQah50lz1KtcFBa2vsdOnwbbnxJwVM4wty6udA5w==",
"license": "MIT"
},
"node_modules/@types/trusted-types": { "node_modules/@types/trusted-types": {
"version": "2.0.7", "version": "2.0.7",
"resolved": "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.7.tgz", "resolved": "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.7.tgz",
@ -828,18 +822,6 @@
"node": ">=16.20.0" "node": ">=16.20.0"
} }
}, },
"node_modules/chart.js": {
"version": "4.6.1",
"resolved": "https://registry.npmjs.org/chart.js/-/chart.js-4.6.1.tgz",
"integrity": "sha512-hJyniucsFmq//EvXYvJ6c6mahPoI3kaxdjVH8awTaJ/fDNWeSqqqArixMJWkWNN6fcYvPU3HneSANFNYtRi61g==",
"license": "MIT",
"dependencies": {
"@kurkle/color": "^0.3.0"
},
"engines": {
"pnpm": ">=8"
}
},
"node_modules/dompurify": { "node_modules/dompurify": {
"version": "3.4.17", "version": "3.4.17",
"resolved": "https://registry.npmjs.org/dompurify/-/dompurify-3.4.17.tgz", "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-3.4.17.tgz",
@ -982,7 +964,6 @@
"version": "0.0.0", "version": "0.0.0",
"dependencies": { "dependencies": {
"@hive/shared": "*", "@hive/shared": "*",
"chart.js": "4.6.1",
"preact": "11.2.0" "preact": "11.2.0"
}, },
"devDependencies": { "devDependencies": {

View file

@ -1,10 +1,6 @@
// esbuild build for @hive/agent. Output layout (`dist/`): // esbuild build for @hive/agent. Output layout (`dist/`):
// //
// dist/stats.html served at GET /stats
// dist/screen.html served at GET /screen // dist/screen.html served at GET /screen
// dist/static/stats.js served at /static/stats.js (pulls in
// chart.js/auto)
// dist/static/stats.js.map source map sibling
// dist/static/agent.css served at /static/agent.css (@import // dist/static/agent.css served at /static/agent.css (@import
// resolved from @hive/shared) // resolved from @hive/shared)
// //
@ -26,22 +22,6 @@ const staticDir = (p) => resolve(here, "dist", "static", p);
rmSync(dist(""), { recursive: true, force: true }); rmSync(dist(""), { recursive: true, force: true });
mkdirSync(staticDir(""), { recursive: true }); mkdirSync(staticDir(""), { recursive: true });
// The stats page's own bundle — chart.js/auto, its own deps.
await build({
entryPoints: [src("stats.js")],
outdir: staticDir(""),
bundle: true,
format: "esm",
platform: "browser",
target: ["es2022"],
sourcemap: true,
logLevel: "info",
// `@hive/shared/modal.js` imports its shadow-DOM component CSS as raw
// text (see dashboard/build.mjs's matching comment) — same reasoning
// applies here.
loader: { ".css": "text" },
});
// Bundle the CSS. `colors.css` re-exports the standalone base16 palette // Bundle the CSS. `colors.css` re-exports the standalone base16 palette
// (the theme swap contract — its own output file so a swap replaces only // (the theme swap contract — its own output file so a swap replaces only
// it, no bundle rebuild); `theme.css` is the semantic derivation layer; // it, no bundle rebuild); `theme.css` is the semantic derivation layer;
@ -57,8 +37,6 @@ for (const entry of ["colors.css", "theme.css", "agent.css"]) {
}); });
} }
for (const html of ["stats.html", "screen.html"]) { copyFileSync(src("screen.html"), dist("screen.html"));
copyFileSync(src(html), dist(html));
}
console.log("agent build ok →", dist("")); console.log("agent build ok →", dist(""));

View file

@ -10,7 +10,6 @@
}, },
"dependencies": { "dependencies": {
"@hive/shared": "*", "@hive/shared": "*",
"chart.js": "4.6.1",
"preact": "11.2.0" "preact": "11.2.0"
}, },
"devDependencies": { "devDependencies": {

View file

@ -1,8 +1,8 @@
/* Shared Catppuccin palette + body typography. /* Shared Catppuccin palette + body typography.
Bundled in front of the stats and screen pages' rules below via esbuild. */ Bundled in front of the screen page's rules below via esbuild. */
@import "@hive/shared/base.css"; @import "@hive/shared/base.css";
/* Shared page-header chrome (page-header / page-back / page-title) — gives the /* Shared page-header chrome (page-header / page-back / page-title) — gives the
agent SUB-pages (stats, screen) the same back-link nav the dashboard's agent sub-page (screen) the same back-link nav the dashboard's
standalone pages use. */ standalone pages use. */
@import "@hive/shared/chrome.css"; @import "@hive/shared/chrome.css";
/* Shared pill/chip/badge shape (the "extract pill component" issue, /* Shared pill/chip/badge shape (the "extract pill component" issue,
@ -19,104 +19,9 @@ body {
margin: 0; margin: 0;
} }
/* Normal-document scroll for the stats page. screen-shell is excluded: it
manages its own full-viewport layout below. */
body:not(.screen-shell) {
max-width: 110em;
margin: 1.5em auto;
padding: 0 1.5em;
height: auto;
}
/* ─── /stats page ────────────────────────────────────────────────────────── */
/* The stats page header (back link + title) uses the shared
`.page-header` chrome (chrome.css), same as /screen. */
.window-tabs {
display: flex;
gap: 0.4rem;
margin: 0.5rem 0 1rem;
}
.window-tabs button {
background: var(--bg-elev);
color: var(--fg);
border: 1px solid var(--border);
padding: 0.3rem 0.8rem;
font-family: inherit;
cursor: pointer;
}
/* Active window: createTabStrip (@hive/shared/tabs.js) toggles
`.hive-tab--active`. The window selector keeps its own pill look rather
than the shared `.hive-tab` text-tab base. */
.window-tabs button.hive-tab--active {
background: var(--purple-dim);
border-color: var(--purple);
color: var(--purple);
}
.summary {
display: flex;
gap: 0.75rem;
flex-wrap: wrap;
margin-bottom: 1rem;
}
.summary .chip {
display: flex;
flex-direction: column;
justify-content: center;
align-items: stretch;
background: var(--bg-elev);
border: 1px solid var(--border);
padding: 0.5rem 0.9rem;
border-radius: 4px;
box-sizing: border-box;
min-width: 9rem;
height: 3.4rem;
line-height: 1.2;
}
.summary .chip .label {
color: var(--muted);
font-size: 0.75rem;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.summary .chip .value {
color: var(--cyan);
font-weight: bold;
font-size: 1.05rem;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
gap: 1rem;
}
.stats-card {
background: var(--bg-elev);
border: 1px solid var(--border);
padding: 0.75rem 1rem 1rem;
border-radius: 4px;
}
.stats-card h3 {
margin: 0 0 0.5rem;
color: var(--purple);
font-size: 0.95rem;
font-weight: normal;
}
.stats-card .chart-wrap {
position: relative;
height: 220px;
}
.stats-card.wide {
grid-column: 1 / -1;
}
.stats-card.wide .chart-wrap {
height: 260px;
}
/* ─── /screen page ─── */ /* ─── /screen page ─── */
/* Full-screen VNC canvas viewer (screen.html). All rules scoped to /* Full-screen VNC canvas viewer (screen.html). All rules scoped to
body.screen-shell so they can coexist with the agent/stats rules. */ body.screen-shell so they stay out of any other page's way. */
body.screen-shell { body.screen-shell {
font-size: 14px; font-size: 14px;

View file

@ -1,83 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>hyperhive agent — stats</title>
<link rel="icon" type="image/svg+xml" href="icon" />
<link rel="stylesheet" href="static/colors.css" />
<link rel="stylesheet" href="static/theme.css" />
<link rel="stylesheet" href="static/agent.css" />
</head>
<body>
<header class="page-header">
<a class="page-back" id="dashboard-link" href="#">dashboard ↗</a>
<span class="page-title" id="title">◆ … ◆</span>
</header>
<div class="window-tabs" id="window-tabs" role="tablist">
<button type="button" data-tab="1h">last 1h</button>
<button type="button" data-tab="4h">last 4h</button>
<button type="button" data-tab="24h">last 24h</button>
<button type="button" data-tab="3d">last 3d</button>
<button type="button" data-tab="7d">last 7d</button>
<button type="button" data-tab="30d">last 30d</button>
<button type="button" data-tab="all">all</button>
</div>
<div class="summary" id="summary"></div>
<div class="stats-grid">
<div class="stats-card wide">
<h3>turns per bucket</h3>
<div class="chart-wrap"><canvas id="chart-turns"></canvas></div>
</div>
<div class="stats-card wide">
<h3>turn duration (ms) — p50 / p95 / avg</h3>
<div class="chart-wrap"><canvas id="chart-duration"></canvas></div>
</div>
<div class="stats-card wide">
<h3>context tokens (last inference per turn) — avg / max</h3>
<div class="chart-wrap"><canvas id="chart-ctx"></canvas></div>
</div>
<div class="stats-card wide">
<h3>token cost per bucket (sum across inferences)</h3>
<div class="chart-wrap"><canvas id="chart-cost"></canvas></div>
</div>
<div class="stats-card wide">
<h3>turns by model per bucket — model drives token cost</h3>
<div class="chart-wrap"><canvas id="chart-model"></canvas></div>
</div>
<div class="stats-card">
<h3>top tools</h3>
<div class="chart-wrap"><canvas id="chart-tools"></canvas></div>
</div>
<!-- "favorite tools": most-run shell commands. Hidden until the
bash_commands capture (hive-bash-daemon) has recorded data, so the
card never shows a permanently-empty doughnut. -->
<div class="stats-card" id="card-bash" hidden>
<h3>favorite tools (bash)</h3>
<div class="chart-wrap"><canvas id="chart-bash"></canvas></div>
</div>
<div class="stats-card">
<h3>wake source mix</h3>
<div class="chart-wrap"><canvas id="chart-wake"></canvas></div>
</div>
<div class="stats-card">
<h3>result mix</h3>
<div class="chart-wrap"><canvas id="chart-result"></canvas></div>
</div>
<div class="stats-card wide">
<h3>
result trend per bucket — errors / rate-limits / compactions over time
</h3>
<div class="chart-wrap"><canvas id="chart-result-trend"></canvas></div>
</div>
</div>
<!-- Chart.js is now bundled into stats.js by esbuild (npm dep
chart.js@4.4.4), so the page works offline / on operator
machines without internet egress. No SRI hash to maintain. -->
<script type="module" src="static/stats.js" defer></script>
</body>
</html>

View file

@ -1,533 +0,0 @@
// Per-agent stats page. Fetches /api/state for the title + dashboard link
// once on load, then /api/stats?window=... for the chart data — re-fetches
// when the operator clicks a window tab.
import Chart from "chart.js/auto";
import { createTabStrip } from "@hive/shared/tabs.js";
// Expose for the IIFE below — pre-split this was a window global from
// the jsDelivr CDN script tag. esbuild now bundles chart.js into
// stats.js; once the IIFE opens up we can use the imported `Chart`
// directly.
window.Chart = Chart;
(function () {
"use strict";
const cssVar = (name) =>
getComputedStyle(document.documentElement).getPropertyValue(name).trim();
const palette = {
bg: cssVar("--bg"),
bgElev: cssVar("--bg-elev"),
fg: cssVar("--fg"),
muted: cssVar("--muted"),
purple: cssVar("--purple"),
cyan: cssVar("--cyan"),
pink: cssVar("--pink"),
amber: cssVar("--amber"),
green: cssVar("--green"),
red: cssVar("--red"),
border: cssVar("--border"),
};
// Distinct hues for categorical charts (top tools / wake mix / result mix).
const wheel = [
palette.purple,
palette.cyan,
palette.pink,
palette.amber,
palette.green,
palette.red,
"#94e2d5",
"#f9e2af",
"#74c7ec",
"#b4befe",
];
// Apply Catppuccin defaults globally so each Chart inherits without per-call
// overrides. Chart.js v4 reads these on chart construction.
Chart.defaults.color = palette.fg;
Chart.defaults.borderColor = palette.border;
Chart.defaults.font.family = '"JetBrains Mono", "Fira Code", monospace';
Chart.defaults.font.size = 11;
Chart.defaults.plugins.legend.labels.color = palette.fg;
const charts = {};
let currentWindow = "24h";
function fmtMs(ms) {
if (!Number.isFinite(ms) || ms <= 0) return "0";
if (ms < 1000) return ms.toFixed(0) + "ms";
return (ms / 1000).toFixed(ms < 10000 ? 2 : 1) + "s";
}
function fmtInt(n) {
if (!Number.isFinite(n)) return "0";
return new Intl.NumberFormat().format(Math.round(n));
}
function bucketLabel(ts, bucketSecs) {
const d = new Date(ts * 1000);
if (bucketSecs >= 86400) {
return d.toISOString().slice(5, 10); // MM-DD
}
return d.toISOString().slice(11, 16); // HH:MM
}
function destroy(name) {
if (charts[name]) {
charts[name].destroy();
delete charts[name];
}
}
function paintEmpty(canvasId, msg) {
destroy(canvasId);
const cv = document.getElementById(canvasId);
if (!cv) return;
const ctx = cv.getContext("2d");
ctx.clearRect(0, 0, cv.width, cv.height);
ctx.fillStyle = palette.muted;
ctx.font = "12px monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(msg, cv.width / 2, cv.height / 2);
}
// Sum the four token streams across every bucket in the window.
function tokenTotals(s) {
let input = 0,
output = 0,
cacheRead = 0,
cacheCreation = 0;
for (const b of s.buckets || []) {
input += b.input_tokens || 0;
output += b.output_tokens || 0;
cacheRead += b.cache_read_input_tokens || 0;
cacheCreation += b.cache_creation_input_tokens || 0;
}
return { input, output, cacheRead, cacheCreation };
}
function renderSummary(s) {
const root = document.getElementById("summary");
root.replaceChildren();
const chips = [
["turns", fmtInt(s.turn_count)],
["avg duration", fmtMs(s.duration_summary.avg_ms)],
["p50 duration", fmtMs(s.duration_summary.p50_ms)],
["p95 duration", fmtMs(s.duration_summary.p95_ms)],
["window", s.window],
];
// Token-efficiency chips: cache hit-rate (cached input vs all
// input-side tokens) and average tokens billed per turn.
const t = tokenTotals(s);
const inputSide = t.input + t.cacheRead + t.cacheCreation;
if (inputSide > 0) {
chips.push([
"cache hit-rate",
((100 * t.cacheRead) / inputSide).toFixed(1) + "%",
]);
}
if (s.turn_count > 0) {
const perTurn =
(t.input + t.output + t.cacheRead + t.cacheCreation) / s.turn_count;
chips.push(["tokens/turn", fmtInt(perTurn)]);
}
if (s.reminder_stats) {
chips.push(
["reminders scheduled", fmtInt(s.reminder_stats.scheduled)],
["reminders delivered", fmtInt(s.reminder_stats.delivered)],
["reminders pending", fmtInt(s.reminder_stats.pending)],
);
}
// Session count: fresh claude sessions started in the window (each
// new-session or auto-compaction-fallback mints one). Omitted until
// the sessions table exists (older db).
if (typeof s.session_count === "number") {
chips.push(["sessions", fmtInt(s.session_count)]);
}
// First-turn ctx: input tokens of the most recent fresh session's
// first turn — the cold system-prompt + AGENTS.md cost, a sprawl
// proxy. Omitted from the JSON (and so absent here) until the
// per-session capture has data.
if (typeof s.first_turn_ctx === "number") {
chips.push(["first-turn ctx", fmtInt(s.first_turn_ctx)]);
}
for (const [label, value] of chips) {
const chip = document.createElement("span");
chip.className = "chip";
const l = document.createElement("span");
l.className = "label";
l.textContent = label;
const v = document.createElement("span");
v.className = "value";
v.textContent = value;
chip.append(l, v);
root.append(chip);
}
}
function renderTurnsChart(s) {
const id = "chart-turns";
destroy(id);
const labels = s.buckets.map((b) => bucketLabel(b.ts, s.bucket_seconds));
const data = s.buckets.map((b) => b.turn_count);
charts[id] = new Chart(document.getElementById(id), {
type: "bar",
data: {
labels,
datasets: [
{
label: "turns",
data,
backgroundColor: palette.purple,
borderColor: palette.purple,
borderWidth: 1,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: { legend: { display: false } },
scales: {
x: { grid: { color: palette.border } },
y: {
beginAtZero: true,
grid: { color: palette.border },
ticks: { precision: 0 },
},
},
},
});
}
function renderDurationChart(s) {
const id = "chart-duration";
destroy(id);
const labels = s.buckets.map((b) => bucketLabel(b.ts, s.bucket_seconds));
const ds = (label, color, key) => ({
label,
data: s.buckets.map((b) => b[key]),
borderColor: color,
backgroundColor: color + "33",
tension: 0.25,
pointRadius: 0,
borderWidth: 2,
spanGaps: true,
});
charts[id] = new Chart(document.getElementById(id), {
type: "line",
data: {
labels,
datasets: [
ds("p50", palette.cyan, "p50_duration_ms"),
ds("p95", palette.pink, "p95_duration_ms"),
ds("avg", palette.amber, "avg_duration_ms"),
],
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: { grid: { color: palette.border } },
y: {
beginAtZero: true,
grid: { color: palette.border },
ticks: { callback: (v) => fmtMs(v) },
},
},
},
});
}
function renderCtxChart(s) {
const id = "chart-ctx";
destroy(id);
const labels = s.buckets.map((b) => bucketLabel(b.ts, s.bucket_seconds));
charts[id] = new Chart(document.getElementById(id), {
type: "line",
data: {
labels,
datasets: [
{
label: "avg ctx",
data: s.buckets.map((b) => b.avg_ctx_tokens),
borderColor: palette.cyan,
backgroundColor: palette.cyan + "33",
tension: 0.25,
pointRadius: 0,
borderWidth: 2,
spanGaps: true,
},
{
label: "max ctx",
data: s.buckets.map((b) => b.max_ctx_tokens),
borderColor: palette.amber,
backgroundColor: palette.amber + "33",
tension: 0.25,
pointRadius: 0,
borderWidth: 2,
spanGaps: true,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: { grid: { color: palette.border } },
y: {
beginAtZero: true,
grid: { color: palette.border },
ticks: { callback: (v) => fmtInt(v) },
},
},
},
});
}
function renderCostChart(s) {
const id = "chart-cost";
destroy(id);
const labels = s.buckets.map((b) => bucketLabel(b.ts, s.bucket_seconds));
// Stacked bars: cache_read (cheap) / cache_creation / input / output.
// Highlights "what's actually getting billed at full rate" vs cache hits.
charts[id] = new Chart(document.getElementById(id), {
type: "bar",
data: {
labels,
datasets: [
{
label: "cache_read",
data: s.buckets.map((b) => b.cache_read_input_tokens),
backgroundColor: palette.muted,
},
{
label: "cache_creation",
data: s.buckets.map((b) => b.cache_creation_input_tokens),
backgroundColor: palette.cyan,
},
{
label: "input",
data: s.buckets.map((b) => b.input_tokens),
backgroundColor: palette.amber,
},
{
label: "output",
data: s.buckets.map((b) => b.output_tokens),
backgroundColor: palette.pink,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: { stacked: true, grid: { color: palette.border } },
y: {
stacked: true,
beginAtZero: true,
grid: { color: palette.border },
ticks: { callback: (v) => fmtInt(v) },
},
},
},
});
}
function renderModelChart(s) {
const id = "chart-model";
destroy(id);
const models = s.models || [];
if (!models.length) {
paintEmpty(id, "no turns in window");
return;
}
const labels = s.buckets.map((b) => bucketLabel(b.ts, s.bucket_seconds));
// One stacked series per model. Model choice drives token cost,
// so this lines up against the cost chart above it.
const datasets = models.map((m, i) => ({
label: m,
data: s.buckets.map((b) => (b.model_counts && b.model_counts[m]) || 0),
backgroundColor: wheel[i % wheel.length],
}));
charts[id] = new Chart(document.getElementById(id), {
type: "bar",
data: { labels, datasets },
options: {
responsive: true,
maintainAspectRatio: false,
plugins: { legend: { position: "top", labels: { boxWidth: 12 } } },
scales: {
x: { stacked: true, grid: { color: palette.border } },
y: {
stacked: true,
beginAtZero: true,
grid: { color: palette.border },
ticks: { precision: 0 },
},
},
},
});
}
function renderResultTrendChart(s) {
const id = "chart-result-trend";
destroy(id);
// Series = the result kinds seen in the window (same order +
// colours as the result-mix doughnut). One stacked bar series per
// kind, so error / rate-limit / compaction spikes line up in time.
const kinds = (s.result_mix || []).map((kc) => kc.key);
if (!kinds.length) {
paintEmpty(id, "no results");
return;
}
const labels = s.buckets.map((b) => bucketLabel(b.ts, s.bucket_seconds));
const datasets = kinds.map((k, i) => ({
label: k,
data: s.buckets.map((b) => (b.result_counts && b.result_counts[k]) || 0),
backgroundColor: wheel[i % wheel.length],
}));
charts[id] = new Chart(document.getElementById(id), {
type: "bar",
data: { labels, datasets },
options: {
responsive: true,
maintainAspectRatio: false,
plugins: { legend: { position: "top", labels: { boxWidth: 12 } } },
scales: {
x: { stacked: true, grid: { color: palette.border } },
y: {
stacked: true,
beginAtZero: true,
grid: { color: palette.border },
ticks: { precision: 0 },
},
},
},
});
}
function renderKeyCount(canvasId, items, emptyMsg) {
destroy(canvasId);
if (!items || items.length === 0) {
paintEmpty(canvasId, emptyMsg);
return;
}
const labels = items.map((kc) => kc.key);
const data = items.map((kc) => kc.count);
const colors = items.map((_, i) => wheel[i % wheel.length]);
charts[canvasId] = new Chart(document.getElementById(canvasId), {
type: "doughnut",
data: {
labels,
datasets: [
{
data,
backgroundColor: colors,
borderColor: palette.bg,
borderWidth: 2,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: { legend: { position: "right", labels: { boxWidth: 12 } } },
},
});
}
// "favorite tools" doughnut: most-run shell commands. The capture
// (hive-bash-daemon -> bash_commands table) lands separately, so until
// there's data we hide the whole card rather than show an empty
// doughnut. Runs independently of turn_count (a bash task is tied to
// a turn, but we don't want to couple the two reads).
function renderBashCard(s) {
const card = document.getElementById("card-bash");
const items = s.bash_breakdown || [];
if (!items.length) {
if (card) card.hidden = true;
destroy("chart-bash");
return;
}
if (card) card.hidden = false;
renderKeyCount("chart-bash", items, "no bash commands");
}
function render(s) {
renderSummary(s);
renderBashCard(s);
if (s.turn_count === 0) {
paintEmpty("chart-turns", "no turns in window");
paintEmpty("chart-duration", "no turns in window");
paintEmpty("chart-ctx", "no turns in window");
paintEmpty("chart-cost", "no turns in window");
paintEmpty("chart-model", "no turns in window");
paintEmpty("chart-tools", "no tool calls");
paintEmpty("chart-wake", "no wakes");
paintEmpty("chart-result", "no results");
paintEmpty("chart-result-trend", "no results");
return;
}
renderTurnsChart(s);
renderDurationChart(s);
renderCtxChart(s);
renderCostChart(s);
renderModelChart(s);
renderKeyCount("chart-tools", s.tool_breakdown, "no tool calls");
renderKeyCount("chart-wake", s.wake_mix, "no wakes");
renderKeyCount("chart-result", s.result_mix, "no results");
renderResultTrendChart(s);
}
async function loadStats() {
try {
const resp = await fetch(
"api/stats?window=" + encodeURIComponent(currentWindow),
);
if (!resp.ok) throw new Error("http " + resp.status);
const snap = await resp.json();
render(snap);
} catch (e) {
document.getElementById("summary").textContent =
"stats fetch failed: " + e;
}
}
async function loadIdentity() {
try {
const resp = await fetch("api/state");
if (!resp.ok) return;
const s = await resp.json();
document.title = "stats · " + s.label;
document.getElementById("title").textContent = "◆ " + s.label + " ◆";
const dl = document.getElementById("dashboard-link");
// When accessed via hive-gateway the page lives at `/agent/<name>/`
// on the same origin as the dashboard. Detect via path prefix
// rather than the direct port (which is unreachable or wrong scheme
// behind HTTPS TLS termination). The dashboard SPA is the `/` root.
dl.href = window.location.pathname.startsWith("/agent/")
? window.location.origin + "/"
: "http://" + window.location.hostname + ":" + s.dashboard_port + "/";
} catch (_) {
/* non-fatal */
}
}
document.addEventListener("DOMContentLoaded", () => {
loadIdentity();
// The shared hash-routed tab strip drives the stat-window selector,
// making it deep-linkable (#1h / #24h / …). onShow updates the
// window + reloads; the strip's initial show fires onShow once, so
// there's no separate loadStats() call here (avoids a double fetch).
createTabStrip(document.getElementById("window-tabs"), {
defaultId: currentWindow,
onShow: (w) => {
currentWindow = w;
loadStats();
},
});
});
})();

View file

@ -1,7 +1,7 @@
/* ─── page chrome: back-link header ──────────────────────────────── /* ─── page chrome: back-link header ────────────────────────────────
The minimal sticky "frosted bar" header used by every standalone page of The minimal sticky "frosted bar" header used by every standalone page of
an agent's own UI (/stats, /screen). A back link on the left, then a page an agent's own UI (/screen). A back link on the left, then a page
title and, on /stats, a sub-tab nav. The operator dashboard uses its own title. The operator dashboard uses its own
richer sticky chrome (`.dashboard-chrome`); these classes cover the simple richer sticky chrome (`.dashboard-chrome`); these classes cover the simple
pages. pages.

View file

@ -1,448 +1,14 @@
//! Read-side aggregations over the per-agent `turn_stats.sqlite` for //! The shell-command heads the bash daemon records in the turn-stats database,
//! the agent's `/stats` web page. Owned by the agent (same process //! read for the OTEL bash-command counter.
//! that writes the sink) so per-MCP extensions can register more
//! providers without the host needing to know their schemas.
//!
//! Best-effort: any sqlite error returns an empty snapshot rather than
//! propagating — the stats page is decorative, not authoritative, and
//! a missing db on a brand-new agent shouldn't 500 the route.
use std::collections::{HashMap, HashSet}; use std::path::PathBuf;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use chrono::Utc;
use rusqlite::{Connection, OpenFlags}; use rusqlite::{Connection, OpenFlags};
use serde::Serialize;
use hive_sh4re::inbox::ReminderStats;
/// Window param accepted by `/api/stats?window=`. Each maps to a
/// total span + the bucket width used to roll up trend series.
#[derive(Debug, Clone, Copy)]
pub enum Window {
Hour,
FourHour,
Day,
ThreeDay,
Week,
Month,
/// All available data: the range starts at the earliest recorded turn
/// (`MIN(started_at)`) rather than a fixed lookback, with an adaptive
/// bucket width so the trend series stays bounded at any span.
All,
}
impl Window {
#[must_use]
pub fn parse(s: &str) -> Self {
match s {
"1h" => Self::Hour,
"4h" => Self::FourHour,
"3d" => Self::ThreeDay,
"7d" => Self::Week,
"30d" => Self::Month,
"all" => Self::All,
// Default (incl. `label()`'s own canonical `"24h"`/`"1d"`).
_ => Self::Day,
}
}
fn label(self) -> &'static str {
match self {
Self::Hour => "1h",
Self::FourHour => "4h",
Self::Day => "24h",
Self::ThreeDay => "3d",
Self::Week => "7d",
Self::Month => "30d",
Self::All => "all",
}
}
#[must_use]
pub fn span_secs(self) -> i64 {
match self {
Self::Hour => 3600,
Self::FourHour => 4 * 3600,
Self::Day => 24 * 3600,
Self::ThreeDay => 3 * 24 * 3600,
Self::Week => 7 * 24 * 3600,
Self::Month => 30 * 24 * 3600,
// `All` has no fixed lookback — its range is computed from
// `MIN(started_at)` in `snapshot()`. 0 is only a safe fallback
// (→ `from == now` → empty range) if ever reached generically.
Self::All => 0,
}
}
fn bucket_secs(self) -> i64 {
match self {
// 5-min buckets for 1h (12 buckets), 15-min for 4h (16 buckets),
// hourly for 24h + 3d, daily for 7d + 30d. `All` shares the daily
// fallback, but its real bucket width is chosen adaptively in
// `snapshot()` via `adaptive_bucket_secs` — this arm is only
// reached defensively.
Self::Hour => 300,
Self::FourHour => 900,
Self::Day | Self::ThreeDay => 3600,
Self::Week | Self::Month | Self::All => 24 * 3600,
}
}
}
/// Bucket width for the unbounded `all` window, laddered by the actual
/// span so the trend series stays bounded (≈ ≤100 buckets) at any range:
/// hourly ≤ 2d, daily ≤ 90d, weekly ≤ 2y, ~monthly (30d) beyond.
fn adaptive_bucket_secs(span_secs: i64) -> i64 {
const HOUR: i64 = 3600;
const DAY: i64 = 24 * HOUR;
match span_secs {
s if s <= 2 * DAY => HOUR,
s if s <= 90 * DAY => DAY,
s if s <= 730 * DAY => 7 * DAY,
_ => 30 * DAY,
}
}
#[derive(Debug, Serialize)]
pub struct Snapshot {
pub window: &'static str,
pub bucket_seconds: i64,
/// Total turns in the window.
pub turn_count: u64,
/// Time-bucketed trend series, oldest first. Always covers the
/// full window even for empty buckets (so charts paint a stable
/// x-axis instead of skipping gaps).
pub buckets: Vec<Bucket>,
/// Top tools by call count across the window. Capped to 10.
pub tool_breakdown: Vec<KeyCount>,
/// Top shell commands ("favorite tools") by invocation count across
/// the window, capped to 10. Normalised command heads recorded per
/// bash task into the `bash_commands` table by hive-bash-daemon. Empty
/// until that capture lands (or on any agent that hasn't run a bash
/// task) — the table is created lazily by the writer, so a read
/// before the first insert returns an empty list, not an error.
pub bash_breakdown: Vec<KeyCount>,
pub wake_mix: Vec<KeyCount>,
pub result_mix: Vec<KeyCount>,
/// Distinct models seen in the window, sorted. Each bucket's
/// `model_counts` keys into this set; the stats page uses it as
/// the stacked-bar series list (stable order + colours).
pub models: Vec<String>,
/// Across-window p50 / p95 / avg of `duration_ms`. Same numbers
/// as the per-bucket fields but aggregated over the whole window
/// for the headline summary chips.
pub duration_summary: DurationSummary,
/// Reminder activity stats: counts of scheduled, delivered, and
/// pending reminders over the window (fetched from the broker RPC).
/// None if the RPC call failed or hasn't been integrated yet.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reminder_stats: Option<ReminderStats>,
/// First-turn input tokens of the most recent fresh claude session
/// that started in the window — a proxy for system-prompt + AGENTS.md
/// sprawl (a fresh session's first turn pays the full static prefix
/// uncached, so this is the current "cold context" cost). `None` until
/// the sessions capture (per-session `session_id`) has data; every
/// pre-capture `turn_stats` row has a NULL `session_id` and is excluded.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub first_turn_ctx: Option<u64>,
/// Number of fresh claude sessions (i.e. rows in the `sessions` table)
/// that started within the window. A new session is minted whenever the
/// harness creates a session instead of resuming it by title (manual
/// `/new-session`, auto-compaction fallback, or first-ever turn). `None` when
/// the `sessions` table doesn't exist yet (older db) — same inert-until-data
/// pattern as `first_turn_ctx`.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub session_count: Option<u64>,
}
#[derive(Debug, Serialize)]
pub struct Bucket {
/// Unix timestamp of the bucket start.
pub ts: i64,
pub turn_count: u64,
pub avg_duration_ms: f64,
pub p50_duration_ms: f64,
pub p95_duration_ms: f64,
/// Sums across the bucket. JS picks how to combine them
/// (input + output for cost, etc.) so we don't bake a policy in.
pub input_tokens: u64,
pub output_tokens: u64,
pub cache_read_input_tokens: u64,
pub cache_creation_input_tokens: u64,
/// Mean of `last_input_tokens` across the bucket (the context
/// size at turn-end — useful for spotting drift toward compaction).
pub avg_ctx_tokens: f64,
pub max_ctx_tokens: u64,
/// Turn count per model in this bucket. Model choice greatly
/// affects token cost, so this lets the operator line model usage
/// up against the cost series over time.
pub model_counts: HashMap<String, u64>,
/// Turn count per `result_kind` in this bucket. Lets the stats
/// page chart error / rate-limit / compaction outcomes *over time*
/// (the window-total lives in `Snapshot::result_mix`).
pub result_counts: HashMap<String, u64>,
}
#[derive(Debug, Serialize)]
pub struct KeyCount {
pub key: String,
pub count: u64,
}
// Field names drop the `_ms` unit suffix (satisfies `clippy::struct_field_names`
// once this crate is a bin — pub structs lose the lib API-name exemption), but
// the serialized keys keep `_ms` via `serde(rename)` so the `/api/stats` JSON
// contract the agent web UI reads (`frontend/packages/agent/src/stats.js`) is
// unchanged.
#[derive(Debug, Default, Serialize)]
pub struct DurationSummary {
#[serde(rename = "avg_ms")]
pub avg: f64,
#[serde(rename = "p50_ms")]
pub p50: f64,
#[serde(rename = "p95_ms")]
pub p95: f64,
}
#[must_use]
pub fn snapshot_default(window: Window) -> Snapshot {
let path = default_path();
match snapshot(&path, window) {
Ok(s) => s,
Err(e) => {
tracing::warn!(error = ?e, path = %path.display(), "stats: snapshot failed");
empty_snapshot(window)
}
}
}
fn default_path() -> PathBuf { fn default_path() -> PathBuf {
crate::paths::harness_dir().join("hyperhive-turn-stats.sqlite") crate::paths::harness_dir().join("hyperhive-turn-stats.sqlite")
} }
fn empty_snapshot(window: Window) -> Snapshot {
let now = Utc::now().timestamp();
let from = now - window.span_secs();
let buckets = fill_buckets(from, now, window.bucket_secs(), &HashMap::new());
Snapshot {
window: window.label(),
bucket_seconds: window.bucket_secs(),
turn_count: 0,
buckets,
tool_breakdown: Vec::new(),
bash_breakdown: Vec::new(),
wake_mix: Vec::new(),
result_mix: Vec::new(),
models: Vec::new(),
duration_summary: DurationSummary::default(),
reminder_stats: None,
first_turn_ctx: None,
session_count: None,
}
}
/// Accumulated totals across all rows fetched for one stats window.
/// Built by iterating `turn_stats` rows; passed to `fill_buckets` /
/// `summarize_durations` / `top_n` to produce the final `Snapshot`.
#[derive(Default)]
struct TurnAccum {
by_bucket: HashMap<i64, BucketAcc>,
tool_totals: HashMap<String, u64>,
wake_totals: HashMap<String, u64>,
result_totals: HashMap<String, u64>,
model_set: HashSet<String>,
all_durations: Vec<i64>,
turn_count: u64,
}
impl TurnAccum {
fn push(&mut self, r: Row, bucket_secs: i64) {
self.turn_count += 1;
let bucket_ts = (r.started_at / bucket_secs) * bucket_secs;
let b = self.by_bucket.entry(bucket_ts).or_default();
b.turn_count += 1;
b.durations.push(r.duration_ms.max(0));
b.input_tokens = b.input_tokens.saturating_add(r.input_tokens);
b.output_tokens = b.output_tokens.saturating_add(r.output_tokens);
b.cache_read_input_tokens = b
.cache_read_input_tokens
.saturating_add(r.cache_read_input_tokens);
b.cache_creation_input_tokens = b
.cache_creation_input_tokens
.saturating_add(r.cache_creation_input_tokens);
b.ctx_sum = b.ctx_sum.saturating_add(r.last_input_tokens);
b.ctx_max = b.ctx_max.max(r.last_input_tokens);
*b.model_counts.entry(r.model.clone()).or_insert(0) += 1;
*b.result_counts.entry(r.result_kind.clone()).or_insert(0) += 1;
self.all_durations.push(r.duration_ms.max(0));
*self.wake_totals.entry(r.wake_from).or_insert(0) += 1;
*self.result_totals.entry(r.result_kind).or_insert(0) += 1;
self.model_set.insert(r.model);
if let Some(json) = r.tool_breakdown_json
&& let Ok(map) = serde_json::from_str::<HashMap<String, u64>>(&json)
{
for (k, v) in map {
*self.tool_totals.entry(k).or_insert(0) += v;
}
}
}
}
fn snapshot(path: &Path, window: Window) -> Result<Snapshot> {
// Read-only open so we can't corrupt the db via a query bug.
let conn = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY)
.with_context(|| format!("open {} read-only", path.display()))?;
// turn_stats is rollback-journal (not WAL): a read landing while the
// harness's own sink is mid-INSERT gets SQLITE_BUSY, which propagates up
// and blanks the whole stats page. Wait out the brief write instead.
conn.busy_timeout(std::time::Duration::from_millis(500))
.with_context(|| format!("set busy_timeout on {}", path.display()))?;
let now = Utc::now().timestamp();
// Fixed windows look back a constant span; `all` starts at the earliest
// recorded turn and sizes its buckets adaptively from that span.
let (from, bucket_secs) = match window {
Window::All => {
let min_ts: Option<i64> =
conn.query_row("SELECT MIN(started_at) FROM turn_stats", [], |row| {
row.get(0)
})?;
let from = min_ts.unwrap_or(now);
(from, adaptive_bucket_secs(now - from))
}
_ => (now - window.span_secs(), window.bucket_secs()),
};
let mut stmt = conn.prepare(
"SELECT started_at, duration_ms,
input_tokens, output_tokens,
cache_read_input_tokens, cache_creation_input_tokens,
last_input_tokens,
tool_call_breakdown_json,
wake_from, result_kind, model
FROM turn_stats
WHERE started_at >= ?1
ORDER BY started_at ASC",
)?;
let rows = stmt.query_map([from], |row| {
Ok(Row {
started_at: row.get(0)?,
duration_ms: row.get::<_, i64>(1)?,
input_tokens: u64_from_i64(row.get::<_, i64>(2)?),
output_tokens: u64_from_i64(row.get::<_, i64>(3)?),
cache_read_input_tokens: u64_from_i64(row.get::<_, i64>(4)?),
cache_creation_input_tokens: u64_from_i64(row.get::<_, i64>(5)?),
last_input_tokens: u64_from_i64(row.get::<_, i64>(6)?),
tool_breakdown_json: row.get::<_, Option<String>>(7)?,
wake_from: row.get::<_, String>(8)?,
result_kind: row.get::<_, String>(9)?,
model: row.get::<_, String>(10)?,
})
})?;
let mut acc = TurnAccum::default();
for r in rows {
acc.push(r?, bucket_secs);
}
let buckets = fill_buckets(from, now, bucket_secs, &acc.by_bucket);
let duration_summary = summarize_durations(&mut acc.all_durations);
let mut models: Vec<String> = acc.model_set.into_iter().collect();
models.sort_unstable();
Ok(Snapshot {
window: window.label(),
bucket_seconds: bucket_secs,
turn_count: acc.turn_count,
buckets,
tool_breakdown: top_n(acc.tool_totals, 10),
bash_breakdown: read_bash_breakdown(&conn, from).unwrap_or_default(),
wake_mix: top_n(acc.wake_totals, 20),
result_mix: top_n(acc.result_totals, 20),
models,
duration_summary,
reminder_stats: None, // filled in by api_stats in web_ui.rs via fetch_reminder_stats RPC
// Inert-until-capture: `.ok()` maps both "no sessions in the window
// yet" and "sessions table absent on an older db" to None.
first_turn_ctx: read_first_turn_ctx(&conn, from).ok(),
session_count: read_session_count(&conn, from).ok(),
})
}
/// First-turn input tokens of the most recent fresh claude session that
/// started in `[from, now]`. Tracks system-prompt + AGENTS.md sprawl:
/// the first turn of a fresh session (created, not resumed by title) pays the
/// full static prefix as uncached input, so watching this over time
/// surfaces creep. Uses the agreed per-session derive — the first turn
/// (`ORDER BY started_at LIMIT 1`) of the latest session row.
///
/// Returns `Err` when the `sessions` table doesn't exist (older db) or no
/// fresh session in the window has a recorded turn yet; the caller maps
/// that to `None` (inert-until-capture), same as `read_bash_breakdown`.
fn read_first_turn_ctx(conn: &Connection, from: i64) -> rusqlite::Result<u64> {
conn.query_row(
"SELECT input_tokens FROM turn_stats
WHERE session_id = (
SELECT id FROM sessions WHERE started_at >= ?1 ORDER BY started_at DESC LIMIT 1
)
ORDER BY started_at ASC
LIMIT 1",
[from],
|row| row.get::<_, i64>(0).map(u64_from_i64),
)
}
/// Count of fresh claude sessions that started within `[from, now]`.
///
/// Each row in the `sessions` table represents one fresh `claude --print`
/// invocation that created a session rather than resuming by title: a manual
/// `/new-session`, an auto-compaction fallback, or the very first turn after
/// spawning.
///
/// Returns `Err` when the `sessions` table doesn't exist (older db) so the
/// caller can map to `None` — same inert-until-data pattern as
/// [`read_first_turn_ctx`].
fn read_session_count(conn: &Connection, from: i64) -> rusqlite::Result<u64> {
conn.query_row(
"SELECT COUNT(*) FROM sessions WHERE started_at >= ?1",
[from],
|row| row.get::<_, i64>(0).map(u64_from_i64),
)
}
/// Aggregate the top shell-command heads ("favorite tools") over
/// `[from, now]` from the `bash_commands` table — one row per bash task
/// (`ts INTEGER NOT NULL, head TEXT NOT NULL`), written by hive-bash-daemon.
///
/// Returns `Err` (which the caller maps to an empty list) when the
/// table doesn't exist yet — the writer creates it lazily on first
/// insert, so any agent that hasn't run a bash task since the capture
/// shipped simply has no table. Decoupling it this way means the read
/// side is inert-until-data and needs no schema coordination here.
fn read_bash_breakdown(conn: &Connection, from: i64) -> rusqlite::Result<Vec<KeyCount>> {
let mut stmt = conn.prepare(
"SELECT head, COUNT(*) AS n
FROM bash_commands
WHERE ts >= ?1
GROUP BY head",
)?;
let rows = stmt.query_map([from], |row| {
Ok((
row.get::<_, String>(0)?,
u64_from_i64(row.get::<_, i64>(1)?),
))
})?;
let mut totals: HashMap<String, u64> = HashMap::new();
for r in rows {
let (head, n) = r?;
*totals.entry(head).or_insert(0) += n;
}
Ok(top_n(totals, 10))
}
/// The shell-command heads recorded after `since` (unix seconds, exclusive), /// The shell-command heads recorded after `since` (unix seconds, exclusive),
/// with how often each ran, for the OTEL bash-command counter. Reads the live /// with how often each ran, for the OTEL bash-command counter. Reads the live
/// database read-only; a missing database or `bash_commands` table (no bash /// database read-only; a missing database or `bash_commands` table (no bash
@ -463,352 +29,22 @@ fn read_bash_heads_since(conn: &Connection, since: i64) -> rusqlite::Result<Vec<
let rows = stmt.query_map([since], |row| { let rows = stmt.query_map([since], |row| {
Ok(( Ok((
row.get::<_, String>(0)?, row.get::<_, String>(0)?,
u64_from_i64(row.get::<_, i64>(1)?), u64::try_from(row.get::<_, i64>(1)?).unwrap_or(0),
)) ))
})?; })?;
rows.collect() rows.collect()
} }
struct Row {
started_at: i64,
duration_ms: i64,
input_tokens: u64,
output_tokens: u64,
cache_read_input_tokens: u64,
cache_creation_input_tokens: u64,
last_input_tokens: u64,
tool_breakdown_json: Option<String>,
wake_from: String,
result_kind: String,
model: String,
}
#[derive(Default)]
struct BucketAcc {
turn_count: u64,
durations: Vec<i64>,
input_tokens: u64,
output_tokens: u64,
cache_read_input_tokens: u64,
cache_creation_input_tokens: u64,
ctx_sum: u64,
ctx_max: u64,
model_counts: HashMap<String, u64>,
result_counts: HashMap<String, u64>,
}
fn fill_buckets(
from: i64,
now: i64,
bucket_secs: i64,
by_bucket: &HashMap<i64, BucketAcc>,
) -> Vec<Bucket> {
let start = (from / bucket_secs) * bucket_secs;
let mut out = Vec::new();
let mut ts = start;
while ts <= now {
let bucket = if let Some(acc) = by_bucket.get(&ts) {
let mut sorted = acc.durations.clone();
sorted.sort_unstable();
let avg = if sorted.is_empty() {
0.0
} else {
#[allow(
clippy::cast_precision_loss,
reason = "stat magnitudes (counts, token + duration sums) stay well under f64's 2^53 exact-integer range, so this averaging cast loses no precision in practice"
)]
let sum_f = sorted.iter().sum::<i64>() as f64;
#[allow(
clippy::cast_precision_loss,
reason = "stat magnitudes (counts, token + duration sums) stay well under f64's 2^53 exact-integer range, so this averaging cast loses no precision in practice"
)]
let len_f = sorted.len() as f64;
sum_f / len_f
};
let p50 = percentile(&sorted, 50);
let p95 = percentile(&sorted, 95);
let avg_ctx = if acc.turn_count == 0 {
0.0
} else {
#[allow(
clippy::cast_precision_loss,
reason = "stat magnitudes (counts, token + duration sums) stay well under f64's 2^53 exact-integer range, so this averaging cast loses no precision in practice"
)]
let sum_f = acc.ctx_sum as f64;
#[allow(
clippy::cast_precision_loss,
reason = "stat magnitudes (counts, token + duration sums) stay well under f64's 2^53 exact-integer range, so this averaging cast loses no precision in practice"
)]
let cnt_f = acc.turn_count as f64;
sum_f / cnt_f
};
Bucket {
ts,
turn_count: acc.turn_count,
avg_duration_ms: avg,
p50_duration_ms: p50,
p95_duration_ms: p95,
input_tokens: acc.input_tokens,
output_tokens: acc.output_tokens,
cache_read_input_tokens: acc.cache_read_input_tokens,
cache_creation_input_tokens: acc.cache_creation_input_tokens,
avg_ctx_tokens: avg_ctx,
max_ctx_tokens: acc.ctx_max,
model_counts: acc.model_counts.clone(),
result_counts: acc.result_counts.clone(),
}
} else {
Bucket {
ts,
turn_count: 0,
avg_duration_ms: 0.0,
p50_duration_ms: 0.0,
p95_duration_ms: 0.0,
input_tokens: 0,
output_tokens: 0,
cache_read_input_tokens: 0,
cache_creation_input_tokens: 0,
avg_ctx_tokens: 0.0,
max_ctx_tokens: 0,
model_counts: HashMap::new(),
result_counts: HashMap::new(),
}
};
out.push(bucket);
ts += bucket_secs;
}
out
}
fn summarize_durations(all: &mut [i64]) -> DurationSummary {
if all.is_empty() {
return DurationSummary::default();
}
all.sort_unstable();
#[allow(
clippy::cast_precision_loss,
reason = "stat magnitudes (counts, token + duration sums) stay well under f64's 2^53 exact-integer range, so this averaging cast loses no precision in practice"
)]
let sum_f = all.iter().sum::<i64>() as f64;
#[allow(
clippy::cast_precision_loss,
reason = "stat magnitudes (counts, token + duration sums) stay well under f64's 2^53 exact-integer range, so this averaging cast loses no precision in practice"
)]
let len_f = all.len() as f64;
DurationSummary {
avg: sum_f / len_f,
p50: percentile(all, 50),
p95: percentile(all, 95),
}
}
#[allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
fn percentile(sorted: &[i64], pct: u8) -> f64 {
if sorted.is_empty() {
return 0.0;
}
if sorted.len() == 1 {
return sorted[0] as f64;
}
// Nearest-rank, clamped.
let rank = ((f64::from(pct) / 100.0) * (sorted.len() as f64 - 1.0)).round() as usize;
sorted[rank.min(sorted.len() - 1)] as f64
}
fn top_n(map: HashMap<String, u64>, n: usize) -> Vec<KeyCount> {
let mut v: Vec<KeyCount> = map
.into_iter()
.map(|(key, count)| KeyCount { key, count })
.collect();
v.sort_unstable_by(|a, b| b.count.cmp(&a.count).then_with(|| a.key.cmp(&b.key)));
v.truncate(n);
v
}
fn u64_from_i64(v: i64) -> u64 {
u64::try_from(v).unwrap_or(0)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use rusqlite::params; use rusqlite::params;
use std::sync::atomic::{AtomicU32, Ordering};
static SEQ: AtomicU32 = AtomicU32::new(0);
fn tmp_db() -> PathBuf {
let n = SEQ.fetch_add(1, Ordering::SeqCst);
let pid = std::process::id();
std::env::temp_dir().join(format!("hyperhive-stats-test-{pid}-{n}.sqlite"))
}
fn seed_db(path: &Path, rows: &[(i64, i64, &str, &str, &str, &str)]) {
let conn = Connection::open(path).unwrap();
conn.execute_batch(
"CREATE TABLE turn_stats (
id INTEGER PRIMARY KEY,
started_at INTEGER NOT NULL,
ended_at INTEGER NOT NULL,
duration_ms INTEGER NOT NULL,
model TEXT NOT NULL,
wake_from TEXT NOT NULL,
input_tokens INTEGER NOT NULL DEFAULT 0,
output_tokens INTEGER NOT NULL DEFAULT 0,
cache_read_input_tokens INTEGER NOT NULL DEFAULT 0,
cache_creation_input_tokens INTEGER NOT NULL DEFAULT 0,
last_input_tokens INTEGER NOT NULL DEFAULT 0,
last_output_tokens INTEGER NOT NULL DEFAULT 0,
last_cache_read_input_tokens INTEGER NOT NULL DEFAULT 0,
last_cache_creation_input_tokens INTEGER NOT NULL DEFAULT 0,
tool_call_count INTEGER NOT NULL DEFAULT 0,
tool_call_breakdown_json TEXT,
open_threads_count INTEGER,
open_reminders_count INTEGER,
result_kind TEXT NOT NULL,
note TEXT
);",
)
.unwrap();
for (started, dur, model, wake, result, tools_json) in rows {
conn.execute(
"INSERT INTO turn_stats
(started_at, ended_at, duration_ms, model, wake_from,
last_input_tokens, tool_call_breakdown_json, result_kind)
VALUES (?1, ?2, ?3, ?4, ?5, 1000, ?6, ?7)",
params![
started,
started + dur / 1000,
dur,
model,
wake,
tools_json,
result
],
)
.unwrap();
}
}
#[test]
fn snapshot_aggregates_rows() {
let db = tmp_db();
let _ = std::fs::remove_file(&db);
let now = Utc::now().timestamp();
seed_db(
&db,
&[
(
now - 600,
5_000,
"opus",
"recv",
"ok",
r#"{"Read":2,"Bash":1}"#,
),
(now - 300, 10_000, "opus", "recv", "ok", r#"{"Read":3}"#),
(now - 100, 20_000, "sonnet", "operator", "failed", "{}"),
],
);
let s = snapshot(&db, Window::Day).unwrap();
assert_eq!(s.turn_count, 3);
assert_eq!(s.window, "24h");
assert_eq!(s.bucket_seconds, 3600);
let tool_map: HashMap<_, _> = s
.tool_breakdown
.iter()
.map(|kc| (kc.key.clone(), kc.count))
.collect();
assert_eq!(tool_map.get("Read").copied(), Some(5));
assert_eq!(tool_map.get("Bash").copied(), Some(1));
let wake_map: HashMap<_, _> = s
.wake_mix
.iter()
.map(|kc| (kc.key.clone(), kc.count))
.collect();
assert_eq!(wake_map.get("recv").copied(), Some(2));
assert_eq!(wake_map.get("operator").copied(), Some(1));
let result_map: HashMap<_, _> = s
.result_mix
.iter()
.map(|kc| (kc.key.clone(), kc.count))
.collect();
assert_eq!(result_map.get("ok").copied(), Some(2));
assert_eq!(result_map.get("failed").copied(), Some(1));
// Model breakdown: 2 opus + 1 sonnet, all in the same hour
// bucket given the 24h window.
assert_eq!(s.models, vec!["opus".to_string(), "sonnet".to_string()]);
let mut model_totals: HashMap<String, u64> = HashMap::new();
for b in &s.buckets {
for (k, v) in &b.model_counts {
*model_totals.entry(k.clone()).or_insert(0) += v;
}
}
assert_eq!(model_totals.get("opus").copied(), Some(2));
assert_eq!(model_totals.get("sonnet").copied(), Some(1));
// Durations: [5000, 10000, 20000] → avg ≈ 11666.67, p50 = 10000, p95 ~ 20000
assert!((s.duration_summary.avg - 11_666.666_666_666_666).abs() < 1.0);
assert!((s.duration_summary.p50 - 10_000.0).abs() < 1.0);
assert!((s.duration_summary.p95 - 20_000.0).abs() < 1.0);
}
#[test]
fn empty_window_still_paints_buckets() {
let db = tmp_db();
let _ = std::fs::remove_file(&db);
seed_db(&db, &[]);
let s = snapshot(&db, Window::Day).unwrap();
assert_eq!(s.turn_count, 0);
// 24h / 1h buckets = ~24-25 buckets covering the window.
assert!(s.buckets.len() >= 24);
assert!(s.buckets.iter().all(|b| b.turn_count == 0));
}
#[test]
fn week_uses_daily_buckets() {
let db = tmp_db();
let _ = std::fs::remove_file(&db);
seed_db(&db, &[]);
let s = snapshot(&db, Window::Week).unwrap();
assert_eq!(s.window, "7d");
assert_eq!(s.bucket_seconds, 86_400);
assert!(s.buckets.len() >= 7);
}
/// `bash_breakdown` degrades gracefully when the `bash_commands`
/// table hasn't been created yet (the capture side hasn't shipped /
/// run on this agent). A `seed_db` DB has no such table, so the read
/// must yield an empty list rather than erroring the whole snapshot.
#[test]
fn bash_breakdown_empty_without_table() {
let db = tmp_db();
let _ = std::fs::remove_file(&db);
seed_db(
&db,
&[(
Utc::now().timestamp() - 100,
1000,
"opus",
"recv",
"ok",
"{}",
)],
);
let s = snapshot(&db, Window::Day).unwrap();
assert!(s.bash_breakdown.is_empty());
}
/// The OTEL bash-command counter reads only the heads recorded after its /// The OTEL bash-command counter reads only the heads recorded after its
/// watermark, and a database without the table yields nothing. /// watermark, and a database without the table yields nothing.
#[test] #[test]
fn bash_heads_since_counts_only_newer_rows() { fn bash_heads_since_counts_only_newer_rows() {
let db = tmp_db(); let conn = Connection::open_in_memory().unwrap();
let _ = std::fs::remove_file(&db);
let conn = Connection::open(&db).unwrap();
assert!(read_bash_heads_since(&conn, 0).is_err(), "no table yet"); assert!(read_bash_heads_since(&conn, 0).is_err(), "no table yet");
conn.execute_batch("CREATE TABLE bash_commands (ts INTEGER NOT NULL, head TEXT NOT NULL);") conn.execute_batch("CREATE TABLE bash_commands (ts INTEGER NOT NULL, head TEXT NOT NULL);")
.unwrap(); .unwrap();
@ -824,75 +60,4 @@ mod tests {
assert_eq!(got, vec![("cargo".to_owned(), 2), ("git".to_owned(), 1)]); assert_eq!(got, vec![("cargo".to_owned(), 2), ("git".to_owned(), 1)]);
assert!(read_bash_heads_since(&conn, 30).unwrap().is_empty()); assert!(read_bash_heads_since(&conn, 30).unwrap().is_empty());
} }
/// With a populated `bash_commands` table, `bash_breakdown` rolls up
/// per-head counts (busiest first) and respects the window cutoff.
#[test]
fn bash_breakdown_aggregates_heads() {
let db = tmp_db();
let _ = std::fs::remove_file(&db);
seed_db(&db, &[]);
let now = Utc::now().timestamp();
let conn = Connection::open(&db).unwrap();
conn.execute_batch("CREATE TABLE bash_commands (ts INTEGER NOT NULL, head TEXT NOT NULL);")
.unwrap();
// 3x cargo + 2x git inside the window, 1x rg outside it.
for (ts, head) in [
(now - 100, "cargo"),
(now - 200, "cargo"),
(now - 300, "cargo"),
(now - 400, "git"),
(now - 500, "git"),
(now - (2 * 24 * 3600), "rg"), // older than the 24h window
] {
conn.execute(
"INSERT INTO bash_commands (ts, head) VALUES (?1, ?2)",
params![ts, head],
)
.unwrap();
}
let s = snapshot(&db, Window::Day).unwrap();
let map: HashMap<_, _> = s
.bash_breakdown
.iter()
.map(|kc| (kc.key.clone(), kc.count))
.collect();
assert_eq!(map.get("cargo").copied(), Some(3));
assert_eq!(map.get("git").copied(), Some(2));
// `rg` fell outside the 24h window — excluded.
assert_eq!(map.get("rg").copied(), None);
// top_n orders busiest first.
assert_eq!(
s.bash_breakdown.first().map(|kc| kc.key.as_str()),
Some("cargo")
);
}
/// `GET /api/stats` carries exactly the fields the stats page reads.
#[test]
fn the_stats_snapshot_serialises_exactly_its_read_fields() {
let value = serde_json::to_value(empty_snapshot(Window::Hour)).unwrap();
let mut keys: Vec<&str> = value
.as_object()
.unwrap()
.keys()
.map(String::as_str)
.collect();
keys.sort_unstable();
assert_eq!(
keys,
[
"bash_breakdown",
"bucket_seconds",
"buckets",
"duration_summary",
"models",
"result_mix",
"tool_breakdown",
"turn_count",
"wake_mix",
"window",
]
);
}
} }

View file

@ -1,8 +1,6 @@
//! Per-container HTTP server: the stats and screen pages (`GET /stats`, //! Per-container HTTP server: the screen page (`GET /screen`, `GET /static/*`),
//! `GET /screen`, `GET /static/*`), `GET /` redirecting to `/stats`, the small //! the small JSON the dashboard card and that page read (`/api/state`,
//! JSON the dashboard card and those pages read (`/api/state`, //! `/api/dashboard-state`) and the live event stream `/events/stream`.
//! `/api/dashboard-state`, `/api/stats`) and the live event stream
//! `/events/stream`.
//! //!
//! Handlers are split by concern into the submodules below; this file owns the //! Handlers are split by concern into the submodules below; this file owns the
//! shared [`AppState`], the listener + router wiring in [`serve`], and the //! shared [`AppState`], the listener + router wiring in [`serve`], and the
@ -13,7 +11,6 @@ pub(crate) use auth::force_logout;
mod proxy; mod proxy;
mod screen; mod screen;
mod state; mod state;
mod stats;
mod stream; mod stream;
use std::net::SocketAddr; use std::net::SocketAddr;
@ -23,7 +20,7 @@ use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use axum::{Router, response::Redirect, routing::get}; use axum::{Router, routing::get};
use backon::{ExponentialBuilder, Retryable}; use backon::{ExponentialBuilder, Retryable};
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
@ -47,11 +44,9 @@ struct AppState {
/// The API routes, without the extra proxies and the static fallback. /// The API routes, without the extra proxies and the static fallback.
fn routes() -> Router<AppState> { fn routes() -> Router<AppState> {
Router::new() Router::new()
.route("/", get(|| async { Redirect::temporary("stats") }))
.route("/api/state", get(state::api_state)) .route("/api/state", get(state::api_state))
.route("/api/dashboard-state", get(state::api_dashboard_state)) .route("/api/dashboard-state", get(state::api_dashboard_state))
.route("/events/stream", get(stream::events_stream)) .route("/events/stream", get(stream::events_stream))
.route("/api/stats", get(stats::api_stats))
.route("/screen/ws", get(screen::screen_ws)) .route("/screen/ws", get(screen::screen_ws))
.route("/icon", get(screen::serve_icon)) .route("/icon", get(screen::serve_icon))
} }
@ -92,11 +87,9 @@ pub async fn serve(label: String, port: u16, bus: Bus) -> Result<()> {
// Mount any `services.hyperhive.agent.extraWebProxies` under `/extra/<name>/` before the // Mount any `services.hyperhive.agent.extraWebProxies` under `/extra/<name>/` before the
// static fallback so declared proxies win over `ServeDir`. // static fallback so declared proxies win over `ServeDir`.
let app = proxy::mount_extra_proxies(app) let app = proxy::mount_extra_proxies(app)
// Anything else (`/`, `/stats`, `/screen`, `/static/*`) // Anything else (`/screen`, `/static/*`) falls through to the merged
// falls through to the merged dist. ServeDir auto-appends // dist. ServeDir auto-appends `.html` when the URL is a bare path that
// `.html` when the URL is a bare path that matches a file // matches a file (so `/screen` → `dist/screen.html`). Per-agent `extraFiles` additions are
// (so `/stats` → `dist/stats.html`, `/screen` → `dist/
// screen.html`). Per-agent `extraFiles` additions are
// already layered into this same directory (see // already layered into this same directory (see
// services.hyperhive.agent.frontend.mergedDist in nix). // services.hyperhive.agent.frontend.mergedDist in nix).
.fallback_service(ServeDir::new(&static_dir)) .fallback_service(ServeDir::new(&static_dir))

View file

@ -1,4 +1,4 @@
//! `/api/state` (the agent's name, for the stats and screen pages) and //! `/api/state` (the agent's name, for the screen page) and
//! `/api/dashboard-state` (what the hive dashboard's card reads). //! `/api/dashboard-state` (what the hive dashboard's card reads).
use axum::extract::State; use axum::extract::State;
@ -6,7 +6,7 @@ use serde::Serialize;
use super::AppState; use super::AppState;
/// What the stats and screen pages read of the agent: its name, for the title /// What the screen page reads of the agent: its name, for the title
/// and the screen's login, and the port of the hive dashboard for the way back. /// and the screen's login, and the port of the hive dashboard for the way back.
#[derive(Serialize)] #[derive(Serialize)]
pub(super) struct Identity { pub(super) struct Identity {
@ -67,8 +67,8 @@ struct AgentLink {
#[derive(Serialize, Clone, Copy)] #[derive(Serialize, Clone, Copy)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
enum AgentLinkKind { enum AgentLinkKind {
/// `url` is a path on the agent's container web UI (`/stats`, /// `url` is a path on the agent's container web UI
/// `/screen`). Agent page: same-origin path. Dashboard: /// (`/screen`). Agent page: same-origin path. Dashboard:
/// `http://<host>:<container.port><url>`. /// `http://<host>:<container.port><url>`.
Container, Container,
/// `url` is already a fully-qualified absolute URL — use as-is. /// `url` is already a fully-qualified absolute URL — use as-is.
@ -137,13 +137,6 @@ fn read_own_status() -> (Option<String>, Option<i64>) {
fn agent_links(gui_enabled: bool) -> Vec<AgentLink> { fn agent_links(gui_enabled: bool) -> Vec<AgentLink> {
let mut links = Vec::new(); let mut links = Vec::new();
links.push(AgentLink {
url: "stats.html".to_owned(),
icon: "📊".to_owned(),
label: "stats".to_owned(),
kind: AgentLinkKind::Container,
});
if gui_enabled { if gui_enabled {
links.push(AgentLink { links.push(AgentLink {
url: "screen.html".to_owned(), url: "screen.html".to_owned(),
@ -195,7 +188,7 @@ mod tests {
keys keys
} }
/// `GET /api/state` carries exactly what the stats and screen pages read. /// `GET /api/state` carries exactly what the screen page reads.
#[test] #[test]
fn the_identity_serialises_exactly_its_read_fields() { fn the_identity_serialises_exactly_its_read_fields() {
let identity = super::Identity { let identity = super::Identity {

View file

@ -1,40 +0,0 @@
//! The stats read endpoint.
use axum::extract::State;
use serde::Deserialize;
use super::AppState;
#[derive(Deserialize)]
pub(super) struct StatsQuery {
window: Option<String>,
}
pub(super) async fn api_stats(
State(_state): State<AppState>,
axum::extract::Query(q): axum::extract::Query<StatsQuery>,
) -> axum::Json<crate::stats::Snapshot> {
let window = crate::stats::Window::parse(q.window.as_deref().unwrap_or("24h"));
let mut snapshot = crate::stats::snapshot_default(window);
// Pass the window span so the local reminder rollup filters its counts
// to the same time range as the chart data.
let window_secs = window.span_secs();
let window_secs_u = u64::try_from(window_secs).unwrap_or(0);
snapshot.reminder_stats = fetch_reminder_stats(window_secs_u).await;
axum::Json(snapshot)
}
/// Fetch reminder activity stats from the harness-local reminder store over
/// `HIVE_AGENT_SOCKET` — was a broker RPC before reminders moved
/// in-container. Returns `None` on any transport / decode failure or
/// when the socket is unset — the stats are decorative, not authoritative.
async fn fetch_reminder_stats(window_secs: u64) -> Option<hive_sh4re::inbox::ReminderStats> {
match crate::todo_server::dial(&hive_agent_sock::Request::ReminderRollup {
since_secs: window_secs,
})
.await?
{
hive_agent_sock::Response::ReminderRollup { stats } => Some(stats),
_ => None,
}
}

View file

@ -62,7 +62,7 @@ const PROXY_HEADER_BLOCK: &str = " proxy_http_version 1.1;
/// ///
/// When `frontend_dir` is `Some(path)` each agent gets split location /// When `frontend_dir` is `Some(path)` each agent gets split location
/// blocks: /// blocks:
/// - Exact HTML pages (`/`, `/stats`, `/screen`) → `alias` from the nix /// - Exact HTML pages (`/screen`) → `alias` from the nix
/// store dist. These load even while the agent daemon is restarting. /// store dist. These load even while the agent daemon is restarting.
/// - Static assets (`/static/`) → `alias` from the nix store with cache /// - Static assets (`/static/`) → `alias` from the nix store with cache
/// headers. Served by nginx directly, no agent socket round-trip. /// headers. Served by nginx directly, no agent socket round-trip.
@ -112,7 +112,7 @@ fn render(names: &[String], frontend_dir: Option<&str>) -> String {
// //
// try_files resolution (nginx applies the alias mapping before checking): // try_files resolution (nginx applies the alias mapping before checking):
// $uri — exact file match (/static/app.js → static/app.js) // $uri — exact file match (/static/app.js → static/app.js)
// $uri.html — bare-path .html fallback (/stats → stats.html) // $uri.html — bare-path .html fallback (/screen → screen.html)
// $uri/index.html — directory index (/ → index.html) // $uri/index.html — directory index (/ → index.html)
// @<name>_dynamic — proxy catchall: api, events, icon, login, … // @<name>_dynamic — proxy catchall: api, events, icon, login, …
// //

Some files were not shown because too many files have changed in this diff Show more