Compare commits

..
Author SHA1 Message Date
jason.woltje 196505e6ef fix(bus): review round: the restart notice shows only on a ready catalogue, since a fenced, colliding or down proxy still refuses after a restart (#1873, lane pickup-launch)
A bus booted with the proxy on a fenced instance reads the same unavailable catalogue, so the
notice sent the owner to a restart that stops running agents and starts none. The test proves
that and shows the notice again once the fence is gone. The disabled check goes away with it;
the disabled test now says the routes verb answers disabled itself.
2026-11-19 12:08:41 -06:00
jason.woltje f7c0e4f46c fix(bus): a bus that picked up the shared proxy says on its catalogue that a restart is needed to start agents on these models (#1873, lane pickup-launch)
A picked-up client serves accounts and the catalogue, but a launch on it is refused at bus
admission (model-provider-unavailable): the host launcher keeps its boot system with no proxy,
and the bus has no ModelKeys. While the bus holds a late client and the catalogue is not
disabled, the catalogue appends one notice after its own: "Restart this organization to start
agents on these models." A boot proxy, a disabled catalogue, refused settings and a dropped
client carry no such notice. Tests cover the launch refusal, refused settings, a dropped
client and a key scan over the launch path.
2026-11-19 12:08:41 -06:00
jason.woltje 5c975faddd fix(model-proxy): review round: the preflight comment says every v8 proxy applies a key PUT by an async reload; a re-probe waits about the time left, not the client default; the README says the proxy unit reads PIN from the tree it was installed from and names .codex-refresh; two test headers stop naming only 8.0.42 (#1873, lane proxy-pin) 2026-11-19 11:08:23 -06:00
jason.woltje 462215b4bd chore(model-proxy): pin CLIProxyAPI 8.0.79; proxy.json from an earlier PIN stays valid; preflight re-probes a just-issued key's 401 briefly (#1873, lane proxy-pin)
The PIN moves from 8.0.42 to 8.0.79 with the upstream archive checksum and the
extracted binary checksum. The panel stays 1.26.7.

proxy.json keeps the binary path of the PIN it was created under. A path of the
form <dataRoot>/opt/cliproxyapi/<version>/cli-proxy-api now validates and reads
as the current pinned path, so an instance created on 8.0.42 needs no edit. The
runner still runs only the verified pinned install.

8.0.79 applies a key-list PUT by an async reload a few milliseconds after it
answers, so a just-issued key answers 401 at first. Preflight now probes again
on 401 only, for at most ISSUE_PROBE_TIMEOUT_MS (2 s). Any other non-2xx still
refuses on the first answer.
2026-11-19 11:08:23 -06:00
jason.woltje d0293d6602 fix(bus): review round: proxy init creates the lock before it installs the client, so a lock that refuses leaves no client; a default-instance bus test proves the catalogue refreshes at once on pickup and on drop (#1873, lane proxy-pickup) 2026-11-19 09:51:39 -06:00
jason.woltje 912138ff61 feat(bus): a running organization bus picks up the shared proxy without a restart (#1873, lane proxy-pickup)
A bus started before the instance had a proxy checks once at the next accounts, page, catalogue
or fence request, and loads the client through the start path's checks, loader and lock. A client
got that way, or by the bus's own init, is checked again at each such request and dropped when
proxy.json goes away, no longer loads, moves port, the key stops loading, the lock refuses, or a
dev fixture the host would refuse is in use. While there is no client, the bus names the cause in
one plain line on not-configured and on the unavailable catalogue, and the Console shows it.
2026-11-19 09:39:41 -06:00
jason.woltje 4a8d310cd5 docs(webui): the Triage settings comment says the host reads the stored route at its next admit, now that the host has a route (#1873, lane triage-wiring) 2026-11-18 16:01:19 -06:00
jason.woltje c28ca57af6 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) 2026-11-18 16:01:12 -06:00
jason.woltje 255c1575bb feat(triage): the Console resolves Triage's model route at each admit
The Triage host got route: null. It now gets a route read fresh at each
admit from the route store, the key file, each active organization's
model catalogue (held to checkRoutes) and the harness module. The spawn
env adds only ANTHROPIC_BASE_URL (the instance proxy) and
ANTHROPIC_AUTH_TOKEN (the Triage key); the host sets every MOSAIC_ name.

The key file reader moves to packages/cli/src/triage-key-file.mjs so the
Console reads the key with the key verbs' checks without loading the
proxy management client; triage-key.mjs re-exports it. Each refusal (no
route, no key, unsafe key, proxy down, model not offered, no catalogue,
harness incompatible) launches nothing and answers 409 with one plain
line. Every admit runs the host's fences on the answer, a session
running or not.

The cap fails closed: two turns in a row with unknown usage end the
session with a host line; the next message starts a new run with a
fresh session directory, and its usage counts again.

#1873, lane triage-wiring
2026-11-18 16:01:12 -06:00
jason.woltje 0ba50111f6 test(webui): the Triage drawer's no-Triage-request checks skip the static triage-settings.js script every page loads; it carries no data, and the Settings panel's own test shows a non-owner reads no route (#1873, lane triage-settings) 2026-11-18 15:06:03 -06:00
jason.woltje dab07f0ff5 fix(webui): Triage route review round: a save sends the revision its fields were read at, so a route saved elsewhere answers as stale instead of being written over, and an untouched page follows the store; a read follows the repo GET convention, so a LAN dev host with no Fetch Metadata reads the route while a write still needs one matching Origin and same-origin; a zone in another case is stored in its canonical spelling; focus lands on the saved line; the organization catalogues are read in parallel (#1873, lane triage-settings) 2026-11-18 14:40:22 -06:00
jason.woltje 561ff77e48 feat(webui): Settings > Triage reads and saves the Triage route (#1873, lane triage-settings)
GET and POST /api/triage/route, owner only behind the proxy-admin gate, with one
Origin and Sec-Fetch-Site same-origin on a write and Fetch Metadata on a read.
GET returns the stored route or says it is not set, plus the merged catalogue
choices (model keys, availability, compatible installed harnesses) with no
provider, upstream, module, key or path. POST takes model, harness, cap, time
zone and revision, checks the route with checkRoutes against the merged
catalogue (offered, available, compatible, installed), requires a known time
zone with no default, and writes through the store's writeRoute, so the lock,
the revision compare and the receipt are the store's. A stale revision answers
409 with the stored revision. updatedBy is the signed-in owner.

The Settings > Triage page shows only to the owner /api/config marks proxyAdmin.
Model and harness selects (the harness list is the chosen model's), a cap in
tokens, reset 00:00 read only, a time zone, Save. Not set before a first save;
a refusal is one line and keeps the fields; a stale save offers Reload route; a
second Save while one runs does nothing; one line says saving does not start
Triage. cli.mjs keeps route: null for the Triage host.
2026-11-18 14:40:21 -06:00
jason.woltje e1642be760 test(webui): drawer Stop review round: two Stops run at once in two drawers; coder's Stop the bus takes ends and repaints the page while ideas' still runs, and ideas keeps its running state; then ideas' is refused and each drawer keeps its own line (#1873, lane sp20-drawer-stop) 2026-11-18 14:33:52 -06:00
jason.woltje b23ccdfc12 fix(webui): Stop in the agent drawer: an archived or unavailable organization says so in Restart's plain line ending Nothing changed, not Outcome unknown, with one test each; each Stop keeps its own state per agent, as each restart does, so a second Stop or question in another drawer no longer drops a running Stop's line or its running state, with a two-drawer test (#1873, lane sp20-drawer-stop) 2026-11-18 14:33:52 -06:00
37 changed files with 3535 additions and 196 deletions

No files matched your search

+6 -1
View File
@@ -280,6 +280,11 @@ const canonical = (x) =>
typeof x === 'string' && /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/.test(x) &&
Number.isFinite(Date.parse(x)) &&
new Date(x).toISOString() === x;
// proxy-pickup (#1873): the one plain line a bus without a proxy client gives for not-configured.
// The runtime writes it from static text and a refusal code; only that line passes, and never
// into refusal evidence.
const proxyCause = error => error instanceof BusError && error.code === 'not-configured' && typeof error.reason === 'string' && error.reason.length <= 300 ? error.reason : undefined;
export class BusError extends Error {
// Guidance is static policy text; never quote arguments or file bytes here.
constructor(code, reason) {
@@ -6089,7 +6094,7 @@ export class Broker {
const args = request.args ?? {};
if (!object(args)) fail('invalid-request');
return Promise.resolve().then(() => this.#proxyPage(request.verb, args, { business: s.business, human: s.human }))
.catch(error => { throw this.#refused(s, new BusError(PROXY_PAGE_CODES.includes(error.code) ? error.code : 'proxy-down'), { verb: request.verb }); });
.catch(error => { throw this.#refused(s, new BusError(PROXY_PAGE_CODES.includes(error.code) ? error.code : 'proxy-down', proxyCause(error)), { verb: request.verb }); });
}
// #1873: the fence clear takes restart evidence, which the runtime re-proves on the host
// before the lock clears. Owner only, before every other gate, like the proxy page verbs.
+17 -3
View File
@@ -6,6 +6,8 @@ import { isDeepStrictEqual } from 'node:util';
import { BusError } from './broker.mjs';
export const REVOKE_TIMEOUT_MS = 5000;
export const REVOKE_POLL_MS = 25;
export const ISSUE_PROBE_TIMEOUT_MS = 2000;
export const ISSUE_REPROBE_MIN_MS = 250;
export const MODEL_STOP_TIMEOUT_MS = 5000;
const keyId = key => createHash('sha256').update(key).digest('hex').slice(0, 12);
const fail = code => { throw new BusError(code); };
@@ -223,9 +225,21 @@ export class ModelKeys {
this.#sentinel(keys);
return this.#issue(instance, false, keys);
});
let status;
try { status = await this.#client.probeKey(proxy.key); }
catch { fail('proxy-down'); }
// Every v8 CLIProxyAPI applies a key-list PUT by an async reload after it answers, so a
// just-issued key can answer 401 for a moment. On 8.0.79 that moment outlasts the confirming
// GET (measured, #1873). Only 401 is probed again, until a short deadline; a re-probe waits
// about the time left, at least ISSUE_REPROBE_MIN_MS. Any other non-2xx refuses on the first
// answer, as before.
const deadline = performance.now() + ISSUE_PROBE_TIMEOUT_MS;
let status, first = true;
for (;;) {
const options = first ? undefined : { timeout: Math.max(ISSUE_REPROBE_MIN_MS, Math.ceil(deadline - performance.now())) };
first = false;
try { status = await this.#client.probeKey(proxy.key, options); }
catch { fail('proxy-down'); }
if (status !== 401 || performance.now() >= deadline) break;
await new Promise(resolve => setTimeout(resolve, REVOKE_POLL_MS));
}
if (status < 200 || status >= 300) fail('proxy-key-refused');
return proxy;
}
+81 -9
View File
@@ -32,7 +32,7 @@ import { loadManagementClient } from '../../model-proxy/src/client.mjs';
import { ProxyPage } from './proxy-page.mjs';
import { createModelCatalogue } from './model-catalogue.mjs';
import { sha256 } from '../../model-proxy/src/common.mjs';
import { initializeProxy } from '../../model-proxy/src/settings.mjs';
import { initializeProxy, loadProxy } from '../../model-proxy/src/settings.mjs';
import { createProxyLock } from './proxy-lock.mjs';
import { verifyRestart } from '../../model-proxy/src/admin.mjs';
import { ModelKeys, modelOrganization, stopModelRuns, MODEL_STOP_TIMEOUT_MS } from './model-keys.mjs';
@@ -631,6 +631,7 @@ export async function startBroker({
builtinBinding = null,
modelCatalogue = null, // in-process fixture seam; boot JSON cannot supply a service
makeProviderLoader = createLoader, // in-process fixture seam; boot JSON cannot supply a function
initProxy = initializeProxy, // in-process fixture seam (a test install pin); boot JSON cannot supply a function
// #1873 A3: the boot's root and project agent copies (cli config.mjs copiesAtBoot), plain data. Every
// reload of the organization file below re-applies this snapshot, never the root file.
copies = null,
@@ -638,6 +639,7 @@ export async function startBroker({
// In-process only, like librarySync; production passes none.
copyTransport = null,
}) {
if (typeof initProxy !== 'function') throw new BusError('invalid-host-request');
if (copies !== null && (typeof copies !== 'object' || Array.isArray(copies) || typeof copies.org !== 'string'
|| !Object.hasOwn(businesses ?? {}, copies.org) || copies.copies === null || typeof copies.copies !== 'object' || Array.isArray(copies.copies)))
throw new BusError('invalid-host-request');
@@ -835,7 +837,72 @@ export async function startBroker({
// records. A daily key quota moved within one agent's lineage keeps its since and its count.
const keyLineage=(business,keyId)=>modelKeyLineage(credentials.status(),business,keyId);
const settings=organization?launchSettings({...organization,sessions:()=>readSessions(dataRoot),levels,keyLineage}):null;
modelCatalogues = modelCatalogue ?? (settings ? createModelCatalogue({ pluginDataRoot:instanceRoot ?? dataRoot, client: () => modelProxyClient, settings: id => settings.current(id).plugins ?? {}, proxyLock }) : null);
// #1873 proxy-pickup: a bus started before the instance had a shared proxy picks it up at the
// first request that needs one, through the start path's checks, loader and lock. A client got
// that way (or by this bus's own proxy.init) is checked again at every such request and dropped
// when the start path would now refuse it. A bus started with a proxy keeps its boot client:
// its ModelKeys holds that client. `proxyCause` is the one plain line the owner sees while there
// is no client: static text and a refusal code, never a path or a key.
let lateProxy = false, proxyCause = null, proxySync = Promise.resolve();
const dropLateProxy = cause => {
if (lateProxy) { modelProxyClient = null; proxyPage = null; proxyPort = null; proxyLock = null; lateProxy = false; modelCatalogues?.invalidate?.(); }
proxyCause = cause;
};
// The host refuses fixture-proxy-configured at boot when a dev fixture is recorded for the
// instance and the organization routes a launch to the fixture (cli dev-fixture.mjs
// fixtureLauncherOptions). Anything but a plain absence of the record counts as recorded.
const fixtureRecorded = () => { try { lstatSync(join(instanceRoot ?? dataRoot, 'dev-fixture', 'fixture.json')); return true; } catch (error) { return error.code !== 'ENOENT'; } };
const usesFixture = id => {
try {
const doc = settings.current(id), providers = doc.plugins?.['model-provider'];
return (Array.isArray(providers) && providers.includes('fixture')) || Object.values(doc.launch?.models ?? {}).some(m => m?.module === 'fixture');
} catch { return true; }
};
async function pickProxy() {
if (proxy || !organization || !settings) return;
if (org !== null && (!org || typeof org.id !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(org.id) || typeof instanceDataRoot !== 'string' || typeof instanceConfigDir !== 'string'))
return dropLateProxy('This bus has no valid organization binding for the shared proxy (invalid-host-request). Restart it from the host.');
const configDir = org ? instanceConfigDir : organization.dir;
let found;
try { found = loadProxy(configDir, instanceRoot ?? dataRoot); }
catch (error) { return dropLateProxy(`The model proxy settings do not load (${/^proxy-[a-z-]+$/.test(error?.code ?? '') ? error.code : 'proxy-config-invalid'}). This organization reads no accounts or catalogue until they do.`); }
if (found === undefined) return dropLateProxy('The model proxy has not been set up for this instance yet. Set it up on the Model proxy page.');
if (modelProxyClient && found.port !== proxyPort) dropLateProxy(null);
if (fixtureRecorded() && Object.keys(businesses).some(usesFixture))
return dropLateProxy('This organization routes launches to the dev fixture, which the host refuses next to a model proxy (fixture-proxy-configured). Remove the fixture or stop using it here.');
let client;
try { client = await loadManagementClient({ configDir, proxy: found, receipt: proxyReceipt }); }
catch { return dropLateProxy('The model proxy management key does not load (proxy-management-key-invalid). This bus cannot reach the proxy until the key file is fixed.'); }
let lock = null;
if (org) {
try { lock = createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${found.port}` }); }
catch { return dropLateProxy('The shared proxy lock does not load (proxy-lock-invalid). Check the proxy state for this instance, then restart this bus.'); }
}
proxyCause = null;
// Same port, key and lock still load: keep the client and its page, which holds login states.
if (modelProxyClient) return;
modelProxyClient = client; proxyPort = found.port; proxyLock = lock; lateProxy = true;
configureProxyPage(); modelCatalogues?.invalidate?.();
}
// One check at a time; an unexpected failure drops a late client, never keeps it.
const syncProxy = () => (proxySync = proxySync.then(pickProxy).catch(() => dropLateProxy('The model proxy could not be checked (proxy-down). Try again shortly.')));
// Unavailable with no client: the catalogue names the cause instead of a bare unavailable.
// pickup-launch: a late client serves accounts and the catalogue, but a launch on it needs what
// only the start path builds (ModelKeys here, the proxy in the host launcher's boot system), so
// while this bus holds one a ready catalogue says plainly that a restart is needed. Not on an
// unavailable one: a fence, a collision or a down proxy still refuses after a restart.
const RESTART_TO_LAUNCH = 'Restart this organization to start agents on these models.';
const explainCatalogue = value => {
if (value?.status === 'unavailable' && value.reason === undefined && !modelProxyClient && proxyCause)
return { ...value, reason: 'proxy-not-configured', notices: [proxyCause] };
if (lateProxy && value?.status === 'ready')
return { ...value, notices: [...(value?.notices ?? []), RESTART_TO_LAUNCH] };
return value;
};
const baseCatalogue = settings ? createModelCatalogue({ pluginDataRoot:instanceRoot ?? dataRoot, client: () => modelProxyClient, settings: id => settings.current(id).plugins ?? {},
// A view, so a lock taken by a later pickup or init counts as it does for the start path's.
proxyLock: { assertCertain: () => proxyLock?.assertCertain(), generation: () => proxyLock?.generation() } }) : null;
modelCatalogues = modelCatalogue ?? (baseCatalogue ? { ...baseCatalogue, read: async (id, options) => { await syncProxy(); return explainCatalogue(await baseCatalogue.read(id, options)); } } : null);
// Resolve legacy table entries with the enabled modules' declarations. A
// missing or ambiguous mapping never becomes an available route.
async function catalogueFor(id, options = {}) {
@@ -1340,22 +1407,27 @@ export async function startBroker({
// Row 199 (#1697, design section 6): issue, rotate, revoke and delete, live with no restart; each
// rewrites or records through the organization writer, so changes.jsonl holds the receipt.
proxyPage: async (verb, args, scope) => {
if (!proxyPage) throw new BusError('not-configured');
await syncProxy();
if (!proxyPage) throw new BusError('not-configured', proxyCause ?? undefined);
return proxyPage.request(verb, args, scope);
},
proxyInit: organization ? async args => {
const settings = await initializeProxy({ configDir: organization.dir, dataRoot: instanceRoot ?? dataRoot, ...args });
modelProxyClient = await loadManagementClient({ configDir: organization.dir, proxy: settings, receipt: proxyReceipt });
proxyPort = settings.port;
const settings = await initProxy({ configDir: organization.dir, dataRoot: instanceRoot ?? dataRoot, ...args });
const client = await loadManagementClient({ configDir: organization.dir, proxy: settings, receipt: proxyReceipt });
// #1873: an org bus that initializes the shared proxy also holds its write lock from now on.
if (org && !proxyLock) proxyLock = createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${settings.port}` });
configureProxyPage();
// proxy-pickup: the lock comes first, so a lock that refuses leaves no client behind.
const lock = org && !proxyLock ? createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${settings.port}` }) : proxyLock;
// proxy-pickup: a client this bus did not boot with is checked again like a picked-up one.
if (!proxy) { lateProxy = true; proxyCause = null; }
modelProxyClient = client; proxyPort = settings.port; proxyLock = lock;
configureProxyPage(); modelCatalogues?.invalidate?.();
return settings;
} : null,
// #1873: clear the uncertain-write fence only on restart evidence this host re-proves: the
// process on the proxy port is new, alive and started after the fence was set.
proxyFenceClear: organization ? async ({ evidence }) => {
if (!proxyLock || !modelProxyClient) throw new BusError('not-configured');
await syncProxy();
if (!proxyLock || !modelProxyClient) throw new BusError('not-configured', proxyCause ?? undefined);
return proxyLock.clear({ restarted: true, read: () => modelProxyClient.getKeys(), verify: previous => verifyRestart(evidence, { fence: previous, port: proxyPort }) });
} : null,
credentialVerbs: organization
+26 -2
View File
@@ -11,7 +11,7 @@ import { tmpdir } from 'node:os';
import { createHash, randomBytes } from 'node:crypto';
import { Credentials } from '../src/credentials.mjs';
import { ManagementClient } from '../../model-proxy/src/client.mjs';
import { ModelKeys, REVOKE_TIMEOUT_MS, REVOKE_POLL_MS } from '../src/model-keys.mjs';
import { ModelKeys, REVOKE_TIMEOUT_MS, REVOKE_POLL_MS, ISSUE_PROBE_TIMEOUT_MS, ISSUE_REPROBE_MIN_MS } from '../src/model-keys.mjs';
import { startBroker } from '../src/runtime.mjs';
import { busOrganization } from '../src/organization-context.mjs';
import { Registry } from '../../seat/src/session.mjs';
@@ -42,6 +42,7 @@ async function fixture(t, { delayed = 0 } = {}) {
const key = req.headers.authorization?.slice(7);
const accepted = state.keys.includes(key) || (state.removed.has(key) && performance.now() - state.removed.get(key) < state.delay);
state.onProbe?.(state.status ?? (accepted ? 200 : 401));
if (state.stallProbe?.()) return;
res.writeHead(state.status ?? (accepted ? 200 : 401)); res.end('{}');
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
@@ -123,10 +124,33 @@ test('proxy-revoke-revision-conflict keeps local revoking record and ids-only re
assert.equal(JSON.stringify(f.receipts).includes(key), false);
await f.create().reconcile(); assert.equal(f.credentials.modelRecord('demo/coder') === null, true);
});
for (const status of [401, 503, 302]) test(`proxy-key-refused: preflight rejects HTTP ${status} with exactly one probe`, async t => {
for (const status of [503, 302]) test(`proxy-key-refused: preflight rejects HTTP ${status} with exactly one probe`, async t => {
const f = await fixture(t); await f.manager.reconcile(); await f.manager.ensure('demo/coder'); f.state.status = status;
await assert.rejects(f.manager.preflight('demo/coder'), failCode('proxy-key-refused')); assert.equal(f.state.probes, 1);
});
test('proxy-key-refused: a key that keeps answering 401 refuses once the short probe deadline passes', async t => {
const f = await fixture(t); await f.manager.reconcile(); await f.manager.ensure('demo/coder'); f.state.status = 401;
const started = performance.now();
await assert.rejects(f.manager.preflight('demo/coder'), failCode('proxy-key-refused'));
const took = performance.now() - started;
assert.ok(f.state.probes > 1, 'a 401 is probed again'); assert.ok(took >= ISSUE_PROBE_TIMEOUT_MS && took < ISSUE_PROBE_TIMEOUT_MS + 2000, `bounded: ${took}`);
});
test('proxy-down: a re-probe that stalls ends near the probe deadline, not the client default', async t => {
const f = await fixture(t); await f.manager.reconcile(); await f.manager.ensure('demo/coder'); f.state.status = 401;
f.state.stallProbe = () => f.state.probes >= 2;
const started = performance.now();
await assert.rejects(f.manager.preflight('demo/coder'), failCode('proxy-down'));
const took = performance.now() - started;
assert.equal(f.state.probes, 2); assert.ok(took < ISSUE_PROBE_TIMEOUT_MS + ISSUE_REPROBE_MIN_MS + 1000, `bounded: ${took}`);
});
// Every v8 CLIProxyAPI applies the PUT by an async reload; on 8.0.79 a just-issued key answers 401
// past the confirming GET (#1873).
test('first launch passes when the proxy applies the new key a moment after the PUT answers', async t => {
const f = await fixture(t); await f.manager.reconcile(); f.state.status = 401;
f.state.onProbe = () => { if (f.state.probes === 3) f.state.status = null; };
const answer = await f.manager.preflight('demo/coder');
assert.equal(f.state.probes, 3); assert.equal(f.state.keys.includes(answer.key), true);
});
test('rotate keeps old key while its run lives, new launches use new, exit removes old', async t => {
const f = await fixture(t); await f.manager.reconcile(); const first = await f.manager.ensure('demo/coder');
f.runs.push({ instance: 'demo/coder', run: 'old', keyId: hash(first.key) });
+299
View File
@@ -0,0 +1,299 @@
// #1873 pickup-launch: a bus that picked up the shared proxy late serves accounts and a ready
// catalogue, but a launch on a proxy model still needs a restart (no ModelKeys on the bus, no proxy
// in the host launcher's boot system). While it holds such a client, its catalogue answer says so
// in one plain notice, and a launch refuses exactly as before. A refused or dropped client shows
// its cause instead. Real buses, a real launcher whose model ops go to the bus, a fake loopback
// management server; no live config.
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import http from 'node:http';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { createHash, randomBytes } from 'node:crypto';
import { pathToFileURL } from 'node:url';
import { startBroker } from '../src/runtime.mjs';
import { busOrganization } from '../src/organization-context.mjs';
import { createProxyLock } from '../src/proxy-lock.mjs';
import { Registry, launchLogFile } from '../../seat/src/session.mjs';
import { loadBusiness } from '../../organization/src/organization-config.mjs';
import { resolveInstance } from '../../organization/src/resolve.mjs';
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
import { PIN } from '../../model-proxy/src/common.mjs';
import { initializeProxy, installPaths, loadProxy } from '../../model-proxy/src/settings.mjs';
import { createLauncher } from '../../cli/src/launcher.mjs';
import { initializeControl } from '../../processing-control/src/state.mjs';
import { ZAI_URL } from '../../plugins/model-provider/cliproxy/index.mjs';
import { fakeHarnesses, humanEnv, withoutHostCredentials } from '../../cli/tests/helpers.mjs';
import { REPO_SKILLS } from '../../harness/tests/helpers.mjs';
const RESTART = 'Restart this organization to start agents on these models.';
const MODEL = 'claude-claude-opus-5-5';
const sha = value => createHash('sha256').update(value).digest('hex');
// A loopback stand-in for the proxy's management API: one ready Claude account with one model,
// and an access key list that keeps what a PUT sends, so a boot ModelKeys reconciles against it.
async function fakeProxy(t, key, port = 0, { groups = [] } = {}) {
const seen = { calls: 0, wrongKey: 0 };
let accessKeys = [];
const server = http.createServer((req, res) => {
seen.calls++;
if (req.headers.authorization !== `Bearer ${key}`) { seen.wrongKey++; res.writeHead(401); res.end('{}'); return; }
const url = new URL(req.url, 'http://127.0.0.1');
if (url.pathname === '/v8/management/config/access/api-keys' && req.method === 'PUT') {
const chunks = [];
req.on('data', c => chunks.push(c));
req.on('end', () => { accessKeys = JSON.parse(Buffer.concat(chunks)); res.end(JSON.stringify({ status: 'ok' })); });
return;
}
const answer = url.pathname === '/v8/management/credentials' ? { files: [{ id: 'acct-1', name: 'acct-1', provider: 'claude', status: 'ready', priority: 1 }] }
: url.pathname === '/v8/management/credentials/models' ? { models: [{ id: 'claude-opus-5-5' }] }
: url.pathname === '/v8/management/config/api-keys/claude' ? groups
: url.pathname === '/v8/management/config/access/api-keys' ? accessKeys
: null;
if (answer === null) { res.writeHead(404); res.end('{}'); return; }
res.end(JSON.stringify(answer));
});
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(port, '127.0.0.1', resolve); });
t.after(async () => { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); });
return { port: server.address().port, seen, server };
}
// One instance (config dir + data root) and a fake proxy install whose bytes a test pin names.
function instance(t) {
const root = fs.mkdtempSync(join(tmpdir(), 'pl-'));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const configDir = join(root, 'config'), dataRoot = join(root, 'instance');
fs.mkdirSync(join(configDir, 'secrets', 'agents'), { recursive: true, mode: 0o700 });
fs.chmodSync(join(configDir, 'secrets'), 0o700);
fs.mkdirSync(dataRoot, { mode: 0o700 });
const paths = installPaths(dataRoot);
fs.mkdirSync(join(paths.dir, 'static'), { recursive: true, mode: 0o700 });
fs.writeFileSync(paths.binary, 'fixture proxy binary\n', { mode: 0o700 });
fs.writeFileSync(paths.panel, '<!doctype html>\n', { mode: 0o600 });
const pin = { ...PIN, binary: { ...PIN.binary, sha256: sha('fixture proxy binary\n') }, panel: { ...PIN.panel, sha256: sha('<!doctype html>\n') } };
const rolesDir = rolesCopy(root), system = systemFor(root);
const key = `pl1873-${randomBytes(24).toString('hex')}`;
return { root, configDir, dataRoot, paths, pin, rolesDir, system, key };
}
// A real organization bus on the instance, started with no proxy unless `binding.proxy` gives one.
async function orgBus(t, f, id, { providers = ['cliproxy'], binding = {} } = {}) {
const doc = businessDoc(f.root, id);
doc.plugins = { harness: ['pi'], 'model-provider': providers };
writeJson(join(f.configDir, 'businesses', `${id}.json`), doc);
const business = loadBusiness(id, { dir: f.configDir, rolesDir: f.rolesDir }), resolved = {};
for (const name of Object.keys(business.roles)) resolved[name] = resolveInstance({ system: f.system, business, instance: name });
const dataRoot = join(f.root, `${id}-bus`); fs.mkdirSync(dataRoot, { mode: 0o700 }); new Registry(dataRoot);
const bus = await startBroker({ dataRoot, businesses: { [id]: busOrganization(business, resolved) },
organization: { dir: f.configDir, rolesDir: f.rolesDir, system: f.system },
instanceRoot: f.dataRoot, org: { id }, instanceDataRoot: f.dataRoot, instanceConfigDir: f.configDir,
initProxy: args => initializeProxy({ ...args, pin: f.pin }), ...binding });
t.after(() => bus.close().catch(() => {}));
const human = bus.broker.bindHuman({ business: id, human: 'jason', via: 'cli', outsideAgent: true });
const ask = (verb, args) => Promise.resolve().then(() => bus.broker.request(human, { verb, args }));
return { id, bus, ask, dataRoot };
}
// Every stderr line written while `run` is in flight.
async function capture(run) {
const write = process.stderr.write, lines = [];
process.stderr.write = (chunk, ...rest) => { lines.push(String(chunk)); return write.call(process.stderr, chunk, ...rest); };
try { return { value: await run(), lines }; } finally { process.stderr.write = write; }
}
async function initThroughA(t, f, a, options) {
const settings = await a.ask('proxy.init', { key: f.key });
const proxy = await fakeProxy(t, f.key, settings.port, options);
return { settings, proxy };
}
// The owner picks the catalogue's proxy model for the coder on the Routing page, through the bus.
async function pickModel(o) {
const view = await o.ask('routes');
assert.equal(view.catalogue.status, 'ready');
assert.ok(view.catalogue.models.some(m => m.key === MODEL));
await o.ask('routes.set', { revision: view.revision, routes: { coder: { harness: 'pi', model: MODEL, thinking: 'low', fallback: null } } });
}
// The host launcher as cli.mjs builds it for a bus that booted with no proxy.json: its system has
// no proxy. The model ops go to the bus itself; the rest are the launch-routes stub, and
// authorizeLaunch stops a launch that passed every check before it.
async function hostLauncher(t, f, o, { system = {} } = {}) {
initializeControl(o.dataRoot);
// A launch whose role names a skill that is not enabled refuses before the model gate.
if (!fs.existsSync(join(f.dataRoot, 'skills-enabled'))) fs.cpSync(REPO_SKILLS, join(f.dataRoot, 'skills-enabled'), { recursive: true });
const env = withoutHostCredentials(humanEnv({ MOSAIC_CONFIG: join(f.configDir, 'config.json'), MOSAIC_ROLES_DIR: f.rolesDir }), { piKey: true });
const answers = [], ops = [];
const host = { business: o.id, beforeClose: () => {}, op: async m => {
ops.push(m.op);
if (m.op === 'identity') return { business: o.id, role: 'pm' };
if (m.op === 'mayLaunch') return { allow: true };
if (m.op === 'modelAdmission' || m.op === 'modelPreflight') { const answer = await o.bus[m.op](m); answers.push({ op: m.op, answer }); return answer; }
if (m.op === 'authorizeLaunch') throw Object.assign(Error('fixture authorization stop'), { code: 'fixture-policy' });
return {};
} };
const cliproxy = JSON.stringify(pathToFileURL(new URL('../../plugins/model-provider/cliproxy/index.mjs', import.meta.url).pathname).href);
const makeLoader = await fakeHarnesses(t, { pi: null }, write => write('model-provider', 'cliproxy', `export * from ${cliproxy};\n`));
// The validated system config shape (mosaic-config.mjs), as cli.mjs passes it with the org root.
const config = { configVersion: 1, environment: 'development', dataRoot: o.dataRoot, instanceRoot: f.dataRoot,
execution: { backend: 'docker', provider: 'zai', model: 'glm-5.3-flash', adapter: 'mock' }, ...system };
const launcher = createLauncher({ host, system: config, env, makeLoader, bwrap: '/usr/bin/true', log: () => {} });
t.after(() => launcher.close?.().catch?.(() => {}));
const events = () => { try { return fs.readFileSync(launchLogFile(o.dataRoot, o.id), 'utf8').trim().split('\n').filter(Boolean).map(l => JSON.parse(l)); } catch { return []; } };
return { launch: () => launcher.fromSession('fixture-cap', 'coder'), answers, ops, events };
}
const restartNotices = view => view.catalogue.notices.filter(n => n === RESTART).length;
test('a bus that picked up the proxy says on its ready catalogue that a restart starts agents on these models, and the launch refuses as before', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
const before = await b.ask('routes');
assert.equal(before.catalogue.status, 'unavailable');
assert.equal(restartNotices(before), 0, 'no client yet: the not-set-up cause, not the restart notice');
await initThroughA(t, f, a);
await pickModel(b);
const view = await b.ask('routes');
assert.equal(view.catalogue.status, 'ready');
assert.equal(restartNotices(view), 1);
assert.equal(restartNotices(await b.ask('routes')), 1, 'one notice at every read');
// The bus that ran init holds a client it did not boot with too.
const own = await a.ask('routes');
assert.equal(own.catalogue.status, 'ready');
assert.equal(restartNotices(own), 1);
// The launch refuses as before, with the existing code and line, before any key or run: the
// launcher booted with no proxy sends no proxy transport, and the bus's admission refuses that.
const host = await hostLauncher(t, f, b);
assert.deepEqual(await host.launch(), { ok: false, error: 'model-provider-unavailable' });
assert.deepEqual(host.answers, [{ op: 'modelAdmission', answer: { allow: false, code: 'model-provider-unavailable' } }]);
assert.equal(host.ops.includes('authorizeLaunch'), false);
assert.equal(host.ops.includes('modelPreflight'), false);
const refused = host.events().filter(e => e.event === 'refused');
assert.equal(refused.length, 1);
assert.equal(refused[0].code, 'model-provider-unavailable');
assert.equal(refused[0].why, 'model cliproxy/claude/claude-opus-5-5 unavailable (model-provider-unavailable)');
// The bus side has no ModelKeys: a preflight issues no key.
assert.equal(await b.bus.modelPreflight({ business: 'beta', role: 'coder' }), null);
});
test('the restart notice is added after the notices the catalogue already carries', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
// A z.ai upstream group with no models: the cliproxy catalogue's own notice.
await initThroughA(t, f, a, { groups: [{ 'base-url': ZAI_URL, keys: [{ name: 'group-1' }] }] });
const view = await b.ask('routes');
assert.equal(view.catalogue.status, 'ready');
assert.deepEqual(view.catalogue.notices, ['z.ai account has no models configured. Add them on Model proxy.', RESTART]);
});
test('a bus started with the proxy shows no restart notice', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha');
await initThroughA(t, f, a);
const c = await orgBus(t, f, 'gamma', { binding: { proxy: loadProxy(f.configDir, f.dataRoot) } });
const view = await c.ask('routes');
assert.equal(view.catalogue.status, 'ready', JSON.stringify(view.catalogue));
assert.deepEqual(view.catalogue.notices, []);
});
test('an organization with no model provider shows a disabled catalogue with no restart notice (the routes verb answers disabled itself)', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta', { providers: [] });
await initThroughA(t, f, a);
await b.ask('proxy.accounts', {});
assert.ok(b.bus.modelProxy, 'the client is held');
const view = await b.ask('routes');
assert.equal(view.catalogue.status, 'disabled');
assert.deepEqual(view.catalogue.notices, []);
});
test('a fenced catalogue on a picked-up client reads unavailable with no restart notice, since a restart would not help', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
const { proxy } = await initThroughA(t, f, a);
assert.equal(restartNotices(await b.ask('routes')), 1);
createProxyLock({ stateDir: join(f.dataRoot, 'proxy'), identity: `${f.configDir}:${proxy.port}` }).fence({ org: 'alpha' });
const view = await b.ask('routes');
assert.equal(view.catalogue.status, 'unavailable');
assert.ok(b.bus.modelProxy);
assert.equal(restartNotices(view), 0);
// A bus that booted with the proxy on the same fenced instance, which is what a restart gives,
// reads the same unavailable catalogue: the restart notice would have sent the owner nowhere.
const c = await orgBus(t, f, 'gamma', { binding: { proxy: loadProxy(f.configDir, f.dataRoot) } });
assert.equal((await c.ask('routes')).catalogue.status, 'unavailable');
// Once the fence is gone, the ready catalogue says again that a restart is needed.
fs.rmSync(join(f.dataRoot, 'proxy', 'mosaic-keys.uncertain'));
const after = await b.ask('routes');
assert.equal(after.catalogue.status, 'ready', JSON.stringify(after.catalogue));
assert.equal(restartNotices(after), 1);
});
test('refused settings still refuse the launch and name their cause, not the restart notice', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
await initThroughA(t, f, a);
await pickModel(b);
// The management key file stops being private: the start path refuses it.
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o644);
const view = await b.ask('routes');
assert.equal(view.catalogue.status, 'unavailable');
assert.equal(view.catalogue.reason, 'proxy-not-configured');
assert.equal(view.catalogue.notices.length, 1);
assert.match(view.catalogue.notices[0], /management key does not load/);
assert.equal(b.bus.modelProxy, null);
const host = await hostLauncher(t, f, b);
// The bus itself refuses now: the catalogue keeps the model it saw, marked unavailable.
assert.deepEqual(await host.launch(), { ok: false, error: 'model-provider-unavailable' });
assert.deepEqual(host.answers, [{ op: 'modelAdmission', answer: { allow: false, code: 'model-provider-unavailable' } }]);
assert.match(host.events().at(-1).why, /^model cliproxy\/claude\/claude-opus-5-5 unavailable \(model-provider-unavailable\)$/);
});
test('a dropped client refuses the next launch, and the catalogue names the cause instead of the restart notice', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
await initThroughA(t, f, a);
await pickModel(b);
const host = await hostLauncher(t, f, b);
assert.deepEqual(await host.launch(), { ok: false, error: 'model-provider-unavailable' });
assert.equal(restartNotices(await b.ask('routes')), 1);
fs.rmSync(join(f.configDir, 'proxy.json'));
// The next launch refuses with the same code and line.
assert.deepEqual(await host.launch(), { ok: false, error: 'model-provider-unavailable' });
assert.deepEqual(host.answers.map(a => a.answer), [{ allow: false, code: 'model-provider-unavailable' }, { allow: false, code: 'model-provider-unavailable' }]);
assert.equal(host.events().filter(e => e.event === 'refused').length, 2);
// The page's next read drops the client and names the cause.
const view = await b.ask('routes');
assert.equal(b.bus.modelProxy, null);
assert.equal(view.catalogue.status, 'unavailable');
assert.equal(view.catalogue.reason, 'proxy-not-configured');
assert.deepEqual(view.catalogue.notices.length, 1);
assert.match(view.catalogue.notices[0], /not been set up/);
});
test('the management key is in no stderr line, response, refusal, launch log or file the launch path writes', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
await initThroughA(t, f, a);
await pickModel(b);
const host = await hostLauncher(t, f, b);
const outputs = [];
const { lines } = await capture(async () => {
outputs.push(await b.ask('routes'));
outputs.push(await host.launch());
outputs.push(await b.bus.modelPreflight({ business: 'beta', role: 'coder' }));
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o644);
outputs.push(await b.ask('routes'));
outputs.push(await host.launch());
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o600);
outputs.push(await b.ask('routes'));
outputs.push(await host.launch());
});
outputs.push(host.answers, host.events());
assert.equal(lines.join('').includes(f.key), false, 'stderr');
assert.equal(JSON.stringify(outputs).includes(f.key), false, 'responses, answers and launch log');
const walk = dir => fs.readdirSync(dir, { withFileTypes: true }).flatMap(e => e.isDirectory() ? walk(join(dir, e.name)) : e.isFile() ? [join(dir, e.name)] : []);
const files = [...walk(b.dataRoot), ...walk(join(f.dataRoot, 'proxy')).filter(p => !p.endsWith('/config.yaml'))];
assert.ok(files.some(p => p === launchLogFile(b.dataRoot, 'beta')), 'the launch log is scanned');
for (const file of files) assert.equal(fs.readFileSync(file).includes(f.key), false, file);
});
+371
View File
@@ -0,0 +1,371 @@
// #1873 proxy-pickup: a running organization bus picks up the shared model proxy that another
// organization's bus initialized, with no restart, through the start path's loader and lock. A
// bus whose proxy settings the start path would refuse still refuses, and names the cause in one
// plain line. Real buses and a fake loopback management server; no live config.
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import http from 'node:http';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { createHash, randomBytes } from 'node:crypto';
import { startBroker } from '../src/runtime.mjs';
import { busOrganization } from '../src/organization-context.mjs';
import { createProxyLock } from '../src/proxy-lock.mjs';
import { Registry } from '../../seat/src/session.mjs';
import { loadBusiness } from '../../organization/src/organization-config.mjs';
import { resolveInstance } from '../../organization/src/resolve.mjs';
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
import { PIN } from '../../model-proxy/src/common.mjs';
import { initializeProxy, installPaths } from '../../model-proxy/src/settings.mjs';
import { FIXTURE_ENTRY, FIXTURE_KEY_NAME } from '../../plugins/model-provider/fixture/index.mjs';
const sha = value => createHash('sha256').update(value).digest('hex');
// A loopback stand-in for the proxy's management API: one ready Claude account with one model.
async function fakeProxy(t, key, port = 0) {
const seen = { calls: 0, wrongKey: 0 };
const server = http.createServer((req, res) => {
seen.calls++;
if (req.headers.authorization !== `Bearer ${key}`) { seen.wrongKey++; res.writeHead(401); res.end('{}'); return; }
const url = new URL(req.url, 'http://127.0.0.1');
const answer = url.pathname === '/v8/management/credentials' ? { files: [{ id: 'acct-1', name: 'acct-1', provider: 'claude', status: 'ready', priority: 1 }] }
: url.pathname === '/v8/management/credentials/models' ? { models: [{ id: 'claude-opus-5-5' }] }
: url.pathname === '/v8/management/config/api-keys/claude' ? []
: url.pathname === '/v8/management/config/access/api-keys' ? []
: null;
if (answer === null) { res.writeHead(404); res.end('{}'); return; }
res.end(JSON.stringify(answer));
});
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(port, '127.0.0.1', resolve); });
t.after(async () => { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); });
return { port: server.address().port, seen, server };
}
// One instance (config dir + data root) and a fake proxy install whose bytes a test pin names.
// The pin keeps the shipped version, so the pinned binary path is the one loadProxy accepts.
function instance(t) {
const root = fs.mkdtempSync(join(tmpdir(), 'pk-'));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const configDir = join(root, 'config'), dataRoot = join(root, 'instance');
fs.mkdirSync(join(configDir, 'secrets', 'agents'), { recursive: true, mode: 0o700 });
fs.chmodSync(join(configDir, 'secrets'), 0o700);
fs.mkdirSync(dataRoot, { mode: 0o700 });
const paths = installPaths(dataRoot);
fs.mkdirSync(join(paths.dir, 'static'), { recursive: true, mode: 0o700 });
fs.writeFileSync(paths.binary, 'fixture proxy binary\n', { mode: 0o700 });
fs.writeFileSync(paths.panel, '<!doctype html>\n', { mode: 0o600 });
const pin = { ...PIN, binary: { ...PIN.binary, sha256: sha('fixture proxy binary\n') }, panel: { ...PIN.panel, sha256: sha('<!doctype html>\n') } };
const rolesDir = rolesCopy(root), system = systemFor(root);
const key = `pk1873-${randomBytes(24).toString('hex')}`;
return { root, configDir, dataRoot, paths, pin, rolesDir, system, key, closers: [] };
}
// A real organization bus on the instance, started with no proxy (as cli bootConfig does when the
// instance has no proxy.json at boot).
async function orgBus(t, f, id, { providers = ['cliproxy'], edit = () => {}, org = { id }, binding = {} } = {}) {
const doc = businessDoc(f.root, id);
doc.plugins = { harness: ['pi'], 'model-provider': providers };
edit(doc);
writeJson(join(f.configDir, 'businesses', `${id}.json`), doc);
const business = loadBusiness(id, { dir: f.configDir, rolesDir: f.rolesDir }), resolved = {};
for (const name of Object.keys(business.roles)) resolved[name] = resolveInstance({ system: f.system, business, instance: name });
const dataRoot = join(f.root, `${id}-bus`); fs.mkdirSync(dataRoot, { mode: 0o700 }); new Registry(dataRoot);
const bus = await startBroker({ dataRoot, businesses: { [id]: busOrganization(business, resolved) },
organization: { dir: f.configDir, rolesDir: f.rolesDir, system: f.system },
instanceRoot: f.dataRoot, org, instanceDataRoot: f.dataRoot, instanceConfigDir: f.configDir,
initProxy: args => initializeProxy({ ...args, pin: f.pin }), ...binding });
t.after(() => bus.close().catch(() => {}));
const human = bus.broker.bindHuman({ business: id, human: 'jason', via: 'cli', outsideAgent: true });
const ask = (verb, args) => Promise.resolve().then(() => bus.broker.request(human, { verb, args }));
return { bus, ask, dataRoot };
}
// Every stderr line written while `run` is in flight.
async function capture(run) {
const write = process.stderr.write, lines = [];
process.stderr.write = (chunk, ...rest) => { lines.push(String(chunk)); return write.call(process.stderr, chunk, ...rest); };
try { return { value: await run(), lines }; } finally { process.stderr.write = write; }
}
// The proxy init through bus A writes proxy.json and the key, then the proxy starts on that port.
async function initThroughA(t, f, a) {
const settings = await a.ask('proxy.init', { key: f.key });
const proxy = await fakeProxy(t, f.key, settings.port);
return { settings, proxy };
}
const notConfigured = (pattern) => e => e.code === 'not-configured' && typeof e.reason === 'string' && pattern.test(e.reason) && e.reason.length <= 300;
test('a running organization bus answers accounts and a cliproxy catalogue once another bus initialized the shared proxy, with no restart', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
// Before init: bus B has nothing to pick up, and says so.
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/not been set up/));
const before = await b.ask('routes');
assert.equal(before.catalogue.status, 'unavailable');
assert.equal(before.catalogue.reason, 'proxy-not-configured');
assert.equal(before.catalogue.notices.length, 1);
const { proxy } = await initThroughA(t, f, a);
// Bus A holds the client from its own init; bus B picks it up at its next read.
assert.deepEqual((await a.ask('proxy.accounts', {})).accounts.map(r => r.provider), ['claude']);
const accounts = await b.ask('proxy.accounts', {});
assert.deepEqual(accounts.accounts.map(r => [r.provider, r.status]), [['claude', 'ready']]);
const after = await b.ask('routes');
assert.equal(after.catalogue.status, 'ready');
assert.deepEqual(after.catalogue.models.map(m => m.key), ['claude-claude-opus-5-5']);
assert.equal(after.catalogue.reason, undefined);
assert.equal(proxy.seen.wrongKey, 0);
// Bus B reached the proxy through the same lock identity the start path uses.
assert.ok(createProxyLock({ stateDir: join(f.dataRoot, 'proxy'), identity: `${f.configDir}:${proxy.port}` }));
});
test('a bus started before the proxy existed and asked only for a catalogue picks the proxy up', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
assert.equal((await b.ask('routes')).catalogue.status, 'unavailable');
await initThroughA(t, f, a);
assert.equal((await b.ask('routes')).catalogue.status, 'ready');
});
// Each case writes the shared proxy's files by hand into a state the start path refuses.
const refusedStates = [
['proxy.json names an unpinned binary', /settings do not load/, f => writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: '/elsewhere/cli-proxy-api' })],
['proxy.json is not JSON', /settings do not load/, f => fs.writeFileSync(join(f.configDir, 'proxy.json'), '{', { mode: 0o600 })],
['the management key file is readable by others', /management key does not load/, f => {
writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: f.paths.binary });
fs.writeFileSync(join(f.configDir, 'secrets', 'proxy-management.key'), f.key, { mode: 0o644 });
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o644);
}],
['the management key file is missing', /management key does not load/, f => writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: f.paths.binary })],
['the proxy state names another port', /proxy lock/, f => {
writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: f.paths.binary });
fs.writeFileSync(join(f.configDir, 'secrets', 'proxy-management.key'), f.key, { mode: 0o600 });
fs.mkdirSync(join(f.dataRoot, 'proxy'), { mode: 0o700 });
createProxyLock({ stateDir: join(f.dataRoot, 'proxy'), identity: `${f.configDir}:40998` });
}],
['the proxy state directory is open to others', /proxy lock/, f => {
writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: f.paths.binary });
fs.writeFileSync(join(f.configDir, 'secrets', 'proxy-management.key'), f.key, { mode: 0o600 });
fs.mkdirSync(join(f.dataRoot, 'proxy'), { mode: 0o755 }); fs.chmodSync(join(f.dataRoot, 'proxy'), 0o755);
}],
];
for (const [name, cause, arrange] of refusedStates) test(`no pickup when ${name}: accounts and catalogue refuse with the cause`, async t => {
const f = instance(t);
const b = await orgBus(t, f, 'beta');
arrange(f);
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(cause));
const view = await b.ask('routes');
assert.equal(view.catalogue.status, 'unavailable');
assert.equal(view.catalogue.reason, 'proxy-not-configured');
assert.match(view.catalogue.notices[0], cause);
assert.equal(b.bus.modelProxy, null);
});
// The host refuses fixture-proxy-configured only when a dev fixture is recorded for the instance.
const recordFixture = f => {
fs.mkdirSync(join(f.dataRoot, 'dev-fixture'), { mode: 0o700 });
fs.writeFileSync(join(f.dataRoot, 'dev-fixture', 'fixture.json'), '{}\n', { mode: 0o600 });
};
test('no pickup for an organization on the fixture model provider, which the host refuses with a proxy', async t => {
const f = instance(t);
recordFixture(f);
const a = await orgBus(t, f, 'alpha');
const b = await orgBus(t, f, 'beta', { providers: ['fixture', 'cliproxy'], edit: doc => { doc.launch.models[FIXTURE_KEY_NAME] = { ...FIXTURE_ENTRY }; } });
await initThroughA(t, f, a);
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
assert.equal(b.bus.modelProxy, null);
const view = await b.ask('routes');
assert.equal(view.catalogue.reason, 'proxy-not-configured');
assert.match(view.catalogue.notices[0], /fixture/);
});
test('no pickup for an organization whose launch table names the fixture module only', async t => {
const f = instance(t);
recordFixture(f);
const a = await orgBus(t, f, 'alpha');
const b = await orgBus(t, f, 'beta', { edit: doc => { doc.launch.models[FIXTURE_KEY_NAME] = { ...FIXTURE_ENTRY }; } });
await initThroughA(t, f, a);
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
assert.equal(b.bus.modelProxy, null);
});
test('an organization that lists the fixture still picks the proxy up when no fixture is recorded, as the host would start it', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha');
const b = await orgBus(t, f, 'beta', { providers: ['fixture', 'cliproxy'] });
await initThroughA(t, f, a);
await b.ask('proxy.accounts', {});
assert.ok(b.bus.modelProxy);
// A fixture recorded later drops the client at the next request.
recordFixture(f);
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
assert.equal(b.bus.modelProxy, null);
});
test('a picked-up client is dropped when proxy.json goes away, its port changes, or the key stops loading', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
const { settings } = await initThroughA(t, f, a);
await b.ask('proxy.accounts', {});
assert.ok(b.bus.modelProxy);
const file = join(f.configDir, 'proxy.json'), keyFile = join(f.configDir, 'secrets', 'proxy-management.key');
const saved = fs.readFileSync(file);
// Port change: the lock identity is bound to the port, so the bus refuses until a restart.
writeJson(file, { ...settings, port: settings.port + 1 === 8317 ? settings.port + 2 : settings.port + 1 });
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/proxy lock/));
assert.equal(b.bus.modelProxy, null);
assert.equal((await b.ask('routes')).catalogue.status, 'unavailable');
// Back to the original port, the bus picks it up again.
fs.writeFileSync(file, saved);
await b.ask('proxy.accounts', {});
assert.ok(b.bus.modelProxy);
// Key file no longer 0600.
fs.chmodSync(keyFile, 0o640);
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/management key does not load/));
assert.equal(b.bus.modelProxy, null);
fs.chmodSync(keyFile, 0o600);
await b.ask('proxy.accounts', {});
// proxy.json removed.
fs.rmSync(file);
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/not been set up/));
assert.equal(b.bus.modelProxy, null);
const view = await b.ask('routes');
assert.equal(view.catalogue.status, 'unavailable');
assert.equal(view.catalogue.reason, 'proxy-not-configured');
});
test('a default-instance bus (no organization binding, no lock) follows a port change to the new client, never the old one', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), d = await orgBus(t, f, 'delta', { org: null });
const { settings, proxy } = await initThroughA(t, f, a);
assert.deepEqual((await d.ask('proxy.accounts', {})).accounts.map(r => r.provider), ['claude']);
const old = d.bus.modelProxy;
// A listener on the new port that answers nothing to the right key: reaching it proves the move.
const moved = await fakeProxy(t, `other-${f.key.slice(0, 6)}`);
writeJson(join(f.configDir, 'proxy.json'), { ...settings, port: moved.port });
const before = proxy.seen.calls;
await assert.rejects(d.ask('proxy.accounts', {}));
assert.ok(d.bus.modelProxy && d.bus.modelProxy !== old);
assert.ok(moved.seen.calls > 0);
assert.equal(proxy.seen.calls, before);
});
test('a picked-up bus reads its catalogue through the shared lock: a fence makes it unavailable', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
const { proxy } = await initThroughA(t, f, a);
assert.equal((await b.ask('routes')).catalogue.status, 'ready');
createProxyLock({ stateDir: join(f.dataRoot, 'proxy'), identity: `${f.configDir}:${proxy.port}` }).fence({ org: 'alpha' });
const view = await b.ask('routes');
assert.equal(view.catalogue.status, 'unavailable');
assert.ok(b.bus.modelProxy, 'the client stays; the fence alone holds the catalogue');
});
test('a fixture record path in any state but absent counts as recorded', async t => {
const f = instance(t);
fs.writeFileSync(join(f.dataRoot, 'dev-fixture'), 'not a directory\n', { mode: 0o600 });
const a = await orgBus(t, f, 'alpha');
const b = await orgBus(t, f, 'beta', { providers: ['fixture', 'cliproxy'] });
await initThroughA(t, f, a);
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
assert.equal(b.bus.modelProxy, null);
});
test('with a fixture recorded, an organization document that no longer validates counts as using it', async t => {
const f = instance(t);
recordFixture(f);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
await initThroughA(t, f, a);
fs.writeFileSync(join(f.configDir, 'businesses', 'beta.json'), '{', { mode: 0o600 });
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
assert.equal(b.bus.modelProxy, null);
});
test('the bus that ran init also drops its client when the key stops loading', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha');
await initThroughA(t, f, a);
await a.ask('proxy.accounts', {});
fs.rmSync(join(f.configDir, 'secrets', 'proxy-management.key'));
await assert.rejects(a.ask('proxy.accounts', {}), notConfigured(/management key does not load/));
assert.equal(a.bus.modelProxy, null);
});
test('an init whose lock refuses leaves the init bus holding no client: no ready catalogue without the lock', async t => {
const f = instance(t);
// The real init, then the proxy state dir is left readable by the group, which the lock refuses.
const initProxy = async args => { const settings = await initializeProxy({ ...args, pin: f.pin });
fs.mkdirSync(join(f.dataRoot, 'proxy'), { recursive: true }); fs.chmodSync(join(f.dataRoot, 'proxy'), 0o750); return settings; };
const a = await orgBus(t, f, 'alpha', { binding: { initProxy } });
await assert.rejects(a.ask('proxy.init', { key: f.key }));
await fakeProxy(t, f.key, JSON.parse(fs.readFileSync(join(f.configDir, 'proxy.json'), 'utf8')).port);
assert.equal(a.bus.modelProxy, null);
const view = await a.ask('routes');
assert.equal(view.catalogue.status, 'unavailable');
assert.match(view.catalogue.notices[0], /proxy-lock-invalid/);
assert.equal(a.bus.modelProxy, null);
});
test('a default-instance bus (no lock generation) refreshes its catalogue at once on pickup and on drop', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), d = await orgBus(t, f, 'delta', { org: null });
// An unavailable catalogue read before init must not be served after the pickup.
assert.equal((await d.ask('routes')).catalogue.status, 'unavailable');
await initThroughA(t, f, a);
assert.equal((await d.ask('routes')).catalogue.status, 'ready');
// A ready catalogue read through the picked-up client must not be served after the drop.
fs.rmSync(join(f.configDir, 'proxy.json'));
const gone = await d.ask('routes');
assert.equal(gone.catalogue.status, 'unavailable');
assert.equal(gone.catalogue.reason, 'proxy-not-configured');
assert.equal(d.bus.modelProxy, null);
});
test('a picked-up bus clears a proxy fence through the same client and lock', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
await initThroughA(t, f, a);
// proxy.fence.clear on a bus that has not read anything yet still picks the proxy up first; the
// restart evidence check is the existing one and refuses this empty evidence.
await assert.rejects(b.ask('proxy.fence.clear', { evidence: {} }));
assert.ok(b.bus.modelProxy);
});
test('a bus serving an invalid organization id never picks up a proxy', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha');
await initThroughA(t, f, a);
let b;
try { b = await orgBus(t, f, 'beta', { binding: { instanceConfigDir: 42 } }); }
catch (error) { assert.equal(error.code, 'invalid-host-request'); return; }
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/organization binding/));
assert.equal(b.bus.modelProxy, null);
});
test('the management key is in no stderr line, response, refusal, event or file the pickup path writes', async t => {
const f = instance(t);
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
await initThroughA(t, f, a);
const outputs = [];
const { lines } = await capture(async () => {
outputs.push(await b.ask('proxy.accounts', {}));
outputs.push(await b.ask('routes'));
// A refusal after the key stops loading, and the pickup after it loads again.
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o644);
await b.ask('proxy.accounts', {}).catch(error => outputs.push({ code: error.code, reason: error.reason, message: error.message, next: error.next }));
outputs.push(await b.ask('routes'));
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o600);
outputs.push(await b.ask('proxy.accounts', {}));
});
assert.ok(lines.some(line => line.startsWith('model-proxy: ')), 'the pickup client writes its usual receipts');
assert.equal(lines.join('').includes(f.key), false, 'stderr');
assert.equal(JSON.stringify(outputs).includes(f.key), false, 'responses');
// Every file under bus B's data root (store, WAL, sessions) and the proxy state directory,
// except the proxy's own config.yaml that init writes for the proxy process.
const walk = dir => fs.readdirSync(dir, { withFileTypes: true }).flatMap(e => e.isDirectory() ? walk(join(dir, e.name)) : e.isFile() ? [join(dir, e.name)] : []);
const files = [...walk(b.dataRoot), ...walk(join(f.dataRoot, 'proxy')).filter(p => !p.endsWith('/config.yaml'))];
assert.ok(files.length > 0);
for (const file of files) assert.equal(fs.readFileSync(file).includes(f.key), false, file);
});
+117
View File
@@ -0,0 +1,117 @@
// #1873 triage-wiring: the Triage key file's reader, moved out of triage-key.mjs so the Console can
// read the key at each Triage spawn without loading the proxy management client or the proxy lock.
// It imports node built-ins only; packages/webui/tests/triage-wiring.test.mjs fails if the Console's
// Triage modules load model-proxy/src/client.mjs. triage-key.mjs (the owner's key verbs) imports
// these and re-exports what it exported before, so its callers are unchanged.
//
// <configDir>/secrets/triage/model (directory 0700, file 0600, the running user's)
// { version, key, keyId, issuedAt, rotateBy, retiring: [{ key, keyId, issuedAt, retireAt }] }
//
// Messages name files, never values.
import { closeSync, constants, fchmodSync, fstatSync, lstatSync, mkdirSync, openSync, readFileSync } from "node:fs";
import { createHash } from "node:crypto";
import { join } from "node:path";
export const KEY_VERSION = 1;
const FIELDS = Object.freeze(["version", "key", "keyId", "issuedAt", "rotateBy", "retiring"]);
const RETIRING_FIELDS = Object.freeze(["key", "keyId", "issuedAt", "retireAt"]);
const MAX_BYTES = 64 * 1024;
export const MAX_RETIRING = 16;
export const SECRET = /^[A-Za-z0-9_-]{43}$/;
const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
const DAY = /^\d{4}-\d{2}-\d{2}$/;
export class TriageKeyError extends Error {
constructor(code, message, exitCode = 3) {
super(message);
this.name = "TriageKeyError";
this.code = code;
this.exitCode = exitCode;
}
}
export const unreadable = (why) => new TriageKeyError("triage-key-unreadable", `the Triage key store is unreadable: ${why}`);
export const keyIdOf = (key) => createHash("sha256").update(key).digest("hex").slice(0, 12);
export const instanceTag = (configDir) => createHash("sha256").update(configDir).digest("hex").slice(0, 12);
export const keyPrefix = (configDir) => `mst-${instanceTag(configDir)}-triage-`;
const ownKey = (configDir) => (k) => typeof k === "string" && k.startsWith(keyPrefix(configDir)) && SECRET.test(k.slice(keyPrefix(configDir).length));
export function keyPaths(configDir) {
const secrets = join(configDir, "secrets");
const dir = join(secrets, "triage");
return { secrets, dir, file: join(dir, "model") };
}
const plain = (v) => v !== null && typeof v === "object" && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;
const exactKeys = (v, keys) => plain(v) && Object.keys(v).length === keys.length && keys.every((k) => Object.hasOwn(v, k));
const canonicalTime = (s) => typeof s === "string" && ISO.test(s) && !Number.isNaN(Date.parse(s)) && new Date(s).toISOString() === s;
const canonicalDay = (s) => typeof s === "string" && DAY.test(s) && !Number.isNaN(Date.parse(`${s}T00:00:00Z`)) && new Date(`${s}T00:00:00Z`).toISOString().slice(0, 10) === s;
export function validRecord(doc, configDir) {
const own = ownKey(configDir);
return exactKeys(doc, FIELDS)
&& doc.version === KEY_VERSION
&& own(doc.key) && doc.keyId === keyIdOf(doc.key)
&& canonicalTime(doc.issuedAt) && canonicalDay(doc.rotateBy)
&& Array.isArray(doc.retiring) && doc.retiring.length <= MAX_RETIRING
&& doc.retiring.every((r) => exactKeys(r, RETIRING_FIELDS) && own(r.key) && r.key !== doc.key && r.keyId === keyIdOf(r.key)
&& canonicalTime(r.issuedAt) && canonicalTime(r.retireAt))
&& new Set(doc.retiring.map((r) => r.key)).size === doc.retiring.length;
}
const uidOf = (opts) => opts.uid ?? process.getuid();
// A real directory, the running user's, mode 0700. Missing: false, or made when `create` is on.
export function checkDir(dir, uid, what, { create }) {
let st;
try { st = lstatSync(dir); }
catch (e) {
if (e.code !== "ENOENT") throw unreadable(`${what} cannot be read`);
if (!create) return false;
try { mkdirSync(dir, { mode: 0o700 }); } catch { throw unreadable(`${what} cannot be made`); }
try {
const fd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
try { fchmodSync(fd, 0o700); } finally { closeSync(fd); }
} catch { throw unreadable(`${what} cannot be made`); }
st = lstatSync(dir);
}
if (st.isSymbolicLink()) throw unreadable(`${what} is a link`);
if (!st.isDirectory()) throw unreadable(`${what} is not a directory`);
if (st.uid !== uid) throw unreadable(`${what} is not owned by this user`);
if ((st.mode & 0o777) !== 0o700) throw unreadable(`${what} mode is not 0700`);
return true;
}
export function checkFile(fd, uid, what, fail = unreadable) {
const st = fstatSync(fd);
if (!st.isFile()) throw fail(`${what} is not a regular file`);
if (st.nlink !== 1) throw fail(`${what} has more than one link`);
if (st.uid !== uid) throw fail(`${what} is not owned by this user`);
if ((st.mode & 0o777) !== 0o600) throw fail(`${what} mode is not 0600`);
return st;
}
// The stored record, or null when no key is issued. Throws triage-key-unreadable on anything unsafe.
// opts.uid (both directories and the file) and opts.fileUid (the file only) are test seams for the
// owner checks.
export function readKey(configDir, opts = {}) {
const { secrets, dir, file } = keyPaths(configDir);
const uid = uidOf(opts);
if (!checkDir(secrets, uid, "the secrets directory", { create: false })) return null;
if (!checkDir(dir, uid, "the triage secrets directory", { create: false })) return null;
let fd;
try { fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
catch (e) {
if (e.code === "ENOENT") return null;
if (e.code === "ELOOP") throw unreadable("the key file is a link");
throw unreadable("the key file cannot be opened");
}
try {
const st = checkFile(fd, opts.fileUid ?? uid, "the key file");
if (st.size > MAX_BYTES) throw unreadable("the key file is too large");
let doc;
try { doc = JSON.parse(readFileSync(fd, "utf8")); } catch { throw unreadable("the key file is not JSON"); }
if (!validRecord(doc, configDir)) throw unreadable("the key file does not match the key shape");
return doc;
} finally { closeSync(fd); }
}
+8 -106
View File
@@ -22,15 +22,19 @@
//
// Nothing here prints, logs or records a key. Receipts and output carry key ids only (the first 12
// hex of sha256(key), the id the bus uses). Messages name files and options, never values.
// Nothing wires the key into the Triage host or the Console; that is a later slice.
import { closeSync, constants, fchmodSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, unlinkSync, writeSync } from "node:fs";
import { createHash, randomBytes } from "node:crypto";
// The Console reads the key at each Triage spawn through triage-key-file.mjs, never this module.
import { closeSync, constants, fchmodSync, fsyncSync, lstatSync, openSync, readdirSync, renameSync, unlinkSync, writeSync } from "node:fs";
import { randomBytes } from "node:crypto";
import { join } from "node:path";
import { isDeepStrictEqual } from "node:util";
import { createProxyLock } from "../../bus/src/proxy-lock.mjs";
import { loadManagementClient } from "../../model-proxy/src/client.mjs";
export const KEY_VERSION = 1;
// #1873 triage-wiring: the key file's reader lives in triage-key-file.mjs (node built-ins only), so
// the Console reads the key without this module's proxy client. Re-exported here unchanged.
import { KEY_VERSION, MAX_RETIRING, SECRET, TriageKeyError, checkDir, checkFile, instanceTag, keyIdOf, keyPaths, keyPrefix, readKey, unreadable, validRecord } from "./triage-key-file.mjs";
export { KEY_VERSION, TriageKeyError, instanceTag, keyIdOf, keyPaths, keyPrefix, readKey, validRecord };
// The org key lifetime and the rotate grace default. The bus holds both as literals
// (packages/bus/src/model-keys.mjs #issue: 30 days to rotateBy; packages/bus/src/runtime.mjs:
// `proxy.rotateGraceMinutes ?? 120`) and exports neither, and this lane may not change the bus.
@@ -39,113 +43,11 @@ export const KEY_VERSION = 1;
// The grace itself is read from proxy.json first, where the bus reads it.
export const KEY_LIFETIME_DAYS = 30;
export const DEFAULT_ROTATE_GRACE_MINUTES = 120;
const FIELDS = Object.freeze(["version", "key", "keyId", "issuedAt", "rotateBy", "retiring"]);
const RETIRING_FIELDS = Object.freeze(["key", "keyId", "issuedAt", "retireAt"]);
const MAX_BYTES = 64 * 1024;
const MAX_RETIRING = 16;
const SECRET = /^[A-Za-z0-9_-]{43}$/;
const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
const DAY = /^\d{4}-\d{2}-\d{2}$/;
export class TriageKeyError extends Error {
constructor(code, message, exitCode = 3) {
super(message);
this.name = "TriageKeyError";
this.code = code;
this.exitCode = exitCode;
}
}
const unreadable = (why) => new TriageKeyError("triage-key-unreadable", `the Triage key store is unreadable: ${why}`);
export const keyIdOf = (key) => createHash("sha256").update(key).digest("hex").slice(0, 12);
export const instanceTag = (configDir) => createHash("sha256").update(configDir).digest("hex").slice(0, 12);
export const keyPrefix = (configDir) => `mst-${instanceTag(configDir)}-triage-`;
const sentinelPrefix = (configDir) => `msk-${instanceTag(configDir)}-sentinel-`;
const ownKey = (configDir) => (k) => typeof k === "string" && k.startsWith(keyPrefix(configDir)) && SECRET.test(k.slice(keyPrefix(configDir).length));
// Any name under the prefix is Triage's to remove, well formed or not, as the bus treats its own
// `msk-<tag>-` names. Only a well-formed key may be stored in the file.
const inNamespace = (configDir) => (k) => typeof k === "string" && k.startsWith(keyPrefix(configDir));
export function keyPaths(configDir) {
const secrets = join(configDir, "secrets");
const dir = join(secrets, "triage");
return { secrets, dir, file: join(dir, "model") };
}
const plain = (v) => v !== null && typeof v === "object" && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;
const exactKeys = (v, keys) => plain(v) && Object.keys(v).length === keys.length && keys.every((k) => Object.hasOwn(v, k));
const canonicalTime = (s) => typeof s === "string" && ISO.test(s) && !Number.isNaN(Date.parse(s)) && new Date(s).toISOString() === s;
const canonicalDay = (s) => typeof s === "string" && DAY.test(s) && !Number.isNaN(Date.parse(`${s}T00:00:00Z`)) && new Date(`${s}T00:00:00Z`).toISOString().slice(0, 10) === s;
export function validRecord(doc, configDir) {
const own = ownKey(configDir);
return exactKeys(doc, FIELDS)
&& doc.version === KEY_VERSION
&& own(doc.key) && doc.keyId === keyIdOf(doc.key)
&& canonicalTime(doc.issuedAt) && canonicalDay(doc.rotateBy)
&& Array.isArray(doc.retiring) && doc.retiring.length <= MAX_RETIRING
&& doc.retiring.every((r) => exactKeys(r, RETIRING_FIELDS) && own(r.key) && r.key !== doc.key && r.keyId === keyIdOf(r.key)
&& canonicalTime(r.issuedAt) && canonicalTime(r.retireAt))
&& new Set(doc.retiring.map((r) => r.key)).size === doc.retiring.length;
}
const uidOf = (opts) => opts.uid ?? process.getuid();
// A real directory, the running user's, mode 0700. Missing: false, or made when `create` is on.
function checkDir(dir, uid, what, { create }) {
let st;
try { st = lstatSync(dir); }
catch (e) {
if (e.code !== "ENOENT") throw unreadable(`${what} cannot be read`);
if (!create) return false;
try { mkdirSync(dir, { mode: 0o700 }); } catch { throw unreadable(`${what} cannot be made`); }
try {
const fd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
try { fchmodSync(fd, 0o700); } finally { closeSync(fd); }
} catch { throw unreadable(`${what} cannot be made`); }
st = lstatSync(dir);
}
if (st.isSymbolicLink()) throw unreadable(`${what} is a link`);
if (!st.isDirectory()) throw unreadable(`${what} is not a directory`);
if (st.uid !== uid) throw unreadable(`${what} is not owned by this user`);
if ((st.mode & 0o777) !== 0o700) throw unreadable(`${what} mode is not 0700`);
return true;
}
function checkFile(fd, uid, what, fail = unreadable) {
const st = fstatSync(fd);
if (!st.isFile()) throw fail(`${what} is not a regular file`);
if (st.nlink !== 1) throw fail(`${what} has more than one link`);
if (st.uid !== uid) throw fail(`${what} is not owned by this user`);
if ((st.mode & 0o777) !== 0o600) throw fail(`${what} mode is not 0600`);
return st;
}
// The stored record, or null when no key is issued. Throws triage-key-unreadable on anything unsafe.
// opts.uid (both directories and the file) and opts.fileUid (the file only) are test seams for the
// owner checks.
export function readKey(configDir, opts = {}) {
const { secrets, dir, file } = keyPaths(configDir);
const uid = uidOf(opts);
if (!checkDir(secrets, uid, "the secrets directory", { create: false })) return null;
if (!checkDir(dir, uid, "the triage secrets directory", { create: false })) return null;
let fd;
try { fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
catch (e) {
if (e.code === "ENOENT") return null;
if (e.code === "ELOOP") throw unreadable("the key file is a link");
throw unreadable("the key file cannot be opened");
}
try {
const st = checkFile(fd, opts.fileUid ?? uid, "the key file");
if (st.size > MAX_BYTES) throw unreadable("the key file is too large");
let doc;
try { doc = JSON.parse(readFileSync(fd, "utf8")); } catch { throw unreadable("the key file is not JSON"); }
if (!validRecord(doc, configDir)) throw unreadable("the key file does not match the key shape");
return doc;
} finally { closeSync(fd); }
}
// Both directories, checked (and the triage one made) before any proxy write, so a store that cannot
// be written refuses while the proxy is still untouched.
function prepareDirs(configDir, uid) {
+5 -5
View File
@@ -1,12 +1,12 @@
{
"version": "8.0.42",
"version": "8.0.79",
"archive": {
"asset": "CLIProxyAPI_8.0.42_linux_amd64.tar.gz",
"url": "https://github.com/router-for-me/CLIProxyAPI/releases/download/v8.0.42/CLIProxyAPI_8.0.42_linux_amd64.tar.gz",
"sha256": "1e10566fe0042ed5a9b80306cf296bf86a83de590cd967a8b177988b896c8d31"
"asset": "CLIProxyAPI_8.0.79_linux_amd64.tar.gz",
"url": "https://github.com/router-for-me/CLIProxyAPI/releases/download/v8.0.79/CLIProxyAPI_8.0.79_linux_amd64.tar.gz",
"sha256": "7cbb9709ed939ff3684e10592a45cc538ee82d5da759daa7e18c7e1dc3b32acd"
},
"binary": {
"sha256": "74661f8cf949977870231359e8bf4344e07946766a673c08fa25a3e5f72fc95e"
"sha256": "4968bd27b04f813849add020b95a26dc7abf3e30810b97d291c0d88f495fe601"
},
"panel": {
"version": "1.26.7",
+21 -1
View File
@@ -56,8 +56,28 @@ uses a new temp root and private network namespace, with made-up credentials,
and calls the page's bus verbs. If the page cannot pass against the new PIN,
the upgrade stops; Sage takes Q46's fallback to Jason through Mos.
A running instance takes a new PIN in three steps, in this order. First run
the new code: update the checkout or pinned copy the bus, Console and proxy
unit start from. The proxy unit runs `run.mjs` and reads `PIN` from the tree
it was installed from (`systemctl --user cat <unit>` shows it). If the new
code is in that same tree, a restart picks it up. If it is in another tree,
the unit keeps the old PIN until it is installed again from the new tree:
stop it, remove its unit file, then `mosaic proxy unit install` from the new
tree, because install never replaces a unit. Then run `mosaic proxy fetch` (or Fetch on the Console's
Model proxy settings), which installs the new release beside the old one
under `<dataRoot>/opt/cliproxyapi/<version>/`. Then restart the proxy with
`mosaic proxy unit restart`. Before the fetch, the proxy fails to start with
`proxy-install-missing`, and keeps failing until the fetch is done. Restart the bus and
Console as well so their status and Fetch use the new PIN. `proxy.json` keeps
the path from the PIN it was created under. That path stays valid, and the
proxy always runs the hash-verified install for the current PIN. Signed-in
accounts in `<dataRoot>/proxy/auth/` carry over. From 8.0.55 the proxy also
keeps a `kv.db` file in that directory, and 8.0.79 adds a `.codex-refresh/`
directory there once a Codex account renews. The old release directory can be
removed once the new one runs.
The native `/proxy` page uses human-only bus verbs; only the bus holds the
management client. On PIN 8.0.42, OAuth credentials are listed under
management client. On PIN 8.0.79, OAuth credentials are listed under
`/v8/management/credentials` and priority is patched as `{name, priority}`.
API-key groups are absent from that list: z.ai keys use the Claude-compatible
provider configuration at `/v8/management/config/api-keys/claude`, whose keys
+13 -2
View File
@@ -7,12 +7,23 @@ export function installPaths(dataRoot, pin = PIN) {
const dir = path.join(dataRoot, 'opt', 'cliproxyapi', pin.version);
return { dir, binary: path.join(dir, 'cli-proxy-api'), panel: path.join(dir, 'static', 'management.html') };
}
// proxy.json records the install path from the PIN it was created under, and nothing
// rewrites it. After a PIN bump an earlier release's path under this data root stays valid
// and reads as the current pinned path. The runner executes only the hash-verified pinned
// install, never the recorded path, so an old value selects nothing.
const releasePath = (dataRoot, binary) => {
const base = path.join(dataRoot, 'opt', 'cliproxyapi') + path.sep;
if (typeof binary !== 'string' || !binary.startsWith(base)) return false;
const [version, file, ...rest] = binary.slice(base.length).split(path.sep);
return rest.length === 0 && file === 'cli-proxy-api' && /^\d{1,4}\.\d{1,4}\.\d{1,6}$/.test(version);
};
export function validateProxy(value, dataRoot, pin = PIN) {
const pinned = installPaths(dataRoot, pin).binary;
if (!value || typeof value !== 'object' || Array.isArray(value)
|| Object.keys(value).some(k => !['binary', 'port', 'rotateGraceMinutes', 'firstOutputSeconds'].includes(k))
|| !Number.isInteger(value.port) || value.port < 1024 || value.port > 65535 || value.port === 8317
|| value.binary !== installPaths(dataRoot, pin).binary) refuse('proxy-config-invalid');
return { port: value.port, binary: value.binary, ...proxyTiming(value) };
|| (value.binary !== pinned && !releasePath(dataRoot, value.binary))) refuse('proxy-config-invalid');
return { port: value.port, binary: pinned, ...proxyTiming(value) };
}
export function loadProxy(configDir, dataRoot) {
const file = path.join(configDir, 'proxy.json');
@@ -1,4 +1,4 @@
// P4 pinned8.0.42 regression: absent Claude provider config is HTTP404.
// P4 regression since PIN 8.0.42 (still so on 8.0.79): absent Claude provider config is HTTP 404.
// Normalize only that fixed GET; all other methods/endpoints/statuses refuse.
import test from 'node:test';
import assert from 'node:assert/strict';
@@ -13,3 +13,13 @@ for (const [field, maximum, code] of [['rotateGraceMinutes', 1440, 'proxy-rotate
assert.throws(() => validateProxy({ ...base, [field]: value }, dataRoot), e => e.code === code);
});
}
// #1873 PIN bump: a sidecar written under an earlier PIN keeps the instance config valid.
test('a proxy.json written under an earlier PIN reads as the current pinned path', () => {
const old = { ...base, binary: '/fixture-data/opt/cliproxyapi/8.0.42/cli-proxy-api' };
assert.deepEqual(validateProxy(old, dataRoot), base);
assert.deepEqual(validateProxy({ ...old, rotateGraceMinutes: 60 }, dataRoot), { ...base, rotateGraceMinutes: 60 });
});
for (const binary of ['/bad', '/fixture-data/opt/cliproxyapi/8.0.42/other', '/fixture-data/opt/cliproxyapi/latest/cli-proxy-api',
'/fixture-data/opt/cliproxyapi/8.0.42/x/cli-proxy-api', '/fixture-data/opt/cliproxyapi/../cliproxyapi/8.0.42/cli-proxy-api',
'/other-data/opt/cliproxyapi/8.0.42/cli-proxy-api', '/fixture-data/opt/cliproxyapi/8.0/cli-proxy-api', 7])
test(`proxy-config-invalid: binary ${JSON.stringify(binary)}`, () => assert.throws(() => validateProxy({ ...base, binary }, dataRoot), e => e.code === 'proxy-config-invalid'));
@@ -1,4 +1,4 @@
// P4 client contract; endpoint/payload cases are probe-pending on PIN8.0.42.
// P4 client contract; endpoint/payload cases were probe-pending on PIN 8.0.42.
// Catches management key/body/query values leaking into receipts, redirects/retries.
import test from 'node:test';
import assert from 'node:assert/strict';
+45 -5
View File
@@ -35,9 +35,12 @@ import { actionTrail } from './action-trail.mjs';
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';
import { triageRouteRoutes } from './triage-route-http.mjs';
import { RESTART_EXIT } from '../../cli/src/org-console.mjs';
import { ID_PATTERN, systemOnly } from '../../organization/src/vocabulary.mjs';
import { selectorScope } from '../../organization/src/selector.mjs';
@@ -231,10 +234,30 @@ try {
},
}) : null;
// #1873 triage-host: the instance owner's Triage session, hosted by the Console in no organization.
// Nothing starts here; the owner's first message starts it. No model route store exists without a
// schema change, so none is wired: the read says no route is set and nothing launches.
const triage = system && (devOwner || ssoOn) ? createTriageHost({
dataRoot: system.dataRoot, route: null, devOwner: Boolean(devOwner), log: line => console.log(line),
// Nothing starts here; the owner's first message starts it.
// #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.
// 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 () => {
// A bus that does not answer gives a catalogue that is not ready, so it confirms nothing.
const read = async call => { try { return (await call('routes'))?.catalogue ?? { status: 'unknown' }; } catch { return { status: 'bus-unavailable' }; } };
if (!organizations) return ownerCall ? [await read(ownerCall)] : [];
const active = (await listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance })).filter(org => org.state === 'active');
return Promise.all(active.map(org => {
let paths; try { paths = orgPaths(org.root); } catch { return { status: 'bus-unavailable' }; }
return read(busCaller({ dataRoot: org.root, socket: paths.socket, business: org.id, timeoutMs: 5000 }));
}));
},
}) : null;
const triage = triageRoute ? createTriageHost({
dataRoot: system.dataRoot, route: triageRoute, proxyPort: triageProxyPort, tokenPattern: triageTokenPattern(configDir()),
devOwner: Boolean(devOwner), log: line => console.log(line),
readSnapshot: () => composeSnapshot({
orgs: () => listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance }),
busStatus: id => orgBusStatus({ configDir: configDir(), dataRoot: system.dataRoot, instance }, id),
@@ -262,8 +285,25 @@ try {
const trail = system ? actionTrail(join(system.dataRoot, 'console')) : null;
// #1873 A2: Settings > Agents writes for the root and project levels, beside the levels read.
const agentLevelRoutes = organizations ? [agentLevelsWriteRoutes({ configDir: configDir(), rolesDir: rolesDir(), trail })] : [];
// #1873 triage-settings: Settings > Triage reads and saves the route store for the instance owner.
// The model is checked against the catalogue each active organization's bus answers with, as an
// organization's route change is. Saving starts nothing: the Triage host above reads the store at its
// next admit.
const triageRoutes = system && (devOwner || ssoOn) ? [triageRouteRoutes({
dataRoot: system.dataRoot, log: line => console.log(line),
catalogues: async () => {
const read = async call => { try { return (await call('routes'))?.catalogue ?? { status: 'unknown' }; } catch { return { status: 'bus-unavailable' }; } };
if (!organizations) return ownerCall ? [await read(ownerCall)] : [];
// Every active organization is asked at once, so a hung bus costs one timeout, not one each.
const active = (await listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance })).filter(org => org.state === 'active');
return Promise.all(active.map(org => {
let paths; try { paths = orgPaths(org.root); } catch { return { status: 'bus-unavailable' }; }
return read(busCaller({ dataRoot: org.root, socket: paths.socket, business: org.id, timeoutMs: 5000 }));
}));
},
})] : [];
try {
server = await startServer({ restart, trail, ...(lan ? { host: lan.bind, devAllowHosts: lan.allowHosts } : {}), board: options.board, port: options.port, bus, reads, preview: options.preview, sso, proof, send, settingsWrite, projectFiles, projectFile, libraryFile, libraryStatus, librarySync, libraryRevert, librarySave, projectSave, chat, nameWrite, assistantStop, instancesWrite, routesWrite, agentWrite, proxyCall, proxyAdmin, triage, processing, decisionWrite, requestRead, requestWrite, userOptions, federation: system?.federation ?? null, organizations, agentLevels, routes: agentLevelRoutes, preselect: options.business ?? null });
server = await startServer({ restart, trail, ...(lan ? { host: lan.bind, devAllowHosts: lan.allowHosts } : {}), board: options.board, port: options.port, bus, reads, preview: options.preview, sso, proof, send, settingsWrite, projectFiles, projectFile, libraryFile, libraryStatus, librarySync, libraryRevert, librarySave, projectSave, chat, nameWrite, assistantStop, instancesWrite, routesWrite, agentWrite, proxyCall, proxyAdmin, triage, processing, decisionWrite, requestRead, requestWrite, userOptions, federation: system?.federation ?? null, organizations, agentLevels, routes: [...agentLevelRoutes, ...triageRoutes], preselect: options.business ?? null });
} catch (err) { sso?.close(); throw err; }
if (testerSocket) {
const call = busCaller({ dataRoot: system.dataRoot, socket: socketPath(system.dataRoot), business: options.business ?? null, cli: TESTER_CLI });
+3 -1
View File
@@ -51,6 +51,8 @@ export async function proxyHTTP(req, res, { path, call, named, tester, granted,
try { return done(200, await call(route[1], args)); }
catch (error) {
const code = codes.includes(error.code) || (path === '/api/proxy/plugins/set' && typeof error.code === 'string' && /^[a-z-]{1,64}$/.test(error.code)) ? error.code : 'outcome-unknown';
return done(code === 'human-required' ? 403 : ['not-configured', 'proxy-down', 'outcome-unknown'].includes(code) ? 503 : 400, { error: code });
// proxy-pickup (#1873): a bus with no proxy client names the cause in one plain line.
const reason = code === 'not-configured' && typeof error.reason === 'string' ? { reason: error.reason.slice(0, 300) } : {};
return done(code === 'human-required' ? 403 : ['not-configured', 'proxy-down', 'outcome-unknown'].includes(code) ? 503 : 400, { error: code, ...reason });
}
}
+12 -7
View File
@@ -321,7 +321,7 @@
// render. That read marks nothing. Row 150 (#1644): since row 142, `mosaic talk` prints only the
// reply to its own message, so a reply to a Console send stays unread in `mosaic inbox` until
// the human marks it read with `mosaic inbox --read`.
let isTester = false, orgSetup = false;
let isTester = false, orgSetup = false, triageOwner = false;
// Both Docs and project direction use the same owner-only records setup action.
window.MosaicRecords = {
markup(project) {
@@ -1334,7 +1334,7 @@
// fixture-only catalogue is ready with no model proxy). Before the setting is read it falls
// back to the catalogue status, as before.
const proxyOn=harnessSettings?(Array.isArray(harnessSettings.plugins?.['model-provider'])?harnessSettings.plugins['model-provider']:[]).includes('cliproxy'):v.catalogue?.status!=='disabled';
const catalogueInfo=v.catalogue?.status==='ready'?`<p class="small muted">Model catalogue, observed ${txt(window.MosaicLocalTime(v.catalogue.observedAt))}.</p>${(v.catalogue.notices??[]).map(n=>`<p class="small warn">${txt(n)}</p>`).join('')}`:v.catalogue?.status==='unavailable'?`<p class="small warn">Model catalogue unavailable. A change to thinking alone still saves.${proxyOn&&canSend&&!isTester?' A new model needs the catalogue: set up the model proxy, or, when this organization has none, press Disable model proxy catalogue.':''}</p>`:'<p class="small muted">Model catalogue not enabled.</p>';
const catalogueInfo=v.catalogue?.status==='ready'?`<p class="small muted">Model catalogue, observed ${txt(window.MosaicLocalTime(v.catalogue.observedAt))}.</p>${(v.catalogue.notices??[]).map(n=>`<p class="small warn">${txt(n)}</p>`).join('')}`:v.catalogue?.status==='unavailable'?`<p class="small warn">Model catalogue unavailable. A change to thinking alone still saves.${proxyOn&&canSend&&!isTester?' A new model needs the catalogue: set up the model proxy, or, when this organization has none, press Disable model proxy catalogue.':''}</p>${(v.catalogue.notices??[]).map(n=>`<p class="small warn">${txt(n)}</p>`).join('')}`:'<p class="small muted">Model catalogue not enabled.</p>';
const providerControl=canSend&&!isTester?`<button type="button" id="provider-switch" data-enabled="${proxyOn}">${proxyOn?'Disable':'Enable'} model proxy catalogue</button><p role="status" id="provider-switch-status"></p>`:'';
const harnessControl=canSend&&!isTester?`<fieldset class="harness-control"><legend>Harnesses</legend>${harnessSettings ? [...new Set([...harnessSettings.available.harness,...(harnessSettings.plugins.harness??[])])].map(name=>`<label><input type="checkbox" data-harness="${esc(name)}"${(harnessDraft??harnessSettings.plugins.harness??[]).includes(name)?' checked':''}${harnessBusy?' disabled':''}> ${txt(name)}</label>`).join('')+` <button type="button" id="harness-save"${harnessBusy?' disabled':''}>Save harnesses</button>` : '<p>Harnesses unavailable.</p><button type="button" id="harness-read">Read harnesses again</button>'}<p role="status" id="harness-status">${txt(harnessReceipt)}</p></fieldset>`:'';
const off=v.harnesses.length?'':'<p class="small warn">No harness is switched on in the business file\'s plugins.harness, so every launch refuses harness-off and a save that changes a harness refuses too. Switch one on under Harnesses.</p>';
@@ -1431,13 +1431,13 @@
});
});
// Organizations is writable when the server allows org setup, which it does in organization mode where send is off.
const readonlyPage = page => !['appearance','business','console'].includes(page) && !(page === 'organizations' && orgSetup) && !canSend && !isTester;
const settingsPages = [['appearance','Appearance'],['business',window.MosaicOrg ? 'Organization' : 'Business'],['limits','Launch limits'],['organizations','Organizations'],['routing','Routing'],['agents','Agents'],['assistant','Assistant'],['tokens','Token use'],['model-proxy','Model proxy'],['console','Console'],['about','About']];
const readonlyPage = page => !['appearance','business','console','triage'].includes(page) && !(page === 'organizations' && orgSetup) && !canSend && !isTester;
const settingsPages = [['appearance','Appearance'],['business',window.MosaicOrg ? 'Organization' : 'Business'],['limits','Launch limits'],['organizations','Organizations'],['routing','Routing'],['agents','Agents'],['assistant','Assistant'],['tokens','Token use'],['model-proxy','Model proxy'],['triage','Triage'],['console','Console'],['about','About']];
function markSettingsDraft(page,dirty){const link=document.querySelector(`#settings-nav a[href="#/settings/${page}"]`), dot='<span class="settings-dot"><span class="sr-only">unsaved changes</span></span>';if(link && dirty && !link.querySelector('.settings-dot'))link.insertAdjacentHTML('beforeend',dot);const button=$('settings-disclosure');if(dirty && link?.getAttribute('aria-current')==='page' && button && !button.querySelector('.settings-dot'))button.querySelector('strong').insertAdjacentHTML('beforeend',dot);}
function settingsShell(page, body, helper='') {
const pages=isTester ? [['appearance','Appearance'],['business','Business'],['routing','Routing'],['agents','Agents'],['names','Seat names']] : settingsPages;
const pages=isTester ? [['appearance','Appearance'],['business','Business'],['routing','Routing'],['agents','Agents'],['names','Seat names']] : settingsPages.filter(([id])=>id!=='triage'||triageOwner);
const label=pages.find(([id])=>id===page)?.[1] || 'Appearance';
const isDirty=id=>(id==='routing'&&routesDraft)||(id==='limits'&&settingsInputs)||(id==='assistant'&&(Object.keys(assistantDrafts).length||Object.keys(nameDrafts).length))||(id==='agents'&&(instanceDrafts.add||instanceDrafts.change||window.MosaicAgentLevels?.hasDraft()));
const isDirty=id=>(id==='routing'&&routesDraft)||(id==='limits'&&settingsInputs)||(id==='assistant'&&(Object.keys(assistantDrafts).length||Object.keys(nameDrafts).length))||(id==='agents'&&(instanceDrafts.add||instanceDrafts.change||window.MosaicAgentLevels?.hasDraft()))||(id==='triage'&&window.MosaicTriageRoute?.hasDraft());
const dot='<span class="settings-dot"><span class="sr-only">unsaved changes</span></span>';
const links=pages.map(([id,name],i)=>`${i===0?'<div class="settings-group">Your browser</div>':i===1?'<div class="settings-group">This instance</div>':''}<a href="#/settings/${id}"${id===page?' aria-current="page"':''}>${name}${isDirty(id)?dot:''}</a>`).join('') + (!isTester ? '<div class="settings-group">Members and invites <span class="muted">not built</span></div>' : '');
return `${head('Settings','Your browser and this instance.')}<div class="settings-layout"><div><button type="button" id="settings-disclosure" aria-expanded="false" aria-controls="settings-nav"><small>Section</small><strong>${label}${isDirty(page)?dot:''}</strong><span aria-hidden="true">⌄</span></button><nav id="settings-nav" aria-label="Settings sections">${links}</nav></div><div class="settings-content settings-${page}${readonlyPage(page)?' readonly':''}"><h2 id="settings-page-title" tabindex="-1">${label}</h2><p class="settings-helper">${helper}</p>${readonlyPage(page)?'<p class="settings-readonly">Read only. Saving requires signed-in send mode.</p>':''}${body}</div></div>`;
@@ -1447,6 +1447,8 @@
if (page==='appearance') return settingsShell(page,appearance(),'These choices apply in this browser only.');
if (page==='organizations') { await configRead; return settingsShell(page,window.MosaicOrgCreate.markup,'Create an organization from the template, edit it, and finish its setup. Each step names the command it equals.'); }
if (page==='model-proxy') { await configRead; return settingsShell(page,window.MosaicProxy.markup,'Accounts used by this instance, their priority, and the client keys assigned to agents.'); }
// #1873 triage-settings: Settings > Triage (triage-settings.js), the instance owner's only; anyone else gets no page.
if (page==='triage') { await configRead; if (!triageOwner || !window.MosaicTriageRoute) return notFound(); return settingsShell(page,await window.MosaicTriageRoute.section(),'The model, harness and daily token cap Triage uses. Only the instance owner sees this page.'); }
// F3: Settings > Console (console-restart.js). The server checks the owner, Origin and trail.
if (page==='console') { await configRead; await window.MosaicConsole?.ready; return settingsShell(page,window.MosaicConsole?.settingsMarkup()??'<section class="panel"><p class="small warn">The Console controls did not load. Reload the page to try again.</p></section>','Restart this Console on the same address and configuration.'); }
const s = await get.api('settings'), n = s.notifier || {};
@@ -1910,13 +1912,14 @@
return;
}
if (path === '/settings/model-proxy' && shown === route && !navigated) return;
if (path === '/settings/triage' && shown === route && !navigated) return;
if (path === '/settings/organizations' && shown?.split('?')[0] === path && !navigated) return;
if (($('agent-dialog')?.open || document.querySelector('.agent-more[aria-expanded=true]')) && !navigated) { schedule(); return; }
if (navigated || (shown !== route && !samePage)) { unmountEditor(); $('bus-view').innerHTML = loadingView(SOURCES[path.split('/')[1]]); }
let html, state = 'fresh';
const live = reader(true);
try {
if (path !== '/organization' && !wsPage(path) && !(path === '/agents' && window.MosaicOrg) && path !== '/messages' && !['/settings','/settings/appearance','/settings/model-proxy','/settings/organizations'].includes(path)) {
if (path !== '/organization' && !wsPage(path) && !(path === '/agents' && window.MosaicOrg) && path !== '/messages' && !['/settings','/settings/appearance','/settings/model-proxy','/settings/triage','/settings/organizations'].includes(path)) {
try { await live('messages'); }
catch (err) { messageCount = null; if (err.status === 403 || err.code === 'signed-out') throw err; }
}
@@ -2113,6 +2116,8 @@
canMessage = !isTester && c?.send === true;
chatSend = !isTester && c?.chatSend === true;
orgSetup = !isTester && c?.orgSetup === true;
// #1873 triage-settings: /api/config says proxyAdmin to the instance owner only, the same owner /api/triage/route serves.
triageOwner = !isTester && c?.proxyAdmin === true;
window.MosaicOrgBusStopped = !!window.MosaicOrg && c?.devOwner === true && orgSetup && c?.chatSend === false;
// The header menu offers "New organization" to the owner of a Console with organizations; nobody else can create one.
if (orgSetup && !$('org-new-link')) { const a = document.createElement('a'); a.id = 'org-new-link'; a.href = NEW_ORG; a.setAttribute('role', 'menuitem'); a.textContent = 'New organization'; document.querySelector('#avatar-menu > a[href="#/settings"]')?.after(a); }
+4 -2
View File
@@ -268,6 +268,8 @@ window.MosaicLocalTime = value => Number.isFinite(Date.parse(value)) ? new Date(
if (code === 'triage-route-invalid' || code === 'triage-start-failed') return { text: `Not delivered: ${code === 'triage-start-failed' ? 'the Triage session did not start' : 'Triage did not start because the model route\'s answer is not usable'}. The message is in the conversation with the Console's note; it was not sent again.` };
// This one can come before or after the message is written.
if (code === 'triage-dir-unsafe') return { text: 'Not delivered: a Triage directory is not safe to use. It was not sent again: check the conversation before you send it again.' };
// A route refusal (no key, the proxy down, the model no longer offered, ...) carries its own plain line.
if (status === 409 && /^triage-(key|proxy|model|catalogue|harness)-/.test(code) && typeof body.message === 'string') return { text: `Not sent. ${clean(body.message)}` };
if (status === 409) return { text: code === 'triage-no-route' ? 'Not sent: no model route is set for Triage. The instance owner sets the route first.' : code === 'triage-route-refused' ? 'Not sent: the model route refused to start Triage.' : 'Not sent: the model route\'s answer is not usable.' };
if (status === 429) {
if (code === 'triage-over-cap') {
@@ -345,7 +347,7 @@ window.MosaicLocalTime = value => Number.isFinite(Date.parse(value)) ? new Date(
return orgOf(hash) || !seg || system(hash) ? hash : `#/orgs/${seg}${path}`;
};
// /api/workspace reads and writes across organizations (U1, U4b), so it keeps its own path.
const unscoped = /^\/api\/(?:workspace(?:\/|\?|$)|orgs(?:\/|$)|themes(?:\?|$)|org-template(?:\?|$)|org-presets(?:\?|$)|org-admin(?:\?|$)|org-validate(?:\?|$)|agents\/(?:levels|root)(?:\?|$))/;
const unscoped = /^\/api\/(?:workspace(?:\/|\?|$)|orgs(?:\/|$)|themes(?:\?|$)|org-template(?:\?|$)|org-presets(?:\?|$)|org-admin(?:\?|$)|org-validate(?:\?|$)|agents\/(?:levels|root)(?:\?|$)|triage\/route(?:\?|$))/;
window.fetch = (input, init) => {
if (typeof input !== 'string' || !input.startsWith('/api/') || unscoped.test(input)) return nativeFetch(input, init);
const send = () => nativeFetch(current ? `/api/orgs/${current}/${input.slice('/api/'.length)}` : input, init);
@@ -545,7 +547,7 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
const value = await response.json();
if (!response.ok) {
if (response.status === 401) throw new Error('Sign in to the Console again.');
if (value.error === 'not-configured') throw new Error('The model proxy is not configured for this instance.');
if (value.error === 'not-configured') throw new Error(typeof value.reason === 'string' && value.reason ? value.reason : 'The model proxy is not configured for this instance.');
if (value.error === 'proxy-down') throw Object.assign(new Error('The model proxy is not answering.'), { code: 'proxy-down' });
if (value.error === 'login-state-unknown') throw new Error('This sign-in has ended. Start a new sign-in.');
if (value.error === 'apikey-invalid') throw new Error('Enter a nonempty API key, with no spaces or line breaks.');
+1 -1
View File
@@ -7,7 +7,7 @@
<link rel="stylesheet" href="/processing.css"><script src="/processing.js" defer></script>
<link rel="stylesheet" href="/org-create.css"><script src="/org-create.js" defer></script>
<link rel="stylesheet" href="/onboarding.css"><script src="/onboarding.js" defer></script><link rel="stylesheet" href="/assistant.css">
<script src="/workspace-data.js" defer></script><script src="/workspace.js" defer></script><script src="/tasks-board.js" defer></script><script src="/console-restart.js" defer></script><script src="/display.js" defer></script><script src="/inbox.js" defer></script><script src="/files.js" defer></script><script src="/project-remote.js" defer></script><link rel="stylesheet" href="/autonomy.css"><script src="/autonomy.js" defer></script><link rel="stylesheet" href="/packs.css"><script src="/packs.js" defer></script><link rel="stylesheet" href="/workflows.css"><script src="/workflows.js" defer></script><script src="/federation.js" defer></script><script src="/brand.js" defer></script><script src="/app.js" defer></script><script src="/markdown-preview.js" defer></script><script src="/projects.js" defer></script><script src="/chat.js" defer></script><script src="/project-archive.js" defer></script><script src="/agents.js" defer></script><script src="/assistant.js" defer></script><script src="/bus.js" defer></script><script src="/requests.js" defer></script></head>
<script src="/workspace-data.js" defer></script><script src="/workspace.js" defer></script><script src="/tasks-board.js" defer></script><script src="/console-restart.js" defer></script><script src="/triage-settings.js" defer></script><script src="/display.js" defer></script><script src="/inbox.js" defer></script><script src="/files.js" defer></script><script src="/project-remote.js" defer></script><link rel="stylesheet" href="/autonomy.css"><script src="/autonomy.js" defer></script><link rel="stylesheet" href="/packs.css"><script src="/packs.js" defer></script><link rel="stylesheet" href="/workflows.css"><script src="/workflows.js" defer></script><script src="/federation.js" defer></script><script src="/brand.js" defer></script><script src="/app.js" defer></script><script src="/markdown-preview.js" defer></script><script src="/projects.js" defer></script><script src="/chat.js" defer></script><script src="/project-archive.js" defer></script><script src="/agents.js" defer></script><script src="/assistant.js" defer></script><script src="/bus.js" defer></script><script src="/requests.js" defer></script></head>
<body>
<div hidden aria-hidden="true"><select id="palette"></select><select id="mode"><option value="system">system</option><option value="light">light</option><option value="dim">dim</option><option value="dark">dark</option></select></div>
<a class="skip" href="#main">Skip to content</a>
+1 -1
View File
@@ -14,7 +14,7 @@
const value = await response.json();
if (!response.ok) {
if (response.status === 401) throw new Error('Sign in to the Console again.');
if (value.error === 'not-configured') throw new Error('The model proxy is not configured for this instance.');
if (value.error === 'not-configured') throw new Error(typeof value.reason === 'string' && value.reason ? value.reason : 'The model proxy is not configured for this instance.');
if (value.error === 'login-state-unknown') throw new Error('This sign-in has ended. Start a new sign-in.');
if (value.error === 'apikey-invalid') throw new Error('Enter a nonempty API key, with no spaces or line breaks.');
throw new Error('The operation could not be confirmed. Nothing was retried. Refresh before trying again.');
@@ -0,0 +1,133 @@
/* #1873 triage-settings: Settings > Triage, the instance owner's Triage model route. It reads and
writes /api/triage/route (triage-route-http.mjs); the server checks the owner, the Origin and Fetch
Metadata, the catalogue and the time zone, and the store keeps the revision and the receipt. This
file only asks and reports. bus.js offers the page only when /api/config says proxyAdmin, which is
the same owner the route serves. The fields live here, so a repaint keeps what the owner typed. */
(() => {
'use strict';
const $ = id => document.getElementById(id);
const esc = s => String(s).replace(/[&<>"']/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[c]);
const PATH = '/api/triage/route';
// The time zones this browser knows, offered as suggestions only; the server decides.
const zones = (() => { try { return Intl.supportedValuesOf('timeZone'); } catch { return []; } })();
// draft holds the typed fields; base is the revision they were read at, which a save sends, so a
// change saved elsewhere since then answers as stale instead of being written over.
let data = null, readLine = '', draft = null, base = 0, busy = false, line = '', conflict = false;
const call = init => window.fetch(PATH, { cache: 'no-store', redirect: 'manual', ...init });
// The stored route's fields, or empty ones: there is no default model, harness, cap or zone.
const fromRoute = r => ({ model: r?.model ?? '', harness: r?.harness ?? '', cap: r ? String(r.cap) : '', timeZone: r?.timeZone ?? '' });
async function read() {
readLine = '';
let r, a = null;
try { r = await call({ method: 'GET' }); } catch { data = null; readLine = 'The Triage route could not be read. Reload the page to try again.'; return; }
try { a = await r.json(); } catch { /* refused below */ }
if (r.type === 'opaqueredirect') { data = null; readLine = 'Sign in again, then reload.'; return; }
if (!r.ok || !a || typeof a !== 'object') { data = null; readLine = typeof a?.message === 'string' ? a.message : 'The Triage route could not be read.'; return; }
// An untouched page follows the store; typed fields stay, with the revision they were read at.
const untouched = draft === null || JSON.stringify(draft) === JSON.stringify(fromRoute(data?.route ?? null));
data = a;
if (untouched) { draft = fromRoute(a.route); base = Number.isSafeInteger(a.revision) ? a.revision : 0; }
}
const models = () => (Array.isArray(data?.choices?.models) ? data.choices.models : []);
const harnessesOf = key => models().find(m => m.key === key)?.harnesses ?? [];
function stored() {
const r = data?.route;
if (!r) return `<p id="triage-route-stored"><strong>Not set.</strong> ${esc(data?.routeMessage ?? 'No Triage route is set.')}</p>`;
return `<p id="triage-route-stored">Stored route: <strong>${esc(r.model)}</strong> on <strong>${esc(r.harness)}</strong>, cap ${esc(Number(r.cap).toLocaleString('en-US'))} tokens a day, reset ${esc(r.reset)} ${esc(r.timeZone)}. Revision ${esc(r.revision)}, saved ${esc(r.updatedAt)}.</p>`;
}
function form() {
const d = draft ?? fromRoute(null);
const send = data?.send === true;
const off = busy || !send ? ' disabled' : '';
const list = models();
const offered = list.some(m => m.key === d.model);
const modelOptions = `<option value=""${d.model === '' ? ' selected' : ''}>Choose a model</option>`
+ (d.model && !offered ? `<option value="${esc(d.model)}" selected>${esc(d.model)} (not offered now)</option>` : '')
+ list.map(m => `<option value="${esc(m.key)}"${m.key === d.model ? ' selected' : ''}${m.available ? '' : ' disabled'}>${esc(m.label && m.label !== m.key ? `${m.label} (${m.key})` : m.key)}${m.available ? '' : ' (unavailable)'}</option>`).join('');
const hs = harnessesOf(d.model);
const harnessOptions = `<option value=""${d.harness === '' ? ' selected' : ''}>${d.model ? 'Choose a harness' : 'Choose a model first'}</option>`
+ (d.harness && !hs.includes(d.harness) ? `<option value="${esc(d.harness)}" selected>${esc(d.harness)} (not offered for this model)</option>` : '')
+ hs.map(h => `<option value="${esc(h)}"${h === d.harness ? ' selected' : ''}>${esc(h)}</option>`).join('');
const status = data?.choices?.status;
const catalogue = status === 'ready' ? '' : `<p class="small warn" id="triage-route-catalogue">No organization bus offered a model catalogue (${esc(status ?? 'unknown')}), so no route can be saved now.</p>`;
const conflictButton = conflict ? ' <button type="button" class="btn" id="triage-route-reload">Reload route</button>' : '';
return `${catalogue}<form id="triage-route-form" novalidate><div class="filters">`
+ `<label>Model <select id="triage-route-model"${off}>${modelOptions}</select></label>`
+ `<label>Harness <select id="triage-route-harness"${off}>${harnessOptions}</select></label>`
+ `<label>Daily cap, tokens <input id="triage-route-cap" type="number" min="1" step="1" inputmode="numeric" value="${esc(d.cap)}"${off}></label>`
+ `<label>Reset <input id="triage-route-reset" value="00:00" readonly aria-readonly="true"></label>`
+ `<label>Time zone <input id="triage-route-zone" list="triage-route-zones" autocomplete="off" spellcheck="false" placeholder="Region/City" value="${esc(d.timeZone)}"${off}></label>`
+ `</div><datalist id="triage-route-zones">${zones.map(z => `<option value="${esc(z)}"></option>`).join('')}</datalist>`
+ `<div class="settings-actions"><button type="submit" id="triage-route-save"${off}>${busy ? 'Saving…' : 'Save'}</button>${conflictButton} <span role="status" id="triage-route-line" tabindex="-1">${esc(line)}</span></div>`
+ `</form>${send ? '' : '<p class="small muted" id="triage-route-readonly">This Console is read-only, so the route cannot be saved here.</p>'}`;
}
function markup() {
const body = data === null
? `<p class="small warn">${esc(readLine || 'The Triage route could not be read.')}</p><p role="status" id="triage-route-line" tabindex="-1">${esc(line)}</p>`
: `${stored()}${form()}<p class="small muted" id="triage-route-note">${esc(data.note ?? 'Saving a route does not start Triage. The key and the wiring are separate.')}</p>`;
return `<section class="panel" id="triage-route" aria-labelledby="triage-route-title"><h3 id="triage-route-title">Triage model route</h3>${body}</section>`;
}
const repaint = () => { const panel = $('triage-route'); if (panel) panel.outerHTML = markup(); };
// Called by bus.js when the page draws: a fresh read each time, the typed fields kept.
async function section() { await read(); return markup(); }
async function save() {
if (busy || data?.send !== true) return;
const d = draft ?? fromRoute(null);
const cap = /^\d+$/.test(d.cap.trim()) ? Number(d.cap.trim()) : d.cap.trim() === '' ? null : Number.NaN;
const body = { model: d.model, harness: d.harness, cap: Number.isFinite(cap) ? cap : d.cap, timeZone: d.timeZone.trim(), revision: base };
busy = true; conflict = false; line = 'Saving…'; repaint();
let r, a = null;
try { r = await call({ method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) }); }
catch { busy = false; line = 'Outcome unknown. Reload the route to see whether it was saved.'; conflict = true; repaint(); return; }
try { a = await r.json(); } catch { /* refused below */ }
busy = false;
if (r.ok && a?.route) {
data = { ...data, route: a.route, revision: a.revision, routeMessage: undefined };
draft = fromRoute(a.route); base = a.revision;
line = `Saved. Revision ${a.revision}.`;
repaint();
$('triage-route-line')?.focus?.();
return;
}
if (r.type === 'opaqueredirect') line = 'Sign in again, then retry.';
else line = typeof a?.message === 'string' ? a.message : 'The route was refused. Nothing was saved.';
if (r.status === 409 && a?.error === 'triage-route-stale') conflict = true;
repaint();
$(conflict ? 'triage-route-reload' : 'triage-route-save')?.focus();
}
async function reload() {
if (busy) return;
draft = null; conflict = false; line = '';
await read();
line = data ? 'Route read again. Your fields now show the stored route.' : '';
repaint();
}
const field = { 'triage-route-model': 'model', 'triage-route-harness': 'harness', 'triage-route-cap': 'cap', 'triage-route-zone': 'timeZone' };
document.addEventListener('input', e => {
const k = field[e.target?.id]; if (!k || !$('triage-route')) return;
draft = { ...(draft ?? fromRoute(null)), [k]: e.target.value };
});
document.addEventListener('change', e => {
if (e.target?.id !== 'triage-route-model') return;
// A new model narrows the harness list; a harness the model does not take is cleared.
const d = draft = { ...(draft ?? fromRoute(null)), model: e.target.value };
const hs = harnessesOf(d.model);
if (!hs.includes(d.harness)) d.harness = hs.length === 1 ? hs[0] : '';
repaint(); $('triage-route-model')?.focus();
});
document.addEventListener('submit', e => { if (e.target?.id !== 'triage-route-form') return; e.preventDefault(); save(); });
document.addEventListener('click', e => { if (e.target?.id === 'triage-route-reload') reload(); });
const hasDraft = () => !!(draft && data && JSON.stringify(draft) !== JSON.stringify(fromRoute(data.route)));
window.MosaicTriageRoute = { section, hasDraft };
})();
+25 -17
View File
@@ -1117,16 +1117,21 @@
};
const DRAWER_LIMIT = 5, STOP_REASON = 'Stopped from the agent drawer.';
const STOP_UNKNOWN = 'Outcome unknown. Read the agents again before trying; nothing was retried.';
// The Stop state of the drawer's agent survives repaints: `confirm` while it asks, `busy` while the
// SP20: an organization that refuses a Stop or a Restart, in the same line for both.
const orgRefusal = (e, orgName) => e === 'org-archived' ? `${orgName} is archived, so its agents are not changed. Nothing changed.`
: e === 'org-unavailable' ? `${orgName} is not available to this Console. Nothing changed.` : '';
// Each drawer agent's question and Stop survive repaints: `confirm` while it asks, `busy` while the
// request is open, then the result in words beside the button. SP19: `kind` says whether Stop or
// Restart asks (no kind is Stop). While either asks or runs, both buttons wait.
let stopState = null; // { id, kind, confirm, busy, text }
// Restart asks (no kind is Stop). While either asks or runs, both buttons wait. SP20: kept per agent,
// as restarts are, so two Stops in two drawers each keep their line. An entry goes when the drawer
// closes after it finished, when its question is cancelled, or when its Restart starts.
const stops = new Map(); // id -> { kind, confirm, busy, text }
// SP19: each drawer agent's restart, kept per agent so several can run at once and none loses its
// line when another drawer asks or stops: `busy` while it runs, then its outcome in words. An entry
// goes when a new question starts on that agent, or when the drawer closes after it finished.
const restarts = new Map(); // id -> { busy, text }
// The state a drawer draws: its own question or Stop first, else its restart.
const actState = id => stopState?.id === id ? stopState : restarts.has(id) ? { id, kind: 'restart', ...restarts.get(id) } : null;
const actState = id => stops.has(id) ? { id, ...stops.get(id) } : restarts.has(id) ? { id, kind: 'restart', ...restarts.get(id) } : null;
const RESTART_REASON = 'restart from the agent drawer';
let drawerFocus = false, drawerShown = null;
// The drawer holds at most one history entry. A row opened from the page without a drawer pushes one
@@ -1396,7 +1401,7 @@
// Closing goes Back when a row link on this page pushed the current entry; otherwise it drops
// `agent` from the address in place. Either way the page repaints without it and focus returns to the row.
function closeDrawer() {
if (!stopState?.busy) stopState = null;
for (const [k, st] of stops) if (!st.busy) stops.delete(k);
for (const [k, r] of restarts) if (!r.busy) restarts.delete(k);
if (stateOf()?.[DRAWER_MARK] && queryOf().has('agent')) { history.back(); return; }
history.replaceState(unmarked(), '', sansAgent());
@@ -1433,10 +1438,11 @@
}
async function stopAgent(d) {
const id = d.dataset.wsDrawer, org = d.dataset.drawerOrgId, name = id.slice(org.length + 1), orgName = d.dataset.drawerOrgName;
stopState = { id, busy: true, text: `Stopping ${name}…` };
stops.set(id, { busy: true, text: `Stopping ${name}…` });
const repaint = () => document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));
const paint = () => { const now = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"]`); if (!now) return; const s = now.querySelector('[data-drawer-status]'); if (s) s.textContent = stopState.text; const b = now.querySelector('[data-drawer-stop]'); if (b) { b.disabled = !!stopState.busy; b.firstChild.textContent = stopState.busy ? 'Stopping…' : 'Stop'; } const rb = now.querySelector('[data-drawer-restart]'); if (rb) rb.disabled = !!stopState.busy; now.querySelector('[data-drawer-stop-confirm]')?.remove(); };
const finish = text => { stopState = { id, text }; paint(); const b = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"] [data-drawer-stop]`); b?.focus({ preventScroll: true }); };
// Paint this agent's drawer in place, when it is on screen.
const paint = () => { const now = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"]`), st = stops.get(id); if (!now || !st) return; const s = now.querySelector('[data-drawer-status]'); if (s) s.textContent = st.text; const b = now.querySelector('[data-drawer-stop]'); if (b) { b.disabled = !!st.busy; b.firstChild.textContent = st.busy ? 'Stopping…' : 'Stop'; } const rb = now.querySelector('[data-drawer-restart]'); if (rb) rb.disabled = !!st.busy; now.querySelector('[data-drawer-stop-confirm]')?.remove(); };
const finish = text => { stops.set(id, { text }); paint(); const b = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"] [data-drawer-stop]`); b?.focus({ preventScroll: true }); };
paint();
let res, body = null;
try { res = await fetch(`/api/orgs/${enc(org)}/agent/terminate`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ instance: name, reason: STOP_REASON }), cache: 'no-store', redirect: 'manual' }); }
@@ -1444,6 +1450,8 @@
if (res.type === 'opaqueredirect' || res.status === 401) return finish('Not stopped: sign in again, then read the agents.');
try { body = await res.json(); } catch { body = null; }
if (!res.ok || body?.sent !== true && !body?.request) {
const orgWhy = orgRefusal(body?.error, orgName);
if (orgWhy) return finish(`Not stopped: ${orgWhy}`);
if (body?.sent === false && typeof body.message === 'string' && body.message) return finish(`Not stopped: ${body.message}`);
if (res.status === 403 && body?.error === 'read-only') return finish(`Not stopped: this Console only reads agents in ${orgName} (read-only).`);
if (body?.sent === false && typeof body.error === 'string') return finish(`Not stopped: the bus refused (${body.error}).`);
@@ -1451,14 +1459,14 @@
}
const request = typeof body?.request === 'string' ? body.request : '';
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(request)) return finish(STOP_UNKNOWN);
stopState = { id, busy: true, text: `Stop asked: request ${request} is open.` }; paint();
stops.set(id, { busy: true, text: `Stop asked: request ${request} is open.` }); paint();
for (let tries = 0; tries < 90; tries++) {
await new Promise(r => setTimeout(r, 2000));
let r = null;
try { const got = await fetch(`/api/orgs/${enc(org)}/agent/request?id=${enc(request)}`, { cache: 'no-store', redirect: 'manual' }); if (got.type === 'opaqueredirect') return finish('Sign in again, then read the agents.'); r = got.ok ? await got.json() : null; } catch { continue; }
if (r?.request === request && r.state === 'closed') {
if (r.reason) return finish(`Not stopped: ${r.reason}.`);
stopState = { id, text: `Stopped ${name}.` }; repaint(); return;
stops.set(id, { text: `Stopped ${name}.` }); repaint(); return;
}
}
finish(`Request ${request} is still open. Read the agents again later.`);
@@ -1472,8 +1480,8 @@
if (d?.status === 403 && e === 'read-only') return `this Console only reads agents in ${orgName} (read-only).`;
if (d?.status === 403 && (e === 'unavailable' || e === 'owner-only')) return `this Console is not the owner of ${orgName}, so it does not restart its agents.`;
if (e === 'no-bus-host') return `the bus of ${orgName} is not running. Nothing changed.`;
if (e === 'org-archived') return `${orgName} is archived, so its agents are not changed. Nothing changed.`;
if (e === 'org-unavailable') return `${orgName} is not available to this Console. Nothing changed.`;
const orgWhy = orgRefusal(e, orgName);
if (orgWhy) return orgWhy;
if (d?.result?.sent === false && typeof m === 'string' && m) return m;
if (d?.result?.sent === false && typeof e === 'string') return `the bus refused (${e}).`;
return STOP_UNKNOWN;
@@ -1501,11 +1509,11 @@
const thread = d.querySelector('[data-drawer-restart]')?.dataset.drawerThread === 'yes';
if (restarts.get(id)?.busy) return;
// The question is answered: from now on this agent's drawer draws its restart.
if (stopState?.id === id) stopState = null;
stops.delete(id);
restarts.set(id, { busy: true, text: '' });
// Paint this agent's drawer in place, when it is on screen and no question of its own holds it.
const paint = () => {
const now = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"]`), r = restarts.get(id); if (!now || !r || stopState?.id === id) return;
const now = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"]`), r = restarts.get(id); if (!now || !r || stops.has(id)) return;
const s = now.querySelector('[data-drawer-status]'); if (s) s.textContent = r.text;
const rb = now.querySelector('[data-drawer-restart]'), sb = now.querySelector('[data-drawer-stop]');
if (rb) { rb.disabled = r.busy; rb.firstChild.textContent = r.busy ? 'Restarting…' : 'Restart'; }
@@ -1548,11 +1556,11 @@
if (e.target.closest('[data-drawer-close]')) { closeDrawer(); return; }
const id = d.dataset.wsDrawer;
// SP19: nothing starts while this agent's Stop or Restart runs.
const held = (stopState?.id === id && stopState.busy) || !!restarts.get(id)?.busy;
const held = !!stops.get(id)?.busy || !!restarts.get(id)?.busy;
const ask = e.target.closest('[data-drawer-stop]') ? 'stop' : e.target.closest('[data-drawer-restart]') ? 'restart' : null;
if (ask && !held) {
const b = d.querySelector(`[data-drawer-${ask}]`), name = d.dataset.drawerName, who = `${name} in ${d.dataset.drawerOrgName}`;
stopState = { id, kind: ask, confirm: true, text: '' }; restarts.delete(id);
stops.set(id, { kind: ask, confirm: true, text: '' }); restarts.delete(id);
d.querySelector('.ws-drawer-confirm')?.remove();
b.insertAdjacentHTML('afterend', ask === 'stop' ? stopConfirm(name, who) : restartConfirm(name, who, b.dataset.drawerThread === 'yes'));
for (const x of d.querySelectorAll('[data-drawer-stop], [data-drawer-restart]')) x.disabled = true;
@@ -1569,7 +1577,7 @@
// Cancel the Stop or Restart question: nothing sent, both buttons enabled again, focus on the one that asked.
function cancelStop(d) {
const kind = d.querySelector('[data-drawer-restart-confirm]') ? 'restart' : 'stop';
stopState = null;
stops.delete(d.dataset.wsDrawer);
d.querySelector('.ws-drawer-confirm')?.remove();
for (const x of d.querySelectorAll('[data-drawer-stop], [data-drawer-restart]')) x.disabled = false;
d.querySelector(`[data-drawer-${kind}]`)?.focus({ preventScroll: true });
+1 -1
View File
@@ -66,7 +66,7 @@ const files = new Map([
['/', ['index.html', 'text/html; charset=utf-8']],
['/proxy.js', ['proxy.js', 'text/javascript; charset=utf-8']],
['/project-remote.js', ['project-remote.js', 'text/javascript; charset=utf-8']],
...['app.js', 'brand.js', 'bus.js', 'markdown-preview.js', 'chat.js', 'project-archive.js', 'processing.js', 'agents.js', 'federation.js', 'requests.js', 'org-create.js', 'console-restart.js', 'onboarding.js', 'workspace-data.js', 'workspace.js', 'tasks-board.js', 'assistant.js'].map(f => ['/' + f, [f, 'text/javascript; charset=utf-8']]),
...['app.js', 'brand.js', 'bus.js', 'markdown-preview.js', 'chat.js', 'project-archive.js', 'processing.js', 'agents.js', 'federation.js', 'requests.js', 'org-create.js', 'console-restart.js', 'onboarding.js', 'workspace-data.js', 'workspace.js', 'tasks-board.js', 'assistant.js', 'triage-settings.js'].map(f => ['/' + f, [f, 'text/javascript; charset=utf-8']]),
['/files.js', ['files.js', 'text/javascript; charset=utf-8']],
['/projects.js', ['projects.js', 'text/javascript; charset=utf-8']],
['/projects.css', ['projects.css', 'text/css; charset=utf-8']],
+150 -19
View File
@@ -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";
@@ -74,6 +85,28 @@ export const NOT_TAKEN = "Triage stopped before it read this message. Send it ag
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.";
// #1873 triage-wiring: two turns in a row whose usage reads unknown end the session (cap fail closed).
export const UNKNOWN_TURNS_MAX = 2;
export const USAGE_UNKNOWN_ENDED = "Triage ended this session: the token usage of two turns in a row could not be read, so the daily cap could not be kept. The next message starts a new session.";
// #1873 triage-wiring: the route's refusals (triage-route.mjs), each with the one plain line the
// drawer shows. None carries a path, a key or an env value.
export const ROUTE_REFUSALS = Object.freeze({
"triage-key-missing": "No Triage key is issued, so Triage will not start. The instance owner issues one first.",
"triage-key-unsafe": "The Triage key file is not safe to use, so Triage will not start.",
"triage-proxy-down": "The model proxy is not running, so Triage will not start.",
"triage-model-not-offered": "The model in the Triage route is no longer offered, so Triage will not start.",
"triage-catalogue-unavailable": "No model catalogue can confirm the Triage model now, so Triage will not start.",
"triage-harness-incompatible": "The harness in the Triage route cannot run its model, so Triage will not start.",
});
// The host's own words for each code message() throws.
const ADMIT_MESSAGES = Object.freeze({
"triage-over-cap": "Triage is over its daily cap",
"triage-no-route": "no model route is set for Triage",
"triage-usage-unreadable": "Triage's token usage could not be read",
"triage-route-unreadable": "the model route could not be read",
"triage-dir-unsafe": "a Triage directory is not a real directory",
...ROUTE_REFUSALS,
});
// 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$/;
@@ -85,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;
@@ -121,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;
}
@@ -256,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
@@ -264,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];
@@ -288,6 +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
// 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 = () => {
@@ -333,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 });
@@ -341,8 +420,11 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
// turns/ left by the last run stops the next one at its first turn.
for (const f of ["session.json", "relay.sock", "tools.sock", "turn.done", "turns"]) rmSync(join(a, f), { recursive: true, force: true });
// The session's history stays across starts, unless the session put something other than a
// real directory there.
try { realDir(join(a, "session")); } catch { freshDir(join(a, "session")); }
// 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.
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");
const workspace = join(a, "work");
@@ -352,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 ?? "",
@@ -387,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"); }
@@ -404,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.
@@ -429,6 +523,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
const result = delta(base, await sample(u));
const known = result.status === "known";
if (!known) log(`triage host: turn usage unknown: ${result.code}`);
u.unknownInRow = known ? 0 : u.unknownInRow + 1;
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 {
@@ -437,6 +532,25 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
usageLost = true;
log(`triage host: usage line not written: ${e?.code ?? "error"}`);
}
if (u.unknownInRow >= UNKNOWN_TURNS_MAX && !u.halted) endUnknown(u);
};
// #1873 triage-wiring: the cap cannot be kept on turns it cannot count, so the second unknown turn
// in a row ends the session: a host line says why, the runner is told its session is gone at its
// next state or receive, and the process group gets SIGTERM, then SIGKILL. The next message starts
// a new run with a fresh session directory.
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"); }
const c = u.child;
if (!c || child !== c) return;
try { process.kill(-c.pid, "SIGTERM"); } catch {}
const t = setTimeout(() => { if (child === c) { try { process.kill(-c.pid, "SIGKILL"); } catch {} } }, 5000);
t.unref?.();
};
const usageEnd = async (u, turn) => {
u.inTurn = false;
@@ -485,6 +599,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
// refused by admit. A state read inside a turn (a continuation) is not stopped: the turn finishes.
const capStop = (verb) => {
const u = usage;
if (u?.halted) return true;
if (verb === "state" && u?.inTurn) return false;
const c = capNow();
if (!refusedByCap(c)) return false;
@@ -499,11 +614,17 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
return true;
};
const start = async (answer) => {
const { admitted, installRoots, transcriptRoot } = await checkAdmitted(answer);
const start = async ({ admitted, installRoots, transcriptRoot, mod }) => {
ensureDirs();
const run = writeBundle(admitted);
const u = { run, root: transcriptRoot, harness: admitted.harness, baselines: new Map(), ended: new Set(), inTurn: false };
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");
relay = await createRelay({
@@ -523,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);
@@ -552,6 +673,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
closeSync(out);
}
const mine = child;
u.child = mine;
const closeRelay = () => {
relay?.close();
relay = null;
@@ -608,7 +730,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
let a;
try { a = await route.admit(); } catch { return { ok: false, code: "triage-route-unreadable" }; }
if (a?.ok === true) return a;
const code = ["triage-no-route", "triage-over-cap"].includes(a?.code) ? a.code : "triage-route-refused";
const code = ["triage-no-route", "triage-over-cap", "triage-route-unreadable"].includes(a?.code) || Object.hasOwn(ROUTE_REFUSALS, a?.code ?? "") ? a.code : "triage-route-refused";
if (code !== "triage-over-cap") return { ok: false, code };
const until = typeof a.until === "string" && ISO_TIME.test(a.until) ? Date.parse(a.until) : NaN;
const ahead = until - now().getTime();
@@ -621,7 +743,8 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
paths,
get running() { return child !== null; },
get pid() { return child?.pid ?? null; },
routeSet: () => Boolean(route && typeof route.admit === "function"),
// A route that can say whether one is stored (triage-route.mjs isSet) answers for the store.
routeSet: () => Boolean(route && typeof route.admit === "function" && (typeof route.isSet !== "function" || route.isSet() === true)),
async state() {
const c = capNow();
const unset = c.state === "not set";
@@ -648,14 +771,22 @@ 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" : 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 } : {});
if (!admitted.ok) throw Object.assign(new TriageError(admitted.code, ADMIT_MESSAGES[admitted.code] ?? "the model route refused"), admitted.until ? { until: admitted.until } : {});
await refresh();
const at = now().toISOString();
const id = `owner-${randomBytes(6).toString("hex")}`;
append({ at, from: "owner", id, text });
// #1873 triage-wiring: every admitted answer passes the fences, a running session or not. One
// that fails launches nothing and the message is not queued.
let checked;
try { checked = await checkAdmitted(admitted); }
catch (e) {
try { append({ at: now().toISOString(), from: "host", in_reply_to: id, text: e?.code === "triage-route-invalid" ? ROUTE_REFUSED : NOT_STARTED }); } catch {}
throw e instanceof TriageError ? e : new TriageError("triage-route-invalid", "the model route's answer is not usable");
}
queue.push({ id, body: text, at, delivered: false });
if (!child) {
starting ??= start(admitted).finally(() => { starting = null; });
starting ??= start(checked).finally(() => { starting = null; });
try { await starting; }
catch (e) {
const i = queue.findIndex((m) => m.id === id);
+5
View File
@@ -5,6 +5,8 @@
// needs --send and the same-origin checks the org admin routes use. No body, path, token or
// exception text reaches a response or a log line.
import { ROUTE_REFUSALS } from "./triage-host.mjs";
export const TRIAGE_PATHS = Object.freeze(["/api/triage", "/api/triage/message"]);
export const BODY_MAX = 16384;
// A read refreshes the snapshot when the one it holds is older than this.
@@ -21,6 +23,8 @@ const STATUS = Object.freeze({
"triage-route-invalid": 409,
"triage-over-cap": 429,
"triage-busy": 429,
// #1873 triage-wiring: the route's refusals. Each launches nothing; the drawer shows its line.
...Object.fromEntries(Object.keys(ROUTE_REFUSALS).map((code) => [code, 409])),
});
// Fixed words for each code; the host's own messages are fixed too, but the route never forwards text.
const MESSAGES = Object.freeze({
@@ -37,6 +41,7 @@ const MESSAGES = Object.freeze({
"triage-dir-unsafe": "a Triage directory is not safe to use",
"triage-start-failed": "the Triage session did not start",
"outcome-unknown": "Triage did not answer",
...ROUTE_REFUSALS,
});
const json = (res, status, value) => {
+263
View File
@@ -0,0 +1,263 @@
// #1873 triage-settings: the owner's read and write of the Triage model route, for Settings > Triage.
// GET and POST /api/triage/route register through serve.mjs's route table, so the server's session
// and Origin checks run first; this module then takes the proxy-admin gate (the instance owner's own
// session on the public name, never a tester or a granted user) and the same-origin checks again.
// A write goes through the route store (triage-route-store.mjs), so the lock, the revision compare
// and the receipt are the store's. Before it, the model and harness are checked the way an
// organization's route change is (checkRoutes in packages/organization/src/launch-settings.mjs):
// offered by the catalogue, available, and compatible with the harness.
//
// The catalogue lives in each organization's bus (the trusted bus boot builds it; nothing outside
// a bus holds the proxy client), so the Console reads it through each active organization's
// `routes` verb, as the proxy setup page does, and merges the answers (mergeCatalogues).
//
// Saving a route starts nothing: the Triage host is not wired to the store in this slice.
// No key, path, env value or exception text reaches a response or a log line.
import { readdirSync, lstatSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { checkRoutes } from '../../organization/src/launch-settings.mjs';
import { isDailyModelQuota } from '../../plugins/src/model-quota.mjs';
import { readRoute, writeRoute } from './triage-route-store.mjs';
export const TRIAGE_ROUTE_PATH = '/api/triage/route';
export const BODY_MAX = 16384;
export const POST_KEYS = Object.freeze(['model', 'harness', 'cap', 'timeZone', 'revision']);
export const NOT_SET_MESSAGE = 'No Triage route is set.';
export const NOT_STARTED_NOTE = 'Saving a route does not start Triage. The key and the wiring are separate.';
const HARNESS_ROOT = fileURLToPath(new URL('../../plugins/harness/', import.meta.url));
// The same shapes the store holds (triage-route-store.mjs).
const MODEL_KEY = /^[a-z0-9][a-z0-9._-]{0,63}$/;
const HARNESS_NAME = /^[a-z][a-z0-9-]{0,31}$/;
const ACTOR = /^[a-z][a-z0-9-]{0,31}$/;
// The store's zone rule (its knownZone): isDailyModelQuota's zone shape, then Intl.
const knownZone = zone => typeof zone === 'string' && zone !== ''
&& isDailyModelQuota('000000000000', { limit: 0, since: '2000-01-01T00:00:00.000Z', reset: '00:00', timeZone: zone });
// The zone as Intl names it, so "america/chicago" is kept as "America/Chicago". A known zone that
// Intl renames to something the store would not take is kept as typed.
export function canonicalZone(zone) {
try {
const named = new Intl.DateTimeFormat('en-US', { timeZone: zone }).resolvedOptions().timeZone;
return knownZone(named) ? named : zone;
} catch { return zone; }
}
const STATUS = Object.freeze({
'human-required': 403,
'read-only': 403,
'cross-origin': 403,
'cross-site': 403,
'method-not-allowed': 405,
'invalid-request': 400,
'triage-cap-invalid': 400,
'triage-zone-required': 400,
'triage-zone-unknown': 400,
'unknown-model': 400,
'model-harness-incompatible': 400,
'harness-off': 400,
'triage-route-invalid': 400,
'triage-route-stale': 409,
'triage-route-busy': 409,
'model-provider-unavailable': 503,
'triage-route-unreadable': 503,
'triage-route-unrecorded': 500,
'outcome-unknown': 503,
});
// Fixed words for each code. The store's own messages name lock files, so none is forwarded.
const MESSAGES = Object.freeze({
'human-required': 'Only the instance owner can read or set the Triage route.',
'read-only': 'This Console was started without writes, so the route cannot be saved.',
'cross-origin': 'The request did not come from this Console page.',
'cross-site': 'The request did not come from this Console page.',
'method-not-allowed': 'This address takes GET and POST only.',
'invalid-request': 'The request is not a Triage route. It takes model, harness, cap, timeZone and revision.',
'triage-cap-invalid': 'The cap must be a whole number of tokens, 1 or more.',
'triage-zone-required': 'A time zone is required. There is no default.',
'triage-zone-unknown': 'That time zone is not known. Use a name like Europe/Berlin or America/Chicago.',
'unknown-model': 'The model catalogue does not offer that model.',
'model-harness-incompatible': 'That harness cannot run that model.',
'harness-off': 'No harness module of that name is installed.',
'triage-route-invalid': 'The route was refused as invalid.',
'triage-route-stale': 'The route changed since this page read it. Reload the route before saving again.',
'triage-route-busy': 'Another save of the route is in progress. Try again.',
'model-provider-unavailable': 'The model catalogue cannot confirm that model now, so the route was not saved.',
'triage-route-unreadable': 'The stored route could not be read.',
'triage-route-unrecorded': 'The route was saved, but its receipt line was not written.',
'outcome-unknown': 'The route service did not answer.',
});
const json = (res, status, value) => {
res.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' });
res.end(JSON.stringify(value));
};
const one = (req, name) => (req.headersDistinct[name]?.length === 1 ? req.headersDistinct[name][0] : null);
const plain = v => v !== null && typeof v === 'object' && !Array.isArray(v);
const identity = m => JSON.stringify([m.provider, m.model, m.module, m.upstream]);
// The installed harness modules: a directory under packages/plugins/harness with an index.mjs.
export function installedHarnesses(root = HARNESS_ROOT) {
try {
return readdirSync(root).filter(name => HARNESS_NAME.test(name) && (() => {
try { return lstatSync(join(root, name, 'index.mjs')).isFile(); } catch { return false; }
})()).sort();
} catch { return []; }
}
// Each active organization's catalogue, merged into the one the Triage route is checked against.
// Only ready answers count. A key is offered when a ready catalogue lists it, and dropped when two
// catalogues disagree on what it is (provider, model, module, upstream). It is available only when
// every catalogue listing it says so, and its harnesses are those every listing names that are
// installed here. Status is ready when one catalogue is; otherwise the first status seen.
export function mergeCatalogues(list, harnesses) {
const ready = (Array.isArray(list) ? list : []).filter(c => c?.status === 'ready' && Array.isArray(c.models));
if (!ready.length) {
const first = (Array.isArray(list) ? list : []).find(c => typeof c?.status === 'string')?.status;
return { status: first && first !== 'ready' ? first : 'unavailable', models: [] };
}
const merged = new Map(), dropped = new Set();
for (const catalogue of ready) {
for (const m of catalogue.models) {
if (!plain(m) || typeof m.key !== 'string' || !MODEL_KEY.test(m.key) || dropped.has(m.key)) continue;
const names = Array.isArray(m.harnesses) ? m.harnesses.filter(h => typeof h === 'string') : [];
const held = merged.get(m.key);
if (!held) {
merged.set(m.key, { key: m.key, label: typeof m.label === 'string' && m.label ? m.label : m.key, provider: m.provider, model: m.model, module: m.module, upstream: m.upstream, available: m.available === true, harnesses: names });
continue;
}
if (identity(held) !== identity(m)) { merged.delete(m.key); dropped.add(m.key); continue; }
held.available = held.available && m.available === true;
held.harnesses = held.harnesses.filter(h => names.includes(h));
}
}
const installed = new Set(harnesses);
const models = [...merged.values()].map(m => ({ ...m, harnesses: m.harnesses.filter(h => installed.has(h)) })).sort((a, b) => a.key.localeCompare(b.key));
return { status: 'ready', models };
}
// The organization route check, called as it is for an organization's change: one role whose
// vars name the harness and model, launch.models holding the catalogue's own entry for that key
// (none when it is not offered), plugins.harness the installed harness modules, and
// plugins.model-provider the entry's module. It throws unknown-model, model-provider-unavailable,
// model-harness-incompatible or harness-off.
export function checkTriageRoute({ model, harness, catalogue, harnesses }) {
if (catalogue?.status !== 'ready') {
const error = new Error('model-provider-unavailable'); error.code = 'model-provider-unavailable'; throw error;
}
const offered = catalogue.models.find(m => m.key === model) ?? null;
const entry = offered ? { provider: offered.provider, model: offered.model, module: offered.module, upstream: offered.upstream } : null;
checkRoutes({
proposed: {
roles: { triage: { vars: { harness, model } } },
launch: { models: entry ? { [model]: entry } : {} },
plugins: { harness: [...harnesses], 'model-provider': entry ? [entry.module] : [] },
},
old: null,
levels: () => null,
catalogue,
});
}
// What the signed-in owner is recorded as: their account id, or the dev local owner's subject.
export function ownerActor(session) {
if (typeof session?.account === 'string' && ACTOR.test(session.account)) return session.account;
if (session?.provider === 'local-dev' && typeof session.sub === 'string' && ACTOR.test(session.sub)) return session.sub;
return null;
}
const view = doc => doc ? {
model: doc.model, harness: doc.harness, cap: doc.cap.limit, reset: doc.cap.reset, timeZone: doc.cap.timeZone,
revision: doc.revision, updatedAt: doc.updatedAt,
} : null;
// The panel's choices: no provider, upstream or module, only what a select shows.
const choices = catalogue => ({
status: catalogue.status,
models: catalogue.models.map(m => ({ key: m.key, label: m.label, available: m.available, harnesses: [...m.harnesses] })),
});
async function readBody(req) {
if ((req.headers['content-type'] ?? '').split(';')[0].trim().toLowerCase() !== 'application/json') throw Error();
const chunks = [];
let size = 0;
for await (const chunk of req) {
size += chunk.length;
if (size > BODY_MAX) throw Error();
chunks.push(chunk);
}
const value = JSON.parse(Buffer.concat(chunks));
if (!plain(value) || Object.keys(value).some(k => !POST_KEYS.includes(k))) throw Error();
return value;
}
// Registers both routes. `catalogues` returns each active organization's `routes` catalogue (or a
// status when its bus did not answer); `harnesses` lists the installed harness modules.
export function triageRouteRoutes({ dataRoot, catalogues, harnesses = installedHarnesses, log = () => {} }) {
async function catalogue() {
let list;
try { list = await catalogues(); } catch { list = []; }
return mergeCatalogues(list, harnesses());
}
async function handle(req, res, context) {
const done = (status, value) => {
// Method, path and status only.
log(`${new Date().toISOString()} ${req.method} ${TRIAGE_ROUTE_PATH} ${status}`);
return json(res, status, value);
};
const refuse = (code, extra = {}) => done(STATUS[code] ?? 503, { error: code, message: MESSAGES[code] ?? MESSAGES['outcome-unknown'], ...extra });
if (req.method !== 'GET' && req.method !== 'POST') return refuse('method-not-allowed');
// The proxy-admin gate: the instance owner's own session, never a tester or a granted user.
if (context.owner !== true || !context.session) return refuse('human-required');
// A write needs this Console's Origin and same-origin Fetch Metadata, exactly once each. A read
// refuses another site's Origin or Fetch Metadata and lets their absence through, as the packs and
// request reads do: a browser sends no Origin on a same-origin GET, and Chromium sends no
// Fetch Metadata to a LAN http origin (serve.mjs marks only a LAN write).
if (req.method === 'POST') {
if (one(req, 'origin') !== context.origin) return refuse('cross-origin');
if (one(req, 'sec-fetch-site') !== 'same-origin') return refuse('cross-site');
} else {
if (req.headersDistinct.origin && one(req, 'origin') !== context.origin) return refuse('cross-origin');
if (req.headersDistinct['sec-fetch-site'] && !['same-origin', 'none'].includes(one(req, 'sec-fetch-site'))) return refuse('cross-site');
}
if (req.method === 'GET') {
let doc;
try { doc = readRoute(dataRoot); } catch (error) { return refuse(error?.code === 'triage-route-unreadable' ? 'triage-route-unreadable' : 'outcome-unknown'); }
const route = view(doc);
return done(200, {
route, ...(route ? {} : { routeMessage: NOT_SET_MESSAGE }), revision: route ? route.revision : 0,
choices: choices(await catalogue()), send: context.send === true, note: NOT_STARTED_NOTE,
});
}
if (context.send !== true) return refuse('read-only');
let body;
try { body = await readBody(req); } catch { return refuse('invalid-request'); }
const { model, harness, cap, timeZone, revision } = body;
if (typeof model !== 'string' || !MODEL_KEY.test(model)) return refuse('unknown-model');
if (typeof harness !== 'string' || !HARNESS_NAME.test(harness)) return refuse('harness-off');
if (!Number.isSafeInteger(cap) || cap < 1) return refuse('triage-cap-invalid');
if (timeZone === undefined || timeZone === null || timeZone === '') return refuse('triage-zone-required');
if (!knownZone(timeZone)) return refuse('triage-zone-unknown');
const zone = canonicalZone(timeZone);
if (!Number.isSafeInteger(revision) || revision < 0) return refuse('invalid-request');
try { checkTriageRoute({ model, harness, catalogue: await catalogue(), harnesses: harnesses() }); }
catch (error) {
const code = ['unknown-model', 'model-provider-unavailable', 'model-harness-incompatible', 'harness-off'].includes(error?.code) ? error.code : 'outcome-unknown';
return refuse(code);
}
const by = ownerActor(context.session);
if (by === null) return refuse('human-required');
try {
const doc = writeRoute(dataRoot, { model, harness, limit: cap, timeZone: zone, by, revision });
return done(200, { route: view(doc), revision: doc.revision, note: NOT_STARTED_NOTE });
} catch (error) {
const code = typeof error?.code === 'string' && Object.hasOwn(MESSAGES, error.code) && error.code.startsWith('triage-route-') ? error.code : 'outcome-unknown';
if (code !== 'triage-route-stale') return refuse(code);
let stored = null;
try { stored = view(readRoute(dataRoot)); } catch { /* the conflict still stands */ }
return refuse(code, { revision: stored ? stored.revision : 0 });
}
}
return registered => {
registered.add('GET', TRIAGE_ROUTE_PATH, handle);
registered.add('POST', TRIAGE_ROUTE_PATH, handle);
};
}
+208
View File
@@ -0,0 +1,208 @@
// #1873 triage-wiring: the Triage host's model route, resolved again at each admit (design report
// reports/triage-route-design.md slice 5). Four sources, each read fresh:
// the route store harness name, model key and cap (triage-route-store.mjs)
// the key file <configDir>/secrets/triage/model (packages/cli/src/triage-key-file.mjs, the
// key verbs' own reader and checks; never the module that holds the proxy client)
// the model catalogue model and provider for the key, read from each active organization's bus
// `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 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
// management client, which the Console never loads, so that is not checked here: a key the proxy does
// not hold fails at the model call.
import { connect } from "node:net";
import { lstatSync, readdirSync } from "node:fs";
import { join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { checkRoutes } from "../../organization/src/launch-settings.mjs";
import { SECRET, keyPrefix, readKey } from "../../cli/src/triage-key-file.mjs";
import { readRoute } from "./triage-route-store.mjs";
import { ROUTE_REFUSALS, refusedHarnessName } from "./triage-host.mjs";
export { ROUTE_REFUSALS };
const HARNESS_ROOT = fileURLToPath(new URL("../../plugins/harness/", import.meta.url));
const HARNESS_NAME = /^[a-z][a-z0-9-]{0,31}$/;
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]);
// The whole shape of this instance's Triage key, for the host's tokenPattern fence.
export const triageTokenPattern = (configDir) => new RegExp(keyPrefix(configDir).replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + SECRET.source.replace(/^\^|\$$/g, ""));
// The installed harness modules: a directory under packages/plugins/harness with an index.mjs.
export function installedHarnesses(root = HARNESS_ROOT) {
try {
return readdirSync(root).filter((name) => HARNESS_NAME.test(name) && (() => {
try { return lstatSync(join(root, name, "index.mjs")).isFile(); } catch { return false; }
})()).sort();
} catch { return []; }
}
// The repository's harness module of that name, or null.
export async function repoHarnessModule(name, root = HARNESS_ROOT) {
if (!HARNESS_NAME.test(name)) return null;
const file = join(root, name, "index.mjs");
try { if (!lstatSync(file).isFile()) return null; } catch { return null; }
return import(pathToFileURL(file).href);
}
// Each active organization's catalogue, merged into one. Only ready answers count. A key is offered
// when a ready catalogue lists it, and dropped when two disagree on what it is (provider, model,
// module, upstream). It is available only when every catalogue listing it says so, and its harnesses
// are those every listing names that are installed here. Status is ready when one catalogue is;
// otherwise the first status seen. The triage-settings lane's Settings check merges the same way.
export function mergeCatalogues(list, harnesses) {
const all = Array.isArray(list) ? list : [];
const ready = all.filter((c) => c?.status === "ready" && Array.isArray(c.models));
if (!ready.length) {
const first = all.find((c) => typeof c?.status === "string")?.status;
return { status: first && first !== "ready" ? first : "unavailable", models: [] };
}
const merged = new Map();
const dropped = new Set();
for (const catalogue of ready) {
for (const m of catalogue.models) {
if (!plain(m) || typeof m.key !== "string" || !MODEL_KEY.test(m.key) || dropped.has(m.key)) continue;
const names = Array.isArray(m.harnesses) ? m.harnesses.filter((h) => typeof h === "string") : [];
const held = merged.get(m.key);
if (!held) {
merged.set(m.key, { key: m.key, provider: m.provider, model: m.model, module: m.module, upstream: m.upstream, available: m.available === true, harnesses: names });
continue;
}
if (identity(held) !== identity(m)) { merged.delete(m.key); dropped.add(m.key); continue; }
held.available = held.available && m.available === true;
held.harnesses = held.harnesses.filter((h) => names.includes(h));
}
}
const installed = new Set(harnesses);
const models = [...merged.values()].map((m) => ({ ...m, harnesses: m.harnesses.filter((h) => installed.has(h)) }));
return { status: "ready", models };
}
// Does something accept a connection on 127.0.0.1:port within the timeout? Nothing is sent.
export function probePort(port, { timeoutMs = PROBE_TIMEOUT_MS } = {}) {
return new Promise((resolve) => {
const sock = connect({ host: "127.0.0.1", port });
const done = (ok) => { sock.destroy(); resolve(ok); };
sock.setTimeout(timeoutMs, () => done(false));
sock.once("connect", () => done(true));
sock.once("error", () => done(false));
});
}
// The organization route check for the stored route against the merged catalogue: null when it
// passes, else the refusal code.
export function checkModel({ model, harness, catalogue, harnesses }) {
// No ready catalogue: whether the model is offered is not known, which is its own refusal.
if (catalogue.status !== "ready") return "triage-catalogue-unavailable";
const offered = catalogue.models.find((m) => m.key === model);
const entry = offered && typeof offered.module === "string" && offered.module
? { provider: offered.provider, model: offered.model, module: offered.module, upstream: offered.upstream } : null;
if (!entry) return "triage-model-not-offered";
const proposed = {
roles: { triage: { vars: { harness, model } } },
launch: { models: { [model]: entry } },
plugins: { harness: [...harnesses], "model-provider": [entry.module] },
};
try {
checkRoutes({ proposed, old: null, levels: () => null, catalogue });
return null;
} catch (e) {
// unknown-model, model-provider-unavailable, an unavailable model, or anything else: not offered.
if (e?.code === "model-harness-incompatible" || e?.code === "harness-off") return "triage-harness-incompatible";
return "triage-model-not-offered";
}
}
// createTriageRoute({ dataRoot, configDir, proxyPort, catalogues, harnessModule, probe, env, installed })
// 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
// env the Console's environment, for the module's mounts (MOSAIC_CLAUDE_BIN)
// installed () -> the installed harness names
// Returns { isSet(), admit() }. isSet answers whether a route is stored (an unreadable store counts
// as set, so the read does not hide it; admit refuses it).
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 && 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; }
},
async admit() {
// 1. The stored route. Unreadable is the host's own code (it also checks the store first).
let stored;
try { stored = readRoute(dataRoot); } catch { return refuse("triage-route-unreadable"); }
if (!stored) return refuse("triage-no-route");
// 2. The key, with the key verbs' checks: a regular file, one link, this user's, 0600, both
// directories 0700, no link, no FIFO (O_NONBLOCK, then the regular-file check).
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, 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(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();
let list = [];
try { list = await catalogues(); } catch { list = []; }
const catalogue = mergeCatalogues(list, harnesses);
const failed = checkModel({ model: stored.model, harness: stored.harness, catalogue, harnesses });
if (failed) return refuse(failed);
const offered = catalogue.models.find((m) => m.key === stored.model);
// 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)
|| typeof mod.bundle !== "function" || typeof mod.env !== "function") return refuse("triage-harness-incompatible");
let adapter, turnEnd, harnessMounts, harnessEnv;
try {
adapter = mod.adapter();
turnEnd = mod.turnEnd();
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 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:${port}`, ANTHROPIC_AUTH_TOKEN: record.key },
};
},
};
}
@@ -910,3 +910,124 @@ for (const [label, answers, line] of [
assert.equal(posts.filter(p => p.url.endsWith('/agent/launch')).length, launched ? 1 : 0);
assert.equal(await b.evaluate('window.__answers.length'), 0, 'every queued answer was asked for');
});
// SP20 (#1873): an organization refusal of Stop says so in one plain line, the line Restart gives.
for (const [label, answer, line] of [
['the organization is archived', { status: 409, body: { error: 'org-archived', message: 'organization is archived' } },
/^Not stopped: Acme Studio is archived, so its agents are not changed\. Nothing changed\.$/],
['the organization is not available', { status: 404, body: { error: 'org-unavailable' } },
/^Not stopped: Acme Studio is not available to this Console\. Nothing changed\.$/],
]) test(`SP20: a Stop refusal says so in the drawer: ${label}`, async t => {
const { b, wait } = await setup(t, { hash: '#/agents?agent=acme%3Acoder' });
const d = '[data-ws-drawer="acme:coder"]';
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-stop]')`);
await b.evaluate(`window.__answers.push(${JSON.stringify(answer)})`);
await b.evaluate(`document.querySelector('${d} [data-drawer-stop]').click()`);
await wait(`document.querySelector('${d} [data-drawer-stop-yes]')`);
await b.evaluate(`document.querySelector('${d} [data-drawer-stop-yes]').click()`);
await wait(`/^Not stopped: /.test(${status(d)}) && ${usable(d)}`);
assert.match(await b.evaluate(status(d)), line);
assert.equal(await b.evaluate(`document.activeElement === document.querySelector('${d} [data-drawer-stop]')`), true, 'focus is back on Stop');
assert.deepEqual((await b.evaluate('window.__posts')).map(p => [p.url, p.method]), [['/api/orgs/acme/agent/terminate', 'POST']]);
});
// SP20: each Stop keeps its own state, as each restart does. Another drawer's question or Stop does not
// lose a running Stop's line or its running state, and a Stop that ends does not drop another drawer's question.
test('SP20: two Stops in two drawers each keep their own line and running state', async t => {
const data = D => { D.acme.instances.ideas = { definition: 'ideas', running: ['run-4'] }; };
const { b, wait } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', data });
const d = '[data-ws-drawer="acme:coder"]', i = '[data-ws-drawer="acme:ideas"]';
const refused = who => ({ status: 409, body: { error: 'instance-protected', message: `instance-protected: ${who} is protected from ending. Nothing changed.`, sent: false } });
const state = x => `[${status(x)}, document.querySelector('${x} [data-drawer-stop]')?.firstChild.textContent, document.querySelector('${x} [data-drawer-stop]')?.disabled, document.querySelector('${x} [data-drawer-restart]')?.disabled]`;
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-stop]')`);
// Coder's Stop runs: its terminate is held.
await b.evaluate(`window.__answers.push({ hold: new Promise(r => { window.__release = r; }), ...${JSON.stringify(refused('coder'))} })`);
await b.evaluate(`document.querySelector('${d} [data-drawer-stop]').click()`);
await wait(`document.querySelector('${d} [data-drawer-stop-yes]')`);
await b.evaluate(`document.querySelector('${d} [data-drawer-stop-yes]').click()`);
await wait(`window.__posts.length === 1`);
// Ideas asks to stop and is cancelled; back on coder, its Stop still shows as running.
await b.evaluate('location.hash = "#/agents?agent=acme%3Aideas"');
await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`);
await b.evaluate(`document.querySelector('${i} [data-drawer-stop]').click()`);
await wait(`document.querySelector('${i} [data-drawer-stop-no]')`);
await b.evaluate(`document.querySelector('${i} [data-drawer-stop-no]').click()`);
await b.evaluate('location.hash = "#/agents?agent=acme%3Acoder"');
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-stop]')`);
assert.deepEqual(await b.evaluate(state(d)), ['Stopping coder…', 'Stopping…', true, true]);
// Ideas' Stop question is open when coder's Stop is refused; each drawer keeps its own.
await b.evaluate('location.hash = "#/agents?agent=acme%3Aideas"');
await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`);
await b.evaluate(`document.querySelector('${i} [data-drawer-stop]').click()`);
await wait(`document.querySelector('${i} [data-drawer-stop-no]')`);
await b.evaluate('window.__release()');
await b.evaluate('new Promise(r => setTimeout(r, 300))');
await b.evaluate('location.hash = "#/agents?agent=acme%3Acoder"');
await wait(`${open('acme:coder')} && /^Not stopped: /.test(${status(d)}) && ${usable(d)}`);
assert.equal(await b.evaluate(status(d)), 'Not stopped: instance-protected: coder is protected from ending. Nothing changed.');
await b.evaluate('location.hash = "#/agents?agent=acme%3Aideas"');
await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`);
assert.equal(await b.evaluate(`!!document.querySelector('${i} [data-drawer-stop-yes]')`), true, 'ideas keeps its question');
// Ideas' Stop runs and is refused; coder's line is still there after, and ideas' is too.
await b.evaluate(`window.__answers.push(${JSON.stringify(refused('ideas'))})`);
await b.evaluate(`document.querySelector('${i} [data-drawer-stop-yes]').click()`);
await wait(`/^Not stopped: /.test(${status(i)}) && ${usable(i)}`);
await b.evaluate('location.hash = "#/agents?agent=acme%3Acoder"');
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-stop]')`);
assert.deepEqual(await b.evaluate(state(d)), ['Not stopped: instance-protected: coder is protected from ending. Nothing changed.', 'Stop', false, false]);
await b.evaluate('location.hash = "#/agents?agent=acme%3Aideas"');
await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`);
assert.deepEqual(await b.evaluate(state(i)), ['Not stopped: instance-protected: ideas is protected from ending. Nothing changed.', 'Stop', false, false]);
assert.deepEqual((await b.evaluate('window.__posts')).map(p => JSON.parse(p.body).instance), ['coder', 'ideas']);
});
// SP20 review: two Stops run at once. One that ends, here the one the bus takes, which repaints the page,
// does not end the other's running state or line, and each drawer keeps its own line after.
test('SP20: two Stops running at once in two drawers each keep their own state', async t => {
const data = D => { D.acme.instances.ideas = { definition: 'ideas', running: ['run-4'] }; };
const { b, wait } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', scripts: [FAST], data });
const d = '[data-ws-drawer="acme:coder"]', i = '[data-ws-drawer="acme:ideas"]';
const REQ = '0f0e0d0c-0b0a-4908-8706-050403020101';
const state = x => `[${status(x)}, document.querySelector('${x} [data-drawer-stop]')?.firstChild.textContent, document.querySelector('${x} [data-drawer-stop]')?.disabled, document.querySelector('${x} [data-drawer-restart]')?.disabled]`;
const ask = async x => {
await b.evaluate(`document.querySelector('${x} [data-drawer-stop]').click()`);
await wait(`document.querySelector('${x} [data-drawer-stop-yes]')`);
await b.evaluate(`document.querySelector('${x} [data-drawer-stop-yes]').click()`);
};
const show = async (agent, x) => {
await b.evaluate(`location.hash = "#/agents?agent=acme%3A${agent}"`);
await wait(`${open(`acme:${agent}`)} && document.querySelector('${x} [data-drawer-stop]')`);
};
// Both terminates are held: coder's the bus takes, ideas' it refuses.
await b.evaluate(`window.__answers.push(
{ hold: new Promise(r => { window.__releaseCoder = r; }), status: 200, body: { sent: true, request: '${REQ}' } },
{ hold: new Promise(r => { window.__releaseIdeas = r; }), status: 409, body: { error: 'instance-protected', message: 'instance-protected: ideas is protected from ending. Nothing changed.', sent: false } },
{ status: 200, body: { request: '${REQ}', state: 'closed' } })`);
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-stop]')`);
await ask(d);
await wait('window.__posts.length === 1');
await show('ideas', i);
await ask(i);
await wait('window.__posts.length === 2');
assert.deepEqual(await b.evaluate(state(i)), ['Stopping ideas…', 'Stopping…', true, true]);
// Coder's Stop ends while ideas' drawer is on screen; ideas' Stop still runs.
await b.evaluate('window.__releaseCoder()');
await wait('window.__posts.length === 3');
await b.evaluate('new Promise(r => setTimeout(r, 300))');
await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`);
assert.deepEqual(await b.evaluate(state(i)), ['Stopping ideas…', 'Stopping…', true, true]);
await show('coder', d);
assert.deepEqual(await b.evaluate(state(d)), ['Stopped coder.', 'Stop', false, false]);
// Ideas' Stop is refused; each drawer keeps its own line.
await show('ideas', i);
assert.deepEqual(await b.evaluate(state(i)), ['Stopping ideas…', 'Stopping…', true, true]);
await b.evaluate('window.__releaseIdeas()');
await wait(`/^Not stopped: /.test(${status(i)}) && ${usable(i)}`);
assert.deepEqual(await b.evaluate(state(i)), ['Not stopped: instance-protected: ideas is protected from ending. Nothing changed.', 'Stop', false, false]);
await show('coder', d);
assert.deepEqual(await b.evaluate(state(d)), ['Stopped coder.', 'Stop', false, false]);
assert.deepEqual((await b.evaluate('window.__posts')).map(p => [p.url, p.method]), [
['/api/orgs/acme/agent/terminate', 'POST'], ['/api/orgs/acme/agent/terminate', 'POST'], [`/api/orgs/acme/agent/request?id=${REQ}`, 'GET']]);
assert.deepEqual((await b.evaluate('window.__posts')).slice(0, 2).map(p => JSON.parse(p.body).instance), ['coder', 'ideas']);
assert.equal(await b.evaluate('window.__answers.length'), 0, 'every queued answer was asked for');
});
+2 -2
View File
@@ -18,8 +18,8 @@ test('served Console browser: real board fixtures, keyboard, drafts, receipts, t
assert.equal(await b.evaluate('document.querySelector("#session-count").textContent'), '3 of 4');
await b.evaluate('document.querySelector("#hide-offline").click()');
assert.equal(await b.evaluate('document.querySelectorAll("table.sessions tbody tr").length'), 4);
// Rows 129, 154, 191, 194 and 294: chat.js, editor.js, processing.js, agents.js and federation.js are the scripts added, and #1839 adds requests.js (the intake panel under an owner's chat message), all same origin; the list is the pin, so another script is a visible change. #797 adds project-archive.js immediately after chat.js. F2 adds onboarding.js after org-create.js, and F3 adds console-restart.js after it. U2 (#1873) adds workspace-data.js and workspace.js before console-restart.js, and U4b adds tasks-board.js after workspace.js. U5 (#1873) added panes.js (Side by side) just before bus.js; SP6 replaces it with assistant.js (the Assistant page), which bus.js mounts.
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("script")].map(s => s.getAttribute("src"))'), ['/editor.js', '/processing.js', '/org-create.js', '/onboarding.js', '/workspace-data.js', '/workspace.js', '/tasks-board.js', '/console-restart.js', '/display.js', '/inbox.js', '/files.js', '/project-remote.js', '/autonomy.js', '/packs.js', '/workflows.js', '/federation.js', '/brand.js', '/app.js', '/markdown-preview.js', '/projects.js', '/chat.js', '/project-archive.js', '/agents.js', '/assistant.js', '/bus.js', '/requests.js']);
// Rows 129, 154, 191, 194 and 294: chat.js, editor.js, processing.js, agents.js and federation.js are the scripts added, and #1839 adds requests.js (the intake panel under an owner's chat message), all same origin; the list is the pin, so another script is a visible change. #797 adds project-archive.js immediately after chat.js. F2 adds onboarding.js after org-create.js, and F3 adds console-restart.js after it. U2 (#1873) adds workspace-data.js and workspace.js before console-restart.js, and U4b adds tasks-board.js after workspace.js. triage-settings (#1873) adds triage-settings.js after console-restart.js. U5 (#1873) added panes.js (Side by side) just before bus.js; SP6 replaces it with assistant.js (the Assistant page), which bus.js mounts.
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("script")].map(s => s.getAttribute("src"))'), ['/editor.js', '/processing.js', '/org-create.js', '/onboarding.js', '/workspace-data.js', '/workspace.js', '/tasks-board.js', '/console-restart.js', '/triage-settings.js', '/display.js', '/inbox.js', '/files.js', '/project-remote.js', '/autonomy.js', '/packs.js', '/workflows.js', '/federation.js', '/brand.js', '/app.js', '/markdown-preview.js', '/projects.js', '/chat.js', '/project-archive.js', '/agents.js', '/assistant.js', '/bus.js', '/requests.js']);
assert.equal(await b.evaluate(`document.querySelectorAll('script[src="/processing.js"]').length`), 1);
assert.equal(await b.evaluate(`document.querySelectorAll('script[src="/chat.js"]').length`), 1);
assert.equal(await b.evaluate('!!window.injected'), false);
+9 -1
View File
@@ -11,9 +11,12 @@
// SPEND <n> add one usage entry of <n> input tokens to the session's spend file (session/spend.jsonl,
// 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 { randomBytes } from "node:crypto";
import { createHash, randomBytes } from "node:crypto";
import { connect } from "node:net";
import { join } from "node:path";
@@ -77,6 +80,11 @@ if (body === "PROBE") {
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 === "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 {
+10
View File
@@ -180,6 +180,16 @@ test('proxy refuses route and body mismatch, unavailable transport, and bus refu
}
});
test('a not-configured refusal relays the bus\'s one cause line, bounded; no other code carries a reason', async t => {
const cause = 'The model proxy management key does not load (proxy-management-key-invalid). ';
const bus = await setup(t, { proxyCall: async () => { throw Object.assign(new Error('private'), { code: 'not-configured', reason: cause.repeat(5) }); } });
const r = await call(bus.s, '/api/proxy/accounts');
assert.equal(r.status, 503);
assert.deepEqual(JSON.parse(r.text), { error: 'not-configured', reason: cause.repeat(5).slice(0, 300) });
const other = await setup(t, { proxyCall: async () => { throw Object.assign(new Error('private'), { code: 'proxy-down', reason: cause }); } });
assert.deepEqual(JSON.parse((await call(other.s, '/api/proxy/accounts')).text), { error: 'proxy-down' });
});
test('ambiguous model mappings pass their named refusal to the owner', async t => {
const {s}=await setup(t,{proxyCall:async()=>{throw Object.assign(new Error('private'),{code:'models-ambiguous'});}});
const response=await call(s,'/api/proxy/group/models/set','POST',{accountId:'a'.repeat(12),models:['fixture']});
+3 -1
View File
@@ -288,8 +288,10 @@ test('D3 Routing: with cliproxy listed and no model proxy the switch reads Disab
await b.navigate(`http://127.0.0.1:${web.address().port}/#/settings/routing`);
const wait=expr=>b.evaluate(`(async()=>{for(let n=0;n<200;n++){if(${expr})return true;await new Promise(r=>setTimeout(r,30));}throw Error('wait failed');})()`);
await wait(`document.querySelector("#provider-switch")?.textContent.startsWith(${JSON.stringify(label)})&&!!document.querySelector('[data-harness]')`);
const info=await b.evaluate('document.querySelector(".routing-panel > p.small")?.textContent');
const [info,...notices]=await b.evaluate('[document.querySelector(".routing-panel > p.small")?.textContent,...[...document.querySelectorAll(".routing-panel > p.small.warn")].slice(1).map(p=>p.textContent)]');
assert.match(info,line,providers.join(','));
// proxy-pickup (#1873): an unavailable catalogue shows the bus's cause line under it.
assert.deepEqual(notices,label==='Disable'?['The model proxy has not been set up for this instance yet. Set it up on the Model proxy page.']:[],providers.join(','));
await b.evaluate('document.querySelector("#provider-switch").click();true');
await wait('posts.length===1');const payload=await b.evaluate('posts[0]');
assert.deepEqual(payload.plugins,{'model-provider':next},providers.join(','));
@@ -98,6 +98,9 @@ async function setup(t, { host = fakeHost(), triage = host.triage, send = true,
// The page focuses its heading when its first render ends (bus.js). Open after that, as a person would, or
// that late focus can land after the drawer's own.
// The Settings > Triage panel script (triage-settings, #1873) is a static file every page loads, like
// every other page module; it carries no data. These checks are about Triage traffic, so they skip it.
const SETTINGS_SCRIPT = 'GET /triage-settings.js';
const open = async f => { await f.wait('document.querySelector("#triage-open")'); await f.wait('document.activeElement?.tagName === "H1"', 100).catch(() => {}); await f.click('#triage-open'); await f.wait('["off", "cap", "running", "stopped"].includes(document.querySelector("#triage-drawer #triage-state")?.dataset.kind)'); };
const type = async (f, words) => { await f.b.evaluate('document.querySelector("#triage-text").focus()'); await f.b.call('Input.insertText', { text: words }); };
const sendAndWait = async (f, before) => { await f.click('#triage-send'); await f.wait(`document.querySelector("#triage-send-status").textContent.trim() && document.querySelector("#triage-send-status").textContent !== ${JSON.stringify(before)} && !document.querySelector("#triage-send").disabled`); return f.text('#triage-send-status'); };
@@ -109,7 +112,7 @@ test('item 1: a session that is not the instance owner gets no entry, no drawer
await f.wait('document.querySelector("#dashboard-kpis")');
await sleep(1500);
assert.equal(await f.b.evaluate('document.querySelector("#triage-open, #triage-drawer, [id^=triage]")'), null);
assert.deepEqual(f.requests.filter(r => /triage/i.test(r)), []);
assert.deepEqual(f.requests.filter(r => /triage/i.test(r) && r !== SETTINGS_SCRIPT), []);
}
// A Triage host, but a signed-in session that is not the root owner.
{
@@ -119,7 +122,7 @@ test('item 1: a session that is not the instance owner gets no entry, no drawer
await sleep(1500);
assert.equal(await f.b.evaluate('document.querySelector("[id^=triage]")'), null);
assert.equal(await f.b.evaluate('document.body.innerHTML.includes("Triage")'), false, 'no Triage words in the page');
assert.deepEqual(f.requests.filter(r => /triage/i.test(r)), []);
assert.deepEqual(f.requests.filter(r => /triage/i.test(r) && r !== SETTINGS_SCRIPT), []);
}
});
@@ -135,7 +138,7 @@ test('item 1: the owner gets one Triage entry in the top bar on the dashboard an
assert.equal(await f.b.evaluate('document.querySelector("#triage-open").getAttribute("aria-expanded")'), 'true');
// From inside an organization the read is still the system route, never the organization's.
assert.ok(f.triageReads() >= 1);
assert.deepEqual(f.requests.filter(r => /triage/.test(r) && r !== 'GET /api/triage'), []);
assert.deepEqual(f.requests.filter(r => /triage/.test(r) && r !== 'GET /api/triage' && r !== SETTINGS_SCRIPT), []);
assert.ok(f.requests.includes('GET /api/config'), 'the unscoped config read');
// The same entry on the system dashboard.
await f.b.evaluate('location.hash = "#/dashboard"');
+155 -4
View File
@@ -11,8 +11,8 @@ 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,
USAGE_NOT_READ, overCapNotTaken, refusedHarnessName, triageEnv,
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";
import { appendUsage, readUsage, usagePath } from "../src/triage-usage.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] };
@@ -518,7 +526,11 @@ test("end to end under the sandbox: the first message starts the session, which
];
let admits = 0;
const route = { admit: async () => { admits += 1; return { ok: true, adapter, harness: "fake", harnessEnv: { TRIAGE_PROBE: JSON.stringify(probe) }, harnessMounts: [], turnEnd: "exit", model: "stub", provider: "stub", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9" } }; } };
const { h } = host(t, root, { route });
// Each read shows one more spend, so every turn reads as known and the session keeps going: two
// turns in a row with unknown usage (a stub turn spends nothing) end it.
let reads = 0;
const ticking = { readTurn: async () => ({ status: "known", entries: Object.fromEntries(Array.from({ length: ++reads }, (_, i) => [`t${i}`, { input: 1, output: 0, cacheWrite: 0, cacheRead: 0 }])) }) };
const { h } = host(t, root, { route, usageSource: ticking });
assert.equal(h.running, false, "nothing starts before the owner's first message");
assert.equal(existsSync(join(dataRoot, "triage", "agent", "session.json")), false);
@@ -1172,3 +1184,142 @@ test("cap: the read carries usageToday, cap and resetsAt, with and without a sto
assert.equal(text.includes(bad), false, bad);
}
});
// #1873 triage-wiring item 7: the cap fails closed. Two turns in a row whose usage reads unknown end
// the session with a host line; the next message starts a new run with a fresh session directory,
// and its turns count again.
const unknownTurn = async (c, id) => { await c.call("usage.begin", { turn: id }); await c.call("usage.end", { turn: id }); };
const knownTurn = async (c, dataRoot, id, n) => { await c.call("usage.begin", { turn: id }); spend(dataRoot, n); await c.call("usage.end", { turn: id }); };
const sessionRun = (dataRoot) => JSON.parse(readFileSync(join(dataRoot, "triage", "agent", "session.json"), "utf8")).run;
test("cap fail closed: two turns in a row with unknown usage end the session with a host line that says why", { 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);
assert.equal(UNKNOWN_TURNS_MAX, 2);
const first = await h.message("one");
const c = await runnerClient(root, dataRoot);
await c.call("message.receive");
await unknownTurn(c, "owner-00000000000a");
assert.equal(h.running, true, "one unknown turn does not end it");
assert.equal((await h.state()).transcript.some((e) => e.text === USAGE_UNKNOWN_ENDED), false);
const t0 = Date.now();
await unknownTurn(c, "owner-00000000000b");
const ended = (await h.state()).transcript.filter((e) => e.from === "host" && e.text === USAGE_UNKNOWN_ENDED);
assert.equal(ended.length, 1, "one host line says why");
assert.ok(logs.includes("triage host: ending the session: two turns in a row with unknown usage"), logs.join(" | "));
await waitFor(() => !h.running);
assert.ok(Date.now() - t0 < 4000, "SIGTERM ends it, before the 5 s SIGKILL fallback");
// The message it took and never answered gets its host line too.
const tr = (await h.state()).transcript;
assert.ok(tr.some((e) => e.from === "host" && e.in_reply_to === first.id), "the taken message is answered by the host");
assert.deepEqual(usageLines(dataRoot).map((l) => l.status), ["unknown", "unknown"]);
});
test("cap fail closed: a runner that ignores SIGTERM is refused at its next receive and killed after 5 s", { timeout: 30000 }, async (t) => {
const root = scratch();
const f = join(root, "stand-in");
// The ignored TERM is inherited across exec, so the sleep outlives SIGTERM.
writeFileSync(f, `#!/bin/sh\ntrap '' TERM\n: > '${join(root, "spawned")}'\ncat > '${join(root, "stdin.json")}'\nexec sleep 600\n`, { mode: 0o755 });
const { h, dataRoot } = host(t, root, { route: counting(root), bwrap: f, usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z") });
storeCap(dataRoot, 1000);
await h.message("one");
const c = await runnerClient(root, dataRoot);
await c.call("message.receive");
await unknownTurn(c, "owner-00000000000a");
const t0 = Date.now();
await unknownTurn(c, "owner-00000000000b");
await new Promise((r) => setTimeout(r, 500));
assert.equal(h.running, true, "SIGTERM is ignored");
// Under the cap, so only the ended session refuses the runner.
await assert.rejects(c.call("message.receive"), (e) => e.code === "not-holder");
await assert.rejects(c.call("state"), (e) => e.code === "not-holder");
await waitFor(() => !h.running, 15000);
assert.ok(Date.now() - t0 >= 4500, "the SIGKILL fallback ends it");
});
test("cap fail closed: one unknown turn between known turns does not end 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, 1000);
await h.message("one");
const c = await runnerClient(root, dataRoot);
await c.call("message.receive");
await unknownTurn(c, "owner-00000000000a");
await knownTurn(c, dataRoot, "owner-00000000000b", 5);
await unknownTurn(c, "owner-00000000000c");
await knownTurn(c, dataRoot, "owner-00000000000d", 5);
await unknownTurn(c, "owner-00000000000e");
assert.deepEqual(usageLines(dataRoot).map((l) => l.status), ["unknown", "known", "unknown", "known", "unknown"]);
assert.equal(h.running, true);
assert.deepEqual(await c.call("message.receive"), [], "the runner still holds its session");
assert.equal((await h.state()).transcript.some((e) => e.text === USAGE_UNKNOWN_ENDED), false);
});
test("cap fail closed: the next message after the end starts a new run with a fresh session, and its usage counts again", { timeout: 30000 }, async (t) => {
const root = scratch();
let clock = "2026-11-18T12:00:00.000Z";
const { h, dataRoot } = host(t, root, { route: counting(root), bwrap: standIn(root), usageSource: spendSource, now: () => new Date(clock) });
storeCap(dataRoot, 1000);
await h.message("one");
const c = await runnerClient(root, dataRoot);
const run1 = sessionRun(dataRoot);
await c.call("message.receive");
await knownTurn(c, dataRoot, "owner-00000000000a", 10);
// The harness's own history: kept across an ordinary start, wiped after an unknown end.
writeFileSync(join(dataRoot, "triage", "agent", "session", "history.jsonl"), "{}\n");
await unknownTurn(c, "owner-00000000000b");
await unknownTurn(c, "owner-00000000000c");
await waitFor(() => !h.running);
// The relay is gone with the session, so the old runner reaches nothing.
await assert.rejects(c.call("message.receive"), (e) => ["not-holder", "outcome-unknown"].includes(e.code));
assert.equal((await h.state()).usageToday.count, 10);
rmSync(join(root, "stdin.json"));
clock = "2026-11-18T12:05:00.000Z"; // the run id is the start time
const second = await h.message("two");
const c2 = await runnerClient(root, dataRoot);
const run2 = sessionRun(dataRoot);
assert.notEqual(run2, run1, "a new run");
assert.deepEqual(readdirSync(join(dataRoot, "triage", "agent", "session")), [], "a fresh session directory");
assert.deepEqual((await c2.call("message.receive")).map((m) => m.id), [second.id]);
await knownTurn(c2, dataRoot, second.id, 25);
const lines = usageLines(dataRoot);
assert.deepEqual(lines.at(-1).run === run2 && [lines.at(-1).status, lines.at(-1).input], ["known", 25]);
assert.equal((await h.state()).usageToday.count, 35, "the new run's turns count again");
// And the counter starts over: one unknown turn in the new run does not end it.
await unknownTurn(c2, "owner-00000000000d");
assert.equal(h.running, true);
// An ordinary later start keeps the history again (freshSession is used once).
writeFileSync(join(dataRoot, "triage", "agent", "session", "history.jsonl"), "{}\n");
process.kill(-h.pid, "SIGKILL");
await waitFor(() => !h.running);
rmSync(join(root, "stdin.json"));
await h.message("three");
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");
});
@@ -0,0 +1,393 @@
// #1873 triage-settings: GET and POST /api/triage/route over the real HTTP boundary. Only the instance
// owner reaches them, a write is same-origin, the model and harness are checked as an organization's
// route change is, the store does the write (one receipt), and no key or path reaches a response.
import test from 'node:test';
import assert from 'node:assert/strict';
import { request } from 'node:http';
import { mkdtempSync, readFileSync, rmSync, writeFileSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { startServer } from '../src/serve.mjs';
import { createDevOwner } from '../src/dev-owner.mjs';
import { consoleAccess } from '../src/access.mjs';
import { createSsoSession } from './helpers/sso-session.mjs';
import { routePaths, readRoute, writeRoute } from '../src/triage-route-store.mjs';
import { triageRouteRoutes, mergeCatalogues, installedHarnesses, ownerActor, TRIAGE_ROUTE_PATH, NOT_STARTED_NOTE } from '../src/triage-route-http.mjs';
const SECRET = 'sk-triage-route-sentinel-0123456789abcdef';
const T = '2026-11-18T12:00:00.000Z';
const model = (key, extra = {}) => ({ key, module: 'anthropic', upstream: 'cliproxy', provider: 'anthropic', model: `claude-${key}`, label: key.toUpperCase(), available: true, reason: null, harnesses: ['claude-code', 'pi'], ...extra });
// One catalogue as an organization's `routes` verb answers. A leaked field would carry the sentinel.
const READY = () => ({ status: 'ready', observedAt: T, notices: [`key ${SECRET} at /srv/proxy/keys.json`], models: [
model('sonnet', { reason: `${SECRET} /home/owner/.config/mosaic-dev/proxy.json` }),
model('glm-flash', { module: 'opencode', provider: 'zai', model: 'glm-5.3-flash', harnesses: ['pi'] }),
model('down', { available: false, reason: 'proxy says no' }),
] });
const BODY = { model: 'sonnet', harness: 'claude-code', cap: 200000, timeZone: 'Europe/Berlin', revision: 0 };
const send = (port, path, { method = 'GET', headers = {}, body } = {}) => new Promise((resolve, reject) => {
const req = request({ host: '127.0.0.1', port, path, method, headers }, res => {
let text = ''; res.on('data', c => { text += c; }); res.on('end', () => resolve({ status: res.statusCode, text, json: (() => { try { return JSON.parse(text); } catch { return null; } })() }));
});
req.on('error', reject); req.end(body === undefined ? undefined : typeof body === 'string' ? body : JSON.stringify(body));
});
const receipts = root => { const f = routePaths(root).receipts; return existsSync(f) ? readFileSync(f, 'utf8').split('\n').filter(Boolean).map(l => JSON.parse(l)) : []; };
async function devServer(t, { catalogues = async () => [READY()], harnesses = () => ['claude-code', 'pi'], ...options } = {}) {
const root = mkdtempSync(join(tmpdir(), 'tr-http-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
const logs = [], routeLogs = [];
const s = await startServer({ port: 0, board: 'http://127.0.0.1:9', sso: createDevOwner(), send: async () => ({}), log: line => logs.push(line),
routes: [triageRouteRoutes({ dataRoot: root, catalogues, harnesses, log: line => routeLogs.push(line) })], ...options });
t.after(() => new Promise(r => s.close(r)));
const port = s.address().port;
const origin = `http://127.0.0.1:${port}`;
const read = { host: `127.0.0.1:${port}`, 'sec-fetch-site': 'same-origin' };
const write = { ...read, origin, 'content-type': 'application/json' };
const get = (headers = read) => send(port, TRIAGE_ROUTE_PATH, { headers });
const post = (body = BODY, headers = write) => send(port, TRIAGE_ROUTE_PATH, { method: 'POST', headers, body });
return { s, port, root, logs, routeLogs, read, write, get, post };
}
test('GET without a stored route says it is not set and offers the catalogue models with their harnesses', async t => {
const f = await devServer(t);
const r = await f.get();
assert.equal(r.status, 200);
assert.equal(r.json.route, null);
assert.equal(r.json.routeMessage, 'No Triage route is set.');
assert.equal(r.json.revision, 0);
assert.equal(r.json.send, true);
assert.equal(r.json.note, NOT_STARTED_NOTE);
assert.deepEqual(r.json.choices, { status: 'ready', models: [
{ key: 'down', label: 'DOWN', available: false, harnesses: ['claude-code', 'pi'] },
{ key: 'glm-flash', label: 'GLM-FLASH', available: true, harnesses: ['pi'] },
{ key: 'sonnet', label: 'SONNET', available: true, harnesses: ['claude-code', 'pi'] },
] });
assert.equal(r.text.includes(SECRET), false);
assert.equal(r.text.includes(f.root), false);
assert.equal(r.text.includes('/srv/') || r.text.includes('/home/'), false);
});
test('GET with a stored route returns its model, harness, cap, reset, zone, revision and time, and no path or actor detail', async t => {
const f = await devServer(t);
writeRoute(f.root, { model: 'sonnet', harness: 'pi', limit: 5000, timeZone: 'America/Chicago', by: 'local-dev-owner' }, { now: () => new Date(T) });
const r = await f.get();
assert.equal(r.status, 200);
assert.deepEqual(r.json.route, { model: 'sonnet', harness: 'pi', cap: 5000, reset: '00:00', timeZone: 'America/Chicago', revision: 1, updatedAt: T });
assert.equal(r.json.revision, 1);
assert.equal(Object.hasOwn(r.json, 'routeMessage'), false);
assert.equal(r.text.includes(f.root), false);
});
test('POST writes through the store: one receipt, the signed-in owner as updatedBy, the revision moves', async t => {
const f = await devServer(t);
const r = await f.post();
assert.equal(r.status, 200, r.text);
assert.equal(r.json.route.model, 'sonnet');
assert.equal(r.json.route.harness, 'claude-code');
assert.equal(r.json.route.cap, 200000);
assert.equal(r.json.route.timeZone, 'Europe/Berlin');
assert.equal(r.json.route.reset, '00:00');
assert.equal(r.json.revision, 1);
assert.equal(r.json.note, NOT_STARTED_NOTE);
const doc = readRoute(f.root);
assert.equal(doc.updatedBy, 'local-dev-owner');
assert.equal(doc.cap.limit, 200000);
const lines = receipts(f.root);
assert.equal(lines.length, 1);
assert.equal(lines[0].by, 'local-dev-owner');
assert.equal(lines[0].revision, 1);
const again = await f.post({ ...BODY, harness: 'pi', revision: 1 });
assert.equal(again.status, 200);
assert.equal(receipts(f.root).length, 2);
for (const line of f.routeLogs) assert.match(line, /^\S+ (GET|POST) \/api\/triage\/route \d{3}$/);
});
test('a stale revision answers 409 with the stored revision and writes nothing', async t => {
const f = await devServer(t);
assert.equal((await f.post()).status, 200);
const stale = await f.post({ ...BODY, cap: 9 });
assert.equal(stale.status, 409);
assert.equal(stale.json.error, 'triage-route-stale');
assert.equal(stale.json.revision, 1);
assert.match(stale.json.message, /changed since this page read it/);
assert.equal(readRoute(f.root).cap.limit, 200000);
assert.equal(receipts(f.root).length, 1);
});
test('a model the catalogue does not offer, or one it marks unavailable, is refused and nothing is written', async t => {
const f = await devServer(t);
const unknown = await f.post({ ...BODY, model: 'opus' });
assert.equal(unknown.status, 400);
assert.equal(unknown.json.error, 'unknown-model');
const down = await f.post({ ...BODY, model: 'down' });
assert.equal(down.status, 503);
assert.equal(down.json.error, 'model-provider-unavailable');
for (const bad of [7, ['sonnet'], { key: 'sonnet' }, 'BAD KEY', '']) {
const r = await f.post({ ...BODY, model: bad });
assert.equal(r.status, 400, JSON.stringify(bad));
assert.equal(r.json.error, 'unknown-model', JSON.stringify(bad));
}
assert.equal(readRoute(f.root), null);
assert.deepEqual(receipts(f.root), []);
});
test('a harness the model is not compatible with, or one not installed, is refused', async t => {
const f = await devServer(t);
const wrong = await f.post({ ...BODY, model: 'glm-flash', harness: 'claude-code' });
assert.equal(wrong.status, 400);
assert.equal(wrong.json.error, 'model-harness-incompatible');
// Installed here is only pi: the catalogue's claude-code is not offered and not accepted.
const g = await devServer(t, { harnesses: () => ['pi'] });
assert.deepEqual((await g.get()).json.choices.models.find(m => m.key === 'sonnet').harnesses, ['pi']);
const off = await g.post({ ...BODY, harness: 'claude-code' });
assert.equal(off.status, 400);
assert.ok(['model-harness-incompatible', 'harness-off'].includes(off.json.error), off.text);
const bad = await f.post({ ...BODY, harness: 'Not A Name' });
assert.equal(bad.json.error, 'harness-off');
assert.equal(readRoute(f.root), null);
assert.equal(readRoute(g.root), null);
});
test('the installed harness check is the organization one: a compatible harness no module provides is harness-off', async t => {
const catalogue = READY(); catalogue.models[0].harnesses = ['claude-code', 'pi', 'ghost'];
// harnesses() lists ghost so the merged catalogue keeps it; the check gets the installed list without it.
let calls = 0;
const f = await devServer(t, { catalogues: async () => [catalogue], harnesses: () => (++calls % 2 === 1 ? ['claude-code', 'pi', 'ghost'] : ['claude-code', 'pi']) });
const r = await f.post({ ...BODY, harness: 'ghost' });
assert.equal(r.json.error, 'harness-off', r.text);
});
test('a catalogue that is not ready, or no organization bus answering, refuses the write and shows no choices', async t => {
for (const catalogues of [async () => [{ status: 'starting', models: [] }], async () => [{ status: 'bus-unavailable' }], async () => [], async () => { throw new Error(SECRET); }]) {
const f = await devServer(t, { catalogues });
const r = await f.get();
assert.equal(r.status, 200);
assert.deepEqual(r.json.choices.models, []);
assert.notEqual(r.json.choices.status, 'ready');
const w = await f.post();
assert.equal(w.status, 503);
assert.equal(w.json.error, 'model-provider-unavailable');
assert.equal(w.text.includes(SECRET) || r.text.includes(SECRET), false);
assert.equal(readRoute(f.root), null);
}
});
test('a missing or unknown time zone is refused; there is no default', async t => {
const f = await devServer(t);
const { timeZone, ...noZone } = BODY;
for (const body of [noZone, { ...BODY, timeZone: '' }, { ...BODY, timeZone: null }]) {
const r = await f.post(body);
assert.equal(r.status, 400, JSON.stringify(body));
assert.equal(r.json.error, 'triage-zone-required', JSON.stringify(body));
}
for (const zone of ['Mars/Olympus', 'not a zone', 42]) {
const r = await f.post({ ...BODY, timeZone: zone });
assert.equal(r.status, 400, String(zone));
assert.equal(r.json.error, 'triage-zone-unknown', String(zone));
}
assert.equal(readRoute(f.root), null);
const lower = await f.post({ ...BODY, timeZone: 'america/chicago' });
assert.equal(lower.status, 200, JSON.stringify(lower.json));
assert.equal(readRoute(f.root).cap.timeZone, 'America/Chicago', 'a zone in another case is stored in its canonical spelling');
assert.equal(lower.json.route.timeZone, 'America/Chicago');
});
test('a cap that is not a positive whole number is refused', async t => {
const f = await devServer(t);
for (const cap of [0, -1, 1.5, '1000', null, undefined, Number.MAX_SAFE_INTEGER + 1, true]) {
const body = { ...BODY, cap }; if (cap === undefined) delete body.cap;
const r = await f.post(body);
assert.equal(r.status, 400, String(cap));
assert.equal(r.json.error, 'triage-cap-invalid', String(cap));
}
assert.equal((await f.post({ ...BODY, cap: 1 })).status, 200, 'one token is a cap');
});
test('a body with another key, a bad revision, a wrong type or too many bytes is invalid-request', async t => {
const f = await devServer(t);
for (const body of [{ ...BODY, by: 'mallory' }, { ...BODY, revision: -1 }, { ...BODY, revision: '0' }, [BODY], 'null', '{', JSON.stringify({ ...BODY, model: 'a'.repeat(17000) })]) {
const r = await f.post(body);
assert.equal(r.status, 400, typeof body === 'string' ? body.slice(0, 20) : JSON.stringify(body).slice(0, 60));
assert.equal(r.json.error, 'invalid-request');
}
const { revision, ...noRevision } = BODY;
assert.equal((await f.post(noRevision)).json.error, 'invalid-request', 'the revision is required');
const text = await f.post(BODY, { ...f.write, 'content-type': 'text/plain' });
assert.equal(text.json.error, 'invalid-request');
assert.equal(readRoute(f.root), null);
});
test('both routes refuse a cross-origin request; a read may omit the metadata; a write needs both checks exactly once', async t => {
const f = await devServer(t);
// Read: a foreign Origin or cross-site Fetch Metadata is refused. Missing metadata is the repo GET convention.
const evil = await f.get({ ...f.read, origin: 'http://evil.example' });
assert.equal(evil.status, 403);
assert.match(evil.json.error, /^cross-origin/, 'the serve gate or the handler refuses it');
const bare = await f.get({ host: f.read.host });
assert.equal(bare.status, 200, 'a read with no Origin and no Fetch Metadata is allowed');
assert.equal((await f.get({ ...f.read, 'sec-fetch-site': 'none' })).status, 200, 'a typed address is allowed');
const cross = await f.get({ ...f.read, 'sec-fetch-site': 'cross-site' });
assert.equal(cross.status, 403);
assert.equal(cross.json.error, 'cross-site');
assert.equal((await f.get({ host: f.read.host, 'sec-fetch-site': 'same-site' })).status, 403);
assert.equal((await f.get({ ...f.read, origin: `http://127.0.0.1:${f.port}` })).status, 200, 'a matching Origin on a read is fine');
// Write: missing Origin, foreign Origin, missing or cross-site Fetch Metadata.
const { origin, ...noOrigin } = f.write;
const missing = await f.post(BODY, noOrigin);
assert.equal(missing.status, 403);
assert.match(missing.json.error, /^cross-origin/, 'the serve gate or the handler refuses it');
assert.equal((await f.post(BODY, { ...f.write, origin: 'http://evil.example' })).status, 403);
const { 'sec-fetch-site': _, ...noSite } = f.write;
assert.equal((await f.post(BODY, noSite)).status, 403);
assert.equal((await f.post(BODY, { ...f.write, 'sec-fetch-site': 'same-site' })).status, 403);
assert.equal(readRoute(f.root), null);
assert.deepEqual(receipts(f.root), []);
});
test('a LAN dev host reads the route with no Fetch Metadata and writes with its own Origin', async t => {
const f = await devServer(t, { devAllowHosts: ['lanbox'] });
const host = `lanbox:${f.port}`;
const r = await f.get({ host });
assert.equal(r.status, 200);
assert.equal(r.json.route, null);
const w = await f.post(BODY, { host, origin: `http://${host}`, 'content-type': 'application/json' });
assert.equal(w.status, 200, JSON.stringify(w.json));
assert.equal(readRoute(f.root).model, BODY.model);
const foreign = await f.post(BODY, { host, origin: 'http://evil.example', 'content-type': 'application/json' });
assert.equal(foreign.status, 403);
assert.equal(readRoute(f.root).revision, 1);
});
test('the handler holds the same-origin line itself: a doubled Origin or Sec-Fetch-Site is refused', async t => {
const root = mkdtempSync(join(tmpdir(), 'tr-unit-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
let handler;
triageRouteRoutes({ dataRoot: root, catalogues: async () => [READY()], harnesses: () => ['claude-code', 'pi'] })({ add: (method, path, fn) => { handler = fn; } });
const call = (method, headersDistinct, extra = {}) => new Promise(resolve => {
const res = { writeHead(status) { this.status = status; }, end(text) { resolve({ status: this.status, json: JSON.parse(text) }); } };
const req = { method, headersDistinct, headers: { 'content-type': 'application/json' }, async *[Symbol.asyncIterator]() { yield Buffer.from(JSON.stringify(BODY)); } };
handler(req, res, { owner: true, session: { provider: 'local-dev', sub: 'local-dev-owner' }, origin: 'https://console.example', send: true, ...extra });
});
const o = 'https://console.example';
for (const method of ['PUT', 'DELETE', 'PATCH']) assert.equal((await call(method, { origin: [o], 'sec-fetch-site': ['same-origin'] })).json.error, 'method-not-allowed', method);
assert.equal(readRoute(root), null);
assert.equal((await call('GET', { origin: [o, o], 'sec-fetch-site': ['same-origin'] })).json.error, 'cross-origin');
assert.equal((await call('GET', { 'sec-fetch-site': ['same-origin', 'same-origin'] })).json.error, 'cross-site');
assert.equal((await call('GET', { origin: ['https://evil.example'], 'sec-fetch-site': ['same-origin'] })).json.error, 'cross-origin');
assert.equal((await call('POST', { origin: [o, o], 'sec-fetch-site': ['same-origin'] })).json.error, 'cross-origin');
assert.equal((await call('POST', { origin: [o], 'sec-fetch-site': ['same-origin', 'same-origin'] })).json.error, 'cross-site');
assert.equal((await call('POST', { origin: [o], 'sec-fetch-site': ['same-origin'] }, { send: false })).json.error, 'read-only');
assert.equal((await call('GET', { 'sec-fetch-site': ['same-origin'] }, { owner: false })).json.error, 'human-required');
assert.equal((await call('POST', { origin: [o], 'sec-fetch-site': ['same-origin'] }, { owner: false })).json.error, 'human-required');
assert.equal((await call('POST', { origin: [o], 'sec-fetch-site': ['same-origin'] }, { session: { provider: 'oidc', sub: 'Owner Sub' } })).json.error, 'human-required', 'an owner with no recordable name is refused');
assert.equal(readRoute(root), null);
assert.equal((await call('POST', { origin: [o], 'sec-fetch-site': ['same-origin'] })).status, 200);
});
test('a read-only Console reads the route but refuses a write', async t => {
const f = await devServer(t, { send: null });
const r = await f.get();
assert.equal(r.status, 200);
assert.equal(r.json.send, false);
const w = await f.post();
assert.equal(w.status, 403);
assert.equal(w.json.error, 'read-only');
assert.equal(readRoute(f.root), null);
});
test('an SSO root owner reads and writes as their account; a granted user and a signed-out caller reach neither route', async t => {
const access = { accessVersion: 1, revision: 1,
users: [{ id: 'alice', home: 'local', subjects: [{ provider: 'writer', sub: 'alice-sub' }] }],
grants: [{ id: 'g-1', user: 'alice', business: 'demo', projects: ['*'], instances: ['*'], level: 'collaborator' }] };
const auth = await createSsoSession(t, { host: 'console.example', users: consoleAccess({ load: () => access, business: () => 'demo' }) });
const root = mkdtempSync(join(tmpdir(), 'tr-sso-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
const s = await startServer({ port: 0, sso: auth.sso, send: async () => ({}), log: () => {},
routes: [triageRouteRoutes({ dataRoot: root, catalogues: async () => [READY()], harnesses: () => ['claude-code', 'pi'] })] });
t.after(() => new Promise(r => s.close(r)));
const port = s.address().port;
const owner = auth.writeHeaders(await auth.signIn(port));
assert.equal((await send(port, TRIAGE_ROUTE_PATH, { headers: owner })).status, 200);
const saved = await send(port, TRIAGE_ROUTE_PATH, { method: 'POST', headers: owner, body: BODY });
assert.equal(saved.status, 200, saved.text);
assert.match(readRoute(root).updatedBy, /^u-[0-9a-f]{12}$/);
const alice = auth.writeHeaders(await auth.signIn(port, { sub: 'alice-sub' }));
const aliceRead = await send(port, TRIAGE_ROUTE_PATH, { headers: alice });
assert.equal(aliceRead.status, 403);
assert.ok(['owner-only', 'human-required'].includes(aliceRead.json.error), aliceRead.text);
assert.equal(aliceRead.text.includes('sonnet'), false, 'a granted user sees no route');
const aliceWrite = await send(port, TRIAGE_ROUTE_PATH, { method: 'POST', headers: alice, body: { ...BODY, revision: 1, cap: 7 } });
assert.equal(aliceWrite.status, 403);
const nobody = await send(port, TRIAGE_ROUTE_PATH, { headers: { host: 'console.example', 'sec-fetch-site': 'same-origin' } });
assert.equal(nobody.status, 401);
const nobodyWrite = await send(port, TRIAGE_ROUTE_PATH, { method: 'POST', headers: { host: 'console.example', origin: 'https://console.example', 'sec-fetch-site': 'same-origin', 'content-type': 'application/json' }, body: { ...BODY, revision: 1 } });
assert.equal(nobodyWrite.status, 401);
// Cross-origin as the owner, on the public name.
assert.equal((await send(port, TRIAGE_ROUTE_PATH, { method: 'POST', headers: { ...owner, origin: 'https://evil.example' }, body: { ...BODY, revision: 1 } })).status, 403);
assert.equal((await send(port, TRIAGE_ROUTE_PATH, { headers: { ...owner, origin: 'https://evil.example' } })).status, 403);
assert.equal(readRoute(root).revision, 1);
assert.equal(readRoute(root).cap.limit, 200000);
assert.equal(receipts(root).length, 1);
});
test('the tester listener reaches neither route', async t => {
const { startTesterSocket } = await import('../src/tester-socket.mjs');
const { testerRequest } = await import('../../cli/src/tester-http.mjs');
const root = mkdtempSync(join(tmpdir(), 'tr-tester-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
const s = await startTesterSocket({ dataRoot: root, log: () => {}, routes: [triageRouteRoutes({ dataRoot: root, catalogues: async () => [READY()], harnesses: () => ['claude-code', 'pi'] })] });
t.after(() => new Promise(r => s.close(r)));
for (const method of ['GET', 'POST']) {
const r = await testerRequest({ dataRoot: root, path: TRIAGE_ROUTE_PATH, method, body: method === 'POST' ? BODY : undefined });
assert.equal(r.status, 403, method);
}
assert.equal(readRoute(root), null);
});
test('a busy store and an unreadable store answer with fixed words and no path', async t => {
const f = await devServer(t);
assert.equal((await f.post()).status, 200);
const { lock, file } = routePaths(f.root);
writeFileSync(lock, JSON.stringify({ pid: process.pid, at: T }), { mode: 0o600 });
const busy = await f.post({ ...BODY, revision: 1 });
assert.equal(busy.status, 409);
assert.equal(busy.json.error, 'triage-route-busy');
assert.equal(busy.text.includes(f.root) || busy.text.includes('route.lock'), false);
rmSync(lock);
writeFileSync(file, '{not json', { mode: 0o600 });
const broken = await f.get();
assert.equal(broken.status, 503);
assert.equal(broken.json.error, 'triage-route-unreadable');
assert.equal(broken.text.includes(f.root) || broken.text.includes('route.json'), false);
const brokenWrite = await f.post({ ...BODY, revision: 1 });
assert.equal(brokenWrite.json.error, 'triage-route-unreadable');
assert.equal(brokenWrite.text.includes(f.root), false);
});
test('no response or log line carries the sentinel, the data root or a store path', async t => {
const f = await devServer(t);
const all = [await f.get(), await f.post(), await f.get(), await f.post({ ...BODY, cap: 0 }), await f.post({ ...BODY, model: 'opus' }), await f.post({ ...BODY, revision: 0 }), await f.post(BODY, { ...f.write, origin: 'http://evil.example' })];
const text = all.map(r => r.text).join('\n') + f.routeLogs.join('\n') + f.logs.join('\n');
for (const needle of [SECRET, f.root, 'route.json', 'route.lock', 'route-receipts', '/srv/', '/home/', 'cliproxy', 'anthropic']) assert.equal(text.includes(needle), false, needle);
});
test('mergeCatalogues: a key two catalogues disagree on is dropped; availability and harnesses must hold everywhere', () => {
const a = { status: 'ready', models: [model('sonnet'), model('glm', { harnesses: ['pi'] }), model('x', { available: false })] };
const b = { status: 'ready', models: [model('sonnet', { harnesses: ['pi'] }), model('glm', { upstream: 'other' }), model('x'), model('y', { available: false })] };
a.models.push(model('y'));
const merged = mergeCatalogues([a, b, { status: 'bus-unavailable' }], ['claude-code', 'pi']);
assert.equal(merged.status, 'ready');
assert.deepEqual(merged.models.map(m => [m.key, m.available, m.harnesses]), [['sonnet', true, ['pi']], ['x', false, ['claude-code', 'pi']], ['y', false, ['claude-code', 'pi']]]);
assert.deepEqual(mergeCatalogues([{ status: 'starting' }], ['pi']), { status: 'starting', models: [] });
assert.deepEqual(mergeCatalogues([], ['pi']), { status: 'unavailable', models: [] });
assert.deepEqual(mergeCatalogues([{ status: 'ready', models: [{ key: 'BAD KEY', harnesses: ['pi'] }, null] }], ['pi']).models, []);
});
test('installedHarnesses lists the harness modules on disk; ownerActor names the account or the dev owner', () => {
assert.deepEqual(installedHarnesses(), ['claude-code', 'pi']);
assert.equal(ownerActor({ account: 'u-0123456789ab', provider: 'writer', sub: 'x' }), 'u-0123456789ab');
assert.equal(ownerActor({ provider: 'local-dev', sub: 'local-dev-owner' }), 'local-dev-owner');
assert.equal(ownerActor({ provider: 'writer', sub: 'owner-sub' }), null);
assert.equal(ownerActor(null), null);
});
@@ -0,0 +1,262 @@
// #1873 triage-settings: Settings > Triage in a real browser. The owner sees "not set", picks a model,
// a harness from that model's list, a cap and a time zone, and saves in three clicks; a refusal is one
// line and keeps the fields; a stale revision offers a re-read; a second Save while one runs does
// nothing; anyone /api/config does not call the owner gets no Triage page and no route read.
import test from 'node:test';
import assert from 'node:assert/strict';
import { w15Fixture } from './w15-fixture.mjs';
const T = '2026-11-18T12:00:00.000Z';
const NOTE = 'Saving a route does not start Triage. The key and the wiring are separate.';
const choices = { status: 'ready', models: [
{ key: 'down', label: 'Down', available: false, harnesses: ['claude-code'] },
{ key: 'glm-flash', label: 'GLM Flash', available: true, harnesses: ['pi'] },
{ key: 'sonnet', label: 'Sonnet', available: true, harnesses: ['claude-code', 'pi'] },
] };
const unset = { route: null, routeMessage: 'No Triage route is set.', revision: 0, choices, send: true, note: NOTE };
const route = (over = {}) => ({ model: 'sonnet', harness: 'pi', cap: 200000, reset: '00:00', timeZone: 'Europe/Berlin', revision: 1, updatedAt: T, ...over });
const config = (over = {}) => ({ send: true, devOwner: true, sso: true, proxyAdmin: true, proxyAdminSend: true, ...over });
const scenario = (read = unset, extra = {}, cfg = {}) => ({ '/api/config': config(cfg), '/api/triage/route': read, ...extra });
const writes = b => b.evaluate('window.__writes.filter(w=>w.path==="/api/triage/route").map(w=>({method:w.method,body:JSON.parse(w.body)}))');
// The fixture answers in the page, so no resource timing exists: count the route reads in a wrapper.
const countReads = b => b.call('Page.addScriptToEvaluateOnNewDocument', { source: '(()=>{window.__routeReads=0;const prev=window.fetch;window.fetch=(i,init)=>{try{if(new URL(typeof i==="string"?i:i.url,location.href).pathname==="/api/triage/route"&&(init?.method??"GET")==="GET")window.__routeReads++;}catch{}return prev(i,init);};})()' });
const reads = b => b.evaluate('window.__routeReads');
const pick = (b, id, value) => b.evaluate(`(()=>{const el=document.getElementById(${JSON.stringify(id)});el.value=${JSON.stringify(value)};el.dispatchEvent(new Event('input',{bubbles:true}));el.dispatchEvent(new Event('change',{bubbles:true}));})()`);
const type = (b, id, value) => b.evaluate(`(()=>{const el=document.getElementById(${JSON.stringify(id)});el.value=${JSON.stringify(value)};el.dispatchEvent(new Event('input',{bubbles:true}));})()`);
const text = (b, id) => b.evaluate(`document.getElementById(${JSON.stringify(id)})?.textContent ?? null`);
const value = (b, id) => b.evaluate(`document.getElementById(${JSON.stringify(id)})?.value ?? null`);
const options = (b, id) => b.evaluate(`[...document.getElementById(${JSON.stringify(id)}).options].map(o=>[o.value,o.textContent,o.disabled])`);
async function fill(b, { model = 'sonnet', harness = 'claude-code', cap = '150000', zone = 'America/Chicago' } = {}) {
await pick(b, 'triage-route-model', model);
if (harness !== null) await pick(b, 'triage-route-harness', harness);
await type(b, 'triage-route-cap', cap);
await type(b, 'triage-route-zone', zone);
}
test('the owner sees Triage in Settings, "not set" before a first save, the catalogue choices and the note', { timeout: 90000 }, async t => {
const { f, b, wait } = await w15Fixture(t, { scenario: scenario() });
await countReads(b);
await b.navigate(f.base + '/#/settings');
await wait('document.querySelector(\'#settings-nav a[href="#/settings/triage"]\')');
await b.evaluate('document.querySelector(\'#settings-nav a[href="#/settings/triage"]\').click()');
await wait('document.getElementById("triage-route-stored")');
assert.match(await text(b, 'triage-route-stored'), /^Not set\. No Triage route is set\.$/);
assert.ok(await reads(b) >= 1, 'the owner page reads the route (the counter works)');
assert.equal(await text(b, 'triage-route-note'), NOTE);
assert.equal(await text(b, 'settings-page-title'), 'Triage');
assert.deepEqual(await options(b, 'triage-route-model'), [['', 'Choose a model', false], ['down', 'Down (down) (unavailable)', true], ['glm-flash', 'GLM Flash (glm-flash)', false], ['sonnet', 'Sonnet (sonnet)', false]]);
assert.deepEqual(await options(b, 'triage-route-harness'), [['', 'Choose a model first', false]]);
assert.equal(await value(b, 'triage-route-cap'), '', 'no default cap');
assert.equal(await value(b, 'triage-route-zone'), '', 'no default time zone');
assert.equal(await value(b, 'triage-route-reset'), '00:00');
assert.equal(await b.evaluate('document.getElementById("triage-route-reset").readOnly'), true);
assert.equal(await b.evaluate('document.querySelector(".settings-readonly")'), null);
assert.equal(await b.evaluate('document.getElementById("triage-route-catalogue")'), null);
// The harness list follows the model; a model with one harness picks it.
await pick(b, 'triage-route-model', 'glm-flash');
assert.deepEqual(await options(b, 'triage-route-harness'), [['', 'Choose a harness', false], ['pi', 'pi', false]]);
assert.equal(await value(b, 'triage-route-harness'), 'pi');
await pick(b, 'triage-route-model', 'sonnet');
assert.deepEqual((await options(b, 'triage-route-harness')).map(o => o[0]), ['', 'claude-code', 'pi']);
assert.equal(await value(b, 'triage-route-harness'), 'pi', 'a harness the new model takes is kept');
assert.deepEqual(await writes(b), []);
assert.deepEqual(await b.evaluate('window.__unexpected'), []);
assert.deepEqual(await b.evaluate('window.__errors'), []);
});
test('Save posts the fields and the read revision once, then shows the stored route; three clicks from Settings', { timeout: 90000 }, async t => {
const saved = route({ harness: 'claude-code', cap: 150000, timeZone: 'America/Chicago' });
const { f, b, wait } = await w15Fixture(t, { scenario: scenario(unset, { 'POST /api/triage/route': { body: { route: saved, revision: 1, note: NOTE } } }) });
await b.navigate(f.base + '/#/settings');
await wait('document.querySelector(\'#settings-nav a[href="#/settings/triage"]\')');
await b.evaluate('document.querySelector(\'#settings-nav a[href="#/settings/triage"]\').click()');
await wait('document.getElementById("triage-route-model")');
await fill(b, { zone: ' America/Chicago ' });
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait('document.getElementById("triage-route-line")?.textContent==="Saved. Revision 1."');
assert.equal(await b.evaluate('document.activeElement?.id'), 'triage-route-line', 'focus lands on the saved line, not the page body');
assert.deepEqual(await writes(b), [{ method: 'POST', body: { model: 'sonnet', harness: 'claude-code', cap: 150000, timeZone: 'America/Chicago', revision: 0 } }]);
assert.match(await text(b, 'triage-route-stored'), /Stored route: sonnet on claude-code, cap 150,000 tokens a day, reset 00:00 America\/Chicago\. Revision 1/);
assert.equal(await value(b, 'triage-route-cap'), '150000');
// The next save carries the new revision.
await type(b, 'triage-route-cap', '9000');
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait('window.__writes.filter(w=>w.path==="/api/triage/route").length===2');
assert.equal((await writes(b))[1].body.revision, 1);
assert.deepEqual(await b.evaluate('window.__unexpected'), []);
});
test('a stored route fills the fields; each refusal is one plain line and keeps the fields', { timeout: 90000 }, async t => {
const { f, b, wait } = await w15Fixture(t, { scenario: scenario({ ...unset, route: route(), routeMessage: undefined, revision: 1 }) });
await b.navigate(f.base + '/#/settings/triage');
await wait('document.getElementById("triage-route-model")?.value==="sonnet"');
assert.equal(await value(b, 'triage-route-harness'), 'pi');
assert.equal(await value(b, 'triage-route-cap'), '200000');
assert.equal(await value(b, 'triage-route-zone'), 'Europe/Berlin');
assert.match(await text(b, 'triage-route-stored'), /^Stored route: sonnet on pi, cap 200,000 tokens a day, reset 00:00 Europe\/Berlin\. Revision 1, saved 2026-11-18T12:00:00\.000Z\.$/);
for (const [error, message, status] of [['triage-zone-unknown', 'The time zone is not one this Console knows.', 400], ['model-harness-incompatible', 'That harness does not run this model.', 400], ['model-provider-unavailable', 'The model provider is not available now.', 503]]) {
await b.evaluate(`Object.assign(window.__scenario, {'POST /api/triage/route': {status: ${status}, body: {error: ${JSON.stringify(error)}, message: ${JSON.stringify(message)}}}})`);
await fill(b, { zone: 'Mars/Olympus', cap: '777' });
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait(`document.getElementById("triage-route-line")?.textContent===${JSON.stringify(message)}`);
assert.equal(await value(b, 'triage-route-model'), 'sonnet');
assert.equal(await value(b, 'triage-route-harness'), 'claude-code');
assert.equal(await value(b, 'triage-route-cap'), '777');
assert.equal(await value(b, 'triage-route-zone'), 'Mars/Olympus');
assert.equal(await b.evaluate('document.getElementById("triage-route-reload")'), null, 'only a conflict offers a re-read');
assert.equal(await b.evaluate('document.getElementById("triage-route-save").disabled'), false);
}
assert.match(await text(b, 'triage-route-stored'), /Revision 1/, 'a refusal leaves the stored line alone');
// A refusal with no readable answer still says one line.
await b.evaluate(`Object.assign(window.__scenario, {'POST /api/triage/route': {status: 500, body: {}}})`);
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait('document.getElementById("triage-route-line")?.textContent==="The route was refused. Nothing was saved."');
assert.equal(await value(b, 'triage-route-cap'), '777');
// Leaving the page and coming back reads the route again and keeps what was typed.
await b.evaluate('location.hash="#/settings/appearance"');
await wait('!document.getElementById("triage-route")');
await b.evaluate('location.hash="#/settings/triage"');
await wait('document.getElementById("triage-route-cap")');
assert.equal(await value(b, 'triage-route-cap'), '777');
assert.equal(await value(b, 'triage-route-zone'), 'Mars/Olympus');
assert.match(await text(b, 'triage-route-stored'), /Revision 1/);
});
test('a stale revision says so, keeps the fields, and Reload route reads the stored route again', { timeout: 90000 }, async t => {
const message = 'The Triage route changed since this page read it. Reload the route, then save again.';
const { f, b, wait } = await w15Fixture(t, { scenario: scenario(unset, { 'POST /api/triage/route': { status: 409, body: { error: 'triage-route-stale', message, revision: 2 } } }) });
await b.navigate(f.base + '/#/settings/triage');
await wait('document.getElementById("triage-route-model")');
await fill(b, { cap: '5000' });
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait(`document.getElementById("triage-route-line")?.textContent===${JSON.stringify(message)}`);
assert.equal(await value(b, 'triage-route-cap'), '5000');
assert.equal(await b.evaluate('document.activeElement?.id'), 'triage-route-reload');
await b.evaluate(`window.__scenario['/api/triage/route'] = ${JSON.stringify({ ...unset, route: route({ revision: 2, model: 'glm-flash', cap: 42 }), routeMessage: undefined, revision: 2 })}`);
await b.evaluate('document.getElementById("triage-route-reload").click()');
await wait('document.getElementById("triage-route-model")?.value==="glm-flash"');
assert.equal(await value(b, 'triage-route-cap'), '42');
assert.match(await text(b, 'triage-route-stored'), /Revision 2/);
assert.equal(await b.evaluate('document.getElementById("triage-route-reload")'), null);
await b.evaluate(`window.__scenario['POST /api/triage/route'] = {body: {route: ${JSON.stringify(route({ revision: 3, model: 'glm-flash', cap: 42 }))}, revision: 3, note: ${JSON.stringify(NOTE)}}}`);
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait('document.getElementById("triage-route-line")?.textContent==="Saved. Revision 3."');
assert.equal((await writes(b)).at(-1).body.revision, 2, 'the save after a re-read carries the stored revision');
});
test('an untouched page follows the store; typed fields keep the revision they were read at, so a later save is stale', { timeout: 90000 }, async t => {
const stored = (over, revision) => ({ ...unset, route: route({ revision, ...over }), routeMessage: undefined, revision });
const message = 'The Triage route changed since this page read it. Reload the route, then save again.';
const { f, b, wait } = await w15Fixture(t, { scenario: scenario(stored({}, 1), { 'POST /api/triage/route': { status: 409, body: { error: 'triage-route-stale', message, revision: 3 } } }) });
const away = async () => {
await b.evaluate('location.hash="#/settings/appearance"');
await wait('!document.getElementById("triage-route")');
await b.evaluate('location.hash="#/settings/triage"');
await wait('document.getElementById("triage-route-cap")');
};
const dot = 'document.querySelector(\'#settings-nav a[href="#/settings/triage"] .settings-dot\') !== null';
await b.navigate(f.base + '/#/settings/triage');
await wait('document.getElementById("triage-route-model")?.value==="sonnet"');
// Saved elsewhere while this page sat untouched: the page shows the new route, with no unsaved mark.
await b.evaluate(`window.__scenario['/api/triage/route'] = ${JSON.stringify(stored({ model: 'glm-flash', cap: 42 }, 2))}`);
await away();
assert.equal(await value(b, 'triage-route-model'), 'glm-flash');
assert.equal(await value(b, 'triage-route-cap'), '42');
assert.match(await text(b, 'triage-route-stored'), /Revision 2/);
assert.equal(await b.evaluate(dot), false);
// Typed at revision 2, then saved elsewhere again: the typed fields stay and the save sends revision 2.
await type(b, 'triage-route-cap', '9000');
await b.evaluate(`window.__scenario['/api/triage/route'] = ${JSON.stringify(stored({ model: 'sonnet', cap: 1 }, 3))}`);
await away();
assert.equal(await value(b, 'triage-route-model'), 'glm-flash');
assert.equal(await value(b, 'triage-route-cap'), '9000');
assert.match(await text(b, 'triage-route-stored'), /Revision 3/);
assert.equal(await b.evaluate(dot), true, 'the typed fields are marked unsaved');
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait(`document.getElementById("triage-route-line")?.textContent===${JSON.stringify(message)}`);
const last = (await writes(b)).at(-1).body;
assert.equal(last.revision, 2, 'the save carries the revision the typed fields were read at, not the newest read');
assert.equal(last.model, 'glm-flash');
assert.equal(last.cap, 9000);
});
test('a second Save while one runs does nothing', { timeout: 90000 }, async t => {
const { f, b, wait } = await w15Fixture(t, { scenario: scenario(unset, { 'POST /api/triage/route': { body: { route: route(), revision: 1, note: NOTE } } }) });
await b.navigate(f.base + '/#/settings/triage');
await wait('document.getElementById("triage-route-model")');
// Hold the POST until the test lets it go.
await b.evaluate('(()=>{const prev=window.fetch;window.__posts=0;window.fetch=(input,init)=>init?.method==="POST"?(window.__posts++,new Promise(r=>{window.__release=()=>r(prev(input,init));})):prev(input,init);})()');
await fill(b);
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait('typeof window.__release==="function"');
assert.equal(await text(b, 'triage-route-save'), 'Saving…');
assert.equal(await b.evaluate('document.getElementById("triage-route-save").disabled'), true);
// A click on the disabled button, a form submit (Enter) and a direct requestSubmit all do nothing.
await b.evaluate('document.getElementById("triage-route-save").click()');
await b.evaluate('document.getElementById("triage-route-form").requestSubmit()');
await b.evaluate('document.getElementById("triage-route-form").dispatchEvent(new Event("submit",{bubbles:true,cancelable:true}))');
assert.equal(await b.evaluate('window.__writes.filter(w=>w.path==="/api/triage/route").length'), 0, 'the held POST is not recorded yet');
assert.equal(await b.evaluate('window.__posts'), 1, 'no second POST left the page');
await b.evaluate('window.__release()');
await wait('document.getElementById("triage-route-line")?.textContent==="Saved. Revision 1."');
assert.equal((await writes(b)).length, 1);
});
test('a read-only Console shows the route with Save off and posts nothing', { timeout: 90000 }, async t => {
const { f, b, wait } = await w15Fixture(t, { scenario: scenario({ ...unset, send: false }, {}, { send: false, proxyAdminSend: false }) });
await b.navigate(f.base + '/#/settings/triage');
await wait('document.getElementById("triage-route-save")');
assert.equal(await b.evaluate('document.getElementById("triage-route-save").disabled'), true);
assert.match(await text(b, 'triage-route-readonly'), /read-only/);
assert.equal(await b.evaluate('document.querySelector(".settings-readonly")'), null, 'the panel says read-only once, from the route\'s own answer');
await b.evaluate('document.getElementById("triage-route-form").dispatchEvent(new Event("submit",{bubbles:true,cancelable:true}))');
assert.deepEqual(await writes(b), []);
});
test('a catalogue that is not ready says so and offers no model', { timeout: 90000 }, async t => {
const { f, b, wait } = await w15Fixture(t, { scenario: scenario({ ...unset, choices: { status: 'bus-unavailable', models: [] } }) });
await b.navigate(f.base + '/#/settings/triage');
await wait('document.getElementById("triage-route-catalogue")');
assert.match(await text(b, 'triage-route-catalogue'), /No organization bus offered a model catalogue \(bus-unavailable\)/);
assert.deepEqual(await options(b, 'triage-route-model'), [['', 'Choose a model', false]]);
});
test('a non-owner sees no Triage section and the page reads no route', { timeout: 90000 }, async t => {
const { f, b, wait } = await w15Fixture(t, { scenario: scenario(unset, {}, { proxyAdmin: undefined, proxyAdminSend: undefined, user: true }) });
await countReads(b);
await b.navigate(f.base + '/#/settings');
await wait('document.querySelector(\'#settings-nav a[href="#/settings/appearance"]\')');
assert.equal(await b.evaluate('document.querySelector(\'#settings-nav a[href="#/settings/triage"]\')'), null);
await b.navigate(f.base + '/#/settings/triage');
await wait('document.querySelector(".nf")');
assert.equal(await b.evaluate('document.getElementById("triage-route")'), null);
assert.equal(await reads(b), 0, 'no route read for a non-owner');
assert.deepEqual(await writes(b), []);
});
// Shots for the build report: run with MOSAIC_UX_SHOTS=<dir>. Not set, filled and one refusal, at 390
// and 1440, light and dark.
test('shots', { timeout: 300000, skip: !process.env.MOSAIC_UX_SHOTS }, async t => {
const { f, b, wait, capture } = await w15Fixture(t, { slice: 'triage', scenario: scenario(unset, { 'POST /api/triage/route': { status: 400, body: { error: 'triage-zone-unknown', message: 'The time zone is not one this Console knows.' } } }) });
const results = [];
for (const scheme of ['light', 'dark']) {
await b.call('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: scheme }] });
for (const width of [390, 1440]) {
await b.viewport(width, width === 390 ? 844 : 900);
// A fresh document each round, so the kept fields of the last round do not carry over.
await b.navigate(`${f.base}/?shot=${scheme}-${width}#/settings/triage`);
await wait('document.getElementById("triage-route-model") && !!window.MosaicAppearance');
await b.evaluate(`window.MosaicAppearance.ready.then(() => window.MosaicAppearance.set({ mode: ${JSON.stringify(scheme)} }))`);
// No sideways scroll at either width.
assert.equal(await b.evaluate('document.documentElement.scrollWidth <= document.documentElement.clientWidth'), true, `${scheme} ${width} scrolls sideways`);
results.push(await capture(`notset-${scheme}`, width, '#/settings/triage'));
await fill(b);
results.push(await capture(`filled-${scheme}`, width, '#/settings/triage'));
await type(b, 'triage-route-zone', 'Mars/Olympus');
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait('document.getElementById("triage-route-line")?.textContent.includes("time zone")');
results.push(await capture(`refusal-${scheme}`, width, '#/settings/triage'));
}
}
for (const r of results) { assert.deepEqual(r.errors, [], r.full); assert.deepEqual(r.unexpected, [], r.full); }
});
+747
View File
@@ -0,0 +1,747 @@
// #1873 triage-wiring: the Console's Triage host gets its model route from the route store, the
// Triage key file, the organizations' model catalogue and the harness module, resolved again at
// each admit. Each refusal launches nothing and gives the drawer one plain line. The key goes only
// into the spawn environment. The Console process never loads the proxy management client or the
// key verbs' module. Everything here runs on a temp data root and config directory, a stand-in
// proxy that only accepts connections, and the stub adapter (fixtures/triage-adapter.mjs); no model
// is called.
import { test } from "node:test";
import assert from "node:assert/strict";
import { createServer } from "node:net";
import { request } from "node:http";
import { execFileSync, spawnSync } from "node:child_process";
import { createHash, randomBytes } from "node:crypto";
import { chmodSync, existsSync, linkSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
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 { 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";
import { startServer } from "../src/serve.mjs";
import { createDevOwner } from "../src/dev-owner.mjs";
import { NO_ROUTE_MESSAGE, capMessage } from "../src/triage-http.mjs";
import { keyIdOf, keyPrefix } from "../../cli/src/triage-key-file.mjs";
const FIXTURE = fileURLToPath(new URL("./fixtures/triage-adapter.mjs", import.meta.url));
const SRC = fileURLToPath(new URL("../src/", import.meta.url));
const REPO_ROOT = fileURLToPath(new URL("../../../", import.meta.url));
const skip = sandboxProblem() ?? false;
const scratch = () => mkdtempSync(join(process.env.TMPDIR, "tw-"));
const NOW = "2026-11-18T12:00:00.000Z";
const DAY = "2026-11-18T00:00:00.000Z";
const UNTIL = "2026-11-19T00:00:00.000Z";
const SNAPSHOT = {
snapshotVersion: 1, at: NOW, console: { state: "answering", at: NOW }, proxy: "not read", systemConfig: { valid: true, exit: 0, code: null },
organizations: { count: 1, list: [{ id: "alpha", bus: { state: "running", lastStart: null }, processing: "not read", config: { valid: true, exit: 0, code: null } }] },
};
const CONSOLE_ENV = { PATH: "/should/not/pass", LANG: "C.UTF-8", TZ: "UTC", HOME: "/home/someone", ANTHROPIC_API_KEY: "from-the-console", MOSAIC_CONFIG: "/x/config.json" };
const sha = (s) => createHash("sha256").update(s).digest("hex");
// The catalogue an organization's `routes` verb answers, for one model key.
const offer = (over = {}) => ({ key: "glm-5", provider: "zai", model: "glm-5.1", module: "zai", upstream: "https://api.example/v1", available: true, harnesses: ["fake"], ...over });
const catalogue = (models = [offer()], status = "ready") => ({ status, models });
// The usage source the host tests use: <run>/session/spend.jsonl, which the stub's SPEND writes.
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 };
},
};
// Issue a key as the key verbs store it: <configDir>/secrets/triage/model, 0600 in two 0700 directories.
function issue(configDir, { forDir = configDir } = {}) {
const secrets = join(configDir, "secrets");
const dir = join(secrets, "triage");
mkdirSync(dir, { recursive: true });
chmodSync(secrets, 0o700);
chmodSync(dir, 0o700);
const key = keyPrefix(forDir) + randomBytes(32).toString("base64url");
const file = join(dir, "model");
writeFileSync(file, JSON.stringify({ version: 1, key, keyId: keyIdOf(key), issuedAt: "2026-11-18T00:00:00.000Z", rotateBy: "2026-12-18", retiring: [] }), { mode: 0o600 });
chmodSync(file, 0o600);
return key;
}
// A launcher that only leaves marks: its argv, the names and values of its environment, one file each.
function spy(root) {
const f = join(root, "spy");
writeFileSync(f, `#!/bin/sh\nprintf '%s\\n' "$@" > '${join(root, "spawn-argv")}'\nenv > '${join(root, "spawn-env")}'\n: > '${join(root, "spawned")}'\nexit 0\n`, { mode: 0o755 });
return f;
}
// A launcher that keeps the relay open and leaves the capability it was handed.
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 waitFor(fn, ms = 30000) {
const end = Date.now() + ms;
while (Date.now() < end) {
const v = await fn();
if (v) return v;
await new Promise((r) => setTimeout(r, 50));
}
throw new Error("timed out");
}
async function runnerClient(root, dataRoot) {
const cap = await waitFor(() => { try { return JSON.parse(readFileSync(join(root, "stdin.json"), "utf8")).cap; } catch { return null; } });
return new Client({ path: join(dataRoot, "triage", "agent", "relay.sock"), cap });
}
const listen = (server) => new Promise((resolve) => server.listen(0, "127.0.0.1", () => resolve(server.address().port)));
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, 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");
mkdirSync(configDir, { mode: 0o700 });
mkdirSync(join(root, "install"));
const adapter = join(root, "install", "adapter.sh");
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" }),
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: portArg, catalogues: async () => (typeof cats.list === "function" ? cats.list() : cats.list), harnessModule: modules, installed: () => ["fake"], env: {}, keyOptions });
const h = createTriageHost({
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());
const setRoute = (limit = 100000, harness = "fake") => {
mkdirSync(join(dataRoot, "triage"), { recursive: true, mode: 0o700 });
chmodSync(dataRoot, 0o700);
chmodSync(join(dataRoot, "triage"), 0o700);
writeRoute(dataRoot, { model: "glm-5", harness, limit, timeZone: "UTC", by: "owner" }, { now: () => new Date(DAY) });
};
return { root, dataRoot, configDir, h, route, mod, cats, proxyPort, logs, setRoute };
}
// The Console's HTTP boundary for the dev local owner, in front of the real host.
async function consoleFor(t, h) {
const logs = [];
const s = await startServer({ port: 0, board: "http://127.0.0.1:9", sso: createDevOwner(), send: async () => ({}), triage: h, log: (line) => logs.push(line) });
t.after(() => new Promise((r) => s.close(r)));
const port = s.address().port;
const headers = { host: `127.0.0.1:${port}`, origin: `http://127.0.0.1:${port}`, "sec-fetch-site": "same-origin", "content-type": "application/json" };
const responses = [];
const send = (path, { method = "GET", body } = {}) => new Promise((resolve, reject) => {
const req = request({ host: "127.0.0.1", port, path, method, headers: method === "GET" ? { host: headers.host } : headers }, (res) => {
let text = "";
res.on("data", (c) => { text += c; });
res.on("end", () => { responses.push(text); resolve({ status: res.statusCode, text, json: (() => { try { return JSON.parse(text); } catch { return null; } })() }); });
});
req.on("error", reject);
req.end(body === undefined ? undefined : JSON.stringify(body));
});
return { logs, responses, read: () => send("/api/triage"), post: (text) => send("/api/triage/message", { method: "POST", body: { text } }) };
}
// Every regular file under dir, as text.
function filesUnder(dir, out = []) {
let names = [];
try { names = readdirSync(dir); } catch { return out; }
for (const n of names) {
const p = join(dir, n);
const st = lstatSync(p);
if (st.isDirectory()) filesUnder(p, out);
else if (st.isFile()) out.push([p, readFileSync(p, "latin1")]);
}
return out;
}
function assertNoKey(key, where) {
const secret = key.slice(key.indexOf("-triage-") + "-triage-".length);
assert.equal(secret.length, 43);
for (const [name, text] of where) {
assert.equal(text.includes(key), false, `the key is in ${name}`);
assert.equal(text.includes(secret), false, `the key's secret is in ${name}`);
}
}
const triageReplies = async (h, n) => waitFor(async () => {
const list = (await h.state()).transcript.filter((e) => e.from === "triage");
return list.length >= n ? list : null;
});
test("end to end: route set, key issued, the owner opens Triage and sends; answered, counted, the cap is hit, the next message is refused with the reset time; the key is only in the spawn", { skip, timeout: 120000 }, async (t) => {
const r = await rig(t);
r.setRoute(100);
const key = issue(r.configDir);
const c = await consoleFor(t, r.h);
const opened = await c.read();
assert.equal(opened.status, 200);
assert.deepEqual([opened.json.route, opened.json.running, opened.json.transcript], ["set", false, []]);
assert.equal(r.h.running, false, "opening Triage starts nothing");
// The session holds the key the file holds: it answers the key's hash, never the key.
assert.equal((await c.post("TOKEN")).status, 202);
assert.equal(r.h.running, true, "the first message starts it");
assert.equal((await triageReplies(r.h, 1))[0].text, `token sha256 ${sha(key)}`);
assert.equal((await c.post("SPEND 60")).status, 202);
assert.equal((await triageReplies(r.h, 2))[1].text, "spent 60");
const lines = await waitFor(() => { const l = readUsage(usagePath(r.dataRoot)).records; return l.some((x) => x.input === 60) ? l : null; });
assert.deepEqual(lines.at(-1).status === "known" && lines.at(-1).input, 60, "usage.jsonl gains the turn's line");
assert.equal((await c.read()).json.usageToday.count, 60);
// Under the cap: admitted; this turn crosses it and still answers.
assert.equal((await c.post("SPEND 50")).status, 202);
assert.equal((await triageReplies(r.h, 3))[2].text, "spent 50");
await waitFor(async () => (await c.read()).json.usageToday.count === 110);
const refused = await c.post("hello again");
assert.equal(refused.status, 429);
assert.deepEqual(refused.json, { error: "triage-over-cap", message: "Triage is over its daily token cap and starts again after the time shown", until: UNTIL });
assert.match(capMessage(refused.json.until), /2026-11-19T00:00:00.000Z/);
const after = await c.read();
assert.equal(after.json.overCapUntil, UNTIL);
assert.equal(after.json.usageToday.count, 110);
await r.h.stop();
assert.equal(r.h.running, false);
// The key scan: every file the host wrote under the data root (session.json, the bundle, the
// route receipts, usage.jsonl, the transcript and its host lines, the session's log), every log
// line, every GET and POST body. The config directory holds the key file itself and is not scanned.
const files = filesUnder(r.dataRoot);
const names = files.map(([p]) => p.slice(r.dataRoot.length + 1));
for (const want of ["triage/agent/session.json", "triage/agent/bundle/policy.json", "triage/host/transcript.jsonl", "triage/usage.jsonl"]) assert.ok(names.includes(want), `scanned ${want}: ${names.join(", ")}`);
assert.ok(names.includes("triage/route.json") && names.includes("triage/route-receipts.jsonl"), "the route store and its receipts are scanned");
assertNoKey(key, [...files, ["host log", r.logs.join("\n")], ["console log", c.logs.join("\n")], ["responses", c.responses.join("\n")], ["state", JSON.stringify(await r.h.state())]]);
});
test("the key reaches only the spawn environment: not the launcher's argv, a file, a log line or the read", { timeout: 30000 }, async (t) => {
const root0 = scratch();
// The module also answers a name it does not declare and a declared name with a non-string value:
// the route drops both, so the launch still goes ahead with only FAKE_OK.
const r = await rig(t, { bwrap: spy(root0), module: {
envNames: ["FAKE_OK", "FAKE_NUM", "MOSAIC_MODEL"],
env: () => ({ FAKE_OK: "1", FAKE_NUM: 3, UNDECLARED: "x", MOSAIC_MODEL: "the host sets this" }),
} });
r.setRoute();
const key = issue(r.configDir);
const c = await consoleFor(t, r.h);
assert.equal((await c.post("hello")).status, 202);
await waitFor(() => existsSync(join(root0, "spawned")));
const env = readFileSync(join(root0, "spawn-env"), "utf8");
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. 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);
rmSync(join(root0, "spawn-env"));
await waitFor(() => !r.h.running);
await c.read();
assertNoKey(key, [["argv", readFileSync(join(root0, "spawn-argv"), "utf8")], ...filesUnder(r.dataRoot), ...filesUnder(root0),
["host log", r.logs.join("\n")], ["console log", c.logs.join("\n")], ["responses", c.responses.join("\n")]]);
});
// Each refusal: nothing launches, the POST answers 409 with the one plain line, and the line holds
// no path, key or env value.
async function refusal(t, label, { setup, code, opts = {} }) {
const root0 = scratch();
const r = await rig(t, { bwrap: spy(root0), ...opts });
const key = await setup(r);
const c = await consoleFor(t, r.h);
const res = await c.post("hello");
assert.equal(res.status, 409, `${label}: ${res.text}`);
assert.equal(res.json.error, code, label);
const line = code === "triage-no-route" ? NO_ROUTE_MESSAGE : ROUTE_REFUSALS[code];
assert.deepEqual(res.json, { error: code, message: line }, label);
for (const bad of ["/", r.root, r.configDir, "127.0.0.1", "ANTHROPIC", "mst-"]) assert.equal(res.text.includes(bad), false, `${label}: the line holds ${bad}`);
if (typeof key === "string") assertNoKey(key, [["response", res.text], ["host log", r.logs.join("\n")], ...filesUnder(r.dataRoot)]);
assert.equal(existsSync(join(root0, "spawned")), false, `${label}: nothing launched`);
assert.equal(r.h.running, false);
return { r, c, res };
}
test("refusal: no route stored; the read still says not set", { timeout: 30000 }, async (t) => {
const { r, c } = await refusal(t, "no route", { setup: (r) => issue(r.configDir), code: "triage-no-route" });
assert.equal((await c.read()).json.route, "not set");
assert.equal(r.route.isSet(), false);
assert.deepEqual(await r.route.admit(), { ok: false, code: "triage-no-route" }, "the route says so on its own, not only the host");
});
test("refusal: no key issued", { timeout: 30000 }, async (t) => {
await refusal(t, "no key", { setup: (r) => { r.setRoute(); }, code: "triage-key-missing" });
await refusal(t, "no secrets directory", { setup: (r) => { r.setRoute(); mkdirSync(join(r.configDir, "secrets"), { mode: 0o700 }); chmodSync(join(r.configDir, "secrets"), 0o700); }, code: "triage-key-missing" });
});
test("refusal: an unsafe key file: mode, link, FIFO, second link, directory mode, linked directory, owner, shape", { timeout: 60000 }, async (t) => {
const file = (r) => join(r.configDir, "secrets", "triage", "model");
const cases = [
["mode 0644", (r) => { const k = issue(r.configDir); chmodSync(file(r), 0o644); return k; }],
["mode 0400", (r) => { const k = issue(r.configDir); chmodSync(file(r), 0o400); return k; }],
["a link", (r) => { const k = issue(r.configDir); const real = join(r.root, "real-model"); writeFileSync(real, readFileSync(file(r)), { mode: 0o600 }); rmSync(file(r)); symlinkSync(real, file(r)); return k; }],
["a FIFO", (r) => { issue(r.configDir); rmSync(file(r)); execFileSync("mkfifo", ["-m", "600", file(r)]); }],
["a second link", (r) => { const k = issue(r.configDir); linkSync(file(r), join(r.configDir, "secrets", "triage", "other")); return k; }],
["a directory", (r) => { issue(r.configDir); rmSync(file(r)); mkdirSync(file(r), { mode: 0o700 }); }],
["triage directory 0755", (r) => { const k = issue(r.configDir); chmodSync(join(r.configDir, "secrets", "triage"), 0o755); return k; }],
["secrets directory 0750", (r) => { const k = issue(r.configDir); chmodSync(join(r.configDir, "secrets"), 0o750); return k; }],
["triage directory a link", (r) => { const k = issue(r.configDir); const d = join(r.configDir, "secrets", "triage"); const moved = join(r.root, "moved"); execFileSync("mv", [d, moved]); symlinkSync(moved, d); return k; }],
["not JSON", (r) => { issue(r.configDir); writeFileSync(file(r), "not json"); }],
["another instance's key", (r) => issue(r.configDir, { forDir: "/elsewhere" })],
];
for (const [label, setup] of cases) {
await refusal(t, label, { setup: (r) => { r.setRoute(); return setup(r); }, code: "triage-key-unsafe" });
}
await refusal(t, "another owner", { setup: (r) => { r.setRoute(); return issue(r.configDir); }, code: "triage-key-unsafe", opts: { keyOptions: { fileUid: process.getuid() + 1 } } });
});
test("refusal: the proxy is not running", { timeout: 30000 }, async (t) => {
await refusal(t, "proxy down", { setup: (r) => { r.setRoute(); return issue(r.configDir); }, code: "triage-proxy-down", opts: { proxy: false } });
// No proxy configured at all.
const root = scratch();
const dataRoot = join(root, "data");
const route = createTriageRoute({ dataRoot, configDir: join(root, "cfg"), proxyPort: null });
mkdirSync(join(dataRoot, "triage"), { recursive: true, mode: 0o700 });
chmodSync(dataRoot, 0o700);
writeRoute(dataRoot, { model: "glm-5", harness: "fake", limit: 10, timeZone: "UTC", by: "owner" });
issue(join(root, "cfg"));
assert.deepEqual(await route.admit(), { ok: false, code: "triage-proxy-down" });
// No port is refused on its own, even with a probe that would say yes.
let probed = 0;
const yes = createTriageRoute({ dataRoot, configDir: join(root, "cfg"), proxyPort: null, probe: async () => { probed += 1; return true; } });
assert.deepEqual(await yes.admit(), { ok: false, code: "triage-proxy-down" });
assert.equal(probed, 0, "nothing is probed without a port");
});
test("refusal: the model is no longer offered (gone, not available, or catalogues disagree)", { timeout: 30000 }, async (t) => {
const setup = (r) => { r.setRoute(); return issue(r.configDir); };
await refusal(t, "gone", { setup, code: "triage-model-not-offered", opts: { catalogues: [catalogue([offer({ key: "other" })])] } });
await refusal(t, "not available", { setup, code: "triage-model-not-offered", opts: { catalogues: [catalogue([offer({ available: false })])] } });
await refusal(t, "no module", { setup, code: "triage-model-not-offered", opts: { catalogues: [catalogue([offer({ module: undefined })])] } });
await refusal(t, "disagree", { setup, code: "triage-model-not-offered", opts: { catalogues: [catalogue(), catalogue([offer({ model: "glm-4" })])] } });
await refusal(t, "no catalogue", { setup, code: "triage-catalogue-unavailable", opts: { catalogues: [] } });
await refusal(t, "catalogue not ready", { setup, code: "triage-catalogue-unavailable", opts: { catalogues: [catalogue([], "unavailable")] } });
await refusal(t, "catalogue read throws", { setup, code: "triage-catalogue-unavailable", opts: { catalogues: () => { throw new Error("/a/socket/path"); } } });
await refusal(t, "harness module loader throws", { setup, code: "triage-harness-incompatible", opts: { module: null } });
});
test("refusal: the harness cannot run the model, or its module is missing or broken", { timeout: 30000 }, async (t) => {
const setup = (r) => { r.setRoute(); return issue(r.configDir); };
await refusal(t, "not listed", { setup, code: "triage-harness-incompatible", opts: { catalogues: [catalogue([offer({ harnesses: ["pi"] })])] } });
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", () => {
// Recorded in the build report. The route reads only the key file; the proxy's list needs the
// management client, which the Console never loads (see the import test).
const text = readFileSync(join(SRC, "triage-route.mjs"), "utf8");
assert.match(text, /cannot be known without the proxy\s*\n?\/\/ management client/);
});
test("fence: a resolved route that fails the host's fences launches nothing and gives the refused line", { timeout: 30000 }, async (t) => {
// An adapter outside the module's install roots.
const root0 = scratch();
const r = await rig(t, { bwrap: spy(root0), module: { adapter: () => join(root0, "spy") } });
r.setRoute();
const key = issue(r.configDir);
await assert.rejects(r.h.message("hello"), (e) => e.code === "triage-route-invalid");
const tr = (await r.h.state()).transcript;
assert.deepEqual(tr.map((e) => [e.from, e.text]), [["owner", "hello"], ["host", ROUTE_REFUSED]]);
assert.equal(tr[1].in_reply_to, tr[0].id);
assert.equal(existsSync(join(root0, "spawned")), false);
assertNoKey(key, [["host log", r.logs.join("\n")], ...filesUnder(r.dataRoot)]);
// A key that is not this instance's shape (the host's token fence).
const root1 = scratch();
const s = await rig(t, { bwrap: spy(root1), tokenDir: "/another/instance" });
s.setRoute();
issue(s.configDir);
await assert.rejects(s.h.message("hello"), (e) => e.code === "triage-route-invalid");
assert.equal(existsSync(join(root1, "spawned")), false);
// A module env value with a control character.
const root2 = scratch();
const u = await rig(t, { bwrap: spy(root2), module: { env: () => ({ FAKE_OK: "a\nb" }) } });
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);
// 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) => {
const root0 = scratch();
const r = await rig(t, { bwrap: standIn(root0) });
r.setRoute();
issue(r.configDir);
const first = await r.h.message("one");
const c = await runnerClient(root0, r.dataRoot);
assert.equal(r.h.running, true);
r.mod.adapter = () => join(root0, "stand-in"); // now outside the install roots
await assert.rejects(r.h.message("two"), (e) => e.code === "triage-route-invalid");
assert.deepEqual((await c.call("message.receive")).map((m) => m.id), [first.id], "only the admitted message reaches the session");
const host = (await r.h.state()).transcript.filter((e) => e.from === "host");
assert.deepEqual(host.map((e) => e.text), [ROUTE_REFUSED]);
// And a refusal from the sources while it runs: the key goes away.
rmSync(join(r.configDir, "secrets", "triage", "model"));
await assert.rejects(r.h.message("three"), (e) => e.code === "triage-key-missing");
assert.deepEqual(await c.call("message.receive"), []);
});
// 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 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: 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"]);
await h.message("hello");
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", () => {
const harnesses = ["claude-code", "fake"];
assert.deepEqual(mergeCatalogues([], harnesses), { status: "unavailable", models: [] });
assert.deepEqual(mergeCatalogues([{ status: "proxy-down" }], harnesses), { status: "proxy-down", models: [] });
const both = mergeCatalogues([catalogue([offer({ harnesses: ["fake", "claude-code"] })]), catalogue([offer({ harnesses: ["fake"], available: true })])], harnesses);
assert.deepEqual(both.models.map((m) => [m.key, m.available, m.harnesses]), [["glm-5", true, ["fake"]]]);
const oneOff = mergeCatalogues([catalogue(), catalogue([offer({ available: false })])], harnesses);
assert.equal(oneOff.models[0].available, false, "available only when every listing says so");
assert.deepEqual(mergeCatalogues([catalogue([offer({ harnesses: ["pi"] })])], harnesses).models[0].harnesses, [], "only installed harnesses");
const ready = mergeCatalogues([catalogue()], harnesses);
assert.equal(checkModel({ model: "glm-5", harness: "fake", catalogue: ready, harnesses }), null);
assert.equal(checkModel({ model: "glm-5", harness: "claude-code", catalogue: ready, harnesses }), "triage-harness-incompatible");
assert.equal(checkModel({ model: "glm-5", harness: "off", catalogue: mergeCatalogues([catalogue([offer({ harnesses: ["off"] })])], ["off"]), harnesses }), "triage-harness-incompatible", "a harness not installed here");
assert.equal(checkModel({ model: "nope", harness: "fake", catalogue: ready, harnesses }), "triage-model-not-offered");
assert.equal(checkModel({ model: "glm-5", harness: "fake", catalogue: { status: "unavailable", models: [] }, harnesses }), "triage-catalogue-unavailable");
assert.deepEqual(installedHarnesses().filter((n) => ["claude-code", "pi"].includes(n)), ["claude-code", "pi"]);
});
// The Console never loads the proxy management client or the key verbs' module. A resolve hook in a
// child process records every module the process loads.
function loaded(t, args) {
const dir = scratch();
const out = join(dir, "loaded.txt");
writeFileSync(out, "");
const hook = join(dir, "hook.mjs");
writeFileSync(hook, `import { register } from "node:module";\nregister("data:text/javascript," + encodeURIComponent(${JSON.stringify(
`import { appendFileSync } from "node:fs";\nexport async function resolve(s, c, next) { const r = await next(s, c); appendFileSync(${JSON.stringify(out)}, r.url + "\\n"); return r; }\n`,
)}));\n`);
const res = spawnSync(process.execPath, ["--import", hook, ...args], { cwd: dir, encoding: "utf8", timeout: 60000, env: { PATH: process.env.PATH, HOME: process.env.HOME, TMPDIR: process.env.TMPDIR, LANG: "C.UTF-8" } });
assert.equal(res.status, 0, res.stderr);
return readFileSync(out, "utf8").split("\n").filter(Boolean).map((u) => (u.startsWith("file:") ? fileURLToPath(u) : u));
}
const CLIENT = join(REPO_ROOT, "packages", "model-proxy", "src", "client.mjs");
const KEY_VERBS = join(REPO_ROOT, "packages", "cli", "src", "triage-key.mjs");
const KEY_FILE = join(REPO_ROOT, "packages", "cli", "src", "triage-key-file.mjs");
test("the Console process never imports the proxy management client or the key verbs' module", { timeout: 120000 }, (t) => {
// The control: the hook sees them when a module that uses them loads.
const control = loaded(t, ["-e", `await import(${JSON.stringify(KEY_VERBS)})`]);
assert.ok(control.includes(CLIENT) && control.includes(KEY_VERBS), "the hook records the client when it loads");
// The Console's own entry, every static import: --help loads the whole graph and exits.
const console_ = loaded(t, [join(SRC, "cli.mjs"), "--help"]);
assert.ok(console_.includes(join(SRC, "triage-route.mjs")) && console_.includes(KEY_FILE), "the Console loads the route and the key reader");
assert.equal(console_.includes(CLIENT), false, "the Console loads the management client");
assert.equal(console_.includes(KEY_VERBS), false, "the Console loads the key verbs' module");
// And what the route loads at an admit: every installed harness module.
const admit = loaded(t, ["-e", `const m = await import(${JSON.stringify(join(SRC, "triage-route.mjs"))}); for (const n of m.installedHarnesses()) await m.repoHarnessModule(n); await import(${JSON.stringify(join(SRC, "triage-host.mjs"))});`]);
assert.ok(admit.some((p) => p.endsWith(join("harness", "claude-code", "index.mjs"))));
assert.equal(admit.includes(CLIENT), false);
assert.equal(admit.includes(KEY_VERBS), false);
});
// The Console's own wiring (cli.mjs): a dev-owner Console on a temp named instance with proxy.json
// naming a stand-in proxy, a stored route, an issued key, and one active organization whose bus (a
// 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). 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");
const { DEV_OWNER } = await import("../../cli/src/dev-local-owner.mjs");
const { installPaths } = await import("../../model-proxy/src/settings.mjs");
const { pathToFileURL } = await import("node:url");
const { spawn } = await import("node:child_process");
const { renameSync } = await import("node:fs");
const root = tmp(t, "tw-c-"), f = fixture(root), home = join(root, "home"), configDir = join(home, ".config", "mosaic-test");
mkdirSync(join(home, ".config"), { recursive: true });
renameSync(join(root, "config"), configDir);
chmodSync(configDir, 0o700);
chmodSync(join(configDir, "businesses"), 0o700);
writeFileSync(join(f.dataRoot, "instance.json"), JSON.stringify({ instanceVersion: 1, name: "test" }));
const doc = structuredClone(f.doc);
doc.id = "alpha"; doc.human = DEV_OWNER.sub; doc.projects.stack.root = "/__mosaic_org__/alpha/projects/stack";
await createOrg({ configDir, dataRoot: f.dataRoot, instance: "test", principal: DEV_OWNER }, "alpha", doc);
const proxyServer = createServer((s) => s.destroy());
const proxyPort = await listen(proxyServer);
t.after(() => closed(proxyServer));
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" });
const key = issue(configDir);
const log = join(root, "calls.jsonl");
writeFileSync(log, "");
const busURL = new URL("../src/bus.mjs", import.meta.url).href, orgBusURL = new URL("../../cli/src/org-bus.mjs", import.meta.url).href;
const busShim = join(root, "bus-shim.mjs"), orgBusShim = join(root, "org-bus-shim.mjs"), hook = join(root, "hook.mjs");
writeFileSync(busShim, `import { appendFileSync } from 'node:fs';
export * from ${JSON.stringify(busURL)};
export const busCaller = o => async (verb) => {
appendFileSync(${JSON.stringify(log)}, JSON.stringify({ business: o.business ?? null, verb }) + '\\n');
if (verb === 'routes') return { catalogue: ${JSON.stringify(catalogue([offer({ harnesses: ["pi"] })]))} };
return verb === 'plugins' ? { plugins: { channel: ['cli', 'webui'] } } : [];
};
`);
writeFileSync(orgBusShim, `export * from ${JSON.stringify(orgBusURL)};\nexport const busStatus = async () => ({ healthy: true });\n`);
writeFileSync(hook, `import { registerHooks } from 'node:module';
registerHooks({ resolve(specifier, context, next) {
if (context.parentURL?.endsWith('/webui/src/cli.mjs')) {
if (specifier === './bus.mjs') return { url: ${JSON.stringify(pathToFileURL(busShim).href)}, shortCircuit: true };
if (specifier === '../../cli/src/org-bus.mjs') return { url: ${JSON.stringify(pathToFileURL(orgBusShim).href)}, shortCircuit: true };
}
return next(specifier, context);
} });
`);
const env = { ...f.env, HOME: home, MOSAIC_CONFIG: join(configDir, "config.json"), NODE_OPTIONS: `--import=${pathToFileURL(hook).href}` };
const child = spawn(process.execPath, [join(SRC, "cli.mjs"), "serve", "--port", "0", "--board", "http://127.0.0.1:9", "--dev-local-owner"], { env, stdio: ["ignore", "pipe", "pipe"] });
let out = "";
child.stdout.on("data", (c) => { out += c; });
child.stderr.on("data", (c) => { out += c; });
t.after(() => { if (child.exitCode === null) child.kill("SIGTERM"); });
for (let i = 0; i < 300 && !/Mosaic Console: (http:\/\/127\.0\.0\.1:\d+\/)/.test(out); i++) {
if (child.exitCode !== null) break;
await new Promise((r) => setTimeout(r, 50));
}
const base = /Mosaic Console: http:\/\/127\.0\.0\.1:(\d+)\//.exec(out);
assert.ok(base, `the Console did not start:\n${out}`);
const port = Number(base[1]);
const call = (path, body) => new Promise((resolve, reject) => {
const headers = body === undefined ? { host: `127.0.0.1:${port}` } : { host: `127.0.0.1:${port}`, origin: `http://127.0.0.1:${port}`, "sec-fetch-site": "same-origin", "content-type": "application/json" };
const req = request({ host: "127.0.0.1", port, path, method: body === undefined ? "GET" : "POST", headers }, (res) => {
let text = "";
res.on("data", (c) => { text += c; });
res.on("end", () => resolve({ status: res.statusCode, text, json: JSON.parse(text) }));
});
req.on("error", reject);
req.end(body === undefined ? undefined : JSON.stringify(body));
});
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 + down.text + res.text], ...filesUnder(f.dataRoot)]);
});