fix(triage): review round: the host writes claude-code's bundle and sets its MOSAIC_ names, so a real harness answers; pi is refused as incompatible; proxy.json is read at each admit; a persistent marker makes the start after an unknown end fresh across a restart; the session key and key-shaped strings are removed from Triage lines (#1873, lane triage-wiring)
This commit is contained in:
1 parent
255c1575bb
commit
c28ca57af6
6 files changed
+361
-45
No files matched your search
@@ -36,6 +36,7 @@ import { reviveBuses } from './bus-revive.mjs';
|
||||
import { createProxyAdmin, orgCatalogue } from './proxy-admin.mjs';
|
||||
import { createTriageHost } from './triage-host.mjs';
|
||||
import { createTriageRoute, triageTokenPattern } from './triage-route.mjs';
|
||||
import { loadProxy } from '../../model-proxy/src/settings.mjs';
|
||||
import { composeSnapshot, readSystemConfig, readOrgConfig } from './triage-snapshot.mjs';
|
||||
import { busesStopped } from '../../cli/src/proxy-admin-host.mjs';
|
||||
import { agentLevelsWriteRoutes } from './agent-levels-write.mjs';
|
||||
@@ -237,7 +238,10 @@ try {
|
||||
// #1873 triage-wiring: the route is resolved at each admit from the route store, the key file, each
|
||||
// active organization's model catalogue (its bus `routes` verb, as proxy setup reads it) and the
|
||||
// harness module (triage-route.mjs). The host's fences hold the proxy port and the key's shape.
|
||||
const triageProxyPort = Number.isInteger(system?.proxy?.port) ? system.proxy.port : null;
|
||||
// The port is read from proxy.json at each admit, so a proxy set up after the Console started counts.
|
||||
const triageProxyPort = () => {
|
||||
try { const p = loadProxy(configDir(), system.dataRoot)?.port; return Number.isInteger(p) ? p : null; } catch { return null; }
|
||||
};
|
||||
const triageRoute = system && (devOwner || ssoOn) ? createTriageRoute({
|
||||
dataRoot: system.dataRoot, configDir: configDir(), proxyPort: triageProxyPort,
|
||||
catalogues: async () => {
|
||||
|
||||
@@ -33,6 +33,17 @@
|
||||
// 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-wiring: the host sets the harness's MOSAIC_ names itself. At each start it calls the
|
||||
// module's bundle() into the run's bundle/ (Claude Code: settings.json with the tool gate hook and
|
||||
// mcp.json with the typed tools' server) and its env() with no proxy, and keeps the MOSAIC_ names the
|
||||
// module declares, with clean string values, never a name the runner sets. Every other name is the
|
||||
// route's, under the fence above, so no name has two sources. The thinking
|
||||
// level is the module's "low", or its first level. These join the route's fenced harnessEnv in
|
||||
// session.json. The key is never among them: env() gets no proxy, and a value that holds the key
|
||||
// refuses the start. MOSAIC_CLAUDE_BIN, the pinned binary's absolute path, passes from the
|
||||
// Console's environment as it does for a bus session. An answer the agent sends has the key, and
|
||||
// anything shaped like it, replaced before it reaches the transcript.
|
||||
//
|
||||
// #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 <dataRoot>/triage/usage.jsonl. When a cap is
|
||||
@@ -45,8 +56,8 @@
|
||||
// is refused for usage and the lines are still written.
|
||||
|
||||
import { spawn } from "node:child_process";
|
||||
import { randomBytes, timingSafeEqual } from "node:crypto";
|
||||
import { appendFileSync, closeSync, lstatSync, mkdirSync, openSync, readFileSync, rmSync, chmodSync, writeFileSync } from "node:fs";
|
||||
import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
|
||||
import { appendFileSync, closeSync, existsSync, lstatSync, mkdirSync, openSync, readFileSync, rmSync, chmodSync, writeFileSync } from "node:fs";
|
||||
import { createServer } from "node:net";
|
||||
import { homedir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
@@ -107,6 +118,16 @@ const CAP = /^[0-9a-f]{64}$/;
|
||||
// The spawn's environment: these names from the Console's own environment, PATH fixed, and the
|
||||
// route's proxy variables. bwrap sets HOME and TMPDIR inside.
|
||||
export const SPAWN_ENV_ALLOW = Object.freeze(["PATH", "LANG", "LC_ALL", "TZ", "TERM"]);
|
||||
// Passed only as an absolute path: the pinned harness binary (packages/seat/src/session.mjs ENV_ALLOW).
|
||||
export const SPAWN_PATH_ALLOW = Object.freeze(["MOSAIC_CLAUDE_BIN"]);
|
||||
// The names the runner sets for every harness (runner.mjs adapterEnv); the host's own names never replace them.
|
||||
const RUNNER_NAMES = new Set(["MOSAIC_SYSTEM_PROMPT_FILE", "MOSAIC_WORKSPACE", "MOSAIC_SESSION_DIR", "MOSAIC_POLICY_FILE", "MOSAIC_TOOLS_FILE",
|
||||
"MOSAIC_TOOL_SOCKET", "MOSAIC_TURN_MARKER", "MOSAIC_PROVIDER", "MOSAIC_MODEL", "MOSAIC_MODEL_ACTIVITY_DIR", "MOSAIC_UNSLOP_LOG",
|
||||
"MOSAIC_MODEL_EVENT_MONITOR", "MOSAIC_FIRST_OUTPUT_DIR", "MOSAIC_REQUEST"]);
|
||||
// What a key the agent sends becomes in the transcript.
|
||||
export const KEY_REMOVED = "[key removed]";
|
||||
// The marker, under host/ (never mounted), that the next start gets a fresh session/.
|
||||
const FRESH_MARKER = "fresh-session";
|
||||
export const ROUTE_ENV_ALLOW = Object.freeze(["ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN"]);
|
||||
const ENV_NAME = /^[A-Z][A-Z0-9_]{0,63}$/;
|
||||
const ENV_VALUE_MAX = 4096;
|
||||
@@ -143,6 +164,7 @@ async function repoHarnessModule(name) {
|
||||
export function triageEnv(source, routeEnv = {}, { node = process.execPath } = {}) {
|
||||
const env = { PATH: `${dirname(node)}:/usr/bin:/bin` };
|
||||
for (const k of SPAWN_ENV_ALLOW) if (k !== "PATH" && typeof source?.[k] === "string" && source[k].length <= 256) env[k] = source[k];
|
||||
for (const k of SPAWN_PATH_ALLOW) if (typeof source?.[k] === "string" && source[k].startsWith("/") && source[k].length <= 256 && cleanValue(source[k])) env[k] = source[k];
|
||||
for (const k of ROUTE_ENV_ALLOW) if (typeof routeEnv?.[k] === "string" && routeEnv[k].length <= 4096) env[k] = routeEnv[k];
|
||||
return env;
|
||||
}
|
||||
@@ -278,7 +300,8 @@ export function createRelay({ socket, cap, take, read, onSend, usage = null, sto
|
||||
// provider, env } | { ok: false, code, until? } } or null for no route
|
||||
// readSnapshot async () -> the snapshot (triage-snapshot.mjs composeSnapshot), written to health/
|
||||
// pollInterval the runner's receive interval in ms, for tests; the runner's default otherwise
|
||||
// proxyPort the port ANTHROPIC_BASE_URL must name, or null for any port
|
||||
// proxyPort the port ANTHROPIC_BASE_URL must name, or null for any port; or a function that
|
||||
// answers it, called at each admit (null from it refuses: no proxy is configured)
|
||||
// tokenPattern a RegExp the whole ANTHROPIC_AUTH_TOKEN must match, or null for no shape check
|
||||
// harnessModule async (name) -> the harness module (envNames, installRoots) or null; the
|
||||
// repository's packages/plugins/harness/<name> by default
|
||||
@@ -286,11 +309,13 @@ export function createRelay({ socket, cap, take, read, onSend, usage = null, sto
|
||||
// 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");
|
||||
const validPort = (p) => Number.isInteger(p) && p > 0 && p <= 65535;
|
||||
if (proxyPort !== null && typeof proxyPort !== "function" && !validPort(proxyPort)) throw new TypeError("proxyPort must be a port number, null or a function");
|
||||
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, ""));
|
||||
const tokenAnywhere = tokenPattern && new RegExp(tokenPattern.source, tokenPattern.flags.replace(/[gmy]/g, "") + "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).
|
||||
const protectedPaths = [dataRoot, systemConfigDir(env), home];
|
||||
@@ -310,7 +335,9 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
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
|
||||
let freshSession = false; // the last session ended on unknown usage: the next start gets a new session/
|
||||
// The last session ended on unknown usage: the next start gets a new session/. Kept as a file under
|
||||
// host/ too, so a Console restart between the end and the next message keeps it.
|
||||
let freshSession = false;
|
||||
const badSeen = new Set();
|
||||
|
||||
const ensureDirs = () => {
|
||||
@@ -356,7 +383,36 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
return last;
|
||||
})().finally(() => { refreshing = null; });
|
||||
|
||||
const writeBundle = (admitted) => {
|
||||
// #1873 triage-wiring: the module's bundle files and its MOSAIC_ names, which the host sets itself.
|
||||
const hostHarnessEnv = (mod, { admitted, bundleDir, files, policy, tools, toolSocket }) => {
|
||||
if (typeof mod?.bundle !== "function" || typeof mod?.env !== "function") return {};
|
||||
const write = (name, text) => {
|
||||
if (typeof name !== "string" || !/^[a-z][a-z0-9.-]{0,63}$/.test(name) || typeof text !== "string") throw new Error("bundle file refused");
|
||||
writeFileSync(join(bundleDir, name), text, { mode: 0o600, flag: "wx" });
|
||||
return [name, createHash("sha256").update(text).digest("hex")];
|
||||
};
|
||||
const quote = (p) => {
|
||||
if (typeof p !== "string" || p.includes("'")) throw new Error("path can't hold a single quote");
|
||||
return `'${p}'`;
|
||||
};
|
||||
let levels = [];
|
||||
try { levels = typeof mod.levels === "function" ? mod.levels() : []; } catch { levels = []; }
|
||||
const thinking = Array.isArray(levels) && levels.includes("low") ? "low" : Array.isArray(levels) && typeof levels[0] === "string" ? levels[0] : "";
|
||||
const session = { model: admitted.model, provider: admitted.provider ?? "", thinking, toolSocket };
|
||||
const own = mod.bundle({ dir: bundleDir, files: { ...files }, session: { ...session }, write, quote, models: {}, proxy: null, transports: {} });
|
||||
const given = mod.env({ files: { ...(plainObject(own?.files) ? own.files : {}) }, policy, tools, session: { ...session }, models: {}, proxy: null });
|
||||
if (!plainObject(given)) throw new Error("env refused");
|
||||
const key = admitted.env?.ANTHROPIC_AUTH_TOKEN;
|
||||
const out = {};
|
||||
for (const [k, v] of Object.entries(given)) {
|
||||
if (!k.startsWith("MOSAIC_") || !mod.envNames.includes(k) || !ENV_NAME.test(k) || RUNNER_NAMES.has(k) || !cleanValue(v)) continue;
|
||||
if ((typeof key === "string" && key && v.includes(key)) || (tokenAnywhere && v.match(tokenAnywhere))) throw new Error("env holds the key");
|
||||
out[k] = v;
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
const writeBundle = (admitted, mod) => {
|
||||
const a = paths.agent;
|
||||
for (const d of ["bundle", "tmp", "work", "home"]) freshDir(join(a, d));
|
||||
mkdirSync(join(a, "work", "health"), { mode: 0o700 });
|
||||
@@ -366,7 +422,8 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
// The session's history stays across starts, unless the session put something other than a
|
||||
// real directory there, or the last session ended because its usage could not be read: then the
|
||||
// next one starts with a fresh session and transcript, so its turns count from a known baseline.
|
||||
if (freshSession) { freshDir(join(a, "session")); freshSession = false; }
|
||||
const marker = join(paths.host, FRESH_MARKER);
|
||||
if (freshSession || existsSync(marker)) { freshDir(join(a, "session")); freshSession = false; rmSync(marker, { force: true }); }
|
||||
else { try { realDir(join(a, "session")); } catch { freshDir(join(a, "session")); } }
|
||||
const resolved = { limits: { authority: role.authority, tools: role.tools }, definition: "triage", pluginTools: [] };
|
||||
const tools = typedTools({ resolved }).filter((t) => t.name === "send_message");
|
||||
@@ -377,9 +434,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" });
|
||||
let hostEnv;
|
||||
try { hostEnv = hostHarnessEnv(mod, { admitted, bundleDir: join(a, "bundle"), files: bundle, policy, tools, toolSocket: join(a, "tools.sock") }); }
|
||||
catch {
|
||||
log("triage host: route refused: harness-bundle");
|
||||
rmSync(join(a, "bundle"), { recursive: true, force: true });
|
||||
throw new TriageError("triage-route-invalid", "the model route's answer is not usable");
|
||||
}
|
||||
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,
|
||||
bundle, adapter: admitted.adapter, harness: admitted.harness, harnessEnv: { ...admitted.harnessEnv, ...hostEnv }, 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,
|
||||
model: admitted.model, provider: admitted.provider ?? "",
|
||||
@@ -412,7 +476,12 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
const re = a.env ?? {};
|
||||
if (!plainObject(re) || Object.entries(re).some(([k, v]) => !ROUTE_ENV_ALLOW.includes(k) || !cleanValue(v))) refuse("route-env");
|
||||
const url = LOOPBACK_URL.exec(re.ANTHROPIC_BASE_URL ?? "");
|
||||
if (!url || Number(url[1]) > 65535 || (proxyPort !== null && Number(url[1]) !== proxyPort)) refuse("route-env");
|
||||
let port = proxyPort;
|
||||
if (typeof proxyPort === "function") {
|
||||
try { port = await proxyPort(); } catch { port = undefined; }
|
||||
if (!validPort(port)) refuse("route-env");
|
||||
}
|
||||
if (!url || Number(url[1]) > 65535 || (port !== null && Number(url[1]) !== port)) refuse("route-env");
|
||||
if (tokenShape && !(typeof re.ANTHROPIC_AUTH_TOKEN === "string" && tokenShape.test(re.ANTHROPIC_AUTH_TOKEN))) refuse("route-env");
|
||||
let installRoots;
|
||||
try { installRoots = structuredClone(mod.installRoots({ env })); } catch { refuse("module"); }
|
||||
@@ -429,7 +498,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
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 };
|
||||
return { admitted: a, installRoots, transcriptRoot, mod };
|
||||
};
|
||||
|
||||
// #1873 triage-cap. A baseline or a reading of the transcript, with the bus's codes.
|
||||
@@ -472,6 +541,8 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
const endUnknown = (u) => {
|
||||
u.halted = true;
|
||||
freshSession = true;
|
||||
try { writeFileSync(join(paths.host, FRESH_MARKER), "", { mode: 0o600 }); }
|
||||
catch { log("triage host: fresh-session marker not written"); }
|
||||
log("triage host: ending the session: two turns in a row with unknown usage");
|
||||
try { append({ at: now().toISOString(), from: "host", text: USAGE_UNKNOWN_ENDED }); }
|
||||
catch { log("triage host: transcript write failed"); }
|
||||
@@ -543,9 +614,16 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
return true;
|
||||
};
|
||||
|
||||
const start = async ({ admitted, installRoots, transcriptRoot }) => {
|
||||
const start = async ({ admitted, installRoots, transcriptRoot, mod }) => {
|
||||
ensureDirs();
|
||||
const run = writeBundle(admitted);
|
||||
const run = writeBundle(admitted, mod);
|
||||
// The key this session holds, and anything shaped like a key, never reaches the transcript.
|
||||
const key = typeof admitted.env?.ANTHROPIC_AUTH_TOKEN === "string" && admitted.env.ANTHROPIC_AUTH_TOKEN ? admitted.env.ANTHROPIC_AUTH_TOKEN : null;
|
||||
const scrub = (body) => {
|
||||
let t = typeof body === "string" ? body : "";
|
||||
if (key) t = t.split(key).join(KEY_REMOVED);
|
||||
return tokenAnywhere ? t.replace(tokenAnywhere, KEY_REMOVED) : t;
|
||||
};
|
||||
const u = { run, root: transcriptRoot, harness: admitted.harness, baselines: new Map(), ended: new Set(), inTurn: false, unknownInRow: 0, halted: false, child: null };
|
||||
usage = u;
|
||||
cap = randomBytes(32).toString("hex");
|
||||
@@ -566,7 +644,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
// a session that ends after a progress note still gets the "unanswered" line. The model's tool
|
||||
// can also pick class RESULT; such a note settles the message as the runner's answer would.
|
||||
onSend: ({ body, in_reply_to, result }) => {
|
||||
const { text, cards, dropped } = extractCards(body, orgIds(), { devOwner: devOwner === true });
|
||||
const { text, cards, dropped } = extractCards(scrub(body), orgIds(), { devOwner: devOwner === true });
|
||||
append({ at: now().toISOString(), from: "triage", text, cards, dropped, ...(in_reply_to ? { in_reply_to } : {}) });
|
||||
const i = in_reply_to && result === true ? queue.findIndex((m) => m.id === in_reply_to) : -1;
|
||||
if (i >= 0) queue.splice(i, 1);
|
||||
|
||||
@@ -7,10 +7,16 @@
|
||||
// `routes` verb and merged (mergeCatalogues), then held to checkRoutes, the
|
||||
// check an organization's route change runs: offered, available, compatible
|
||||
// the harness module adapter, turnEnd, mounts and harnessEnv (packages/plugins/harness/<name>)
|
||||
// proxy.json the proxy's port, read at each admit (proxyPort may be a function)
|
||||
// The answer's env holds only ANTHROPIC_BASE_URL (the instance proxy on 127.0.0.1) and
|
||||
// ANTHROPIC_AUTH_TOKEN (the Triage key). harnessEnv holds only the module's own names that the host's
|
||||
// fence takes (refusedHarnessName): no MOSAIC_* name, which the host sets itself. The host runs its
|
||||
// fences on every answer (triage-host.mjs checkAdmitted), so nothing here is trusted on its own.
|
||||
// fence takes (refusedHarnessName): no MOSAIC_* name, which the host sets itself from the module's
|
||||
// bundle() and env() (triage-host.mjs). A harness runs Triage only when it has bundle() and env() and
|
||||
// takes the proxy through exactly those two variables: env() given a stand-in proxy must answer the
|
||||
// stand-in's URL as ANTHROPIC_BASE_URL and its key as ANTHROPIC_AUTH_TOKEN, and put that key nowhere
|
||||
// else. pi takes its proxy through its own models.json and ANTHROPIC_API_KEY, so it is refused with
|
||||
// triage-harness-incompatible. The host runs its fences on every answer (triage-host.mjs
|
||||
// checkAdmitted), so nothing here is trusted on its own.
|
||||
//
|
||||
// Each refusal is a code from ROUTE_REFUSALS (triage-host.mjs) with a fixed plain line. No code or line carries a path,
|
||||
// a key or an env value. Whether the key is in the proxy's list cannot be known without the proxy
|
||||
@@ -33,6 +39,13 @@ const MODEL_KEY = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
||||
export const PROBE_TIMEOUT_MS = 1000;
|
||||
|
||||
const refuse = (code) => ({ ok: false, code });
|
||||
// The stand-in proxy env() is asked about. Neither value is a real URL or key.
|
||||
const PROXY_PROBE = Object.freeze({ baseUrl: "http://127.0.0.1:9", key: "triage-route-probe-key" });
|
||||
// True when env(), given the stand-in proxy, puts it in the two route variables and nowhere else.
|
||||
const takesRouteProxy = (given) => plain(given)
|
||||
&& given.ANTHROPIC_BASE_URL === PROXY_PROBE.baseUrl && given.ANTHROPIC_AUTH_TOKEN === PROXY_PROBE.key
|
||||
&& Object.entries(given).every(([k, v]) => k === "ANTHROPIC_AUTH_TOKEN" || !String(v).includes(PROXY_PROBE.key));
|
||||
const validPort = (p) => Number.isInteger(p) && p > 0 && p <= 65535;
|
||||
const plain = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
||||
const identity = (m) => JSON.stringify([m.provider, m.model, m.module, m.upstream]);
|
||||
|
||||
@@ -125,7 +138,8 @@ export function checkModel({ model, harness, catalogue, harnesses }) {
|
||||
}
|
||||
|
||||
// createTriageRoute({ dataRoot, configDir, proxyPort, catalogues, harnessModule, probe, env, installed })
|
||||
// proxyPort the instance proxy's port (proxy.json), or null when none is configured
|
||||
// proxyPort the instance proxy's port (proxy.json), or null when none is configured; or a
|
||||
// function that answers it, called at each admit
|
||||
// catalogues async () -> [catalogue] each active organization's `routes` catalogue
|
||||
// harnessModule async (name) -> the harness module or null; the repository's by default
|
||||
// probe async (port) -> true when the proxy accepts a connection
|
||||
@@ -136,7 +150,7 @@ export function checkModel({ model, harness, catalogue, harnesses }) {
|
||||
export function createTriageRoute({ dataRoot, configDir, proxyPort = null, catalogues = async () => [], harnessModule = repoHarnessModule, probe = probePort, env = process.env, installed = installedHarnesses, keyOptions = {} }) {
|
||||
if (typeof dataRoot !== "string" || !dataRoot.startsWith("/")) throw new TypeError("dataRoot must be an absolute path");
|
||||
if (typeof configDir !== "string" || !configDir.startsWith("/")) throw new TypeError("configDir must be an absolute path");
|
||||
if (proxyPort !== null && !(Number.isInteger(proxyPort) && proxyPort > 0 && proxyPort <= 65535)) throw new TypeError("proxyPort must be a port number or null");
|
||||
if (proxyPort !== null && typeof proxyPort !== "function" && !validPort(proxyPort)) throw new TypeError("proxyPort must be a port number, null or a function");
|
||||
return {
|
||||
isSet() {
|
||||
try { return readRoute(dataRoot) !== null; } catch { return true; }
|
||||
@@ -151,10 +165,12 @@ export function createTriageRoute({ dataRoot, configDir, proxyPort = null, catal
|
||||
let record;
|
||||
try { record = readKey(configDir, keyOptions); } catch { return refuse("triage-key-unsafe"); }
|
||||
if (!record) return refuse("triage-key-missing");
|
||||
// 3. The proxy, on this machine.
|
||||
if (proxyPort === null) return refuse("triage-proxy-down");
|
||||
// 3. The proxy, on this machine, at the port proxy.json names now.
|
||||
let port = null;
|
||||
try { port = typeof proxyPort === "function" ? await proxyPort() : proxyPort; } catch { port = null; }
|
||||
if (!validPort(port)) return refuse("triage-proxy-down");
|
||||
let up = false;
|
||||
try { up = await probe(proxyPort); } catch { up = false; }
|
||||
try { up = await probe(port); } catch { up = false; }
|
||||
if (up !== true) return refuse("triage-proxy-down");
|
||||
// 4. The model: offered, available, and runnable on the harness.
|
||||
const harnesses = installed();
|
||||
@@ -167,7 +183,8 @@ export function createTriageRoute({ dataRoot, configDir, proxyPort = null, catal
|
||||
// 5. The harness module's own answers.
|
||||
let mod = null;
|
||||
try { mod = await harnessModule(stored.harness); } catch { mod = null; }
|
||||
if (!mod || typeof mod.adapter !== "function" || typeof mod.turnEnd !== "function" || !Array.isArray(mod.envNames)) return refuse("triage-harness-incompatible");
|
||||
if (!mod || typeof mod.adapter !== "function" || typeof mod.turnEnd !== "function" || !Array.isArray(mod.envNames)
|
||||
|| typeof mod.bundle !== "function" || typeof mod.env !== "function") return refuse("triage-harness-incompatible");
|
||||
let adapter, turnEnd, harnessMounts, harnessEnv;
|
||||
try {
|
||||
adapter = mod.adapter();
|
||||
@@ -175,16 +192,16 @@ export function createTriageRoute({ dataRoot, configDir, proxyPort = null, catal
|
||||
harnessMounts = typeof mod.mounts === "function" ? mod.mounts({ env }) : [];
|
||||
// The module's env() for this model, less every name the host's fence refuses (MOSAIC_*
|
||||
// among them: the host sets those) and every name the module does not declare.
|
||||
const given = typeof mod.env === "function"
|
||||
? mod.env({ files: {}, policy: { tools: [] }, tools: [], session: { model: offered.model, provider: offered.provider ?? "" }, models: {}, proxy: null })
|
||||
: {};
|
||||
const session = { model: offered.model, provider: offered.provider ?? "" };
|
||||
const given = mod.env({ files: {}, policy: { tools: [] }, tools: [], session, models: {}, proxy: null });
|
||||
harnessEnv = Object.fromEntries(Object.entries(plain(given) ? given : {})
|
||||
.filter(([k, v]) => mod.envNames.includes(k) && !refusedHarnessName(k) && typeof v === "string"));
|
||||
if (!takesRouteProxy(mod.env({ files: {}, policy: { tools: [] }, tools: [], session, models: {}, proxy: { ...PROXY_PROBE } }))) return refuse("triage-harness-incompatible");
|
||||
} catch { return refuse("triage-harness-incompatible"); }
|
||||
return {
|
||||
ok: true, harness: stored.harness, adapter, turnEnd, harnessMounts, harnessEnv,
|
||||
model: offered.model, provider: offered.provider ?? "",
|
||||
env: { ANTHROPIC_BASE_URL: `http://127.0.0.1:${proxyPort}`, ANTHROPIC_AUTH_TOKEN: record.key },
|
||||
env: { ANTHROPIC_BASE_URL: `http://127.0.0.1:${port}`, ANTHROPIC_AUTH_TOKEN: record.key },
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
// which the host tests' usage source reads as the transcript), then answer "spent <n>"
|
||||
// GATED-SPEND <n> write work/started, wait for work/gate, then spend <n> as SPEND does
|
||||
// TOKEN answer the sha256 of ANTHROPIC_AUTH_TOKEN (never the token), or "no token"
|
||||
// LEAK <s> answer the raw ANTHROPIC_AUTH_TOKEN and then <s>, as a model that prints its env would;
|
||||
// the host must replace both before the transcript (the key scrub test)
|
||||
// anything else answer "echo: <body>"
|
||||
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
@@ -81,6 +83,8 @@ if (body === "PROBE") {
|
||||
} else if (body === "TOKEN") {
|
||||
const token = process.env.ANTHROPIC_AUTH_TOKEN;
|
||||
process.stdout.write(token ? `token sha256 ${createHash("sha256").update(token).digest("hex")}` : "no token");
|
||||
} else if (body.startsWith("LEAK ")) {
|
||||
process.stdout.write(`env: ANTHROPIC_AUTH_TOKEN=${process.env.ANTHROPIC_AUTH_TOKEN ?? ""} other=${body.slice(5)}`);
|
||||
} else if (body === "SLEEP") {
|
||||
setInterval(() => {}, 1000);
|
||||
} else {
|
||||
|
||||
@@ -11,7 +11,7 @@ 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,
|
||||
NOT_STARTED, NOT_TAKEN, RELAY_REFUSED, ROUTE_ENV_ALLOW, ROUTE_REFUSED, SPAWN_ENV_ALLOW, SPAWN_PATH_ALLOW, UNANSWERED, createRelay, createTriageHost, hostPrompt,
|
||||
USAGE_NOT_READ, USAGE_UNKNOWN_ENDED, UNKNOWN_TURNS_MAX, overCapNotTaken, refusedHarnessName, triageEnv,
|
||||
} from "../src/triage-host.mjs";
|
||||
import { writeRoute } from "../src/triage-route-store.mjs";
|
||||
@@ -51,6 +51,10 @@ test("triageEnv keeps the fixed names and the route's proxy variables, and nothi
|
||||
assert.deepEqual(ROUTE_ENV_ALLOW, ["ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN"]);
|
||||
assert.equal(triageEnv(CONSOLE_ENV, { ANTHROPIC_API_KEY: "from-the-route" }).ANTHROPIC_API_KEY, undefined, "the session gets no key, only the proxy's token");
|
||||
assert.equal(triageEnv(CONSOLE_ENV).ANTHROPIC_API_KEY, undefined, "a key in the Console's environment is not the route's");
|
||||
// #1873 triage-wiring: the pinned harness binary passes as an absolute path, as a bus session's does.
|
||||
assert.deepEqual(SPAWN_PATH_ALLOW, ["MOSAIC_CLAUDE_BIN"]);
|
||||
assert.equal(triageEnv({ ...CONSOLE_ENV, MOSAIC_CLAUDE_BIN: "/opt/claude/bin/claude" }).MOSAIC_CLAUDE_BIN, "/opt/claude/bin/claude");
|
||||
for (const bad of ["claude", "bin/claude", "/opt/a\nb", "/" + "x".repeat(300)]) assert.equal(triageEnv({ ...CONSOLE_ENV, MOSAIC_CLAUDE_BIN: bad }).MOSAIC_CLAUDE_BIN, undefined, bad);
|
||||
});
|
||||
|
||||
test("the relay refuses every verb but a message to the person, and any call without the capability", async (t) => {
|
||||
@@ -417,7 +421,11 @@ test("a host whose HOME holds the repository starts the shipped pi harness's own
|
||||
const piRoute = (root) => answer(root, { harness: "pi", adapter: pi.adapter(), harnessMounts: pi.mounts({ env: {} }), harnessEnv: { PI_MODEL: "m", PI_OFFLINE: "1" } });
|
||||
const s = await admittedRoute(t, "pi under HOME", (root) => () => piRoute(root), opts);
|
||||
assert.equal(s.adapter, pi.adapter());
|
||||
assert.deepEqual(s.harnessEnv, { PI_MODEL: "m", PI_OFFLINE: "1" });
|
||||
// The route's names, and the MOSAIC_ names the host sets itself from the module's env() (#1873
|
||||
// triage-wiring); the module's PI_PROVIDER is not the host's to set.
|
||||
const own = Object.fromEntries(Object.entries(s.harnessEnv).filter(([k]) => !k.startsWith("MOSAIC_")));
|
||||
assert.deepEqual(own, { PI_MODEL: "m", PI_OFFLINE: "1" });
|
||||
assert.deepEqual(Object.keys(s.harnessEnv).filter((k) => k.startsWith("MOSAIC_")).sort(), ["MOSAIC_EXTENSIONS", "MOSAIC_THINKING", "MOSAIC_TOOLS"]);
|
||||
// The same host and module, with HOME's own directory (which holds the repository) as an extra
|
||||
// install root and mount: it is under HOME and not in the code, so it is refused.
|
||||
const wide = { ...pi, installRoots: (o) => [...pi.installRoots(o), home] };
|
||||
@@ -1291,3 +1299,27 @@ test("cap fail closed: the next message after the end starts a new run with a fr
|
||||
await runnerClient(root, dataRoot);
|
||||
assert.deepEqual(readdirSync(join(dataRoot, "triage", "agent", "session")).sort(), ["history.jsonl", "spend.jsonl"], "an ordinary start keeps the session's files");
|
||||
});
|
||||
|
||||
test("cap fail closed: a Console restart between the unknown end and the next message still starts a fresh session", { timeout: 30000 }, async (t) => {
|
||||
const root = scratch();
|
||||
const opts = { route: counting(root), bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z") };
|
||||
const first = host(t, root, opts);
|
||||
storeCap(first.dataRoot, 1000);
|
||||
await first.h.message("one");
|
||||
const c = await runnerClient(root, first.dataRoot);
|
||||
await c.call("message.receive");
|
||||
writeFileSync(join(first.dataRoot, "triage", "agent", "session", "history.jsonl"), "{}\n");
|
||||
await unknownTurn(c, "owner-00000000000a");
|
||||
await unknownTurn(c, "owner-00000000000b");
|
||||
await waitFor(() => !first.h.running);
|
||||
const marker = join(first.dataRoot, "triage", "host", "fresh-session");
|
||||
assert.equal(existsSync(marker), true, "the marker outlives the host");
|
||||
await first.h.stop();
|
||||
// A new host on the same data root: the Console restarted. Its first start wipes session/ once.
|
||||
rmSync(join(root, "stdin.json"));
|
||||
const second = host(t, root, { ...opts, now: () => new Date("2026-11-18T12:05:00.000Z") });
|
||||
await second.h.message("two");
|
||||
await runnerClient(root, second.dataRoot);
|
||||
assert.deepEqual(readdirSync(join(second.dataRoot, "triage", "agent", "session")), [], "a fresh session directory");
|
||||
assert.equal(existsSync(marker), false, "the marker is used once");
|
||||
});
|
||||
@@ -16,7 +16,7 @@ import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { Client } from "../../bus/src/client.mjs";
|
||||
import { sandboxProblem } from "../../seat/src/sandbox.mjs";
|
||||
import { ROUTE_REFUSALS, ROUTE_REFUSED, createTriageHost } from "../src/triage-host.mjs";
|
||||
import { KEY_REMOVED, ROUTE_REFUSALS, ROUTE_REFUSED, createTriageHost } from "../src/triage-host.mjs";
|
||||
import { checkModel, createTriageRoute, installedHarnesses, mergeCatalogues, triageTokenPattern } from "../src/triage-route.mjs";
|
||||
import { writeRoute } from "../src/triage-route-store.mjs";
|
||||
import { readUsage, usagePath } from "../src/triage-usage.mjs";
|
||||
@@ -103,8 +103,11 @@ const closed = (server) => new Promise((resolve) => server.close(() => resolve()
|
||||
|
||||
// The Console's wiring with its seams set for a test: the route resolves from the four sources, the
|
||||
// host runs its fences on the answer. The fake harness module serves both: the route's calls
|
||||
// (adapter, turnEnd, mounts, env) and the host's (envNames, installRoots, transcript).
|
||||
async function rig(t, { catalogues = [catalogue()], proxy = true, bwrap = null, usageSource = spendSource, module: modOver = {}, tokenDir = null, keyOptions = {} } = {}) {
|
||||
// (adapter, turnEnd, mounts, env, bundle) and the host's (envNames, installRoots, transcript, bundle,
|
||||
// env). Its env() takes a proxy through the two route variables, as the route requires, unless the
|
||||
// override sets envRaw: then env() is exactly the override's. portFn(live) builds the proxyPort
|
||||
// function both get, in place of the stand-in proxy's port.
|
||||
async function rig(t, { catalogues = [catalogue()], proxy = true, bwrap = null, usageSource = spendSource, module: modOver = {}, tokenDir = null, keyOptions = {}, portFn = null } = {}) {
|
||||
const root = scratch();
|
||||
const dataRoot = join(root, "data");
|
||||
const configDir = join(root, "cfg");
|
||||
@@ -114,18 +117,25 @@ async function rig(t, { catalogues = [catalogue()], proxy = true, bwrap = null,
|
||||
writeFileSync(adapter, `exec '${process.execPath}' '${FIXTURE}'\n`, { mode: 0o644 });
|
||||
const mod = {
|
||||
envNames: ["FAKE_OK", "MOSAIC_MODEL"], installRoots: () => [join(root, "install")], transcript: ({ runDir }) => join(runDir, "session"),
|
||||
adapter: () => adapter, turnEnd: () => "exit", mounts: () => [], env: () => ({ FAKE_OK: "1", MOSAIC_MODEL: "the host sets this" }), ...(modOver ?? {}),
|
||||
adapter: () => adapter, turnEnd: () => "exit", mounts: () => [], env: () => ({ FAKE_OK: "1", MOSAIC_MODEL: "the host sets this" }),
|
||||
bundle: () => ({ files: {} }), ...(modOver ?? {}),
|
||||
};
|
||||
const rawEnv = mod.env;
|
||||
if (typeof rawEnv === "function" && !mod.envRaw) {
|
||||
mod.env = (args) => ({ ...rawEnv(args), ...(args?.proxy ? { ANTHROPIC_BASE_URL: args.proxy.baseUrl, ANTHROPIC_AUTH_TOKEN: args.proxy.key } : {}) });
|
||||
}
|
||||
delete mod.envRaw;
|
||||
const modules = async (name) => { if (modOver === null) throw new Error("/a/module/path"); return name === "fake" ? mod : null; };
|
||||
const proxyServer = createServer((s) => s.destroy());
|
||||
let proxyPort = await listen(proxyServer);
|
||||
t.after(() => closed(proxyServer));
|
||||
if (!proxy) { await closed(proxyServer); proxyServer.listen = () => {}; }
|
||||
const portArg = portFn ? portFn(proxyPort) : proxyPort;
|
||||
const cats = { list: catalogues };
|
||||
const logs = [];
|
||||
const route = createTriageRoute({ dataRoot, configDir, proxyPort, catalogues: async () => (typeof cats.list === "function" ? cats.list() : cats.list), harnessModule: modules, installed: () => ["fake"], env: {}, keyOptions });
|
||||
const route = createTriageRoute({ dataRoot, configDir, proxyPort: portArg, catalogues: async () => (typeof cats.list === "function" ? cats.list() : cats.list), harnessModule: modules, installed: () => ["fake"], env: {}, keyOptions });
|
||||
const h = createTriageHost({
|
||||
dataRoot, route, proxyPort, tokenPattern: triageTokenPattern(tokenDir ?? configDir), harnessModule: modules, readSnapshot: async () => SNAPSHOT,
|
||||
dataRoot, route, proxyPort: portArg, tokenPattern: tokenDir === false ? null : triageTokenPattern(tokenDir ?? configDir), harnessModule: modules, readSnapshot: async () => SNAPSHOT,
|
||||
env: CONSOLE_ENV, readOnly: [FIXTURE], pollInterval: 100, usageSource, now: () => new Date(NOW), log: (l) => logs.push(l), ...(bwrap ? { bwrap } : {}),
|
||||
});
|
||||
t.after(() => h.stop());
|
||||
@@ -246,7 +256,8 @@ test("the key reaches only the spawn environment: not the launcher's argv, a fil
|
||||
assert.ok(env.split("\n").includes(`ANTHROPIC_AUTH_TOKEN=${key}`), "the spawn env holds the key");
|
||||
assert.ok(env.split("\n").includes(`ANTHROPIC_BASE_URL=http://127.0.0.1:${r.proxyPort}`));
|
||||
assert.equal(env.includes("the host sets this"), false, "a MOSAIC_ name from the module does not");
|
||||
// The module's own declared names go to the runner through session.json, less any MOSAIC_ name.
|
||||
// The module's own declared names go to the runner through session.json. MOSAIC_MODEL is a name
|
||||
// the runner sets, so neither the route nor the host's own env() call keeps it.
|
||||
const session = JSON.parse(readFileSync(join(r.dataRoot, "triage", "agent", "session.json"), "utf8"));
|
||||
assert.deepEqual(session.harnessEnv, { FAKE_OK: "1" });
|
||||
assert.equal(env.includes("from-the-console"), false);
|
||||
@@ -345,6 +356,80 @@ test("refusal: the harness cannot run the model, or its module is missing or bro
|
||||
await refusal(t, "not installed", { setup: (r) => { r.setRoute(100000, "nope"); return issue(r.configDir); }, code: "triage-harness-incompatible", opts: { catalogues: [catalogue([offer({ harnesses: ["nope"] })])] } });
|
||||
await refusal(t, "module throws", { setup, code: "triage-harness-incompatible", opts: { module: { adapter: () => { throw new Error("/secret/path"); } } } });
|
||||
await refusal(t, "module lacks turnEnd", { setup, code: "triage-harness-incompatible", opts: { module: { turnEnd: undefined } } });
|
||||
// The host writes the module's bundle and sets its MOSAIC_ names from env(), so both must exist,
|
||||
// and env() must take the proxy through the two route variables and put its key nowhere else.
|
||||
await refusal(t, "module lacks bundle", { setup, code: "triage-harness-incompatible", opts: { module: { bundle: undefined } } });
|
||||
await refusal(t, "module lacks env", { setup, code: "triage-harness-incompatible", opts: { module: { env: undefined } } });
|
||||
await refusal(t, "env ignores the proxy", { setup, code: "triage-harness-incompatible", opts: { module: { envRaw: true } } });
|
||||
const viaApiKey = ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_BASE_URL: proxy.baseUrl, ANTHROPIC_API_KEY: proxy.key } : {}) });
|
||||
await refusal(t, "env puts the key in another variable", { setup, code: "triage-harness-incompatible", opts: { module: { envRaw: true, env: viaApiKey } } });
|
||||
const twice = ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_BASE_URL: proxy.baseUrl, ANTHROPIC_AUTH_TOKEN: proxy.key, FAKE_COPY: `k=${proxy.key}` } : {}) });
|
||||
await refusal(t, "env puts the key in a second variable too", { setup, code: "triage-harness-incompatible", opts: { module: { envRaw: true, envNames: ["FAKE_OK", "FAKE_COPY"], env: twice } } });
|
||||
const otherUrl = ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_BASE_URL: "http://127.0.0.1:1", ANTHROPIC_AUTH_TOKEN: proxy.key } : {}) });
|
||||
await refusal(t, "env names another proxy URL", { setup, code: "triage-harness-incompatible", opts: { module: { envRaw: true, env: otherUrl } } });
|
||||
});
|
||||
|
||||
test("refusal: pi, which takes its proxy through its own models.json and ANTHROPIC_API_KEY, is refused as not compatible", { timeout: 30000 }, async (t) => {
|
||||
const root = scratch();
|
||||
const dataRoot = join(root, "data");
|
||||
const configDir = join(root, "cfg");
|
||||
mkdirSync(configDir, { mode: 0o700 });
|
||||
const proxyServer = createServer((s) => s.destroy());
|
||||
const proxyPort = await listen(proxyServer);
|
||||
t.after(() => closed(proxyServer));
|
||||
const route = createTriageRoute({ dataRoot, configDir, proxyPort, catalogues: async () => [catalogue([offer({ harnesses: ["claude-code", "pi"] })])], env: {} });
|
||||
mkdirSync(join(dataRoot, "triage"), { recursive: true, mode: 0o700 });
|
||||
chmodSync(dataRoot, 0o700);
|
||||
writeRoute(dataRoot, { model: "glm-5", harness: "pi", limit: 1000, timeZone: "UTC", by: "owner" });
|
||||
issue(configDir);
|
||||
assert.deepEqual(await route.admit(), { ok: false, code: "triage-harness-incompatible" });
|
||||
// The same store with claude-code is admitted, so the refusal is pi's own.
|
||||
writeRoute(dataRoot, { model: "glm-5", harness: "claude-code", limit: 1000, timeZone: "UTC", by: "owner", revision: 1 });
|
||||
assert.equal((await route.admit()).ok, true);
|
||||
});
|
||||
|
||||
test("the proxy port is read at each admit: a proxy set up after the Console started counts, and the host's fence asks the same function", { timeout: 30000 }, async (t) => {
|
||||
// No proxy configured yet: the route refuses, nothing launches.
|
||||
let port = null;
|
||||
const root0 = scratch();
|
||||
const r = await rig(t, { bwrap: spy(root0), portFn: () => () => port });
|
||||
r.setRoute();
|
||||
const key = issue(r.configDir);
|
||||
await assert.rejects(r.h.message("one"), (e) => e.code === "triage-proxy-down");
|
||||
assert.equal(existsSync(join(root0, "spawned")), false);
|
||||
// Now proxy.json names the stand-in proxy: the next message is admitted on that port.
|
||||
port = r.proxyPort;
|
||||
await r.h.message("two");
|
||||
await waitFor(() => existsSync(join(root0, "spawned")));
|
||||
assert.ok(readFileSync(join(root0, "spawn-env"), "utf8").split("\n").includes(`ANTHROPIC_BASE_URL=http://127.0.0.1:${r.proxyPort}`));
|
||||
assertNoKey(key, [["host log", r.logs.join("\n")], ...filesUnder(r.dataRoot)]);
|
||||
// The host's fence calls the function again: an answer for a port proxy.json no longer names is refused.
|
||||
const root1 = scratch();
|
||||
let calls = 0;
|
||||
const s = await rig(t, { bwrap: spy(root1), portFn: (live) => () => (++calls % 2 === 1 ? live : live + 1) });
|
||||
s.setRoute();
|
||||
issue(s.configDir);
|
||||
await assert.rejects(s.h.message("hello"), (e) => e.code === "triage-route-invalid");
|
||||
assert.equal(calls, 2, "the route and the host each read it once");
|
||||
assert.equal(existsSync(join(root1, "spawned")), false);
|
||||
// And a function that throws at the host's fence is refused too.
|
||||
const root2 = scratch();
|
||||
let n = 0;
|
||||
const u = await rig(t, { bwrap: spy(root2), portFn: (live) => () => { if (++n === 2) throw new Error("/a/proxy.json"); return live; } });
|
||||
u.setRoute();
|
||||
issue(u.configDir);
|
||||
await assert.rejects(u.h.message("hello"), (e) => e.code === "triage-route-invalid");
|
||||
assert.equal(existsSync(join(root2, "spawned")), false);
|
||||
// And one that answers no port at the host's fence.
|
||||
const root3 = scratch();
|
||||
let m = 0;
|
||||
const v = await rig(t, { bwrap: spy(root3), portFn: (live) => () => (++m === 2 ? null : live) });
|
||||
v.setRoute();
|
||||
issue(v.configDir);
|
||||
await assert.rejects(v.h.message("hello"), (e) => e.code === "triage-route-invalid");
|
||||
assert.equal(existsSync(join(root3, "spawned")), false);
|
||||
assert.throws(() => createTriageRoute({ dataRoot: "/d", configDir: "/c", proxyPort: "7000" }), /proxyPort/);
|
||||
assert.throws(() => createTriageHost({ dataRoot: "/d", proxyPort: 0 }), /proxyPort/);
|
||||
});
|
||||
|
||||
test("refusal: a key the proxy does not hold is not knowable without the management client, so it is not checked here", () => {
|
||||
@@ -380,6 +465,35 @@ test("fence: a resolved route that fails the host's fences launches nothing and
|
||||
issue(u.configDir);
|
||||
await assert.rejects(u.h.message("hello"), (e) => e.code === "triage-route-invalid");
|
||||
assert.equal(existsSync(join(root2, "spawned")), false);
|
||||
// A module whose env(), with no proxy, answers a MOSAIC_ value holding a key: the route drops the
|
||||
// name (the host sets MOSAIC_ names), and the host's own call refuses the start and removes the bundle.
|
||||
const root3 = scratch();
|
||||
let leaked = null;
|
||||
const v = await rig(t, { bwrap: spy(root3), module: { envNames: ["FAKE_OK", "MOSAIC_LEAK"], env: () => ({ FAKE_OK: "1", MOSAIC_LEAK: leaked }) } });
|
||||
v.setRoute();
|
||||
leaked = keyPrefix(v.configDir) + randomBytes(32).toString("base64url");
|
||||
issue(v.configDir);
|
||||
await assert.rejects(v.h.message("hello"), (e) => e.code === "triage-route-invalid");
|
||||
assert.ok(v.logs.includes("triage host: route refused: harness-bundle"), v.logs.join(" | "));
|
||||
assert.equal(existsSync(join(root3, "spawned")), false);
|
||||
assert.equal(existsSync(join(v.dataRoot, "triage", "agent", "bundle")), false, "what the start wrote is removed");
|
||||
// The session's own key in a MOSAIC_ value, with no shape check to find it.
|
||||
const root4 = scratch();
|
||||
const w = await rig(t, { bwrap: spy(root4), tokenDir: false, module: { envNames: ["FAKE_OK", "MOSAIC_LEAK"], env: () => ({ FAKE_OK: "1", MOSAIC_LEAK: `k=${leaked}` }) } });
|
||||
w.setRoute();
|
||||
leaked = issue(w.configDir);
|
||||
await assert.rejects(w.h.message("hello"), (e) => e.code === "triage-route-invalid");
|
||||
assert.equal(existsSync(join(root4, "spawned")), false);
|
||||
// A bundle() that throws, and one that writes a file name the host refuses.
|
||||
for (const bundle of [() => { throw new Error("/a/path"); }, ({ write }) => { write("../escape", "x"); return { files: {} }; }]) {
|
||||
const root5 = scratch();
|
||||
const x = await rig(t, { bwrap: spy(root5), module: { bundle } });
|
||||
x.setRoute();
|
||||
issue(x.configDir);
|
||||
await assert.rejects(x.h.message("hello"), (e) => e.code === "triage-route-invalid");
|
||||
assert.equal(existsSync(join(root5, "spawned")), false);
|
||||
assert.equal(existsSync(join(x.root, "escape")) || existsSync(join(x.dataRoot, "triage", "agent", "escape")), false);
|
||||
}
|
||||
});
|
||||
|
||||
test("fence: every admit runs the fences, a running session or not: a message whose route fails is not queued", { timeout: 30000 }, async (t) => {
|
||||
@@ -401,29 +515,91 @@ test("fence: every admit runs the fences, a running session or not: a message wh
|
||||
assert.deepEqual(await c.call("message.receive"), []);
|
||||
});
|
||||
|
||||
test("the claude-code route the repository ships passes the host's fences and launches", { timeout: 30000 }, async (t) => {
|
||||
// The claude-code harness the repository ships, end to end under the real sandbox: the route admits
|
||||
// it, the host writes its bundle (settings.json with the tool gate hook, mcp.json with the typed
|
||||
// tools' server) and sets its MOSAIC_ names, and the real adapter (adapters/claude/adapter.sh, with
|
||||
// its stream reader) runs a stub claude binary named by MOSAIC_CLAUDE_BIN. The stub writes its argv,
|
||||
// its environment's names (never values) and the key's sha256 to a file in the workspace, then
|
||||
// prints a result line. No model is called.
|
||||
test("the claude-code harness answers: the host writes its bundle and sets its MOSAIC_ names, and the real adapter gets past its checks to a stub claude", { skip, timeout: 60000 }, async (t) => {
|
||||
const root = scratch();
|
||||
const dataRoot = join(root, "data");
|
||||
const configDir = join(root, "cfg");
|
||||
mkdirSync(configDir, { mode: 0o700 });
|
||||
mkdirSync(join(root, "bin"));
|
||||
const stub = join(root, "bin", "claude");
|
||||
writeFileSync(stub, `#!/bin/sh
|
||||
{ for a in "$@"; do printf 'ARG %s\\n' "$a"; done
|
||||
env | sed -n 's/^\\([A-Za-z_][A-Za-z0-9_]*\\)=.*/ENV \\1/p'
|
||||
printf 'KEYSHA %s\\n' "$(printf '%s' "$ANTHROPIC_AUTH_TOKEN" | sha256sum | cut -d' ' -f1)"
|
||||
} > claude-call.txt
|
||||
printf '%s\\n' '{"type":"result","subtype":"success","is_error":false,"result":"stub answer"}'
|
||||
`, { mode: 0o755 });
|
||||
const proxyServer = createServer((s) => s.destroy());
|
||||
const proxyPort = await listen(proxyServer);
|
||||
t.after(() => closed(proxyServer));
|
||||
const route = createTriageRoute({ dataRoot, configDir, proxyPort, catalogues: async () => [catalogue([offer({ harnesses: ["claude-code", "pi"] })])], env: {} });
|
||||
const h = createTriageHost({ dataRoot, route, proxyPort, tokenPattern: triageTokenPattern(configDir), readSnapshot: async () => SNAPSHOT, env: CONSOLE_ENV, bwrap: spy(root), now: () => new Date(NOW) });
|
||||
const logs = [];
|
||||
const route = createTriageRoute({ dataRoot, configDir, proxyPort, catalogues: async () => [catalogue([offer({ harnesses: ["claude-code"] })])], env: { MOSAIC_CLAUDE_BIN: stub } });
|
||||
const h = createTriageHost({ dataRoot, route, proxyPort, tokenPattern: triageTokenPattern(configDir), readSnapshot: async () => SNAPSHOT, env: { ...CONSOLE_ENV, MOSAIC_CLAUDE_BIN: stub }, usageSource: spendSource, now: () => new Date(NOW), log: (l) => logs.push(l) });
|
||||
t.after(() => h.stop());
|
||||
mkdirSync(join(dataRoot, "triage"), { recursive: true, mode: 0o700 });
|
||||
chmodSync(dataRoot, 0o700);
|
||||
writeRoute(dataRoot, { model: "glm-5", harness: "claude-code", limit: 1000, timeZone: "UTC", by: "owner" });
|
||||
writeRoute(dataRoot, { model: "glm-5", harness: "claude-code", limit: 100000, timeZone: "UTC", by: "owner" });
|
||||
const key = issue(configDir);
|
||||
// The route's own answer: the two route variables, and no module name (the host sets those).
|
||||
const a = await route.admit();
|
||||
assert.equal(a.ok, true, JSON.stringify({ ...a, env: undefined }));
|
||||
assert.deepEqual([a.harness, a.model, a.provider, a.turnEnd, a.harnessEnv], ["claude-code", "glm-5.1", "zai", "exit", {}]);
|
||||
assert.deepEqual(Object.keys(a.env).sort(), ["ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL"]);
|
||||
assert.equal(a.env.ANTHROPIC_AUTH_TOKEN, key);
|
||||
|
||||
await h.message("hello");
|
||||
await waitFor(() => existsSync(join(root, "spawned")));
|
||||
assert.ok(readFileSync(join(root, "spawn-env"), "utf8").split("\n").includes(`ANTHROPIC_AUTH_TOKEN=${key}`));
|
||||
const replies = await triageReplies(h, 1);
|
||||
assert.equal(replies[0].text, "stub answer", JSON.stringify((await h.state()).transcript));
|
||||
const bundleDir = join(dataRoot, "triage", "agent", "bundle");
|
||||
const settings = readFileSync(join(bundleDir, "settings.json"), "utf8");
|
||||
assert.match(settings, /claude-gate\.mjs/, "settings.json holds the tool gate hook");
|
||||
assert.ok(settings.includes(join(bundleDir, "policy.json")), "the gate reads the session's policy");
|
||||
const mcp = JSON.parse(readFileSync(join(bundleDir, "mcp.json"), "utf8"));
|
||||
assert.ok(mcp.mcpServers.mosaic.args.includes(join(bundleDir, "tools.json")) && mcp.mcpServers.mosaic.args.includes(join(dataRoot, "triage", "agent", "tools.sock")));
|
||||
const session = JSON.parse(readFileSync(join(dataRoot, "triage", "agent", "session.json"), "utf8"));
|
||||
assert.deepEqual([session.harnessEnv.MOSAIC_THINKING, session.harnessEnv.MOSAIC_CLAUDE_SETTINGS, session.harnessEnv.MOSAIC_CLAUDE_MCP_CONFIG, session.harnessEnv.MOSAIC_MODEL_ACTIVITY_SOURCE],
|
||||
["low", join(bundleDir, "settings.json"), join(bundleDir, "mcp.json"), "adapter"]);
|
||||
assert.equal(Object.keys(session.harnessEnv).some((k) => k.startsWith("ANTHROPIC")), false);
|
||||
// What the stub saw: the adapter passed its checks and gave it the bundle's files.
|
||||
const call = readFileSync(join(dataRoot, "triage", "agent", "work", "claude-call.txt"), "utf8").split("\n");
|
||||
const args = call.filter((l) => l.startsWith("ARG ")).map((l) => l.slice(4));
|
||||
const after = (flag) => args[args.indexOf(flag) + 1];
|
||||
assert.deepEqual([after("--effort"), after("--model"), after("--settings"), after("--mcp-config")], ["low", "glm-5.1", join(bundleDir, "settings.json"), join(bundleDir, "mcp.json")]);
|
||||
const names = call.filter((l) => l.startsWith("ENV ")).map((l) => l.slice(4));
|
||||
for (const want of ["ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL", "MOSAIC_CLAUDE_BIN", "MOSAIC_THINKING", "MOSAIC_CLAUDE_SETTINGS", "MOSAIC_CLAUDE_MCP_CONFIG"]) assert.ok(names.includes(want), `${want}: ${names.join(" ")}`);
|
||||
assert.equal(names.includes("ANTHROPIC_API_KEY"), false, "the Console's own key does not pass");
|
||||
assert.ok(call.includes(`KEYSHA ${sha(key)}`), "the session holds the key the file holds");
|
||||
await h.stop();
|
||||
assertNoKey(key, [...filesUnder(dataRoot), ["host log", logs.join("\n")], ["state", JSON.stringify(await h.state())]]);
|
||||
});
|
||||
|
||||
test("the key scrub: an answer that carries the session's key, or anything shaped like a key, reaches the transcript and the read with the key removed", { skip, timeout: 60000 }, async (t) => {
|
||||
const r = await rig(t);
|
||||
r.setRoute();
|
||||
const key = issue(r.configDir);
|
||||
const other = keyPrefix(r.configDir) + randomBytes(32).toString("base64url");
|
||||
const c = await consoleFor(t, r.h);
|
||||
assert.equal((await c.post(`LEAK ${other}`)).status, 202);
|
||||
const [reply] = await triageReplies(r.h, 1);
|
||||
assert.equal(reply.text, `env: ANTHROPIC_AUTH_TOKEN=${KEY_REMOVED} other=${KEY_REMOVED}`);
|
||||
const read = await c.read();
|
||||
const answers = read.json.transcript.filter((e) => e.from === "triage").map((e) => e.text).join("\n");
|
||||
assert.equal(answers.includes(other), false, "the key-shaped string is gone from the answer");
|
||||
await r.h.stop();
|
||||
assertNoKey(key, [...filesUnder(r.dataRoot), ["host log", r.logs.join("\n")], ["responses", c.responses.join("\n")]]);
|
||||
// With no shape check, the session's own key is still removed by value.
|
||||
const s = await rig(t, { tokenDir: false });
|
||||
s.setRoute();
|
||||
const key2 = issue(s.configDir);
|
||||
await s.h.message("LEAK none");
|
||||
assert.equal((await triageReplies(s.h, 1))[0].text, `env: ANTHROPIC_AUTH_TOKEN=${KEY_REMOVED} other=none`);
|
||||
await s.h.stop();
|
||||
assertNoKey(key2, [...filesUnder(s.dataRoot), ["host log", s.logs.join("\n")]]);
|
||||
});
|
||||
|
||||
test("the merged catalogue and the model check", () => {
|
||||
@@ -483,7 +659,8 @@ test("the Console process never imports the proxy management client or the key v
|
||||
// shim, as org-autonomy-write.test.mjs does it) answers `routes` with a catalogue that does not list
|
||||
// the route's harness for the model. Each source has to be read for the message to reach that
|
||||
// refusal: the route store (else no route), the key file (else no key), proxy.json's port (else the
|
||||
// proxy is down) and the organization's catalogue (else unavailable). Nothing launches.
|
||||
// proxy is down) and the organization's catalogue (else unavailable). proxy.json is written only after
|
||||
// the Console starts, so the first message proves it is read at each admit. Nothing launches.
|
||||
test("the Console wires the route: a dev-owner Console reads the store, the key, proxy.json and the organization's catalogue at admit", { timeout: 60000 }, async (t) => {
|
||||
const { tmp, fixture } = await import("../../cli/tests/helpers.mjs");
|
||||
const { createOrg } = await import("../../organization/src/org-service.mjs");
|
||||
@@ -504,7 +681,7 @@ test("the Console wires the route: a dev-owner Console reads the store, the key,
|
||||
const proxyServer = createServer((s) => s.destroy());
|
||||
const proxyPort = await listen(proxyServer);
|
||||
t.after(() => closed(proxyServer));
|
||||
writeFileSync(join(configDir, "proxy.json"), JSON.stringify({ binary: installPaths(f.dataRoot).binary, port: proxyPort }), { mode: 0o600 });
|
||||
const proxyJson = () => writeFileSync(join(configDir, "proxy.json"), JSON.stringify({ binary: installPaths(f.dataRoot).binary, port: proxyPort }), { mode: 0o600 });
|
||||
mkdirSync(join(f.dataRoot, "triage"), { recursive: true, mode: 0o700 });
|
||||
chmodSync(join(f.dataRoot, "triage"), 0o700);
|
||||
writeRoute(f.dataRoot, { model: "glm-5", harness: "claude-code", limit: 1000, timeZone: "UTC", by: "owner" });
|
||||
@@ -557,10 +734,14 @@ registerHooks({ resolve(specifier, context, next) {
|
||||
const opened = await call("/api/triage");
|
||||
assert.equal(opened.status, 200, opened.text);
|
||||
assert.equal(opened.json.route, "set");
|
||||
// No proxy.json when the Console starts: the proxy is down. Written later, the next message reads it.
|
||||
const down = await call("/api/triage/message", { text: "hello" });
|
||||
assert.deepEqual([down.status, down.json], [409, { error: "triage-proxy-down", message: ROUTE_REFUSALS["triage-proxy-down"] }]);
|
||||
proxyJson();
|
||||
const res = await call("/api/triage/message", { text: "hello" });
|
||||
assert.deepEqual([res.status, res.json], [409, { error: "triage-harness-incompatible", message: ROUTE_REFUSALS["triage-harness-incompatible"] }]);
|
||||
const calls = readFileSync(log, "utf8").trim().split("\n").filter(Boolean).map((l) => JSON.parse(l));
|
||||
assert.ok(calls.some((c) => c.verb === "routes" && c.business === "alpha"), "the catalogue came from the organization's bus");
|
||||
assert.equal(existsSync(join(f.dataRoot, "triage", "agent", "session.json")), false, "nothing launched");
|
||||
assertNoKey(key, [["console output", out], ["responses", opened.text + res.text], ...filesUnder(f.dataRoot)]);
|
||||
assertNoKey(key, [["console output", out], ["responses", opened.text + down.text + res.text], ...filesUnder(f.dataRoot)]);
|
||||
});
|
||||
Reference in new issue
Block a user