dashboard: derive ctx badge thresholds from the model context window

This commit is contained in:
iris 2026-05-21 22:52:32 +02:00 committed by Mara
parent cbd4b71322
commit 4a27ef7304
4 changed files with 122 additions and 26 deletions

View file

@ -4,11 +4,16 @@
(() => {
// ─── constants ──────────────────────────────────────────────────────────
// Context-window token thresholds — mirror the harness compaction watermarks
// in hive-ag3nt (HIVE_COMPACT_WATERMARK_TOKENS = 150k; auto-reset at 100k).
// TODO: source these from model metadata once damocles lands that feature.
const CTX_WARN_TOKENS = 150_000; // ≥ this → compact territory (red)
const CTX_CAUTION_TOKENS = 100_000; // ≥ this → approaching reset (yellow)
// Context-window badge thresholds. Preferred source is each container's
// `context_window_tokens` from /api/state (the real window for the model
// it last ran on) — thresholds are then 75% / 50% of it, matching the
// harness compaction watermarks (compact at 75%, auto-reset at 50%). The
// fixed token constants are the fallback for when that field is absent
// (agent has no turns yet, or no per-model config matched the model).
const CTX_WARN_FRACTION = 0.75; // ≥ this share of the window → red
const CTX_CAUTION_FRACTION = 0.50; // ≥ this share of the window → yellow
const CTX_WARN_TOKENS = 150_000; // fallback red threshold (≈ 75% of 200k)
const CTX_CAUTION_TOKENS = 100_000; // fallback yellow threshold (≈ 50% of 200k)
// ─── helpers ────────────────────────────────────────────────────────────
const $ = (id) => document.getElementById(id);
@ -680,14 +685,20 @@
}
if (c.ctx_tokens != null) {
const k = Math.round(c.ctx_tokens / 1000);
const ctxClass = c.ctx_tokens >= CTX_WARN_TOKENS ? 'badge-ctx-warn'
: c.ctx_tokens >= CTX_CAUTION_TOKENS ? 'badge-ctx-caution'
// Thresholds track the model's real context window when the
// backend supplies it; otherwise fall back to fixed constants.
const win = c.context_window_tokens;
const warn = win != null ? win * CTX_WARN_FRACTION : CTX_WARN_TOKENS;
const caution = win != null ? win * CTX_CAUTION_FRACTION : CTX_CAUTION_TOKENS;
const ctxClass = c.ctx_tokens >= warn ? 'badge-ctx-warn'
: c.ctx_tokens >= caution ? 'badge-ctx-caution'
: 'badge-ctx-ok';
const title = win != null
? `last turn context: ${c.ctx_tokens.toLocaleString()} / ${win.toLocaleString()} `
+ `tokens (${Math.round((c.ctx_tokens / win) * 100)}% of the window)`
: `last turn context size: ${c.ctx_tokens.toLocaleString()} tokens`;
head.append(el('span',
{
class: `badge ${ctxClass}`,
title: `last turn context size: ${c.ctx_tokens.toLocaleString()} tokens`,
},
{ class: `badge ${ctxClass}`, title },
`ctx·${k}k`));
}
body.append(head);