Files
AgentHub/server/lib/loop-delay.js
T
nntrivi2001 1c6dfea5b5 feat(health): System Health in three groups, a score that sees lag; admin user detail
Dashboard → System Health is grouped into AgentHub, Machine and Agent
activity. The machine (GET /api/host, every signed-in user) is drawn by
components/health/HostHealth.tsx: host card beside the CPU/memory history,
then CPU (threads as compact cells + temperatures/GPUs), disks & network,
busiest processes, and per-user tiles. Concurrency bars each show their own
agent type on hover.

The Health Score (client/src/lib/healthScore.ts) is now 100 minus what each
check costs, between an amber and a red threshold: event-loop p99
(server/lib/loop-delay.js, shared with /api/metrics), Linux PSI pressure for
CPU, memory and disk I/O (/api/host `pressure`; load per core without PSI),
RAM, the database's disk, V8 heap, hooks silent while agents work, and agent
errors in the last 24 h. All-time success rate, API errors and the transcript
cache hit rate no longer count. /api/settings/info adds server.event_loop and
activity {last_event_at, working_agents, agents_24h}.

Administration: a user row opens its detail in place (GET
/api/users/:id/details — spend, account pool share, live panes, workspaces,
newest sessions); the server panel is gone, member tmux folded into the table;
the Sessions list shows each row's owner in server mode.
2026-10-27 16:39:38 +07:00

37 lines
1.2 KiB
JavaScript

/**
* @file loop-delay.js
* @description How long this server's event loop was held up — what an
* operator sees as a laggy dashboard or terminal: every request, hook and
* keystroke echo waits for it. One `monitorEventLoopDelay` histogram for the
* whole process, sampled every 20 ms and reset each minute, so `loopDelay()`
* describes the last full minute (the running one until the first minute
* ends). Read by `GET /api/metrics` (Prometheus gauges) and
* `GET /api/settings/info` (the System Health score).
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { monitorEventLoopDelay } = require("perf_hooks");
const WINDOW_MS = 60_000;
const histogram = monitorEventLoopDelay({ resolution: 20 });
histogram.enable();
const read = () => ({
p50: histogram.percentile(50),
p99: histogram.percentile(99),
max: histogram.max,
});
let lastWindow = null; // the last full minute, reported until the next one ends
setInterval(() => {
lastWindow = read();
histogram.reset();
}, WINDOW_MS).unref();
/** `{p50, p99, max}` in nanoseconds over the last minute. */
function loopDelay() {
return lastWindow || read();
}
module.exports = { loopDelay, WINDOW_MS };