feat(orchestrator): show token and commit rates in the header

rates.py folds the token, commit and score journals into per-hour and per-day rates for the header chips and the rates page; /api/rates drops from about 1950 ms to 380-830 ms on a year of data. Tests run once on unify/skills-20261114 after the merge (operator order 2026-11-14).

🎯 Fix: 75 %
💥 Break: 10 %
💸 Tokens: –
⏱️ Velocity: –
🛡️ Security: 95 %
🏆 Score: 64
🚦 Gate: ⛔ fix 75 % < 90, break 10 % > 5; security 95 % < 100: ask the operator before the commit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-14 21:33:22 +00:00
1 parent a2a03995cb
commit 652bc817c1
28 files changed
+1449 -102

No files matched your search

+5
View File
@@ -415,6 +415,8 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi
{% include "style-resources.css" %}
{% include "style-rates.css" %}
{% include "style-legend-chips.css" %}
{% include "style-inbox.css" %}
@@ -454,6 +456,7 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi
<span class="spacer"></span>
<details class="menu" id="time"></details>
<span class="updated" id="updated" aria-live="polite"></span>
<div class="rates" id="rates"></div>
</div>
<div class="banner" id="mayday" role="alert" hidden></div>
</div>
@@ -611,6 +614,8 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi
{% include "page-resources.js" %}
{% include "page-rates.js" %}
{% include "page-home.js" %}
{% include "page-rules.js" %}
+1
View File
@@ -66,6 +66,7 @@
const updated = document.getElementById("updated");
updated.classList.toggle("offline", ui.offline);
updated.textContent = `${ui.offline ? "No connection · " : ""}${live() ? "Live" : "As of"} ${absolute.format(stampOf(live() ? state.now : state.until))}`;
pollRates();
}
const faceParts = (key) => Faces.of(key).parts.map(([tag, attrs]) => s(tag, attrs));
+159
View File
@@ -0,0 +1,159 @@
const RATES = {poll: 60, fewest: 2, most: 3};
const RATE_PERIODS = [
["minute", "min", "per minute"],
["hour", "h", "per hour"],
["day", "d", "per day"],
["week", "wk", "per week"],
["month", "mo", "per month of 30 days"],
["year", "yr", "per year of 365 days"],
["total", "all", "in total since records begin"],
];
const RATE_METRICS = [
["tasks", "bi-check2-square", "Tasks done", "count"],
["commits", "bi-git", "Commits", "count"],
["krs", "bi-bullseye", "Key results met", "count"],
["tokens", "bi-coin", "Tokens", "count"],
["cpu", "bi-cpu", "CPU", "percent"],
["ram", "bi-memory", "RAM", "percent"],
["vram", "bi-gpu-card", "VRAM", "vram"],
["swap", "bi-arrow-left-right", "Swap", "percent"],
["disk", "bi-hdd", "Disk", "disk"],
];
const BYTE_UNITS = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"];
const rateView = {data: null, dom: null, busy: false, at: -Infinity};
const periodOf = (id) => RATE_PERIODS.find(([name]) => name === id) || RATE_PERIODS[1];
const metricOf = (id) => RATE_METRICS.find(([name]) => name === id);
function bytesText(value) {
const power = value > 0 ? Math.min(Math.floor(Math.log2(value) / 10), BYTE_UNITS.length - 1) : 0;
return `${power ? fixed(value / 2 ** (10 * power)) : Math.round(value)} ${BYTE_UNITS[power]}`;
}
function countText(value) {
if (value >= 1000) {
return compactCount(value);
}
if (value >= 10 || Number.isInteger(value)) {
return String(Math.round(value));
}
return value < 0.1 ? String(Number(value.toPrecision(1))) : fixed(value);
}
/**
* @param entry - one metric of one period from /api/rates, or null before the first answer.
* @param kind - the metric's kind: count, percent, vram or disk.
* @param short - the period's short name a count is per, "" for none.
* @returns the cell text: ≈ before an extrapolated count, n/a for a gauge without a sample.
*/
function rateText(entry, kind, short) {
if (!entry) {
return "…";
}
if (entry.value === null) {
return "n/a";
}
if (kind === "count") {
return `${entry.approx ? "≈ " : ""}${countText(entry.value)}${short ? `/${short}` : ""}`;
}
return kind === "vram" && entry.used !== null ? `${percentOf(entry.value)} · ${bytesText(entry.used)}` : percentOf(entry.value);
}
function rateHint(entry, metric, period) {
const [id, , label, kind] = metric;
const [, , per] = period;
if (!entry) {
return `${label} ${per}: loading`;
}
if (entry.value === null) {
return `${label} ${per}: n/a, no sample in this window`;
}
const source = entry.source === "total" ? "all records of this kind" : `the last ${entry.source}`;
const scaled = entry.approx ? `; ≈ none in this window or its records begin later, scaled from ${source}` : "";
const where = id === "disk" ? ` on ${entry.mount}, ${bytesText(entry.used)} of ${bytesText(entry.total)}` : "";
const mean = kind === "count" ? "" : " on average";
return `${label} ${per}: ${rateText(entry, kind, "")}${mean}${where}${scaled}`;
}
function ratesBar() {
const host = document.getElementById("rates");
const period = h("select", {class: "field rates-period", ...named("Rate period", "Rate period: the window the chip counts over"), on: {change: (event) => chooseRates({...settings.rates, period: event.target.value})}},
RATE_PERIODS.map(([id, short, per]) => h("option", {value: id, title: per}, short)));
const chip = h("summary", {class: "rates-chip"});
const body = h("tbody", {});
const head = h("tr", {}, h("th", {scope: "col"}, "Metric"), h("th", {scope: "col", title: "Shown in the chip"}, "Chip"), RATE_PERIODS.map(([id, short, per]) => h("th", {scope: "col", "data-period": id, title: per}, short)));
const box = h("div", {class: "menu-box end rates-box", role: "group", "aria-label": "Rates per period"},
h("h3", {}, "Rates per period"),
h("div", {class: "rates-scroll"}, h("table", {class: "rates-table"}, h("thead", {}, head), body)),
h("p", {class: "muted"}, `≈ means an estimate: none in the window, so the shortest longer window that holds any, scaled down, or a window reaching back before the records of its kind begin, scaled from those records. n/a means no sample in the window. Total counts since records begin; the gauges average over the window. The chip shows ${RATES.fewest} to ${RATES.most} metrics.`));
const menu = h("details", {class: "menu rates-menu"}, chip, box);
host.replaceChildren(period, menu);
rateView.dom = {period, chip, body, menu};
}
function rateCell(metric, period, chosen) {
const entry = rateView.data ? rateView.data.rates[period[0]][metric[0]] : null;
const short = period[0] === "total" ? "" : period[1];
return h("td", {"data-period": period[0], "data-approx": String(Boolean(entry && entry.approx)), "aria-current": period[0] === chosen.period ? "true" : null, title: rateHint(entry, metric, period)},
rateText(entry, metric[3], metric[3] === "count" ? short : ""));
}
function rateRow(metric, chosen) {
const [id, name, label] = metric;
const shown = chosen.metrics.includes(id);
const locked = shown ? chosen.metrics.length <= RATES.fewest : chosen.metrics.length >= RATES.most;
const pick = h("input", {type: "checkbox", checked: shown, disabled: locked, "aria-label": `${label} in the chip`, on: {change: () => chooseRates({...chosen, metrics: shown ? chosen.metrics.filter((other) => other !== id) : [...chosen.metrics, id]})}});
return h("tr", {"data-metric": id}, h("th", {scope: "row"}, icon(name), ` ${label}`), h("td", {}, pick), RATE_PERIODS.map((period) => rateCell(metric, period, chosen)));
}
function drawRates() {
const dom = rateView.dom;
if (!dom) {
return;
}
const chosen = settings.rates;
const period = periodOf(chosen.period);
const short = period[0] === "total" ? "" : period[1];
const entries = chosen.metrics.map((id) => [metricOf(id), rateView.data ? rateView.data.rates[period[0]][id] : null]);
dom.period.value = period[0];
dom.chip.replaceChildren(...entries.map(([metric, entry]) => h("span", {class: "rate", "data-metric": metric[0], "data-approx": String(Boolean(entry && entry.approx)), title: rateHint(entry, metric, period)},
icon(metric[1]), h("span", {class: "rate-value"}, rateText(entry, metric[3], metric[3] === "count" ? short : "")))), caret());
dom.chip.setAttribute("aria-label", `Rates ${period[2]}: ${entries.map(([metric, entry]) => `${metric[2]} ${rateText(entry, metric[3], "")}`).join(", ")}`);
dom.chip.title = "Rates: every metric per period";
const focused = document.activeElement instanceof Element ? document.activeElement.closest("#rates tr[data-metric]") : null;
dom.body.replaceChildren(...RATE_METRICS.map((metric) => rateRow(metric, chosen)));
const again = focused ? dom.body.querySelector(`tr[data-metric="${focused.dataset.metric}"] input`) : null;
if (again) {
again.focus();
}
}
async function chooseRates(next) {
if (!(await saveSettings({...settings, rates: next}))) {
drawRates();
}
}
async function fetchRates() {
rateView.busy = true;
try {
const response = await fetch("/api/rates", {cache: "no-store"});
if (response.ok) {
rateView.data = await response.json();
}
} catch {
ui.offline = true;
}
rateView.at = nowSeconds();
rateView.busy = false;
drawRates();
}
function pollRates() {
if (!rateView.dom) {
ratesBar();
drawRates();
}
if (!document.hidden && !rateView.busy && nowSeconds() - rateView.at >= RATES.poll) {
fetchRates();
}
}
+1 -1
View File
@@ -9,7 +9,7 @@
const RESOURCE_BURN = {done: "Done", scope: "Scope"};
const resourceView = {data: null, dom: null, busy: false, key: "", at: 0, timer: 0};
const resourceKey = () => JSON.stringify([ui.preset, ui.from, ui.to, ui.asOf]);
const compactCount = (value) => (value >= 1e6 ? `${fixed(value / 1e6)}M` : value >= 1e3 ? `${fixed(value / 1e3)}k` : String(Math.round(value)));
const compactCount = Resources.compact;
const resourceLabel = (line) => (line.kind === "vram" ? `${line.name} VRAM` : line.name);
const percentOf = (value) => `${value} %`;
+2
View File
@@ -14,6 +14,7 @@
syncRadio();
musicButton();
syncBubbles();
drawRates();
}
async function saveSettings(next) {
@@ -153,6 +154,7 @@
estimation: settings.estimation,
trend: settings.trend,
bubbles: settings.bubbles,
rates: settings.rates,
};
if (await saveSettings(next)) {
closeSheet();
+13 -1
View File
@@ -8,6 +8,8 @@ const Resources = (() => {
const HOUR = 3600;
const COUNTS = ["input", "output", "cache_read", "cache_write"];
const ZONES = [{kind: "idle", from: 0, to: IDLE}, {kind: "saturated", from: SATURATED, to: 100}];
const COUNT_UNITS = [[1e12, "T"], [1e9, "B"], [1e6, "M"], [1e3, "k"]];
const ROUNDS_UP = 0.99995;
const finite = (value) => typeof value === "number" && Number.isFinite(value);
const share = (part, whole) => (finite(part) && finite(whole) && whole > 0 ? Math.round((1000 * part) / whole) / 10 : null);
const listOf = (row, key) => (Array.isArray(row[key]) ? row[key].filter((item) => item && typeof item === "object") : []);
@@ -178,7 +180,17 @@ const Resources = (() => {
return {top, done: points.map((point) => [point.t, percent(point.done)]), scope: points.map((point) => [point.t, percent(point.scope)])};
}
return {IDLE, SATURATED, CALM, GAP, HOUR, COUNTS, ZONES, lines, hardware, segments, spans, waits, asleep, unknown, stack, accumulate, burnup};
/**
* @param value - a count.
* @returns the count with one decimal in the largest of k, M, B and T it reaches once rounded to that decimal,
* else as a whole number.
*/
function compact(value) {
const unit = COUNT_UNITS.find(([size]) => value >= size * ROUNDS_UP);
return unit ? `${(value / unit[0]).toFixed(1)}${unit[1]}` : String(Math.round(value));
}
return {IDLE, SATURATED, CALM, GAP, HOUR, COUNTS, ZONES, lines, hardware, segments, spans, waits, asleep, unknown, stack, accumulate, burnup, compact};
})();
if (typeof module === "object" && module.exports) {
module.exports = Resources;
+20
View File
@@ -0,0 +1,20 @@
.rates { display: inline-flex; align-items: center; gap: 4px; min-width: 0; max-width: 100%; }
.rates-period { padding: 3px 6px; border-radius: 999px; font-size: var(--fs-xs); }
.rates-menu > summary.rates-chip { gap: 10px; padding: 3px 10px; border-radius: 999px; font-size: var(--fs-sm); font-weight: 560; font-variant-numeric: tabular-nums; white-space: nowrap; }
.rates-chip .rate { display: inline-flex; align-items: center; gap: 4px; }
.rates-chip .bi { color: var(--muted); }
.rates-chip .rate[data-approx="true"] .rate-value, .rates-table td[data-approx="true"] { color: var(--muted); }
.rates-box { max-width: min(1100px, calc(100vw - 32px)); }
.rates-scroll { max-width: 100%; overflow-x: auto; }
.rates-box > p { width: 0; min-width: 100%; margin: 0; }
.rates-table { border-collapse: collapse; font-size: var(--fs-sm); font-variant-numeric: tabular-nums; }
.rates-table th, .rates-table td { padding: 3px 8px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
.rates-table thead th { color: var(--muted); font-size: var(--fs-xs); font-weight: 650; }
.rates-table th[scope="row"], .rates-table thead th:first-child { position: sticky; left: 0; z-index: 1; background: var(--panel); font-weight: 560; text-align: left; }
.rates-table td[aria-current="true"] { background: var(--accent-soft); color: var(--accent); }
@media (min-width: 641px) {
.rates-box { width: max-content; }
}
@media (max-width: 640px) {
.rates-menu > summary.rates-chip { gap: 6px; padding: 3px 8px; }
}