Adds the harness plug-in kind (interface 1). packages/plugins/harness/pi and claude-code hold what core branched on: the adapter, thinking levels, preflight, version probe, sandbox mounts, builtins line, typed tool names, gate timeout, bundle files, adapter env, turn end and session-files path. Core reaches a module only through the loader. A session's route is the instance's harness, model and thinking vars, or the three fallback. vars; a missing one refuses no-route, and the system file's execution keys are never read for a session. The launcher checks the primary route, then the fallback: harness-off, harness-refused, thinking-unsupported, then preflight. A failed preflight falls back when a fallback is set, and any refusal of the fallback names the primary's failure too. Account checks run only when the route's module counts an account. The manifest records route, routeReason and harnessVersion, replacing piVersion and claudeVersion. pi gets --thinking, Claude Code --effort, both from MOSAIC_THINKING. The business file refuses a partial fallback. routes and routes.set are human-only bus verbs outside ORGANIZATION_KEYS; checkRoutes is shared by routes.set, instances.set and limits.set and checks only changed routes. The chief of staff changes an instance's model, and the assistant's whole vars (Q169), through limits.set, so its route write runs the same checks. A model outside launch.models refuses unknown-model, primary or fallback, and routes.set passes that code through. A new run binds only on a switched-on harness module name and records its transcript path, which the usage reader uses. Settings gains a Routing table with six selects per instance and loses the Role models fieldset; it posts changed rows to /api/routes. webui/src/bus.mjs is unchanged. Until Jason's `mosaic plugins set` adds "harness": ["claude-code"], a new launch refuses harness-off.
176 lines
12 KiB
JavaScript
176 lines
12 KiB
JavaScript
// Row 65 (#1558, REQ-PLUG-2, mos-0840): the Console's routing table read and write. The write has
|
|
// the Settings boundary and makes one human-transport call, `routes.set`; the route rules are the
|
|
// bus's and the business writer's, tested there. The first test runs a real broker over a temporary
|
|
// organization directory, so a save reaches the file and the log, and each refusal leaves both.
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { request } from 'node:http';
|
|
import { mkdirSync, readFileSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { startServer } from '../src/serve.mjs';
|
|
import { BusReadError } from '../src/bus.mjs';
|
|
import { BusError } from '../../bus/src/broker.mjs';
|
|
import { startBroker } from '../../bus/src/runtime.mjs';
|
|
import { systemFor } from '../../business/tests/helpers.mjs';
|
|
import { fixture, tmp } from '../../cli/tests/helpers.mjs';
|
|
|
|
const preview = { host: 'console.example', header: 'x-user', user: 'jason' };
|
|
const headers = { host: 'console.example', 'x-user': 'jason', origin: 'https://console.example', 'sec-fetch-site': 'same-origin', 'content-type': 'application/json' };
|
|
const REV = 'a'.repeat(64);
|
|
const ROUTE = { harness: 'pi', model: 'glm-5.3-flash', thinking: 'low', fallback: null };
|
|
const view = (extra = {}) => ({ business: 'acme', revision: 'b'.repeat(64), harnesses: ['pi'], models: ['glm-5.3-flash'], levels: { pi: ['low'] }, instances: { coder: ROUTE }, ...extra });
|
|
|
|
async function setup(t, { on = true, writer = on, write = async () => view(), bus = null } = {}) {
|
|
const calls = [], lines = [];
|
|
const server = await startServer({
|
|
port: 0, preview, bus, log: line => lines.push(line), send: on ? async () => ({}) : null,
|
|
routesWrite: writer ? async args => { calls.push(args); return write(args); } : null,
|
|
});
|
|
t.after(() => new Promise(r => server.close(r)));
|
|
return { calls, lines, port: server.address().port };
|
|
}
|
|
const send = (s, path, data, hs = headers, method = 'POST') => new Promise((resolve, reject) => {
|
|
const req = request({ hostname: '127.0.0.1', port: s.port, path, method, headers: hs }, r => {
|
|
let text = ''; r.on('data', c => text += c); r.on('end', () => resolve({ status: r.statusCode, body: text ? JSON.parse(text) : null }));
|
|
});
|
|
req.on('error', reject);
|
|
req.end(data === undefined ? undefined : typeof data === 'string' ? data : JSON.stringify(data));
|
|
});
|
|
const get = (s, path) => send(s, path, undefined, { host: 'console.example', 'x-user': 'jason' }, 'GET');
|
|
|
|
test('row 65 Console: the signed-in human saves a route through a real broker; each refusal says nothing changed and leaves the file and the log', async t => {
|
|
const root = tmp(t, 'webui-routes-');
|
|
const f = fixture(root, 'acme');
|
|
mkdirSync(f.dataRoot, { mode: 0o700 });
|
|
const dir = join(root, 'config');
|
|
const host = await startBroker({
|
|
dataRoot: f.dataRoot,
|
|
businesses: { acme: { id: 'acme', human: 'jason', arbiters: { technical: 'coder', delivery: 'coder' }, roles: { coder: { authority: { withinRole: [], crossRole: [] } } } } },
|
|
organization: { dir, rolesDir: f.roles, system: systemFor(root) },
|
|
});
|
|
t.after(() => host.close());
|
|
const human = host.broker.bindHuman({ business: 'acme', human: 'jason', via: 'cli', outsideAgent: true });
|
|
const call = async (verb, args) => {
|
|
try { return structuredClone(host.broker.request(human, { verb, args })); } catch (e) {
|
|
if (e instanceof BusError) throw new BusReadError(e.code);
|
|
throw e;
|
|
}
|
|
};
|
|
const s = await setup(t, { bus: { routes: () => call('routes') }, write: args => call('routes.set', args) });
|
|
const file = join(dir, 'businesses', 'acme.json');
|
|
const log = () => readFileSync(join(dir, 'businesses', '.history', 'acme', 'changes.jsonl'), 'utf8').trim().split('\n').filter(Boolean).map(l => JSON.parse(l));
|
|
const read = await get(s, '/api/bus/routes');
|
|
assert.equal(read.status, 200);
|
|
const [current] = read.body.rows;
|
|
assert.deepEqual(current.harnesses, ['pi', 'claude-code']);
|
|
assert.deepEqual(current.levels['claude-code'], ['low', 'medium', 'high', 'xhigh', 'max']);
|
|
assert.deepEqual(current.instances.coder, { harness: 'pi', model: 'glm-5.3-flash', thinking: 'high', fallback: null });
|
|
|
|
const route = { harness: 'claude-code', model: 'claude-opus-5-5', thinking: 'xhigh', fallback: { harness: 'pi', model: 'glm-5.3-flash', thinking: 'off' } };
|
|
const saved = await send(s, '/api/routes', { revision: current.revision, routes: { coder: route } });
|
|
assert.equal(saved.status, 200, JSON.stringify(saved.body));
|
|
assert.deepEqual(saved.body.instances.coder, route);
|
|
assert.deepEqual(s.calls, [{ revision: current.revision, routes: { coder: route } }]);
|
|
assert.match(s.lines.at(-1), / routes user="jason" instances="coder" done$/);
|
|
const entry = log().at(-1);
|
|
assert.deepEqual([entry.actor, entry.state, entry.transaction], ['jason', 'committed', saved.body.transaction]);
|
|
const vars = JSON.parse(readFileSync(file, 'utf8')).roles.coder.vars;
|
|
assert.deepEqual(['harness', 'model', 'thinking', 'fallback.harness', 'fallback.model', 'fallback.thinking'].map(k => vars[k]), ['claude-code', 'claude-opus-5-5', 'xhigh', 'pi', 'glm-5.3-flash', 'off']);
|
|
|
|
const before = readFileSync(file, 'utf8'), entries = log().length;
|
|
const coder = more => ({ coder: { ...route, ...more } });
|
|
for (const [routes, revision, status, code, why] of [
|
|
[coder({ harness: 'cursor' }), saved.body.revision, 400, 'harness-off', /isn't switched on in the business file's plugins\.harness/],
|
|
[coder({ thinking: 'minimal' }), saved.body.revision, 400, 'thinking-unsupported', /doesn't apply that thinking level/],
|
|
[coder({ fallback: { harness: 'pi', model: 'glm-5.3-flash' } }), saved.body.revision, 400, 'organization-partial-fallback', /sets its harness, model and thinking together, or is cleared/],
|
|
[{ ghost: route }, saved.body.revision, 400, 'organization-unknown-instance', /no such instance/],
|
|
[{ coder: route }, saved.body.revision, 400, 'organization-no-change', /already says that/],
|
|
[coder({ thinking: 'low' }), current.revision, 409, 'revision-conflict', /changed since you read it/],
|
|
]) {
|
|
const r = await send(s, '/api/routes', { revision, routes });
|
|
assert.deepEqual([r.status, r.body.error, r.body.sent], [status, code, false], code);
|
|
assert.match(r.body.message, why, code);
|
|
assert.match(r.body.message, new RegExp(`^${code}: .*Nothing changed\\.$`), code);
|
|
}
|
|
assert.equal(readFileSync(file, 'utf8'), before);
|
|
assert.equal(log().length, entries);
|
|
});
|
|
|
|
test('row 65 Console: the routes write needs send on, a writer, the public name, the user, the exact Origin and same-origin Fetch Metadata', async t => {
|
|
const data = { revision: REV, routes: { coder: ROUTE } };
|
|
const off = await setup(t, { on: false });
|
|
assert.equal((await send(off, '/api/routes', data)).status, 403);
|
|
const unsent = await setup(t, { on: false, writer: true });
|
|
assert.equal((await send(unsent, '/api/routes', data)).status, 403);
|
|
assert.equal(unsent.calls.length, 0);
|
|
const unwritten = await setup(t, { writer: false });
|
|
assert.equal((await send(unwritten, '/api/routes', data)).status, 403);
|
|
const s = await setup(t);
|
|
for (const [key, value, status] of [['host', 'wrong.example', 403], ['x-user', 'other', 401], ['x-user', ['jason', 'jason'], 401], ['origin', 'https://else.example', 403], ['origin', undefined, 403], ['sec-fetch-site', 'cross-site', 403], ['sec-fetch-site', undefined, 403]]) {
|
|
const hs = { ...headers };
|
|
if (value === undefined) delete hs[key]; else hs[key] = value;
|
|
assert.equal((await send(s, '/api/routes', data, hs)).status, status, key);
|
|
}
|
|
assert.equal((await send(s, '/api/routes', undefined, headers, 'GET')).status, 405);
|
|
assert.equal(s.calls.length, 0);
|
|
});
|
|
|
|
test('row 65 Console: the routes write takes JSON within 16 KiB with a revision and routes only; an actor is refused and routes are never logged', async t => {
|
|
const s = await setup(t);
|
|
const base = { revision: REV, routes: { coder: ROUTE } };
|
|
for (const value of [
|
|
{ ...base, actor: 'mos' }, { ...base, instances: { coder: 'x' } }, { revision: REV }, { routes: base.routes },
|
|
{ ...base, revision: 'stale' }, { ...base, revision: 7 }, { ...base, routes: {} }, { ...base, routes: [] }, { ...base, routes: null },
|
|
[], 'bad', { ...base, routes: { coder: { ...ROUTE, model: 'x'.repeat(17000) } } },
|
|
]) assert.equal((await send(s, '/api/routes', value)).status, 400, JSON.stringify(value).slice(0, 60));
|
|
assert.equal((await send(s, '/api/routes', base, { ...headers, 'content-type': 'text/plain' })).status, 400);
|
|
assert.equal(s.calls.length, 0);
|
|
// A route with a model the bus refuses: the log names the instance and the code, never the route.
|
|
const refused = await setup(t, { write: async () => { throw new BusReadError('unknown-model'); } });
|
|
await send(refused, '/api/routes', { revision: REV, routes: { coder: { ...ROUTE, model: 'secret-model-name' } } });
|
|
assert.deepEqual(refused.lines.map(l => l.replace(/^\S+ /, '')), ['routes user="jason" instances="coder" error=unknown-model']);
|
|
assert.ok([...s.lines, ...refused.lines].every(l => !/xxxx|secret-model-name|glm/.test(l)), 'routes are never logged');
|
|
});
|
|
|
|
test('row 65 Console: a bus refusal maps to its status and says nothing changed; a lost or odd answer says the change may have been made', async t => {
|
|
const fail = code => async () => { throw new BusReadError(code); };
|
|
for (const [write, status, error, sent] of [
|
|
[fail('harness-off'), 400, 'harness-off', false], [fail('harness-refused'), 400, 'harness-refused', false],
|
|
[fail('thinking-unsupported'), 400, 'thinking-unsupported', false], [fail('unknown-model'), 400, 'unknown-model', false],
|
|
[fail('organization-partial-fallback'), 400, 'organization-partial-fallback', false],
|
|
[fail('organization-unknown-instance'), 400, 'organization-unknown-instance', false],
|
|
[fail('organization-no-change'), 400, 'organization-no-change', false], [fail('revision-conflict'), 409, 'revision-conflict', false],
|
|
[fail('human-required'), 403, 'human-required', false], [fail('invalid-request'), 400, 'invalid-request', false],
|
|
[fail('outcome-unknown'), 503, 'outcome-unknown', 'unknown'], [async () => { throw new Error('lost'); }, 503, 'outcome-unknown', 'unknown'],
|
|
// Each part of the answer's shape wrong alone, and an answer without the instance sent.
|
|
[async () => view({ revision: 'x' }), 503, 'outcome-unknown', 'unknown'],
|
|
[async () => view({ harnesses: null }), 503, 'outcome-unknown', 'unknown'],
|
|
[async () => view({ models: {} }), 503, 'outcome-unknown', 'unknown'],
|
|
[async () => view({ levels: [] }), 503, 'outcome-unknown', 'unknown'],
|
|
[async () => view({ instances: null }), 503, 'outcome-unknown', 'unknown'],
|
|
[async () => view({ instances: {} }), 503, 'outcome-unknown', 'unknown'],
|
|
]) {
|
|
const s = await setup(t, { write });
|
|
const r = await send(s, '/api/routes', { revision: REV, routes: { coder: ROUTE } });
|
|
assert.deepEqual([r.status, r.body.error, r.body.sent], [status, error, sent], error);
|
|
if (sent === false) assert.match(r.body.message, new RegExp(`^${error}: .*Nothing changed\\.$`), error);
|
|
else assert.match(r.body.message, /may have been made/, error);
|
|
assert.equal(s.calls.length, 1);
|
|
assert.match(s.lines[0], new RegExp(` routes user="jason" instances="coder" error=${error}$`));
|
|
}
|
|
});
|
|
|
|
test('row 65 Console: the routes read takes no arguments, needs a bus and refuses an odd answer', async t => {
|
|
const none = await setup(t);
|
|
assert.equal((await get(none, '/api/bus/routes')).status, 503);
|
|
const s = await setup(t, { bus: { routes: async () => view() } });
|
|
assert.equal((await get(s, '/api/bus/routes?x=1')).status, 400);
|
|
assert.deepEqual((await get(s, '/api/bus/routes')).body.rows, [view()]);
|
|
for (const odd of [{ revision: 'x' }, { harnesses: 'pi' }, { models: null }, { levels: [] }, { instances: [] }]) {
|
|
const r = await setup(t, { bus: { routes: async () => view(odd) } });
|
|
assert.equal((await get(r, '/api/bus/routes')).status, 502, JSON.stringify(odd));
|
|
}
|
|
const refused = await setup(t, { bus: { routes: async () => { throw new BusReadError('human-required'); } } });
|
|
assert.equal((await get(refused, '/api/bus/routes')).status, 403);
|
|
});
|