From 17b972b92de6766a4b546d15e874d7763cc9b2f2 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Wed, 18 Nov 2026 13:26:31 -0600 Subject: [PATCH] feat(triage): the host holds the daily token cap Each turn's usage goes to /triage/usage.jsonl, one line per usage.end, 0600 and owned by the running user; a link, wrong mode or owner, or a non-file refuses. The host holds the stored cap through daily-quota mayLaunch and report before each start and each queued message, lets a crossing turn finish, drains the queue with a host line, sums the file again after a restart (a torn line counts as an unknown turn), and GET /api/triage adds usageToday, cap and resetsAt. With no route stored the fields say "not set". cli.mjs keeps route null. (#1873, lane triage-cap) --- packages/webui/src/public/display.js | 21 +- packages/webui/src/public/shell.css | 1 + packages/webui/src/triage-host.mjs | 172 +++++- packages/webui/src/triage-http.mjs | 3 + packages/webui/src/triage-usage.mjs | 220 ++++++++ .../webui/tests/fixtures/triage-adapter.mjs | 15 +- .../tests/triage-drawer-browser.test.mjs | 32 +- packages/webui/tests/triage-host.test.mjs | 502 +++++++++++++++++- packages/webui/tests/triage-http.test.mjs | 19 +- packages/webui/tests/triage-usage.test.mjs | 205 +++++++ 10 files changed, 1174 insertions(+), 16 deletions(-) create mode 100644 packages/webui/src/triage-usage.mjs create mode 100644 packages/webui/tests/triage-usage.test.mjs diff --git a/packages/webui/src/public/display.js b/packages/webui/src/public/display.js index c6acace7f..fced404b5 100644 --- a/packages/webui/src/public/display.js +++ b/packages/webui/src/public/display.js @@ -43,6 +43,7 @@ window.MosaicLocalTime = value => Number.isFinite(Date.parse(value)) ? new Date( x.addEventListener('click', close); hd.append(title, x); const state = el('p', 'triage-state'); state.id = 'triage-state'; state.setAttribute('role', 'status'); + const usage = el('p', 'note triage-usage'); usage.id = 'triage-usage'; usage.hidden = true; const healthSec = el('details', 'triage-health-sec'); healthSec.open = true; const healthSum = el('summary', '', 'Health'); const health = el('ul', 'triage-health'); health.id = 'triage-health'; @@ -51,10 +52,10 @@ window.MosaicLocalTime = value => Number.isFinite(Date.parse(value)) ? new Date( const transcript = el('ol', 'triage-transcript'); transcript.id = 'triage-transcript'; transcript.tabIndex = 0; transcript.setAttribute('aria-label', 'Triage conversation'); const compose = el('div', 'triage-compose'); - drawer.append(hd, state, healthSec, convo, transcript, compose); + drawer.append(hd, state, usage, healthSec, convo, transcript, compose); drawer.addEventListener('keydown', e => { if (e.key === 'Escape') { e.preventDefault(); close(); } }); document.body.append(drawer); - parts = { title, state, health, transcript, compose, box: null, send: null, status: null, off: null }; + parts = { title, state, usage, health, transcript, compose, box: null, send: null, status: null, off: null }; } // The message box is built once and never rewritten or removed by a read, so a draft survives every @@ -109,6 +110,19 @@ window.MosaicLocalTime = value => Number.isFinite(Date.parse(value)) ? new Date( }; const notRead = v => v === 'not read' || v === null || v === undefined || typeof v !== 'object'; + // #1873 triage-cap: one line, today's count against the stored cap and the next reset. Only what the + // read holds: a field the read says is not set or not read is said so, never filled in. + const usageLine = view => { + if (!Object.hasOwn(view, 'usageToday')) return ''; + const u = view.usageToday, c = view.cap; + if (u === 'not set' || c === 'not set') return 'Daily token cap: not set.'; + if (notRead(u) || notRead(c)) return "Today's token usage: not read."; + const n = v => (Number.isSafeInteger(v) ? v.toLocaleString() : 'not read'); + const reset = typeof view.resetsAt === 'string' && Number.isFinite(Date.parse(view.resetsAt)) ? ` Resets at ${when(view.resetsAt)}.` : ''; + const k = Number.isSafeInteger(u.unknownTurns) && u.unknownTurns > 0 ? u.unknownTurns : 0; + const unknown = k ? ` ${k} ${k === 1 ? 'turn has' : 'turns have'} no usage reading.` : ''; + return `Today: ${n(u.count)} of ${n(c.limit)} tokens.${reset}${unknown}`; + }; const configWords = c => (notRead(c) ? 'not read' : c.valid === true ? 'valid' : `not valid${c.code ? ` (${clean(c.code)})` : ''}`); function healthLines(s) { if (notRead(s)) return ['Health: not read']; @@ -173,6 +187,9 @@ window.MosaicLocalTime = value => Number.isFinite(Date.parse(value)) ? new Date( const t = parts.transcript, atBottom = t.scrollHeight - t.scrollTop - t.clientHeight < 24, top = t.scrollTop; parts.state.textContent = stateLine(view); parts.state.dataset.kind = view.route !== 'set' ? 'off' : view.overCapUntil ? 'cap' : view.running === true ? 'running' : 'stopped'; + const usage = usageLine(view); + parts.usage.textContent = usage; + parts.usage.hidden = !usage; const lines = healthLines(view.snapshot); const healthKey = JSON.stringify(lines); if (healthKey !== lastHealth) { lastHealth = healthKey; parts.health.replaceChildren(...lines.map(l => el('li', l.endsWith('not read') ? 'not-read' : '', l))); } diff --git a/packages/webui/src/public/shell.css b/packages/webui/src/public/shell.css index 16301a877..daaec1d47 100644 --- a/packages/webui/src/public/shell.css +++ b/packages/webui/src/public/shell.css @@ -832,6 +832,7 @@ html[data-organizations] .ws-drawer-acts .note{flex-basis:100%} #triage-drawer h2:focus{outline:none} #triage-drawer .x{margin-left:auto;flex:none;width:32px;height:32px;border:1px solid var(--line);border-radius:6px;background:var(--canvas);color:var(--strong);font-size:20px;line-height:1;cursor:pointer} #triage-drawer .triage-state{margin:0;padding:10px 14px;flex:none;font-size:14px;font-weight:600;color:var(--strong);border-bottom:1px solid var(--line);overflow-wrap:anywhere} +#triage-drawer .triage-usage{margin:0;padding:6px 14px;flex:none;font-size:13px;color:var(--muted);border-bottom:1px solid var(--line);overflow-wrap:anywhere} #triage-drawer .triage-state[data-kind=off],#triage-drawer .triage-state[data-kind=cap]{color:var(--warning);background:color-mix(in srgb,var(--warning) 9%,var(--surface))} #triage-drawer .triage-state[data-kind=running]::before{content:"";display:inline-block;width:8px;height:8px;margin-right:7px;border-radius:50%;background:var(--success);vertical-align:1px} #triage-drawer .triage-health-sec{flex:none;padding:8px 14px;border-bottom:1px solid var(--line)} diff --git a/packages/webui/src/triage-host.mjs b/packages/webui/src/triage-host.mjs index 830193547..c17519980 100644 --- a/packages/webui/src/triage-host.mjs +++ b/packages/webui/src/triage-host.mjs @@ -32,6 +32,17 @@ // directory and HOME (HOME not counted against the stack's own code), here and again in // hostedBinds. A refused answer reaches no spawn, answers triage-route-invalid, and writes // ROUTE_REFUSED to the transcript; a refusal at the binds also removes what the start wrote. +// +// #1873 triage-cap: the relay counts each turn. usage.begin takes a baseline of the harness's +// transcript with the transcript usage-source the bus uses; usage.end takes the delta (the bus's +// own rules, triage-usage.mjs) and appends one line to /triage/usage.jsonl. When a cap is +// stored (triage-route-store.mjs), the daily-quota plugin decides over the lines in that file: before +// a start (admit), before the runner takes queued messages (message.receive) and before each +// message's turn (the runner's state read outside a turn). Over the cap, or with the store or the +// file unreadable, the turn in flight finishes, every message still queued gets a host line and +// leaves the queue, and the runner is told its session is gone. Nothing is held in memory: each +// check reads the file again, so a restart counts what the file holds. With no cap stored, nothing +// is refused for usage and the lines are still written. import { spawn } from "node:child_process"; import { randomBytes, timingSafeEqual } from "node:crypto"; @@ -48,6 +59,9 @@ import { builtinTools } from "../../harness/src/bundle.mjs"; import { FOUNDER_ENV } from "../../harness/src/runner.mjs"; import { extractCards, plainText } from "./triage-cards.mjs"; import { SNAPSHOT_FILE, writeSnapshot } from "./triage-snapshot.mjs"; +import { readRoute } from "./triage-route-store.mjs"; +import { ID as USAGE_ID, appendUsage, capState, delta, nextReset, readUsage, snapshot, usagePath } from "./triage-usage.mjs"; +import { readTurn } from "../../plugins/usage-source/transcript/index.mjs"; export const RUNNER = RUNNER_FILES[0]; export const RELAY_REFUSED = "triage-refused"; @@ -58,6 +72,8 @@ export const UNANSWERED = "Triage stopped before it answered this message. Send export const NOT_STARTED = "Triage did not start, so this message was not delivered."; export const NOT_TAKEN = "Triage stopped before it read this message. Send it again."; export const ROUTE_REFUSED = "Triage did not start: the model route's answer did not pass the host's checks, so this message was not delivered."; +export const overCapNotTaken = (until) => `Triage reached its daily token cap before it took this message. Send it again after ${until}.`; +export const USAGE_NOT_READ = "Triage could not read its token usage or its cap, so it did not take this message."; // 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$/; @@ -179,7 +195,10 @@ function sameCap(a, b) { // The relay server. `onSend({ body, in_reply_to, result })` keeps a human-bound send, `result` true // only for class RESULT, the class the runner gives its answer to a turn; `take()` returns the // queued owner messages; `read(id)` drops one that was answered. -export function createRelay({ socket, cap, take, read, onSend, log = () => {} }) { +// #1873 triage-cap: `usage(phase, turn)` (a promise) counts the turn; the relay answers usage.begin +// and usage.end only once it settles. `stopped(verb)` is asked before state and message.receive; +// true answers "not-holder", which ends the runner's loop after the turn in flight. +export function createRelay({ socket, cap, take, read, onSend, usage = null, stopped = () => false, log = () => {} }) { const answer = (conn, value) => { try { conn.end(JSON.stringify(value) + "\n"); } catch {} }; const refuse = (conn) => answer(conn, { ok: false, error: RELAY_REFUSED, reason: "the Triage host answers only a message to the person" }); const server = createServer((conn) => { @@ -200,6 +219,13 @@ export function createRelay({ socket, cap, take, read, onSend, log = () => {} }) const args = req.args && typeof req.args === "object" && !Array.isArray(req.args) ? req.args : {}; const verb = req.verb; try { + if (usage && (verb === "usage.begin" || verb === "usage.end")) { + return void Promise.resolve() + .then(() => usage(verb === "usage.begin" ? "begin" : "end", args.turn)) + .catch((e) => log(`triage relay: ${verb} not counted: ${e?.code ?? "error"}`)) + .then(() => answer(conn, { ok: true, result: {} })); + } + if ((verb === "state" || verb === "message.receive") && stopped(verb)) return answer(conn, { ok: false, error: "not-holder" }); if (Object.hasOwn(LIFECYCLE, verb)) return answer(conn, { ok: true, result: LIFECYCLE[verb]() }); if (verb === "message.receive") return answer(conn, { ok: true, result: take() }); if (verb === "message.read") { read(args.id); return answer(conn, { ok: true, result: {} }); } @@ -235,11 +261,13 @@ export function createRelay({ socket, cap, take, read, onSend, log = () => {} }) // harnessModule async (name) -> the harness module (envNames, installRoots) or null; the // repository's packages/plugins/harness/ by default // home HOME, which no mount may reach outside the stack's own code; the Console user's by default -export function createTriageHost({ dataRoot, route = null, readSnapshot = async () => null, env = process.env, node = process.execPath, bwrap = BWRAP, now = () => new Date(), devOwner = false, log = () => {}, roleDir = join(REPO, "roles"), readOnly = [], pollInterval = null, proxyPort = null, tokenPattern = null, harnessModule = repoHarnessModule, home = homedir() }) { +// usageSource { readTurn({ harness, root }) } the transcript usage-source; the repository's own by default +export function createTriageHost({ dataRoot, route = null, readSnapshot = async () => null, env = process.env, node = process.execPath, bwrap = BWRAP, now = () => new Date(), devOwner = false, log = () => {}, roleDir = join(REPO, "roles"), readOnly = [], pollInterval = null, proxyPort = null, tokenPattern = null, harnessModule = repoHarnessModule, home = homedir(), usageSource = { readTurn } }) { if (typeof dataRoot !== "string" || !dataRoot.startsWith("/")) throw new TriageError("triage-no-data-root", "no data root"); if (proxyPort !== null && !(Number.isInteger(proxyPort) && proxyPort > 0 && proxyPort <= 65535)) throw new TypeError("proxyPort must be a port number or null"); if (tokenPattern !== null && !(tokenPattern instanceof RegExp)) throw new TypeError("tokenPattern must be a RegExp or null"); if (typeof harnessModule !== "function") throw new TypeError("harnessModule must be a function"); + if (typeof usageSource?.readTurn !== "function") throw new TypeError("usageSource must have readTurn"); const tokenShape = tokenPattern && new RegExp(`^(?:${tokenPattern.source})$`, tokenPattern.flags.replace(/[gmy]/g, "")); // What no mount source may lie in or around, besides the run directory and the fixed list. HOME // does not refuse the stack's own code when the stack runs from under it (a copy or a worktree). @@ -257,6 +285,10 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async let cap = null; let overCapUntil = null; // the route's over-cap answer, held until its time passes const queue = []; + let usage = null; // this run's counting: { run, root, harness, baselines, ended, inTurn } + let usageChain = Promise.resolve(); // usage calls and exit records, one at a time + let usageLost = false; // a turn's line could not be written: the count is short, so a stored cap refuses + const badSeen = new Set(); const ensureDirs = () => { mkdirSync(dataRoot, { recursive: true, mode: 0o700 }); @@ -320,14 +352,16 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async writeFileSync(bundle.prompt, hostPrompt(contract, { snapshotPath: join(workspace, "health", SNAPSHOT_FILE), steps }), { mode: 0o600, flag: "wx" }); writeFileSync(bundle.policy, JSON.stringify(policy, null, 2) + "\n", { mode: 0o600, flag: "wx" }); writeFileSync(bundle.tools, JSON.stringify(tools, null, 2) + "\n", { mode: 0o600, flag: "wx" }); + const run = `triage-${now().toISOString().replace(/[^0-9]/g, "").slice(0, 14)}`; const session = { bundle, adapter: admitted.adapter, harness: admitted.harness, harnessEnv: admitted.harnessEnv, turnEnd: admitted.turnEnd, workspace, sessionDir: join(a, "session"), toolSocket: join(a, "tools.sock"), turnMarker: join(a, "turn.done"), - brokerSocket: join(a, "relay.sock"), business: "system", instance: "triage", run: `triage-${now().toISOString().replace(/[^0-9]/g, "").slice(0, 14)}`, + brokerSocket: join(a, "relay.sock"), business: "system", instance: "triage", run, model: admitted.model, provider: admitted.provider ?? "", ...(Number.isInteger(pollInterval) && pollInterval >= 100 ? { pollInterval } : {}), }; writeFileSync(join(a, "session.json"), JSON.stringify(session, null, 2) + "\n", { mode: 0o600, flag: "wx" }); + return run; }; // The route's answer, checked. It returns a copy taken once, so a getter or a later change to @@ -363,13 +397,114 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async if (e?.code === "hosted-binds-refused") refuse(e.fence); throw e; } - return { admitted: a, installRoots }; + // Where the harness keeps its session files, as the bus binds it (the module's transcript()). + // Only a path inside the run directory is read; anything else reads as no transcript. + let transcriptRoot = null; + try { + const r = typeof mod.transcript === "function" ? mod.transcript({ runDir: paths.agent }) : null; + if (typeof r === "string" && r.startsWith(paths.agent + "/") && !r.split("/").includes("..")) transcriptRoot = r; + } catch { transcriptRoot = null; } + return { admitted: a, installRoots, transcriptRoot }; + }; + + // #1873 triage-cap. A baseline or a reading of the transcript, with the bus's codes. + const sample = async (u) => { + if (u.root === null) return { status: "unknown", code: "no-transcript" }; + try { return snapshot(await usageSource.readTurn({ harness: u.harness, root: u.root })); } + catch { return { status: "unknown", code: "source-failed" }; } + }; + // A turn id the runner names, or "unnamed". The first begin for a turn wins, as on the bus. + const turnKey = (turn) => (typeof turn === "string" && USAGE_ID.test(turn) ? turn : null); + const usageBegin = (u, turn) => { + const named = turnKey(turn); + if (!named) log("triage host: usage.begin without a usable turn id"); + const k = named ?? "unnamed"; + u.inTurn = true; + if (!u.baselines.has(k)) u.baselines.set(k, sample(u)); + }; + // One line per turn: the delta since its baseline, known or unknown. + const finishTurn = async (u, k) => { + const base = u.baselines.has(k) ? await u.baselines.get(k) : null; + u.baselines.delete(k); + const result = delta(base, await sample(u)); + const known = result.status === "known"; + if (!known) log(`triage host: turn usage unknown: ${result.code}`); + const line = { at: now().toISOString(), run: u.run, turn: k, status: result.status, + input: known ? result.input : null, output: known ? result.output : null, cacheRead: known ? result.cacheRead : null, cacheWrite: known ? result.cacheWrite : null }; + try { + appendUsage(usagePath(dataRoot), line); + } catch (e) { + usageLost = true; + log(`triage host: usage line not written: ${e?.code ?? "error"}`); + } + }; + const usageEnd = async (u, turn) => { + u.inTurn = false; + const named = turnKey(turn); + if (!named) log("triage host: usage.end without a usable turn id"); + if (named && u.ended.has(named)) { log("triage host: usage.end repeated for a turn; not counted twice"); return; } + if (named) u.ended.add(named); + await finishTurn(u, named ?? "unnamed"); + }; + const chainUsage = (fn) => (usageChain = usageChain.then(fn).catch((e) => log(`triage host: usage failed: ${e?.code ?? "error"}`))); + + // The stored cap's state, read from the store and the usage file each time. + // { state: "not set" } no cap stored + // { state: "route-unreadable" } | { state: "usage-unreadable", cap, resetsAt? } refused + // { state: "under" | "over", cap, usageToday, resetsAt } + const capNow = () => { + let stored; + try { stored = readRoute(dataRoot); } catch { return { state: "route-unreadable" }; } + if (!stored) return { state: "not set" }; + const at = now().getTime(); + let resetsAt; + try { resetsAt = new Date(nextReset(stored.cap, at)).toISOString(); } catch { return { state: "usage-unreadable", cap: stored.cap }; } + if (usageLost) return { state: "usage-unreadable", cap: stored.cap, resetsAt }; + let read; + try { read = readUsage(usagePath(dataRoot)); } catch { return { state: "usage-unreadable", cap: stored.cap, resetsAt }; } + for (const b of read.bad) { + const key = `${read.ino}:${b.line}`; + if (badSeen.has(key)) continue; + badSeen.add(key); + log(`triage host: usage.jsonl line ${b.line} is not a usage line; it counts as an unknown turn`); + } + try { + const s = capState({ cap: stored.cap, records: read.records, now: at }); + return { state: s.allow ? "under" : "over", cap: stored.cap, usageToday: s.usageToday, resetsAt: s.resetsAt }; + } catch { return { state: "usage-unreadable", cap: stored.cap, resetsAt }; } + }; + const unsafeRoot = () => { + let st; + try { st = lstatSync(paths.root); } catch { return false; } + return st.isSymbolicLink() || !st.isDirectory() || (typeof process.getuid === "function" && st.uid !== process.getuid()); + }; + const refusedByCap = (c) => c.state === "over" || c.state === "usage-unreadable" || c.state === "route-unreadable"; + // Before the runner takes messages or starts a message's turn: over the cap, every message still + // queued gets a host line and leaves the queue, and the runner's loop ends. That holds with nothing + // queued too: an idle session over the cap ends at its next receive, and the next message is + // refused by admit. A state read inside a turn (a continuation) is not stopped: the turn finishes. + const capStop = (verb) => { + const u = usage; + if (verb === "state" && u?.inTurn) return false; + const c = capNow(); + if (!refusedByCap(c)) return false; + log(`triage host: stopping the session at ${verb}: ${c.state === "over" ? "over the daily token cap" : "usage or cap not read"}`); + // A message whose turn began and was never answered says so; one never begun says why not. + const begun = (id) => Boolean(u && (u.baselines.has(id) || u.ended.has(id))); + for (const m of queue.splice(0)) { + const text = begun(m.id) ? UNANSWERED : c.state === "over" ? overCapNotTaken(c.resetsAt) : USAGE_NOT_READ; + try { append({ at: now().toISOString(), from: "host", in_reply_to: m.id, text }); } + catch { log("triage host: transcript write failed"); } + } + return true; }; const start = async (answer) => { - const { admitted, installRoots } = await checkAdmitted(answer); + const { admitted, installRoots, transcriptRoot } = await checkAdmitted(answer); ensureDirs(); - writeBundle(admitted); + const run = writeBundle(admitted); + const u = { run, root: transcriptRoot, harness: admitted.harness, baselines: new Map(), ended: new Set(), inTurn: false }; + usage = u; cap = randomBytes(32).toString("hex"); relay = await createRelay({ socket: join(paths.agent, "relay.sock"), cap, @@ -393,6 +528,8 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async const i = in_reply_to && result === true ? queue.findIndex((m) => m.id === in_reply_to) : -1; if (i >= 0) queue.splice(i, 1); }, + usage: (phase, turn) => chainUsage(() => (phase === "begin" ? usageBegin(u, turn) : usageEnd(u, turn))), + stopped: capStop, log, }); let list; @@ -427,6 +564,9 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async if (child !== mine) return; lastExit = { code: Number.isInteger(code) ? code : null, signal: signal ?? null, at: now().toISOString() }; child = null; + // A turn the session began and never ended (killed, crashed) still spent tokens: it gets its line. + // The open turns are read when this runs, after any usage.end already in the chain. + chainUsage(async () => { for (const k of [...u.baselines.keys()]) await finishTurn(u, k); }); // Every message still queued leaves it, so none holds a slot with no session to answer it, and // the transcript says which: one the session took and never answered, or one it never read. for (const m of queue.splice(0)) { @@ -456,6 +596,15 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = 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 }; + // A turn still being counted (a session that just ended mid-turn, or a usage.end in flight) is in + // the file before the count is read. + await usageChain; + const c = capNow(); + // The store sits in the Triage directory: one that is a link or not the Console user's is the + // unsafe directory start would refuse, not an unreadable route. + if (c.state === "route-unreadable") return { ok: false, code: unsafeRoot() ? "triage-dir-unsafe" : "triage-route-unreadable" }; + if (c.state === "usage-unreadable") return { ok: false, code: "triage-usage-unreadable" }; + if (c.state === "over") return { ok: false, code: "triage-over-cap", until: c.resetsAt }; let a; try { a = await route.admit(); } catch { return { ok: false, code: "triage-route-unreadable" }; } if (a?.ok === true) return a; @@ -474,9 +623,15 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async get pid() { return child?.pid ?? null; }, routeSet: () => Boolean(route && typeof route.admit === "function"), async state() { + const c = capNow(); + const unset = c.state === "not set"; return { route: this.routeSet() ? "set" : "not set", - overCapUntil: capUntil(), + overCapUntil: c.state === "over" ? c.resetsAt : capUntil(), + // #1873 triage-cap: today's count against the stored cap. Names and numbers only. + usageToday: unset ? "not set" : c.usageToday ?? "not read", + cap: unset ? "not set" : c.cap ? { limit: c.cap.limit, reset: c.cap.reset, timeZone: c.cap.timeZone } : "not read", + resetsAt: unset ? "not set" : c.resetsAt ?? "not read", running: child !== null, lastExit, snapshot: last, @@ -493,7 +648,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async if (text.length > MESSAGE_MAX) throw new TriageError("triage-message-too-long", `a message holds at most ${MESSAGE_MAX} characters`); if (queue.length >= QUEUE_MAX) throw new TriageError("triage-busy", "Triage has too many messages waiting"); const admitted = await admit(); - if (!admitted.ok) throw Object.assign(new TriageError(admitted.code, admitted.code === "triage-over-cap" ? "Triage is over its daily cap" : admitted.code === "triage-no-route" ? "no model route is set for Triage" : "the model route refused"), admitted.until ? { until: admitted.until } : {}); + if (!admitted.ok) throw Object.assign(new TriageError(admitted.code, admitted.code === "triage-over-cap" ? "Triage is over its daily cap" : admitted.code === "triage-no-route" ? "no model route is set for Triage" : admitted.code === "triage-usage-unreadable" ? "Triage's token usage could not be read" : admitted.code === "triage-route-unreadable" ? "the model route could not be read" : admitted.code === "triage-dir-unsafe" ? "a Triage directory is not a real directory" : "the model route refused"), admitted.until ? { until: admitted.until } : {}); await refresh(); const at = now().toISOString(); const id = `owner-${randomBytes(6).toString("hex")}`; @@ -525,6 +680,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async } relay?.close(); relay = null; + await usageChain; }, }; } diff --git a/packages/webui/src/triage-http.mjs b/packages/webui/src/triage-http.mjs index b43c72076..7f3bbd9a5 100644 --- a/packages/webui/src/triage-http.mjs +++ b/packages/webui/src/triage-http.mjs @@ -31,6 +31,7 @@ const MESSAGES = Object.freeze({ "triage-route-refused": "the model route refused to start Triage", "triage-route-invalid": "the model route's answer is not usable", "triage-route-unreadable": "the model route could not be read", + "triage-usage-unreadable": "Triage's token usage could not be read, so it takes no message", "triage-over-cap": "Triage is over its daily token cap and starts again after the time shown", "triage-busy": "Triage has too many messages waiting", "triage-dir-unsafe": "a Triage directory is not safe to use", @@ -69,6 +70,8 @@ export async function triageView(triage, { send, now = Date.now } = {}) { ...(s.route === "set" ? {} : { routeMessage: NO_ROUTE_MESSAGE }), overCapUntil: s.overCapUntil ?? null, ...(s.overCapUntil ? { capMessage: capMessage(s.overCapUntil) } : {}), + // #1873 triage-cap: today's count, the stored cap and the next reset, or "not set" / "not read". + ...(Object.hasOwn(s, "usageToday") ? { usageToday: s.usageToday, cap: s.cap, resetsAt: s.resetsAt } : {}), running: s.running, lastExit: s.lastExit, snapshot: s.snapshot, diff --git a/packages/webui/src/triage-usage.mjs b/packages/webui/src/triage-usage.mjs new file mode 100644 index 000000000..df66ea65a --- /dev/null +++ b/packages/webui/src/triage-usage.mjs @@ -0,0 +1,220 @@ +// #1873 triage-cap: Triage's token usage and its daily cap. +// +// The Triage host appends one line per turn to /triage/usage.jsonl: +// { at, run, turn, status, input, output, cacheRead, cacheWrite } +// A known turn carries four non-negative safe integers; an unknown turn carries four nulls. The file +// sits outside the run directory, so a new start never clears it. It is held to the route store's +// file checks: a regular file, one link, the running user's, mode 0600. A link, a non-file, a wrong +// mode or owner refuses the read and the append alike, and the append leaves the file as it was. +// Lines are only ever appended. Nothing here prunes the file. +// +// The cap is the daily-quota plugin's own rule (packages/plugins/limit-check/daily-quota), called +// with the organization limits shape and the stored cap as its one leaf, for one account named +// "triage". No counting rule lives here: the count, the window, the DST handling and the hit all +// come from mayLaunch and report. nextReset only asks report when the window turns over. +import { closeSync, constants, fchmodSync, fstatSync, fsyncSync, openSync, readSync, writeSync } from "node:fs"; +import { join } from "node:path"; +import { mayLaunch, report } from "../../plugins/limit-check/daily-quota/index.mjs"; + +export const USAGE_ACCOUNT = "triage"; +export const USAGE_FIELDS = Object.freeze(["at", "run", "turn", "status", "input", "output", "cacheRead", "cacheWrite"]); +const KINDS = ["input", "output", "cacheWrite", "cacheRead"]; +const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/; +// A run id is the host's own triage-<14 digits>; a turn id is the runner's message id, maybe with +// ":retry". Both are held to one short, plain shape so a line never carries free text. +export const ID = /^[A-Za-z0-9:._-]{1,80}$/; +// 32 MiB holds well over a hundred thousand lines; a larger file refuses rather than stalls a read. +export const USAGE_MAX_BYTES = 32 * 1024 * 1024; +const HOUR = 3600000; + +export class UsageError extends Error { + constructor(why) { + super(`the Triage usage file is unreadable: ${why}`); + this.name = "UsageError"; + this.code = "triage-usage-unreadable"; + } +} +const unreadable = (why) => new UsageError(why); + +export const usagePath = (dataRoot) => join(dataRoot, "triage", "usage.jsonl"); + +const plain = (v) => v !== null && typeof v === "object" && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype; +const canonicalTime = (s) => typeof s === "string" && ISO.test(s) && !Number.isNaN(Date.parse(s)) && new Date(s).toISOString() === s; +const count = (n) => Number.isSafeInteger(n) && n >= 0; + +// One line's shape, exactly. +export function validLine(v) { + if (!plain(v) || Object.keys(v).length !== USAGE_FIELDS.length || !USAGE_FIELDS.every((k) => Object.hasOwn(v, k))) return false; + if (!canonicalTime(v.at) || typeof v.run !== "string" || !ID.test(v.run) || typeof v.turn !== "string" || !ID.test(v.turn)) return false; + if (v.status === "known") return KINDS.every((k) => count(v[k])); + if (v.status === "unknown") return KINDS.every((k) => v[k] === null); + return false; +} + +// An opened file's checks, as the route store holds route.json to. +function checkFile(fd, uid) { + const st = fstatSync(fd); + if (!st.isFile()) throw unreadable("usage.jsonl is not a regular file"); + if (st.nlink !== 1) throw unreadable("usage.jsonl has more than one link"); + if (st.uid !== uid) throw unreadable("usage.jsonl is not owned by this user"); + if ((st.mode & 0o777) !== 0o600) throw unreadable("usage.jsonl mode is not 0600"); + return st; +} + +const uidOf = (opts) => opts.uid ?? process.getuid(); + +// Append one line. A torn last line (a write that died) is closed off with a newline first, so the +// new line always stands alone and the torn one stays one bad line. +export function appendUsage(file, line, opts = {}) { + if (!validLine(line)) throw unreadable("the line does not match the usage shape"); + const text = JSON.stringify(Object.fromEntries(USAGE_FIELDS.map((k) => [k, line[k]]))) + "\n"; + const flags = constants.O_RDWR | constants.O_APPEND | constants.O_NOFOLLOW | constants.O_NONBLOCK; + let fd; + try { fd = openSync(file, flags); } + catch (e) { + if (e.code === "ELOOP") throw unreadable("usage.jsonl is a link"); + if (e.code !== "ENOENT") throw unreadable("usage.jsonl cannot be opened"); + try { fd = openSync(file, flags | constants.O_CREAT | constants.O_EXCL, 0o600); } + catch { throw unreadable("usage.jsonl cannot be made"); } + // The umask may have narrowed the mode; set it on the descriptor just made. + try { fchmodSync(fd, 0o600); } catch { closeSync(fd); throw unreadable("usage.jsonl cannot be made"); } + } + try { + const st = checkFile(fd, uidOf(opts)); + let lead = ""; + if (st.size > 0) { + const last = Buffer.alloc(1); + if (readSync(fd, last, 0, 1, st.size - 1) !== 1) throw unreadable("usage.jsonl cannot be read"); + if (last[0] !== 0x0a) lead = "\n"; + } + try { + writeSync(fd, lead + text); + fsyncSync(fd); + } catch { throw unreadable("usage.jsonl cannot be written"); } + } finally { closeSync(fd); } +} + +const nulls = { input: null, output: null, cacheRead: null, cacheWrite: null }; + +// Every line as a daily-quota record for the triage account. A line that does not parse or does +// not match the shape (a torn last line, an edit by hand) is refused: it becomes one unknown turn, +// keyed by its line number, dated by the next good line (or the file's change time when none +// follows), and is listed in `bad` so the host can log it. No file reads as no turns. +export function readUsage(file, opts = {}) { + const maxBytes = opts.maxBytes ?? USAGE_MAX_BYTES; + let fd; + try { fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); } + catch (e) { + if (e.code === "ENOENT") return { records: [], bad: [] }; + if (e.code === "ELOOP") throw unreadable("usage.jsonl is a link"); + throw unreadable("usage.jsonl cannot be opened"); + } + let st; + let buf; + try { + st = checkFile(fd, uidOf(opts)); + if (st.size > maxBytes) throw unreadable("usage.jsonl is too large"); + buf = Buffer.alloc(st.size); + let got = 0; + while (got < st.size) { + const n = readSync(fd, buf, got, st.size - got, got); + if (n === 0) break; + got += n; + } + buf = buf.subarray(0, got); + } finally { closeSync(fd); } + const lines = buf.toString("utf8").split("\n"); + if (lines.at(-1) === "") lines.pop(); + const parsed = lines.map((text) => { + try { + const v = JSON.parse(text); + return validLine(v) ? v : null; + } catch { return null; } + }); + const records = new Array(parsed.length); + const bad = []; + let nextAt = st.mtime.toISOString(); + for (let i = parsed.length - 1; i >= 0; i--) { + const v = parsed[i]; + if (v) { + records[i] = { account: USAGE_ACCOUNT, ...v }; + nextAt = v.at; + } else { + records[i] = { account: USAGE_ACCOUNT, at: nextAt, run: null, turn: `line-${i + 1}`, status: "unknown", ...nulls }; + bad.unshift({ line: i + 1 }); + } + } + return { records, bad, ino: st.ino }; +} + +// The organization limits shape with the stored cap as its one leaf. +export const limitsFor = (cap) => ({ tokens: { daily: { [USAGE_ACCOUNT]: cap } } }); +const SUBJECT = Object.freeze({ account: USAGE_ACCOUNT }); +const iso = (ms) => new Date(ms).toISOString(); + +// The next reset after nowMs, or after the cap's `since` when that is later. A probe record at that +// instant counts while the window holding t still starts at or before it; the reset is the first +// millisecond at which report stops counting it. A day is at most 25 hours, so 49 hours always +// holds the next one. +export function nextReset(cap, nowMs) { + const from = Math.max(nowMs, Date.parse(cap.since)); + const limits = limitsFor(cap); + const probe = [{ account: USAGE_ACCOUNT, at: iso(from), run: "probe", turn: "probe", status: "known", input: 1, output: 0, cacheRead: 0, cacheWrite: 0 }]; + const counts = (t) => report({ limits, records: probe, now: iso(t) })[0].count > 0; + let lo = from; + let hi = from + 49 * HOUR; + if (counts(hi)) throw unreadable("the cap has no reset in the next two days"); + while (hi - lo > 1) { + const mid = Math.floor((lo + hi) / 2); + if (counts(mid)) lo = mid; else hi = mid; + } + return hi; +} + +// Today's count, the decision and the next reset for the stored cap. +export function capState({ cap, records, now }) { + const at = iso(now); + const limits = limitsFor(cap); + const decision = mayLaunch({ limits, subject: SUBJECT, records, now: at }); + const [r] = report({ limits, records, now: at }); + return { + allow: decision.allow, + hit: decision.allow ? null : decision.hit, + usageToday: { count: r.count, ...r.kinds, unknownTurns: r.unknownTurns }, + resetsAt: iso(nextReset(cap, now)), + }; +} + +// The turn delta, copied from packages/bus/src/usage.mjs (snapshot, delta; not exported there and +// that package is not this lane's to change). Same codes, same rules: the turn's usage is the sum +// of the transcript entries that are new since the turn began; a changed or missing earlier entry +// is "transcript-rewritten", and no new entry is "no-new-usage". +const unknown = (code) => ({ status: "unknown", code }); +export function snapshot(value) { + if (value?.status === "unknown") return unknown(/^transcript-(path|size|line|entries|records|changed|parse|usage|harness)$/.test(value.code) ? value.code : "source-unknown"); + if (value?.status !== "known" || !value.entries || typeof value.entries !== "object" || Array.isArray(value.entries)) return unknown("source-invalid"); + const entries = Object.entries(value.entries); + if (entries.length > 20000) return unknown("source-invalid"); + for (const [id, v] of entries) { + if (id.length > 512 || !v || Object.keys(v).length !== 4 || KINDS.some((k) => !Number.isSafeInteger(v[k]) || v[k] < 0)) return unknown("source-invalid"); + } + return { status: "known", entries: value.entries }; +} +export function delta(before, after) { + if (!before || before.status !== "known") return unknown("baseline-unknown"); + if (after.status !== "known") return after; + for (const [id, v] of Object.entries(before.entries)) { + if (!Object.hasOwn(after.entries, id) || KINDS.some((k) => after.entries[id][k] !== v[k])) return unknown("transcript-rewritten"); + } + const sum = { input: 0, output: 0, cacheWrite: 0, cacheRead: 0 }; + let added = false; + for (const [id, v] of Object.entries(after.entries)) { + if (Object.hasOwn(before.entries, id)) continue; + added = true; + for (const k of KINDS) { + sum[k] += v[k]; + if (!Number.isSafeInteger(sum[k])) return unknown("usage-overflow"); + } + } + return added ? { status: "known", ...sum } : unknown("no-new-usage"); +} diff --git a/packages/webui/tests/fixtures/triage-adapter.mjs b/packages/webui/tests/fixtures/triage-adapter.mjs index 4bd405bd0..8c492a61a 100644 --- a/packages/webui/tests/fixtures/triage-adapter.mjs +++ b/packages/webui/tests/fixtures/triage-adapter.mjs @@ -8,8 +8,12 @@ // SLEEP hang until killed // REPLY-SLEEP send the person an answer (class RESULT) to this message through the tool socket, then hang until killed // NOTE-SLEEP send the person a progress note (class INFO) on this message through the tool socket, then hang until killed +// SPEND add one usage entry of input tokens to the session's spend file (session/spend.jsonl, +// which the host tests' usage source reads as the transcript), then answer "spent " +// GATED-SPEND write work/started, wait for work/gate, then spend as SPEND does // anything else answer "echo: " -import { existsSync, readFileSync, writeFileSync } from "node:fs"; +import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; +import { randomBytes } from "node:crypto"; import { connect } from "node:net"; import { join } from "node:path"; @@ -64,6 +68,15 @@ if (body === "PROBE") { const id = request.match(/^Message (\S+) from /m)?.[1]; await toolCall("send_message", { to: "human", body: "still looking", class: "INFO", in_reply_to: id }); setInterval(() => {}, 1000); +} else if (/^(GATED-)?SPEND \d{1,9}$/.test(body)) { + const n = Number(body.split(" ")[1]); + if (body.startsWith("GATED-")) { + writeFileSync(join(workspace, "started"), "x"); + while (!existsSync(join(workspace, "gate"))) await new Promise((r) => setTimeout(r, 50)); + } + mkdirSync(join(workspace, "..", "session"), { recursive: true }); + appendFileSync(join(workspace, "..", "session", "spend.jsonl"), JSON.stringify({ id: randomBytes(6).toString("hex"), n }) + "\n"); + process.stdout.write(`spent ${n}`); } else if (body === "SLEEP") { setInterval(() => {}, 1000); } else { diff --git a/packages/webui/tests/triage-drawer-browser.test.mjs b/packages/webui/tests/triage-drawer-browser.test.mjs index 65f899237..8e05d03b8 100644 --- a/packages/webui/tests/triage-drawer-browser.test.mjs +++ b/packages/webui/tests/triage-drawer-browser.test.mjs @@ -63,7 +63,7 @@ function fakeHost(over = {}) { const triage = { snapshotAt: () => Date.now(), refresh: async () => null, - state: async () => { h.reads += 1; return { route: h.route, running: h.running, lastExit: h.lastExit, overCapUntil: h.overCapUntil, snapshot: h.snapshot, transcript: h.transcript }; }, + state: async () => { h.reads += 1; return { route: h.route, running: h.running, lastExit: h.lastExit, overCapUntil: h.overCapUntil, snapshot: h.snapshot, transcript: h.transcript, ...(Object.hasOwn(h, 'usageToday') ? { usageToday: h.usageToday, cap: h.cap, resetsAt: h.resetsAt } : {}) }; }, message: async text => { h.sent.push(text); const e = h.next; h.next = null; @@ -437,3 +437,33 @@ test('triage-followups: with no drawer open the first render still focuses the h const f = await setup(t); await f.wait('document.activeElement?.tagName === "H1"'); }); + +test('triage-cap: one usage line from the read: not set, the count against the cap with the reset, not read, and markup as text', async t => { + const f = await setup(t, { host: fakeHost({ usageToday: 'not set', cap: 'not set', resetsAt: 'not set' }) }); + await open(f); + assert.equal(await f.text('#triage-usage'), 'Daily token cap: not set.'); + assert.equal(await f.b.evaluate('document.querySelector("#triage-usage").hidden'), false); + const resetsAt = new Date(Date.now() + 5 * 3600000).toISOString(); + const local = await f.b.evaluate(`window.MosaicLocalTime(${JSON.stringify(resetsAt)})`); + Object.assign(f.h, { usageToday: { count: 1234, input: 1200, output: 34, cacheWrite: 0, cacheRead: 0, unknownTurns: 2 }, cap: { limit: 200000, reset: '00:00', timeZone: 'UTC' }, resetsAt }); + await f.wait('/^Today:/.test(document.querySelector("#triage-usage").textContent)', 400); + const line = await f.text('#triage-usage'); + const [count, limit] = await f.b.evaluate('[(1234).toLocaleString(), (200000).toLocaleString()]'); + assert.ok(line.startsWith(`Today: ${count} of ${limit} tokens. Resets at `), line); + assert.ok(line.includes(local), line); + assert.ok(line.endsWith(' 2 turns have no usage reading.'), line); + Object.assign(f.h, { usageToday: 'not read', cap: { limit: 200000, reset: '00:00', timeZone: 'UTC' }, resetsAt: 'not read' }); + await f.wait('/not read/.test(document.querySelector("#triage-usage").textContent)', 400); + assert.equal(await f.text('#triage-usage'), "Today's token usage: not read."); + // A count that is not a whole number is not shown as one; markup stays text. + Object.assign(f.h, { usageToday: { count: '', unknownTurns: 0 }, cap: { limit: 1.5 }, resetsAt: 'x' }); + await f.wait('/^Today: not read/.test(document.querySelector("#triage-usage").textContent)', 400); + assert.equal(await f.text('#triage-usage'), 'Today: not read of not read tokens.'); + assert.equal(await f.b.evaluate('document.querySelector("#triage-usage").children.length'), 0); +}); + +test('triage-cap: a read without the usage fields shows no usage line', async t => { + const f = await setup(t); + await open(f); + assert.equal(await f.b.evaluate('document.querySelector("#triage-usage").hidden'), true); +}); diff --git a/packages/webui/tests/triage-host.test.mjs b/packages/webui/tests/triage-host.test.mjs index 1be02f93a..345f073c7 100644 --- a/packages/webui/tests/triage-host.test.mjs +++ b/packages/webui/tests/triage-host.test.mjs @@ -5,15 +5,18 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { connect } from "node:net"; -import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { appendFileSync, chmodSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { Client } from "../../bus/src/client.mjs"; import { REPO, RUNNER_FILES, checkHostedRoute, sandboxProblem } from "../../seat/src/sandbox.mjs"; import { isProviderVariable } from "../../seat/src/session.mjs"; import { NOT_STARTED, NOT_TAKEN, RELAY_REFUSED, ROUTE_ENV_ALLOW, ROUTE_REFUSED, SPAWN_ENV_ALLOW, UNANSWERED, createRelay, createTriageHost, hostPrompt, - refusedHarnessName, triageEnv, + USAGE_NOT_READ, overCapNotTaken, refusedHarnessName, triageEnv, } from "../src/triage-host.mjs"; +import { writeRoute } from "../src/triage-route-store.mjs"; +import { appendUsage, readUsage, usagePath } from "../src/triage-usage.mjs"; +import { triageView } from "../src/triage-http.mjs"; const FIXTURE = new URL("./fixtures/triage-adapter.mjs", import.meta.url).pathname; const skip = sandboxProblem() ?? false; @@ -114,7 +117,8 @@ const REFUSED_TRIED = [ // The fake harness module: it declares the probe names and every refused one, so only the host's // own refusal stops those. Its one install root is /install. const DECLARED = Object.freeze(["TRIAGE_PROBE", "FAKE_OK", "FAKE_TWO", ...REFUSED_TRIED]); -const fakeModule = (roots, names = DECLARED) => async (name) => name === "fake" ? { envNames: names, installRoots: () => roots } : null; +// Its transcript is /session, where the stub adapter's SPEND writes (spendSource reads it). +const fakeModule = (roots, names = DECLARED) => async (name) => name === "fake" ? { envNames: names, installRoots: () => roots, transcript: ({ runDir }) => join(runDir, "session") } : null; function host(t, root, opts = {}) { const dataRoot = join(root, "data"); @@ -674,3 +678,495 @@ test("with devOwner set, a card's bus start carries the dev-owner flag", { skip, const [reply] = await repliesOf(h, 1); assert.deepEqual(reply.cards, [{ step: "bus-start", org: "alpha", command: "mosaic bus start alpha --dev-local-owner --detach" }]); }); + +// #1873 triage-cap. The host counts each turn into /triage/usage.jsonl and holds the stored +// cap through daily-quota. The usage source here reads /session/spend.jsonl, which the stub +// adapter's SPEND writes (or a test writes by hand), one entry per spend. +const spendSource = { + readTurn: async ({ root }) => { + let text; + try { text = readFileSync(join(root, "spend.jsonl"), "utf8"); } + catch (e) { if (e.code === "ENOENT") return { status: "known", entries: {} }; throw e; } + const entries = {}; + for (const l of text.split("\n").filter(Boolean)) { + const { id, n } = JSON.parse(l); + entries[id] = { input: n, output: 0, cacheWrite: 0, cacheRead: 0 }; + } + return { status: "known", entries }; + }, +}; +const DAY = "2026-11-18T00:00:00.000Z"; +// Store a cap the way the owner's verb does: the route store, its since at `since`. +// The data root's triage directory, as the host's ensureDirs makes it (a test writes before any start). +const triageDir = (dataRoot) => { + mkdirSync(join(dataRoot, "triage"), { recursive: true, mode: 0o700 }); + chmodSync(dataRoot, 0o700); + chmodSync(join(dataRoot, "triage"), 0o700); +}; +const storeCap = (dataRoot, limit, timeZone = "UTC", since = DAY) => triageDir(dataRoot) ?? + writeRoute(dataRoot, { model: "glm-5", harness: "claude-code", limit, timeZone, by: "owner" }, { now: () => new Date(since) }); +const usageLine = (over = {}) => ({ at: "2026-11-18T10:00:00.000Z", run: "triage-20261118100000", turn: "owner-0123456789ab", status: "known", input: 10, output: 0, cacheRead: 0, cacheWrite: 0, ...over }); +const usageLines = (dataRoot) => readUsage(usagePath(dataRoot)).records; +let spendId = 0; +const spend = (dataRoot, n) => { + mkdirSync(join(dataRoot, "triage", "agent", "session"), { recursive: true }); + appendFileSync(join(dataRoot, "triage", "agent", "session", "spend.jsonl"), JSON.stringify({ id: `s${++spendId}`, n }) + "\n"); +}; +const counting = (root) => { const r = { asked: 0, admit: async () => { r.asked++; return answer(root); } }; return r; }; +// A launcher that keeps the relay open and leaves the capability it was handed, so a test can make +// the runner's calls itself without the sandbox. +function standIn(root) { + const f = join(root, "stand-in"); + writeFileSync(f, `#!/bin/sh\n: > '${join(root, "spawned")}'\ncat > '${join(root, "stdin.json")}'\nexec sleep 600\n`, { mode: 0o755 }); + return f; +} +async function runnerClient(root, dataRoot) { + await waitFor(() => { try { return JSON.parse(readFileSync(join(root, "stdin.json"), "utf8")).cap; } catch { return null; } }); + const cap = JSON.parse(readFileSync(join(root, "stdin.json"), "utf8")).cap; + return new Client({ path: join(dataRoot, "triage", "agent", "relay.sock"), cap }); +} + +test("cap: a hit before a start refuses with the next reset, asks no route and spawns nothing; at 00:00 in the stored zone it starts", { timeout: 30000 }, async (t) => { + const root = scratch(); + let clock = Date.parse("2026-11-18T12:00:00.000Z"); + const route = counting(root); + const { h, dataRoot } = host(t, root, { route, bwrap: spy(root), usageSource: spendSource, now: () => new Date(clock) }); + storeCap(dataRoot, 100); + appendUsage(usagePath(dataRoot), usageLine({ input: 60, output: 30, cacheRead: 5, cacheWrite: 5 })); + await assert.rejects(h.message("hello"), (e) => e.code === "triage-over-cap" && e.until === "2026-11-19T00:00:00.000Z"); + assert.equal(route.asked, 0, "the route is not asked over the cap"); + assert.equal(existsSync(join(root, "spawned")), false); + assert.deepEqual((await h.state()).transcript, [], "a refused message is never written"); + const s = await h.state(); + assert.deepEqual([s.overCapUntil, s.resetsAt], ["2026-11-19T00:00:00.000Z", "2026-11-19T00:00:00.000Z"]); + assert.deepEqual(s.usageToday, { count: 100, input: 60, output: 30, cacheWrite: 5, cacheRead: 5, unknownTurns: 0 }); + assert.deepEqual(s.cap, { limit: 100, reset: "00:00", timeZone: "UTC" }); + clock = Date.parse("2026-11-18T23:59:59.999Z"); + await assert.rejects(h.message("hello"), (e) => e.code === "triage-over-cap"); + clock = Date.parse("2026-11-19T00:00:00.000Z"); + const s2 = await h.state(); + assert.deepEqual([s2.overCapUntil, s2.usageToday.count, s2.resetsAt], [null, 0, "2026-11-20T00:00:00.000Z"]); + await h.message("hello"); + await waitFor(() => existsSync(join(root, "spawned"))); + assert.equal(route.asked, 1); +}); + +test("cap: the reset is 00:00 in the stored zone on a DST day (America/Chicago, a 25-hour day)", { timeout: 30000 }, async (t) => { + const root = scratch(); + let clock = Date.parse("2026-11-01T12:00:00.000Z"); + const route = counting(root); + const { h, dataRoot } = host(t, root, { route, bwrap: spy(root), usageSource: spendSource, now: () => new Date(clock) }); + storeCap(dataRoot, 100, "America/Chicago", "2026-10-31T00:00:00.000Z"); + // 2026-11-01 in Chicago runs from 05:00Z (CDT) to 06:00Z the next day (CST). + appendUsage(usagePath(dataRoot), usageLine({ at: "2026-11-01T05:00:00.000Z", input: 100 })); + await assert.rejects(h.message("a"), (e) => e.code === "triage-over-cap" && e.until === "2026-11-02T06:00:00.000Z"); + clock = Date.parse("2026-11-02T05:59:59.999Z"); + await assert.rejects(h.message("b"), (e) => e.code === "triage-over-cap" && e.until === "2026-11-02T06:00:00.000Z"); + clock = Date.parse("2026-11-02T06:00:00.000Z"); + await h.message("c"); + await waitFor(() => existsSync(join(root, "spawned"))); + assert.equal(route.asked, 1); + // A line from before the day began does not count: 04:59:59.999Z was still 2026-10-31 in Chicago. + const other = scratch(); + const o = host(t, other, { route: counting(other), bwrap: spy(other), usageSource: spendSource, now: () => new Date("2026-11-01T12:00:00.000Z") }); + storeCap(o.dataRoot, 100, "America/Chicago", "2026-10-30T00:00:00.000Z"); + appendUsage(usagePath(o.dataRoot), usageLine({ at: "2026-11-01T04:59:59.999Z", input: 100 })); + assert.equal((await o.h.state()).usageToday.count, 0); +}); + +test("cap: the relay writes one line per turn, known or unknown, and a session that ends mid-turn still gets its line", { timeout: 30000 }, async (t) => { + const root = scratch(); + const logs = []; + const { h, dataRoot } = host(t, root, { route: counting(root), bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z"), log: (l) => logs.push(l) }); + const first = await h.message("one"); + const c = await runnerClient(root, dataRoot); + const run = JSON.parse(readFileSync(join(dataRoot, "triage", "agent", "session.json"), "utf8")).run; + assert.match(run, /^triage-\d{14}$/); + spend(dataRoot, 7); // before the turn: the baseline holds it + assert.deepEqual(await c.call("usage.begin", { turn: first.id }), {}); + spend(dataRoot, 40); + spend(dataRoot, 2); + await c.call("usage.end", { turn: first.id }); + // A repeated end does not count the turn twice. + await c.call("usage.end", { turn: first.id }); + await c.call("usage.begin", { turn: `${first.id}:retry` }); + await c.call("usage.end", { turn: `${first.id}:retry` }); // nothing new: unknown + const lines = usageLines(dataRoot); + assert.deepEqual(lines.map((l) => [l.run, l.turn, l.status, l.input, l.output, l.cacheRead, l.cacheWrite]), [ + [run, first.id, "known", 42, 0, 0, 0], + [run, `${first.id}:retry`, "unknown", null, null, null, null], + ]); + assert.ok(lines.every((l) => l.at === "2026-11-18T12:00:00.000Z"), "the line's time is the host's clock"); + assert.ok(logs.includes("triage host: usage.end repeated for a turn; not counted twice")); + assert.ok(logs.includes("triage host: turn usage unknown: no-new-usage")); + assert.equal(lstatSync(usagePath(dataRoot)).mode & 0o777, 0o600); + // A turn begun and never ended: the session is killed; the host writes its line. + await c.call("usage.begin", { turn: "owner-0123456789ff" }); + spend(dataRoot, 5); + process.kill(-h.pid, "SIGKILL"); + await waitFor(() => !h.running); + await h.stop(); + assert.deepEqual(usageLines(dataRoot).slice(2).map((l) => [l.turn, l.status, l.input]), [["owner-0123456789ff", "known", 5]]); + assert.equal(usageLines(dataRoot).length, 3, "the killed turn has one line, not two"); +}); + +test("cap: a turn that crosses the cap finishes; the runner is then refused and every queued message gets a host line", { timeout: 30000 }, async (t) => { + const root = scratch(); + const logs = []; + const route = counting(root); + const { h, dataRoot } = host(t, root, { route, bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z"), log: (l) => logs.push(l) }); + storeCap(dataRoot, 100); + const first = await h.message("one"); + const c = await runnerClient(root, dataRoot); + assert.deepEqual((await c.call("message.receive")).map((m) => m.id), [first.id]); + await c.call("state"); + await c.call("usage.begin", { turn: first.id }); + // Queued while the turn runs, under the cap: admitted. + const second = await h.message("two"); + const third = await h.message("three"); + spend(dataRoot, 150); + // Inside the turn (a continuation's state read) the runner is not stopped. + assert.equal((await c.call("state")).tasks.total, 0); + await c.call("usage.end", { turn: first.id }); + await c.call("message.send", { to: "human", body: "answer", class: "RESULT", in_reply_to: first.id }); + await c.call("message.read", { id: first.id }); + // The next receive is refused: the session is gone, the queue drains with a host line each. + await assert.rejects(c.call("message.receive"), (e) => e.code === "not-holder"); + const tr = (await h.state()).transcript; + assert.deepEqual(tr.filter((e) => e.from === "triage").map((e) => [e.in_reply_to, e.text]), [[first.id, "answer"]]); + const until = "2026-11-19T00:00:00.000Z"; + assert.deepEqual(tr.filter((e) => e.from === "host").map((e) => [e.in_reply_to, e.text]), [[second.id, overCapNotTaken(until)], [third.id, overCapNotTaken(until)]]); + assert.ok(logs.includes("triage host: stopping the session at message.receive: over the daily token cap"), logs.join(" | ")); + await assert.rejects(h.message("four"), (e) => e.code === "triage-over-cap" && e.until === until); + assert.equal(route.asked, 3); + // The session ends: nothing is left to answer, so no second line. + process.kill(-h.pid, "SIGKILL"); + await waitFor(() => !h.running); + assert.equal((await h.state()).transcript.filter((e) => e.from === "host").length, 2); +}); + +test("cap: messages taken together: once a turn crosses the cap, the next message's turn does not start", { timeout: 30000 }, async (t) => { + const root = scratch(); + const logs = []; + const { h, dataRoot } = host(t, root, { route: counting(root), bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z"), log: (l) => logs.push(l) }); + storeCap(dataRoot, 100); + const [a, b] = await Promise.all([h.message("one"), h.message("two")]); + const c = await runnerClient(root, dataRoot); + assert.deepEqual((await c.call("message.receive")).map((m) => m.id), [a.id, b.id]); + await c.call("state"); + await c.call("usage.begin", { turn: a.id }); + spend(dataRoot, 100); + await c.call("usage.end", { turn: a.id }); + await c.call("message.send", { to: "human", body: "answer", class: "RESULT", in_reply_to: a.id }); + await c.call("message.read", { id: a.id }); + // The runner reads the state before each message's turn: refused, so b's turn never begins. + await assert.rejects(c.call("state"), (e) => e.code === "not-holder"); + const hostLines = (await h.state()).transcript.filter((e) => e.from === "host"); + assert.deepEqual(hostLines.map((e) => [e.in_reply_to, e.text]), [[b.id, overCapNotTaken("2026-11-19T00:00:00.000Z")]]); + assert.ok(logs.includes("triage host: stopping the session at state: over the daily token cap"), logs.join(" | ")); + assert.equal(usageLines(dataRoot).length, 1); +}); + +test("cap: a restart counts what the file holds: a new host on the same data root refuses at the same count", { timeout: 30000 }, async (t) => { + const root = scratch(); + const first = host(t, root, { route: counting(root), bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z") }); + storeCap(first.dataRoot, 100); + const m = await first.h.message("one"); + const c = await runnerClient(root, first.dataRoot); + await c.call("usage.begin", { turn: m.id }); + spend(first.dataRoot, 60); + await c.call("usage.end", { turn: m.id }); + // The second turn is open when the host stops: its line is written on the way down. + await c.call("usage.begin", { turn: "owner-0123456789ee" }); + spend(first.dataRoot, 45); + await first.h.stop(); + assert.deepEqual(usageLines(first.dataRoot).map((l) => [l.turn, l.input]), [[m.id, 60], ["owner-0123456789ee", 45]]); + const route = counting(root); + const second = host(t, root, { route, bwrap: spy(root), usageSource: spendSource, now: () => new Date("2026-11-18T13:00:00.000Z") }); + assert.equal((await second.h.state()).usageToday.count, 105); + await assert.rejects(second.h.message("two"), (e) => e.code === "triage-over-cap" && e.until === "2026-11-19T00:00:00.000Z"); + assert.equal(route.asked, 0); +}); + +test("cap: a message whose turn began and was not answered gets the unanswered line when the cap stops the session", { timeout: 30000 }, async (t) => { + const root = scratch(); + const { h, dataRoot } = host(t, root, { route: counting(root), bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z") }); + storeCap(dataRoot, 100); + const [a, b] = await Promise.all([h.message("one"), h.message("two")]); + const c = await runnerClient(root, dataRoot); + await c.call("message.receive"); + await c.call("usage.begin", { turn: a.id }); + spend(dataRoot, 100); + await c.call("usage.end", { turn: a.id }); + // The turn's answer is lost (a halted processing state leaves a message unread): a stays queued. + await assert.rejects(c.call("state"), (e) => e.code === "not-holder"); + const lines = (await h.state()).transcript.filter((e) => e.from === "host").map((e) => [e.in_reply_to, e.text]); + assert.deepEqual(lines, [[a.id, UNANSWERED], [b.id, overCapNotTaken("2026-11-19T00:00:00.000Z")]]); +}); + +test("cap: under the sandbox, the runner's turns are counted, a turn that crosses the cap answers, the queued message drains, and a restart counts the file", { skip, timeout: 120000 }, async (t) => { + const root = scratch(); + const logs = []; + const route = stubRoute(root); + const clock = () => new Date("2026-11-18T12:00:00.000Z"); + const { h, dataRoot } = host(t, root, { route, usageSource: spendSource, now: clock, log: (l) => logs.push(l) }); + storeCap(dataRoot, 1000); + const one = await h.message("SPEND 300"); + const echo = await h.message("plain"); + const replies = await repliesOf(h, 2); + assert.deepEqual(replies.map((e) => [e.text.replace(/\n\n.*$/s, ""), e.in_reply_to]), [["spent 300", one.id], ["echo: plain", echo.id]]); + const lines = usageLines(dataRoot); + assert.deepEqual(lines.map((l) => [l.turn, l.status, l.input]), [[one.id, "known", 300], [echo.id, "unknown", null]]); + assert.match(lines[0].run, /^triage-\d{14}$/); + await h.stop(); + // A restart: a new host on the same data root counts the file. + const again = host(t, root, { route, usageSource: spendSource, now: clock, log: (l) => logs.push(l) }); + const s = await again.h.state(); + assert.deepEqual([s.usageToday.count, s.usageToday.unknownTurns], [300, 1]); + const big = await again.h.message("GATED-SPEND 800"); + await waitFor(() => existsSync(join(dataRoot, "triage", "agent", "work", "started"))); + const queued = await again.h.message("queued"); + writeFileSync(join(dataRoot, "triage", "agent", "work", "gate"), "x"); + const [answer] = await repliesOf(again.h, 3).then((l) => l.slice(2)); + assert.deepEqual([answer.text.replace(/\n\n.*$/s, ""), answer.in_reply_to], ["spent 800", big.id]); + const [note] = await repliesOf(again.h, 1, "host"); + assert.deepEqual([note.in_reply_to, note.text], [queued.id, overCapNotTaken("2026-11-19T00:00:00.000Z")]); + await waitFor(() => !again.h.running); + assert.equal((await again.h.state()).lastExit.code, 22, "the runner ends as a session that is gone"); + assert.equal((await again.h.state()).transcript.filter((e) => e.from === "host").length, 1); + await assert.rejects(again.h.message("after"), (e) => e.code === "triage-over-cap"); + assert.equal(usageLines(dataRoot).at(-1).input, 800); +}); + +test("cap: a hit from another count before a queued message: the file is read again at each check", { timeout: 30000 }, async (t) => { + const root = scratch(); + const { h, dataRoot } = host(t, root, { route: counting(root), bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z") }); + storeCap(dataRoot, 1000); + const first = await h.message("one"); + const c = await runnerClient(root, dataRoot); + await c.call("message.receive"); + const second = await h.message("two"); + // Under the cap the runner takes the queued message. + assert.deepEqual((await c.call("message.receive")).map((m) => m.id), [second.id]); + await c.call("usage.begin", { turn: second.id }); + const third = await h.message("three"); + appendUsage(usagePath(dataRoot), usageLine({ input: 1000 })); + // Over the cap inside second's turn: a continuation's state read is not stopped; the turn finishes. + assert.equal((await c.call("state")).tasks.total, 0); + spend(dataRoot, 4); + await c.call("usage.end", { turn: second.id }); + await c.call("message.send", { to: "human", body: "answer", class: "RESULT", in_reply_to: second.id }); + await c.call("message.read", { id: second.id }); + await assert.rejects(c.call("message.receive"), (e) => e.code === "not-holder"); + const until = overCapNotTaken("2026-11-19T00:00:00.000Z"); + const hostLines = (await h.state()).transcript.filter((e) => e.from === "host").map((e) => [e.in_reply_to, e.text]); + assert.deepEqual(hostLines.sort(), [[first.id, until], [third.id, until]].sort(), "every message still queued leaves it"); + assert.deepEqual(usageLines(dataRoot).at(-1).input, 4, "the finished turn is counted"); +}); + +test("cap: an idle session over the cap ends at its next receive, with nothing queued", { timeout: 30000 }, async (t) => { + const root = scratch(); + const { h, dataRoot } = host(t, root, { route: counting(root), bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z") }); + storeCap(dataRoot, 1000); + const m = await h.message("one"); + const c = await runnerClient(root, dataRoot); + await c.call("message.receive"); + await c.call("message.send", { to: "human", body: "answer", class: "RESULT", in_reply_to: m.id }); + await c.call("message.read", { id: m.id }); + appendUsage(usagePath(dataRoot), usageLine({ input: 1000 })); + await assert.rejects(c.call("message.receive"), (e) => e.code === "not-holder"); + assert.equal((await h.state()).transcript.filter((e) => e.from === "host").length, 0, "nothing was queued, so no host line"); +}); + +test("cap: the count waits for a turn still being counted: usage.end answers after its line, and a message right after a mid-turn kill is refused", { timeout: 30000 }, async (t) => { + const root = scratch(); + let gate = null; // while set, the usage source waits on it + const close = () => { let release; const promise = new Promise((r) => { release = r; }); gate = { promise, release: () => { gate = null; release(); } }; return gate; }; + const gated = { readTurn: async (a) => { if (gate) await gate.promise; return spendSource.readTurn(a); } }; + const route = counting(root); + // A failed assertion must not leave the gate shut: the host's stop waits for the usage chain. After hooks + // run in the order they were added, so this one goes before the host's stop. + t.after(() => gate?.release()); + const { h, dataRoot } = host(t, root, { route, bwrap: standIn(root), usageSource: gated, now: () => new Date("2026-11-18T12:00:00.000Z") }); + storeCap(dataRoot, 100); + const m = await h.message("one"); + const c = await runnerClient(root, dataRoot); + await c.call("message.receive"); + await c.call("usage.begin", { turn: m.id }); + spend(dataRoot, 60); + let g = close(); + let answered = false; + const end = c.call("usage.end", { turn: m.id }).then(() => { answered = true; }); + await new Promise((r) => setTimeout(r, 200)); + assert.equal(answered, false, "usage.end is not answered before its line is written"); + assert.equal(usageLines(dataRoot).length, 0); + g.release(); + await end; + assert.deepEqual(usageLines(dataRoot).map((l) => l.input), [60]); + await c.call("message.send", { to: "human", body: "answer", class: "RESULT", in_reply_to: m.id }); + await c.call("message.read", { id: m.id }); + // A second turn crosses the cap and the session is killed before its usage.end. + const two = await h.message("two"); + assert.deepEqual((await c.call("message.receive")).map((x) => x.id), [two.id]); + await c.call("usage.begin", { turn: two.id }); + spend(dataRoot, 50); + g = close(); + process.kill(-h.pid, "SIGKILL"); + await waitFor(() => !h.running); + const asked = route.asked; + setTimeout(() => g.release(), 200); + await assert.rejects(h.message("three"), (e) => e.code === "triage-over-cap" && e.until === "2026-11-19T00:00:00.000Z"); + assert.equal(route.asked, asked, "the route is not asked: the open turn was counted first"); + assert.deepEqual(usageLines(dataRoot).map((l) => [l.turn, l.input]), [[m.id, 60], [two.id, 50]]); +}); + +test("cap: a harness transcript path outside the run directory reads as no transcript, so the turn's line is unknown", { timeout: 30000 }, async (t) => { + for (const label of ["a sibling of the run directory", "a path through .. from inside it"]) { + const root = scratch(); + const logs = []; + let target = null; + const mod = async (name) => name === "fake" ? { envNames: DECLARED, installRoots: () => [join(root, "install")], transcript: ({ runDir }) => (target = label.startsWith("a sibling") ? join(runDir, "..", "outside") : `${runDir}/session/../../outside`) } : null; + const { h, dataRoot } = host(t, root, { route: counting(root), bwrap: standIn(root), usageSource: spendSource, harnessModule: mod, now: () => new Date("2026-11-18T12:00:00.000Z"), log: (l) => logs.push(l) }); + const m = await h.message("one"); + const c = await runnerClient(root, dataRoot); + mkdirSync(target, { recursive: true }); + await c.call("usage.begin", { turn: m.id }); + appendFileSync(join(target, "spend.jsonl"), JSON.stringify({ id: "x1", n: 5 }) + "\n"); + await c.call("usage.end", { turn: m.id }); + assert.deepEqual(usageLines(dataRoot).map((l) => [l.status, l.input]), [["unknown", null]], label); + assert.ok(logs.includes("triage host: turn usage unknown: baseline-unknown"), label); + } +}); + +test("cap: a torn last line is refused, logged once, and counts as an unknown turn; the next line stands alone", { timeout: 30000 }, async (t) => { + const root = scratch(); + const logs = []; + const { h, dataRoot } = host(t, root, { route: counting(root), bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z"), log: (l) => logs.push(l) }); + storeCap(dataRoot, 1000); + appendUsage(usagePath(dataRoot), usageLine({ input: 30 })); + appendFileSync(usagePath(dataRoot), '{"at":"2026-11-18T11:00:00.000Z","run":"triage-2026111'); + const s = await h.state(); + assert.deepEqual([s.usageToday.count, s.usageToday.unknownTurns], [30, 1]); + await h.state(); + assert.deepEqual(logs.filter((l) => /usage\.jsonl line/.test(l)), ["triage host: usage.jsonl line 2 is not a usage line; it counts as an unknown turn"]); + // Under the cap, a message starts, and its turn's line is written after the torn one. + const m = await h.message("one"); + const c = await runnerClient(root, dataRoot); + await c.call("usage.begin", { turn: m.id }); + spend(dataRoot, 20); + await c.call("usage.end", { turn: m.id }); + const lines = usageLines(dataRoot); + assert.deepEqual(lines.map((l) => [l.turn, l.status, l.input]), [["owner-0123456789ab", "known", 30], ["line-2", "unknown", null], [m.id, "known", 20]]); + const s2 = await h.state(); + assert.deepEqual([s2.usageToday.count, s2.usageToday.unknownTurns], [50, 1]); +}); + +test("cap: a usage file with a wrong mode, a link or a non-file refuses every message and reads as not read", { timeout: 30000 }, async (t) => { + const cases = [ + ["mode 0644", (f) => { appendUsage(f, usageLine()); chmodSync(f, 0o644); }], + ["mode 0400", (f) => { appendUsage(f, usageLine()); chmodSync(f, 0o400); }], + ["a link", (f) => { writeFileSync(f + ".real", JSON.stringify(usageLine()) + "\n", { mode: 0o600 }); symlinkSync(f + ".real", f); }], + ["a directory", (f) => mkdirSync(f)], + ]; + for (const [label, make] of cases) { + const root = scratch(); + const route = counting(root); + const { h, dataRoot } = host(t, root, { route, bwrap: spy(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z") }); + storeCap(dataRoot, 1000); + make(usagePath(dataRoot)); + await assert.rejects(h.message("hello"), (e) => e.code === "triage-usage-unreadable" && e.message === "Triage's token usage could not be read", label); + assert.equal(route.asked, 0, label); + assert.equal(existsSync(join(root, "spawned")), false, label); + const s = await h.state(); + assert.deepEqual([s.usageToday, s.cap, s.resetsAt], ["not read", { limit: 1000, reset: "00:00", timeZone: "UTC" }, "2026-11-19T00:00:00.000Z"], label); + } +}); + +test("cap: a line the host cannot write leaves the count short, so the stored cap refuses from then on", { timeout: 30000 }, async (t) => { + const root = scratch(); + const logs = []; + const { h, dataRoot } = host(t, root, { route: counting(root), bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z"), log: (l) => logs.push(l) }); + storeCap(dataRoot, 1000); + const m = await h.message("one"); + const c = await runnerClient(root, dataRoot); + await c.call("message.receive"); + await c.call("usage.begin", { turn: m.id }); + spend(dataRoot, 5); + appendUsage(usagePath(dataRoot), usageLine({ input: 1 })); + chmodSync(usagePath(dataRoot), 0o644); + await c.call("usage.end", { turn: m.id }); + assert.ok(logs.includes("triage host: usage line not written: triage-usage-unreadable"), logs.join(" | ")); + chmodSync(usagePath(dataRoot), 0o600); + assert.equal(usageLines(dataRoot).length, 1, "the file is as it was"); + // The file reads again, but a turn is missing from it: refused, and the state says not read. + await assert.rejects(h.message("two"), (e) => e.code === "triage-usage-unreadable"); + assert.equal((await h.state()).usageToday, "not read"); + await c.call("message.send", { to: "human", body: "answer", class: "RESULT", in_reply_to: m.id }); + const later = await h.state(); + assert.equal(later.transcript.filter((e) => e.from === "host").length, 0); +}); + +test("cap: with no route stored the fields say not set, nothing is refused for usage, and each turn's line is still written", { timeout: 30000 }, async (t) => { + const root = scratch(); + const { h, dataRoot } = host(t, root, { route: counting(root), bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z") }); + const s = await h.state(); + assert.deepEqual([s.usageToday, s.cap, s.resetsAt, s.overCapUntil], ["not set", "not set", "not set", null]); + triageDir(dataRoot); + // A usage file over any count, even a broken one, refuses nothing while no cap is stored. + appendUsage(usagePath(dataRoot), usageLine({ input: 10 ** 9 })); + appendFileSync(usagePath(dataRoot), "not json"); + const m = await h.message("one"); + const c = await runnerClient(root, dataRoot); + assert.deepEqual((await c.call("message.receive")).map((x) => x.id), [m.id]); + await c.call("state"); + await c.call("usage.begin", { turn: m.id }); + spend(dataRoot, 3); + await c.call("usage.end", { turn: m.id }); + assert.deepEqual(usageLines(dataRoot).at(-1).input, 3); + const two = await h.message("two"); + assert.deepEqual((await c.call("message.receive")).map((x) => x.id), [two.id]); +}); + +test("cap: an unreadable route store refuses every message and reads as not read", { timeout: 30000 }, async (t) => { + const root = scratch(); + const route = counting(root); + const { h, dataRoot } = host(t, root, { route, bwrap: spy(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z") }); + storeCap(dataRoot, 1000); + chmodSync(join(dataRoot, "triage", "route.json"), 0o644); + await assert.rejects(h.message("hello"), (e) => e.code === "triage-route-unreadable" && e.message === "the model route could not be read"); + assert.equal(route.asked, 0); + const s = await h.state(); + assert.deepEqual([s.usageToday, s.cap, s.resetsAt], ["not read", "not read", "not read"]); + // A running session: the store turns unreadable, the next receive is refused and the queue drains. + const other = scratch(); + const r = host(t, other, { route: counting(other), bwrap: standIn(other), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z") }); + storeCap(r.dataRoot, 1000); + const m = await r.h.message("one"); + const c = await runnerClient(other, r.dataRoot); + await c.call("message.receive"); + const two = await r.h.message("two"); + chmodSync(join(r.dataRoot, "triage", "route.json"), 0o644); + await assert.rejects(c.call("message.receive"), (e) => e.code === "not-holder"); + const hostLines = (await r.h.state()).transcript.filter((e) => e.from === "host").map((e) => [e.in_reply_to, e.text]); + assert.deepEqual(hostLines.sort(), [[m.id, USAGE_NOT_READ], [two.id, USAGE_NOT_READ]].sort()); +}); + +test("cap: the read carries usageToday, cap and resetsAt, with and without a stored route, and no path, key or env value", { timeout: 30000 }, async (t) => { + const root = scratch(); + const env = { ...CONSOLE_ENV, ANTHROPIC_AUTH_TOKEN: "tok-secret-value" }; + const route = { admit: async () => answer(root, { env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9", ANTHROPIC_AUTH_TOKEN: "tok-secret-value" } }) }; + const { h, dataRoot } = host(t, root, { route, bwrap: spy(root), env, usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z") }); + const unset = await triageView(h, { send: true }); + assert.deepEqual([unset.usageToday, unset.cap, unset.resetsAt], ["not set", "not set", "not set"]); + storeCap(dataRoot, 500, "Asia/Kolkata", "2026-11-17T00:00:00.000Z"); + appendUsage(usagePath(dataRoot), usageLine({ at: "2026-11-18T11:00:00.000Z", input: 120, output: 3 })); + await h.message("hello"); + const view = await triageView(h, { send: true }); + assert.deepEqual(view.usageToday, { count: 123, input: 120, output: 3, cacheWrite: 0, cacheRead: 0, unknownTurns: 0 }); + assert.deepEqual(view.cap, { limit: 500, reset: "00:00", timeZone: "Asia/Kolkata" }); + assert.equal(view.resetsAt, "2026-11-18T18:30:00.000Z"); + const text = JSON.stringify([unset, view]); + for (const bad of [dataRoot, root, "usage.jsonl", "route.json", "glm-5", "claude-code", "tok-secret-value", "from-the-console", "since", "2026-11-17T00:00:00.000Z", "triage-2026"]) { + assert.equal(text.includes(bad), false, bad); + } +}); diff --git a/packages/webui/tests/triage-http.test.mjs b/packages/webui/tests/triage-http.test.mjs index fff0bb4c8..9c56e4702 100644 --- a/packages/webui/tests/triage-http.test.mjs +++ b/packages/webui/tests/triage-http.test.mjs @@ -4,7 +4,7 @@ import test from 'node:test'; import assert from 'node:assert/strict'; import { request } from 'node:http'; -import { mkdtempSync, existsSync, readFileSync } from 'node:fs'; +import { mkdtempSync, mkdirSync, existsSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; import { startServer } from '../src/serve.mjs'; import { createDevOwner } from '../src/dev-owner.mjs'; @@ -12,6 +12,8 @@ import { consoleAccess } from '../src/access.mjs'; import { createSsoSession } from './helpers/sso-session.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'; +import { writeRoute } from '../src/triage-route-store.mjs'; +import { appendUsage, usagePath } from '../src/triage-usage.mjs'; const LEAK = '/home/owner/.config/mosaic-dev/secret-token-0123456789'; @@ -239,6 +241,21 @@ test('with the real host and no route: the read says so in plain words, the writ assert.equal(JSON.parse(readFileSync(join(host.paths.health, 'snapshot.json'), 'utf8')).snapshotVersion, 1, 'the read wrote the snapshot'); }); +test('triage-cap: the read carries usageToday, cap and resetsAt from the real host, and no path, model or harness', async t => { + const dataRoot = join(mkdtempSync(join(process.env.TMPDIR, 'th-')), 'data'); + mkdirSync(join(dataRoot, 'triage'), { recursive: true, mode: 0o700 }); + writeRoute(dataRoot, { model: 'glm-5', harness: 'claude-code', limit: 1000, timeZone: 'UTC', by: 'owner' }, { now: () => new Date('2026-11-18T00:00:00.000Z') }); + appendUsage(usagePath(dataRoot), { at: '2026-11-18T09:00:00.000Z', run: 'triage-20261118090000', turn: 'owner-0123456789ab', status: 'known', input: 30, output: 5, cacheRead: 2, cacheWrite: 1 }); + const host = createTriageHost({ dataRoot, readSnapshot: async () => null, now: () => new Date('2026-11-18T10:00:00.000Z'), bwrap: '/nonexistent/bwrap' }); + const f = await devServer(t, { triage: host }); + const read = await send(f.port, '/api/triage', { headers: { host: f.headers.host } }); + assert.equal(read.status, 200); + assert.deepEqual(read.json.usageToday, { count: 38, input: 30, output: 5, cacheWrite: 1, cacheRead: 2, unknownTurns: 0 }); + assert.deepEqual(read.json.cap, { limit: 1000, reset: '00:00', timeZone: 'UTC' }); + assert.equal(read.json.resetsAt, '2026-11-19T00:00:00.000Z'); + for (const leak of [dataRoot, 'glm-5', 'claude-code', 'usage.jsonl', '"since"']) assert.equal(read.text.includes(leak), false, leak); +}); + test('the route module keeps its own write checks when called alone: --send, one exact Origin, one same-origin Fetch Metadata', async () => { const triage = fakeTriage(); const call = async (headers, { send = true } = {}) => { diff --git a/packages/webui/tests/triage-usage.test.mjs b/packages/webui/tests/triage-usage.test.mjs new file mode 100644 index 000000000..aec47b27c --- /dev/null +++ b/packages/webui/tests/triage-usage.test.mjs @@ -0,0 +1,205 @@ +// #1873 triage-cap: Triage's usage file and its daily cap. One line per turn in +// /triage/usage.jsonl, held to the same file checks as the route store; the cap is the +// daily-quota plugin's own rule over those lines, with the stored cap as its one leaf. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { chmodSync, linkSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, symlinkSync, writeFileSync } from "node:fs"; +import { execFileSync } from "node:child_process"; +import { join } from "node:path"; +import { mayLaunch, report } from "../../plugins/limit-check/daily-quota/index.mjs"; +import { + USAGE_ACCOUNT, USAGE_FIELDS, appendUsage, capState, delta, limitsFor, nextReset, readUsage, snapshot, usagePath, +} from "../src/triage-usage.mjs"; + +const scratch = () => mkdtempSync(join(process.env.TMPDIR, "tu-")); +const fileIn = () => { + const dir = join(scratch(), "triage"); + mkdirSync(dir, { mode: 0o700 }); + return join(dir, "usage.jsonl"); +}; +const line = (over = {}) => ({ at: "2026-11-18T12:00:00.000Z", run: "triage-20261118120000", turn: "owner-0123456789ab", status: "known", input: 10, output: 5, cacheRead: 2, cacheWrite: 1, ...over }); +const unknownLine = (over = {}) => line({ status: "unknown", input: null, output: null, cacheRead: null, cacheWrite: null, ...over }); +const cap = (over = {}) => ({ limit: 100, since: "2026-10-01T00:00:00.000Z", reset: "00:00", timeZone: "UTC", ...over }); +const ms = (s) => Date.parse(s); + +test("the usage file is /triage/usage.jsonl and a line holds exactly its eight fields", () => { + assert.equal(usagePath("/d"), "/d/triage/usage.jsonl"); + assert.deepEqual(USAGE_FIELDS, ["at", "run", "turn", "status", "input", "output", "cacheRead", "cacheWrite"]); + assert.equal(USAGE_ACCOUNT, "triage"); +}); + +test("each append writes one line, 0600, in field order; a torn last line is closed off before the next line", () => { + const file = fileIn(); + appendUsage(file, line()); + appendUsage(file, unknownLine({ turn: "owner-0123456789ac" })); + assert.equal(lstatSync(file).mode & 0o777, 0o600); + const text = readFileSync(file, "utf8"); + assert.equal(text.split("\n").length, 3); + assert.deepEqual(Object.keys(JSON.parse(text.split("\n")[0])), USAGE_FIELDS); + // A crash in a write leaves a torn line. The next append starts on a line of its own. + writeFileSync(file, '{"at":"2026-11-18T12:0', { flag: "a" }); + appendUsage(file, line({ turn: "owner-0123456789ad" })); + const lines = readFileSync(file, "utf8").split("\n"); + assert.equal(lines.length, 5); + assert.equal(JSON.parse(lines[3]).turn, "owner-0123456789ad"); + // A line that does not match its shape is never written. + for (const bad of [{ ...line(), extra: 1 }, line({ at: "yesterday" }), line({ input: -1 }), line({ status: "unknown" }), unknownLine({ input: 3 }), line({ turn: "a b" }), line({ run: "" })]) { + assert.throws(() => appendUsage(file, bad), (e) => e.code === "triage-usage-unreadable", JSON.stringify(bad)); + } + assert.equal(readFileSync(file, "utf8").split("\n").length, 5); +}); + +test("a narrow umask still leaves a new usage file at 0600", () => { + const file = fileIn(); + const old = process.umask(0o277); + try { appendUsage(file, line()); } finally { process.umask(old); } + assert.equal(lstatSync(file).mode & 0o777, 0o600); +}); + +// Each unsafe state refuses both the read and the append, and the append leaves the target alone. +function unsafe(make) { + const file = fileIn(); + const opts = make(file) ?? {}; + return { file, opts }; +} +test("a wrong mode, a wrong owner, a symlink, a hard link or a non-file refuses the read and the append", () => { + const cases = { + "mode 0644": (f) => { appendUsage(f, line()); chmodSync(f, 0o644); }, + "mode 0400": (f) => { appendUsage(f, line()); chmodSync(f, 0o400); }, + "owner": (f) => { appendUsage(f, line()); return { uid: process.getuid() + 1 }; }, + "symlink": (f) => { const real = join(f, "..", "real.jsonl"); writeFileSync(real, "", { mode: 0o600 }); symlinkSync(real, f); }, + "dangling symlink": (f) => { symlinkSync(join(f, "..", "absent.jsonl"), f); }, + "hard link": (f) => { appendUsage(f, line()); linkSync(f, join(f, "..", "other.jsonl")); }, + "directory": (f) => { mkdirSync(f, { mode: 0o700 }); }, + "fifo": (f) => { execFileSync("mkfifo", ["-m", "600", f]); }, + }; + for (const [label, make] of Object.entries(cases)) { + const { file, opts } = unsafe(make); + const before = lstatSync(file); + assert.throws(() => readUsage(file, opts), (e) => e.code === "triage-usage-unreadable", `read: ${label}`); + assert.throws(() => appendUsage(file, line(), opts), (e) => e.code === "triage-usage-unreadable", `append: ${label}`); + const after = lstatSync(file); + assert.deepEqual([after.size, after.mode, after.ino], [before.size, before.mode, before.ino], `untouched: ${label}`); + } + // The dangling link's target is not made. + const { file } = unsafe((f) => { symlinkSync(join(f, "..", "absent.jsonl"), f); }); + try { appendUsage(file, line()); } catch {} + assert.throws(() => lstatSync(join(file, "..", "absent.jsonl")), (e) => e.code === "ENOENT"); +}); + +test("a usage file over the size bound refuses before it is parsed", () => { + const file = fileIn(); + appendUsage(file, line()); + appendUsage(file, line({ turn: "owner-0123456789ac" })); + const size = lstatSync(file).size; + assert.equal(readUsage(file, { maxBytes: size }).records.length, 2); + assert.throws(() => readUsage(file, { maxBytes: size - 1 }), (e) => e.code === "triage-usage-unreadable"); +}); + +test("no file reads as no turns; good lines read back as records for the triage account", () => { + const file = fileIn(); + assert.deepEqual(readUsage(file), { records: [], bad: [] }); + appendUsage(file, line()); + appendUsage(file, unknownLine({ turn: "owner-0123456789ac" })); + const { records, bad } = readUsage(file); + assert.deepEqual(bad, []); + assert.deepEqual(records, [{ account: "triage", ...line() }, { account: "triage", ...unknownLine({ turn: "owner-0123456789ac" }) }]); +}); + +test("a torn last line is refused and counts as one unknown turn; a wrong-shaped line the same", () => { + const file = fileIn(); + appendUsage(file, line()); + writeFileSync(file, '{"at":"2026-11-18T13:00:00.000Z","run":"triage-2026', { flag: "a" }); + const torn = readUsage(file); + assert.equal(torn.records.length, 2); + assert.deepEqual(torn.bad.map((b) => b.line), [2]); + const [, b] = torn.records; + assert.deepEqual([b.status, b.run, b.turn, b.input, b.output, b.cacheRead, b.cacheWrite], ["unknown", null, "line-2", null, null, null, null]); + assert.ok(!Number.isNaN(Date.parse(b.at)), "a torn last line takes the file's change time"); + const [r] = report({ limits: limitsFor(cap()), records: torn.records, now: "2026-11-18T23:00:00.000Z" }); + assert.deepEqual([r.count, r.unknownTurns], [18, 1]); + // Closed off by the next append, it stays one unknown turn, dated by the line after it. + appendUsage(file, line({ at: "2026-11-18T14:00:00.000Z", turn: "owner-0123456789ac", input: 1, output: 0, cacheRead: 0, cacheWrite: 0 })); + writeFileSync(file, JSON.stringify({ ...line(), input: "9" }) + "\n", { flag: "a" }); + const later = readUsage(file); + assert.deepEqual(later.bad.map((x) => x.line), [2, 4]); + assert.equal(later.records[1].at, "2026-11-18T14:00:00.000Z"); + const [again] = report({ limits: limitsFor(cap()), records: later.records, now: "2026-11-18T23:00:00.000Z" }); + assert.deepEqual([again.count, again.unknownTurns], [19, 2]); +}); + +test("the limits copy the organization shape: one daily leaf for the triage account", () => { + assert.deepEqual(limitsFor(cap()), { tokens: { daily: { triage: cap() } } }); + const records = [{ account: "triage", ...line({ input: 100, output: 0, cacheRead: 0, cacheWrite: 0 }) }]; + assert.deepEqual(mayLaunch({ limits: limitsFor(cap()), subject: { account: "triage" }, records, now: "2026-11-18T13:00:00.000Z" }).allow, false); +}); + +test("the cap state: under, at and over the cap, unknown turns adding nothing, and the cap's since", () => { + const now = ms("2026-11-18T18:00:00.000Z"); + const recs = (...counts) => counts.map((n, i) => ({ account: "triage", ...line({ turn: `owner-00000000000${i}`, input: n, output: 0, cacheRead: 0, cacheWrite: 0 }) })); + const under = capState({ cap: cap(), records: recs(40, 59), now }); + assert.equal(under.allow, true); + assert.deepEqual(under.usageToday, { count: 99, input: 99, output: 0, cacheRead: 0, cacheWrite: 0, unknownTurns: 0 }); + assert.equal(under.resetsAt, "2026-11-19T00:00:00.000Z"); + const at = capState({ cap: cap(), records: recs(40, 60), now }); + assert.equal(at.allow, false); + assert.equal(at.usageToday.count, 100); + const withUnknown = capState({ cap: cap(), records: [...recs(40), { account: "triage", ...unknownLine({ turn: "owner-00000000000f" }) }], now }); + assert.deepEqual([withUnknown.allow, withUnknown.usageToday.count, withUnknown.usageToday.unknownTurns], [true, 40, 1]); + // Lines before the cap's since or before today's reset do not count. + const old = [{ account: "triage", ...line({ at: "2026-11-17T23:59:59.999Z", input: 500, output: 0, cacheRead: 0, cacheWrite: 0 }) }]; + assert.equal(capState({ cap: cap(), records: old, now }).usageToday.count, 0); + const since = [{ account: "triage", ...line({ at: "2026-11-18T10:00:00.000Z", input: 500, output: 0, cacheRead: 0, cacheWrite: 0 }) }]; + assert.equal(capState({ cap: cap({ since: "2026-11-18T11:00:00.000Z" }), records: since, now }).allow, true); + // A cap of 0 takes no turn, as the plugin's own rule says. + assert.equal(capState({ cap: cap({ limit: 0 }), records: [], now }).allow, false); +}); + +test("the reset is 00:00 in the stored zone: the next one is the time shown and the day turns over there", () => { + const z = (timeZone) => cap({ timeZone }); + assert.equal(new Date(nextReset(z("UTC"), ms("2026-11-18T18:00:00.000Z"))).toISOString(), "2026-11-19T00:00:00.000Z"); + assert.equal(new Date(nextReset(z("UTC"), ms("2026-11-18T00:00:00.000Z"))).toISOString(), "2026-11-19T00:00:00.000Z", "at the reset, the next is a day on"); + assert.equal(new Date(nextReset(z("UTC"), ms("2026-11-18T23:59:59.999Z"))).toISOString(), "2026-11-19T00:00:00.000Z"); + assert.equal(new Date(nextReset(z("Asia/Kolkata"), ms("2026-11-18T18:00:00.000Z"))).toISOString(), "2026-11-18T18:30:00.000Z"); + // A since after now: the reset after it. + assert.equal(new Date(nextReset(cap({ since: "2026-11-19T06:00:00.000Z" }), ms("2026-11-18T18:00:00.000Z"))).toISOString(), "2026-11-20T00:00:00.000Z"); +}); + +test("a DST day: America/Chicago falls back on 2026-11-01 (25 hours) and springs forward on 2026-03-08 (23 hours)", () => { + const chicago = cap({ timeZone: "America/Chicago", since: "2026-01-01T00:00:00.000Z", limit: 50 }); + // Fall back: the day runs from 05:00Z (00:00 CDT) to 06:00Z next day (00:00 CST). + assert.equal(new Date(nextReset(chicago, ms("2026-11-01T12:00:00.000Z"))).toISOString(), "2026-11-02T06:00:00.000Z"); + assert.equal(new Date(nextReset(chicago, ms("2026-11-01T05:30:00.000Z"))).toISOString(), "2026-11-02T06:00:00.000Z", "inside the repeated hour's day"); + const fall = [ + { account: "triage", ...line({ at: "2026-11-01T04:59:59.999Z", turn: "owner-000000000001", input: 30, output: 0, cacheRead: 0, cacheWrite: 0 }) }, + { account: "triage", ...line({ at: "2026-11-01T05:00:00.000Z", turn: "owner-000000000002", input: 30, output: 0, cacheRead: 0, cacheWrite: 0 }) }, + { account: "triage", ...line({ at: "2026-11-02T05:30:00.000Z", turn: "owner-000000000003", input: 30, output: 0, cacheRead: 0, cacheWrite: 0 }) }, + ]; + const lateFall = capState({ cap: chicago, records: fall, now: ms("2026-11-02T05:59:59.999Z") }); + assert.deepEqual([lateFall.allow, lateFall.usageToday.count, lateFall.resetsAt], [false, 60, "2026-11-02T06:00:00.000Z"], "the 25th hour still counts the day"); + const nextDay = capState({ cap: chicago, records: fall, now: ms("2026-11-02T06:00:00.000Z") }); + assert.deepEqual([nextDay.allow, nextDay.usageToday.count, nextDay.resetsAt], [true, 0, "2026-11-03T06:00:00.000Z"]); + // Spring forward: the day runs from 06:00Z (00:00 CST) to 05:00Z next day (00:00 CDT). + assert.equal(new Date(nextReset(chicago, ms("2026-03-08T12:00:00.000Z"))).toISOString(), "2026-03-09T05:00:00.000Z"); + const spring = [{ account: "triage", ...line({ at: "2026-03-08T06:00:00.000Z", input: 60, output: 0, cacheRead: 0, cacheWrite: 0 }) }]; + assert.equal(capState({ cap: chicago, records: spring, now: ms("2026-03-09T04:59:59.999Z") }).allow, false); + assert.equal(capState({ cap: chicago, records: spring, now: ms("2026-03-09T05:00:00.000Z") }).allow, true); +}); + +test("the turn delta keeps the bus's rules: new entries summed, a rewritten or missing baseline unknown, no new usage unknown", () => { + const e = (input) => ({ input, output: 1, cacheWrite: 0, cacheRead: 0 }); + const begin = snapshot({ status: "known", entries: { a: e(5) } }); + assert.deepEqual(delta(begin, snapshot({ status: "known", entries: { a: e(5), b: e(7), c: e(1) } })), { status: "known", input: 8, output: 2, cacheWrite: 0, cacheRead: 0 }); + assert.deepEqual(delta(begin, snapshot({ status: "known", entries: { a: e(5) } })), { status: "unknown", code: "no-new-usage" }); + assert.deepEqual(delta(begin, snapshot({ status: "known", entries: { a: e(6), b: e(1) } })), { status: "unknown", code: "transcript-rewritten" }); + assert.deepEqual(delta(begin, snapshot({ status: "known", entries: { b: e(1) } })), { status: "unknown", code: "transcript-rewritten" }); + assert.deepEqual(delta(null, snapshot({ status: "known", entries: { b: e(1) } })), { status: "unknown", code: "baseline-unknown" }); + assert.deepEqual(delta(snapshot({ status: "unknown", code: "transcript-size" }), snapshot({ status: "known", entries: {} })), { status: "unknown", code: "baseline-unknown" }); + assert.deepEqual(delta(begin, snapshot({ status: "unknown", code: "transcript-parse" })), { status: "unknown", code: "transcript-parse" }); + assert.deepEqual(snapshot({ status: "unknown", code: "anything" }), { status: "unknown", code: "source-unknown" }); + assert.deepEqual(snapshot({ status: "known", entries: { a: { input: -1, output: 0, cacheWrite: 0, cacheRead: 0 } } }), { status: "unknown", code: "source-invalid" }); + assert.deepEqual(snapshot({ status: "known", entries: { a: { ...e(1), extra: 0 } } }), { status: "unknown", code: "source-invalid" }); + assert.deepEqual(snapshot(null), { status: "unknown", code: "source-invalid" }); + const big = { status: "known", entries: { a: e(Number.MAX_SAFE_INTEGER), b: e(1) } }; + assert.deepEqual(delta(snapshot({ status: "known", entries: {} }), snapshot(big)), { status: "unknown", code: "usage-overflow" }); +});