<dataRoot>/triage/route.json holds Triage's model route and daily cap as names only: a catalogue model key, a harness name and a daily-quota cap leaf, with no env value and no key. The directory is 0700 and the file 0600, owned by the running user; a link, a hard link, a non-file, a wrong mode or owner, an unknown field or an unknown time zone refuses. A write is compare-and-set on revision under an exclusive lock, atomic (temp file, fsync, rename, directory fsync), and appends one receipt line. The reset is 00:00 in a zone the owner gives, with no default. The file location, field names and reset rule each live in one place. mosaic triage route show|set are human verbs and refuse an agent run before any read. Nothing is wired into the Triage host: the Console keeps route: null. Held for Jason's ruling on the design's four questions (#1873, lane triage-route-store).
102 lines
4.7 KiB
JavaScript
102 lines
4.7 KiB
JavaScript
// #1873 triage-route-store: the owner's terminal verbs for the Triage route file,
|
|
// <dataRoot>/triage/route.json (packages/webui/src/triage-route-store.mjs).
|
|
//
|
|
// mosaic triage route show
|
|
// mosaic triage route set --model KEY --harness NAME --cap TOKENS --time-zone ZONE [--revision N]
|
|
//
|
|
// Both are human verbs: an agent run is refused before anything is read (exit 3), from this
|
|
// process's environment and the one the command was given, as the bus and Console lifecycle verbs
|
|
// refuse it. `show` with no file prints "not set" and exits 0. Without --revision a write
|
|
// compares with the revision read under the store's lock; pass --revision from `show` to make the
|
|
// write conditional on what was shown (0: only when no route is set yet).
|
|
//
|
|
// Nothing here wires the route into the Triage host: the Console keeps `route: null` until the
|
|
// wiring slice. `set` checks the harness against the repository's harness modules; it does not
|
|
// check the model key against a catalogue, since none exists outside an organization's bus yet.
|
|
import { lstatSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { readRoute, RouteStoreError, writeRoute } from "../../webui/src/triage-route-store.mjs";
|
|
import { loadSystem } from "./config.mjs";
|
|
import { CliError } from "./errors.mjs";
|
|
import { refuseInsideAgent } from "./transport.mjs";
|
|
|
|
export const TRIAGE_USAGE = "mosaic triage route show | set --model KEY --harness NAME --cap TOKENS --time-zone ZONE [--revision N]";
|
|
const HARNESS_ROOT = fileURLToPath(new URL("../../plugins/harness/", import.meta.url));
|
|
const VALUES = ["--model", "--harness", "--cap", "--time-zone", "--revision"];
|
|
const WHOLE = /^(0|[1-9][0-9]*)$/;
|
|
// The actor every terminal write records: the instance owner, the only person who runs this verb.
|
|
const ACTOR = "owner";
|
|
|
|
const usage = (why) => new CliError(`${why}\nusage: ${TRIAGE_USAGE}`, 4);
|
|
|
|
// Option names, never values, in a refusal.
|
|
function parse(args) {
|
|
const values = {}, positional = [];
|
|
for (let i = 0; i < args.length; i++) {
|
|
const a = args[i];
|
|
if (VALUES.includes(a)) {
|
|
if (i + 1 >= args.length) throw usage(`${a} needs one value`);
|
|
if (Object.hasOwn(values, a)) throw usage(`${a} is given twice`);
|
|
values[a] = args[++i];
|
|
} else if (a.startsWith("--")) throw usage("unknown option");
|
|
else positional.push(a);
|
|
}
|
|
return { values, positional };
|
|
}
|
|
|
|
function whole(text, name) {
|
|
if (!WHOLE.test(text)) throw new CliError(`triage-route-invalid: ${name} must be a whole number`, 2);
|
|
return Number(text);
|
|
}
|
|
|
|
function knownHarness(name) {
|
|
if (!/^[a-z][a-z0-9-]{0,31}$/.test(name)) return false;
|
|
try { return lstatSync(join(HARNESS_ROOT, name, "index.mjs")).isFile(); } catch { return false; }
|
|
}
|
|
|
|
function storeError(error) {
|
|
if (!(error instanceof RouteStoreError)) throw error;
|
|
return new CliError(`${error.code}: ${error.message}`, error.code === "triage-route-invalid" ? 2 : 3);
|
|
}
|
|
|
|
function show(route) {
|
|
if (!route) return "triage route: not set\n";
|
|
const { cap } = route;
|
|
return [
|
|
`triage route: revision ${route.revision}`,
|
|
`model: ${route.model}`,
|
|
`harness: ${route.harness}`,
|
|
`cap: ${cap.limit} tokens a day, resets ${cap.reset} ${cap.timeZone}, counted since ${cap.since}`,
|
|
`updated: ${route.updatedAt} by ${route.updatedBy}`,
|
|
"",
|
|
].join("\n");
|
|
}
|
|
|
|
export function triageCommand(rest, io, deps = {}) {
|
|
refuseInsideAgent(process.env);
|
|
refuseInsideAgent(io.env);
|
|
const [noun, sub, ...args] = rest;
|
|
if (noun !== "route" || !["show", "set"].includes(sub)) throw usage("unknown triage command");
|
|
const { values, positional } = parse(args);
|
|
if (positional.length) throw usage("unexpected argument");
|
|
const dataRoot = () => (deps.system ?? loadSystem({ env: io.env })).dataRoot;
|
|
if (sub === "show") {
|
|
if (Object.keys(values).length) throw usage("show takes no options");
|
|
let route;
|
|
try { route = readRoute(dataRoot()); } catch (e) { throw storeError(e); }
|
|
io.stdout.write(show(route));
|
|
return;
|
|
}
|
|
for (const name of ["--model", "--harness", "--cap", "--time-zone"]) if (!Object.hasOwn(values, name)) throw usage(`set needs ${name}`);
|
|
if (!knownHarness(values["--harness"])) throw new CliError("triage-route-invalid: no harness module has that name", 2);
|
|
const input = {
|
|
model: values["--model"], harness: values["--harness"], limit: whole(values["--cap"], "--cap"),
|
|
timeZone: values["--time-zone"], by: ACTOR,
|
|
...(Object.hasOwn(values, "--revision") ? { revision: whole(values["--revision"], "--revision") } : {}),
|
|
};
|
|
let route;
|
|
try { route = writeRoute(dataRoot(), input, deps.now ? { now: deps.now } : {}); } catch (e) { throw storeError(e); }
|
|
io.stdout.write(`triage route set: revision ${route.revision}\n`);
|
|
}
|