From 9eed814d3ccf872a9b515c4e2b79338c8c9cbd49 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Mon, 16 Nov 2026 12:11:32 -0600 Subject: [PATCH] feat(webui): triage cap, the host holds the route's over-cap answer until its time (at most two days ahead, ISO only) and refuses without asking again; the read shows overCapUntil and a plain cap message (#1873, lane triage-host) --- packages/webui/src/triage-host.mjs | 20 +++++++++++++++- packages/webui/src/triage-http.mjs | 3 +++ packages/webui/tests/triage-host.test.mjs | 28 +++++++++++++++++++++-- packages/webui/tests/triage-http.test.mjs | 21 ++++++++++++++--- 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/packages/webui/src/triage-host.mjs b/packages/webui/src/triage-host.mjs index c36956371..2f3edfff0 100644 --- a/packages/webui/src/triage-host.mjs +++ b/packages/webui/src/triage-host.mjs @@ -40,6 +40,9 @@ export const RELAY_REFUSED = "triage-refused"; export const MESSAGE_MAX = 4000; export const TRANSCRIPT_KEEP = 200; export const QUEUE_MAX = 20; +// An over-cap answer is held for at most this long; a later time is not taken. +export const CAP_HOLD_MAX_MS = 48 * 3600 * 1000; +const ISO_TIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z$/; const LINE_MAX = 64 * 1024; const CAP = /^[0-9a-f]{64}$/; @@ -188,6 +191,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async let starting = null; let lastExit = null; let cap = null; + let overCapUntil = null; // the route's over-cap answer, held until its time passes const queue = []; const ensureDirs = () => { @@ -311,13 +315,26 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async }); }; + // The cap is the route's to count. Once it answers over the cap with a time in the next two days, + // the host refuses without asking again until that time, so a later answer cannot reopen the day. + const capUntil = () => { + if (overCapUntil !== null && Date.parse(overCapUntil) <= now().getTime()) overCapUntil = null; + return overCapUntil; + }; const admit = async () => { if (!route || typeof route.admit !== "function") return { ok: false, code: "triage-no-route" }; + const held = capUntil(); + if (held !== null) return { ok: false, code: "triage-over-cap", until: held }; let a; try { a = await route.admit(); } catch { return { ok: false, code: "triage-route-unreadable" }; } if (a?.ok === true) return a; const code = ["triage-no-route", "triage-over-cap"].includes(a?.code) ? a.code : "triage-route-refused"; - return { ok: false, code, ...(typeof a?.until === "string" ? { until: a.until } : {}) }; + if (code !== "triage-over-cap") return { ok: false, code }; + const until = typeof a.until === "string" && ISO_TIME.test(a.until) ? Date.parse(a.until) : NaN; + const ahead = until - now().getTime(); + if (!(ahead > 0 && ahead <= CAP_HOLD_MAX_MS)) return { ok: false, code }; + overCapUntil = new Date(until).toISOString(); + return { ok: false, code, until: overCapUntil }; }; return { @@ -328,6 +345,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async async state() { return { route: this.routeSet() ? "set" : "not set", + overCapUntil: capUntil(), running: child !== null, lastExit, snapshot: last, diff --git a/packages/webui/src/triage-http.mjs b/packages/webui/src/triage-http.mjs index 19ebc67eb..b43c72076 100644 --- a/packages/webui/src/triage-http.mjs +++ b/packages/webui/src/triage-http.mjs @@ -9,6 +9,7 @@ export const TRIAGE_PATHS = Object.freeze(["/api/triage", "/api/triage/message"] export const BODY_MAX = 16384; // A read refreshes the snapshot when the one it holds is older than this. export const SNAPSHOT_MAX_AGE_MS = 30000; +export const capMessage = until => `Triage is over its daily token cap. It takes messages again at ${until}.`; export const NO_ROUTE_MESSAGE = "No model route is set for Triage, so Triage will not start. The instance owner sets the route first."; const STATUS = Object.freeze({ @@ -66,6 +67,8 @@ export async function triageView(triage, { send, now = Date.now } = {}) { return { route: s.route, ...(s.route === "set" ? {} : { routeMessage: NO_ROUTE_MESSAGE }), + overCapUntil: s.overCapUntil ?? null, + ...(s.overCapUntil ? { capMessage: capMessage(s.overCapUntil) } : {}), running: s.running, lastExit: s.lastExit, snapshot: s.snapshot, diff --git a/packages/webui/tests/triage-host.test.mjs b/packages/webui/tests/triage-host.test.mjs index 6b61207b3..f938faa14 100644 --- a/packages/webui/tests/triage-host.test.mjs +++ b/packages/webui/tests/triage-host.test.mjs @@ -105,12 +105,36 @@ test("no model route: nothing launches, the message is refused, and the state sa }); test("over the cap: the message is refused until the next day and nothing launches", async (t) => { - const route = { admit: async () => ({ ok: false, code: "triage-over-cap", until: "2026-11-17T00:00:00.000Z" }) }; - const { h, dataRoot } = host(t, scratch(), { route }); + let clock = Date.parse("2026-11-16T15:00:00.000Z"), asked = 0, answer = { ok: false, code: "triage-over-cap", until: "2026-11-17T00:00:00.000Z" }; + const route = { admit: async () => { asked++; return answer; } }; + const { h, dataRoot } = host(t, scratch(), { route, now: () => new Date(clock) }); assert.equal((await h.state()).route, "set"); + assert.equal((await h.state()).overCapUntil, null); await assert.rejects(h.message("hello"), (e) => e.code === "triage-over-cap" && e.until === "2026-11-17T00:00:00.000Z"); assert.equal(h.running, false); assert.equal(existsSync(join(dataRoot, "triage", "agent", "session.json")), false); + assert.equal((await h.state()).overCapUntil, "2026-11-17T00:00:00.000Z"); + // Held: a later route answer cannot reopen the day; the route is not asked again before the time. + answer = { ok: false, code: "triage-no-route" }; + clock = Date.parse("2026-11-16T23:59:59.000Z"); + await assert.rejects(h.message("hello"), (e) => e.code === "triage-over-cap" && e.until === "2026-11-17T00:00:00.000Z"); + assert.equal(asked, 1); + // At the time, the hold ends and the route is asked again. + clock = Date.parse("2026-11-17T00:00:00.000Z"); + assert.equal((await h.state()).overCapUntil, null); + await assert.rejects(h.message("hello"), (e) => e.code === "triage-no-route"); + assert.equal(asked, 2); + assert.deepEqual((await h.state()).transcript, [], "a refused message is never written"); + // An over-cap answer whose time is not usable (past, too far, not an ISO time) refuses without a hold. + for (const until of ["2026-11-17T00:00:00.000Z", "2026-11-20T00:00:00.000Z", "Tue, 17 Nov 2026 12:00:00 GMT", "tomorrow", 5]) { + let n = 0; + const r = { admit: async () => { n++; return { ok: false, code: "triage-over-cap", until }; } }; + const later = host(t, scratch(), { route: r, now: () => new Date("2026-11-17T01:00:00.000Z") }).h; + await assert.rejects(later.message("a"), (e) => e.code === "triage-over-cap" && e.until === undefined, String(until)); + await assert.rejects(later.message("b"), (e) => e.code === "triage-over-cap"); + assert.equal(n, 2, `${until}: not held`); + assert.equal((await later.state()).overCapUntil, null); + } const odd = { admit: async () => ({ ok: false, code: "rm -rf", until: 5 }) }; const other = host(t, scratch(), { route: odd }).h; await assert.rejects(other.message("hello"), (e) => e.code === "triage-route-refused" && e.until === undefined); diff --git a/packages/webui/tests/triage-http.test.mjs b/packages/webui/tests/triage-http.test.mjs index 54b66ba37..afe9e9302 100644 --- a/packages/webui/tests/triage-http.test.mjs +++ b/packages/webui/tests/triage-http.test.mjs @@ -10,7 +10,7 @@ import { startServer } from '../src/serve.mjs'; import { createDevOwner } from '../src/dev-owner.mjs'; import { consoleAccess } from '../src/access.mjs'; import { createSsoSession } from './helpers/sso-session.mjs'; -import { NO_ROUTE_MESSAGE, triageView, triageHTTP, SNAPSHOT_MAX_AGE_MS } from '../src/triage-http.mjs'; +import { NO_ROUTE_MESSAGE, capMessage, triageView, triageHTTP, SNAPSHOT_MAX_AGE_MS } from '../src/triage-http.mjs'; import { createTriageHost, TriageError } from '../src/triage-host.mjs'; const LEAK = '/home/owner/.config/mosaic-dev/secret-token-0123456789'; @@ -56,7 +56,7 @@ test('the dev local owner reads the drawer state and sends a message; the config const f = await devServer(t, { triage }); const read = await send(f.port, '/api/triage', { headers: { host: f.headers.host } }); assert.equal(read.status, 200); - assert.deepEqual(read.json, { route: 'set', running: false, lastExit: null, snapshot: { snapshotVersion: 1 }, transcript: [{ at: 't', from: 'owner', id: 'owner-1', text: 'hi' }], send: true }); + assert.deepEqual(read.json, { route: 'set', overCapUntil: null, running: false, lastExit: null, snapshot: { snapshotVersion: 1 }, transcript: [{ at: 't', from: 'owner', id: 'owner-1', text: 'hi' }], send: true }); const sent = await send(f.port, '/api/triage/message', { method: 'POST', headers: f.headers, body: { text: 'why is alpha down?' } }); assert.equal(sent.status, 202); assert.deepEqual(sent.json, { id: 'owner-abc', at: '2026-11-16T00:00:00.000Z' }); @@ -169,12 +169,27 @@ test('the read refreshes a missing or stale snapshot and keeps a fresh one', asy const calls = []; const triage = { snapshotAt: () => at, refresh: async () => { calls.push(clock); at = clock; }, state: async () => ({ route: 'not set', running: false, lastExit: null, snapshot: null, transcript: [] }) }; const view = await triageView(triage, { send: true, now: () => clock }); - assert.deepEqual(view, { route: 'not set', routeMessage: NO_ROUTE_MESSAGE, running: false, lastExit: null, snapshot: null, transcript: [], send: true }); + assert.deepEqual(view, { route: 'not set', routeMessage: NO_ROUTE_MESSAGE, overCapUntil: null, running: false, lastExit: null, snapshot: null, transcript: [], send: true }); clock += SNAPSHOT_MAX_AGE_MS - 1; await triageView(triage, { now: () => clock }); clock += 2; await triageView(triage, { now: () => clock }); assert.deepEqual(calls, [1_000_000, 1_000_000 + SNAPSHOT_MAX_AGE_MS + 1]); }); +test('over the cap: the read names the time in plain words and the write answers 429 with it', async t => { + const dataRoot = join(mkdtempSync(join(process.env.TMPDIR, 'th-')), 'data'); + const route = { admit: async () => ({ ok: false, code: 'triage-over-cap', until: '2026-11-17T00:00:00.000Z' }) }; + const host = createTriageHost({ dataRoot, route, readSnapshot: async () => null, now: () => new Date('2026-11-16T20:00:00.000Z'), bwrap: '/nonexistent/bwrap' }); + const f = await devServer(t, { triage: host }); + const before = await send(f.port, '/api/triage', { headers: { host: f.headers.host } }); + assert.deepEqual([before.json.route, before.json.overCapUntil, Object.hasOwn(before.json, 'capMessage')], ['set', null, false]); + const sent = await send(f.port, '/api/triage/message', { method: 'POST', headers: f.headers, body: { text: 'hello' } }); + assert.deepEqual([sent.status, sent.json.error, sent.json.until], [429, 'triage-over-cap', '2026-11-17T00:00:00.000Z']); + const after = await send(f.port, '/api/triage', { headers: { host: f.headers.host } }); + assert.equal(after.json.overCapUntil, '2026-11-17T00:00:00.000Z'); + assert.equal(after.json.capMessage, capMessage('2026-11-17T00:00:00.000Z')); + assert.equal(host.running, false); +}); + test('with the real host and no route: the read says so in plain words, the write refuses, nothing launches or is written', async t => { const dataRoot = join(mkdtempSync(join(process.env.TMPDIR, 'th-')), 'data'); const host = createTriageHost({ dataRoot, readSnapshot: async () => ({ snapshotVersion: 1, at: '2026-11-16T00:00:00.000Z' }), bwrap: '/nonexistent/bwrap' });