Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88d08c8437 | ||
|
|
4377941bd4 | ||
|
|
de39750ebb | ||
|
|
3b17326b74 | ||
|
|
d03ae494e8 | ||
|
|
e1b56d9ea7 | ||
|
|
e6f6f5445b | ||
|
|
55f9652f6a | ||
|
|
9e434c0e9c | ||
|
|
e732ae7450 | ||
|
|
66255c9bf2 | ||
|
|
cecc901854 | ||
|
|
81ca8399a2 | ||
|
|
37c3854849 | ||
|
|
db2dc96aa2 |
No files matched your search
@@ -7,11 +7,66 @@ import { homedir } from 'node:os';
|
||||
import { fetchPinned } from './fetch.mjs';
|
||||
import { PIN } from './common.mjs';
|
||||
import { localRide, riddenOrgs } from './ride.mjs';
|
||||
import { releaseState, initState, fenceState, initLockStatus, clearInitLock, appendReceipt, recentReceipts, receiptsText, createUnitControl, FILE_LABELS } from './admin.mjs';
|
||||
import { releaseState, initState, fenceState, initLockStatus, clearInitLock, appendReceipt, recentReceipts, receiptsText, createUnitControl, FILE_LABELS, LOG_LINES, receiptHistory, serviceKeyState } from './admin.mjs';
|
||||
import { secretValues } from './redact.mjs';
|
||||
import { discoverModules, hasModule } from '../../plugins/src/loader.mjs';
|
||||
import { readPluginLock } from '../../plugins/src/pinned.mjs';
|
||||
|
||||
export const proxyCode = (error, fallback) => (/^proxy-[a-z-]+$/.test(error?.code ?? '') ? error.code : fallback);
|
||||
const failure = code => Object.assign(new Error(code), { code });
|
||||
|
||||
// #1873 settings-backend A2: the harness programs installed on this instance, from the plugin tree
|
||||
// through the plugins loader and from the instance's plugin lock, the way the bus lists them
|
||||
// (runtime.mjs harnessLevels). No name list here. A tree that cannot be read answers null; a lock
|
||||
// that cannot be read adds nothing, so a pinned program is then not offered.
|
||||
export function installedHarnesses(dataRoot = null) {
|
||||
let names;
|
||||
try { names = discoverModules('harness').names.filter(name => hasModule('harness', name)); } catch { return null; }
|
||||
if (dataRoot !== null) {
|
||||
try { names = [...new Set([...names, ...Object.keys(readPluginLock(dataRoot).plugins).filter(name => hasModule('harness', name, dataRoot))])]; } catch { /* the tree's list */ }
|
||||
}
|
||||
return names.sort();
|
||||
}
|
||||
|
||||
// #1873 settings-backend A2: every harness an organization document names for an agent: each
|
||||
// `harness` or `fallback.harness` string anywhere in it (role vars, project agent vars, or any
|
||||
// later place), outside its plugins list. Broader than the routes view on purpose: a program is
|
||||
// never turned off where anything in the file still names it.
|
||||
export function harnessesNamed(document) {
|
||||
const named = new Set();
|
||||
const walk = (value, depth) => {
|
||||
if (depth > 32 || value === null || typeof value !== 'object') return;
|
||||
if (Array.isArray(value)) { for (const item of value) walk(item, depth + 1); return; }
|
||||
for (const [key, item] of Object.entries(value)) {
|
||||
if ((key === 'harness' || key === 'fallback.harness') && typeof item === 'string') named.add(item);
|
||||
else walk(item, depth + 1);
|
||||
}
|
||||
};
|
||||
if (document === null || typeof document !== 'object' || Array.isArray(document)) return null;
|
||||
for (const [key, value] of Object.entries(document)) if (key !== 'plugins') walk(value, 1);
|
||||
return named;
|
||||
}
|
||||
|
||||
// #1873 settings-backend A2 (review P1-1): the harnesses the root agents an organization takes name.
|
||||
// `root` is what loadRootAgents answered ({state, agents, assistant, definitions?}). An organization
|
||||
// takes every root agent except the root assistant and the ones that exclude it, the way
|
||||
// materializeCopies copies them at bus boot. Anything not in that shape answers null (unreadable).
|
||||
export function rootHarnessesNamed(root, orgId) {
|
||||
if (root === null || typeof root !== 'object' || root.agents === null || typeof root.agents !== 'object' || Array.isArray(root.agents)) return null;
|
||||
const taken = {};
|
||||
for (const [name, entry] of Object.entries(root.agents)) {
|
||||
if (entry === null || typeof entry !== 'object' || !Array.isArray(entry.excludeOrgs)) return null;
|
||||
if (entry.definition === 'assistant' || entry.excludeOrgs.includes(orgId)) continue;
|
||||
taken[name] = entry.vars ?? {};
|
||||
}
|
||||
return harnessesNamed({ agents: taken, definitions: root.definitions ?? {} });
|
||||
}
|
||||
// A key id is the first 12 hex of the key's sha256. A label is a short plain name; anything that
|
||||
// looks like a Mosaic key prefix is dropped, so an answer can never carry key text.
|
||||
const KEY_ID = /^[a-f0-9]{12}$/;
|
||||
const plainLabel = value => (typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/.test(value) && !/ms[kotp][-_]/i.test(value) ? value : null);
|
||||
const isoTime = value => (typeof value === 'string' && /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:.]{1,18}Z$/.test(value) ? value : null);
|
||||
|
||||
// `buses()` proves the init-lock condition "bus stopped" ({stopped, running:[label]}).
|
||||
// `orgs()` lists the organizations this proxy serves: [{id, catalogue}]. `unit` and `fetch` are
|
||||
// (and `pin`) seams for tests; the defaults run the real proxy-service.sh, systemctl and pinned fetch.
|
||||
@@ -20,7 +75,11 @@ const failure = code => Object.assign(new Error(code), { code });
|
||||
// change log, `call(verb, args)` reaches its bus as the owner and `status()` is its bus status. `ownerModels()` answers
|
||||
// {proxy, riding, models} from the riding buses' catalogues. `move(id, models, principal)` moves a
|
||||
// stopped fixture organization to the owner's models through the organization service.
|
||||
export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir = null, unit = null, fetch = fetchPinned, buses, orgs = async () => [], riders = async () => [], ownerModels = async () => ({ proxy: false, riding: false, models: [] }), move = null, now = () => new Date(), env = process.env, pin = PIN }) {
|
||||
// #1873 settings-backend A2: a rider may also have `file()`, its organization file as the writer
|
||||
// reads it ({document, revision}), read only. `harnesses()` lists the installed harness programs.
|
||||
// `rootAgents()` reads the root agent file (loadRootAgents); with none given, or one that throws,
|
||||
// no organization that has the program on is written (unreadable).
|
||||
export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir = null, unit = null, fetch = fetchPinned, buses, orgs = async () => [], riders = async () => [], harnesses = () => installedHarnesses(dataRoot), rootAgents = null, ownerModels = async () => ({ proxy: false, riding: false, models: [] }), move = null, now = () => new Date(), env = process.env, pin = PIN }) {
|
||||
const port = () => { try { return initState(configDir, dataRoot).settings?.port ?? null; } catch { return null; } };
|
||||
const control = unit ?? createUnitControl({ instance, unitDir, port, env });
|
||||
let job = { state: 'idle', step: null, receipt: null, error: null };
|
||||
@@ -97,6 +156,83 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
return (await riders()).map(r => { const state = decide(r, wasRidden); return state === 'fixture' && typeof r.status !== 'function' ? { id: r.id, state, running: null } : { id: r.id, state }; });
|
||||
} catch { return null; }
|
||||
}
|
||||
// #1873 settings-backend A2, item 5: which harness programs each organization has on, from its
|
||||
// bus's plugins read as the owner. null when the read fails, its answer has no harness list, or
|
||||
// any entry is not a plugin name (review P3-3: no bus text reaches the page).
|
||||
const harnessList = value => (Array.isArray(value?.plugins?.harness) && value.plugins.harness.every(n => typeof n === 'string' && /^[a-z][a-z0-9-]{0,63}$/.test(n)) ? [...value.plugins.harness] : null);
|
||||
async function programsSummary() {
|
||||
let installed = null;
|
||||
try { installed = harnesses(); if (!Array.isArray(installed)) installed = null; } catch { installed = null; }
|
||||
let list;
|
||||
try { list = await riders(); } catch { return { installed, organizations: null }; }
|
||||
const organizations = await Promise.all(list.map(async rider => {
|
||||
try { return { id: rider.id, on: harnessList(await rider.call('plugins')) }; } catch { return { id: rider.id, on: null }; }
|
||||
}));
|
||||
return { installed, organizations };
|
||||
}
|
||||
// Item 5, one organization: turn `name` off unless something still uses it or it is the only one.
|
||||
// Three reads (the plugins read, the routes view and the organization file) must show one revision;
|
||||
// the write carries it, so a change after them is refused by the writer (revision-conflict) and
|
||||
// reported, never retried. Every skip writes nothing. `root` is the root agent file read once per
|
||||
// call (undefined when it could not be read): the root agents this organization takes count too.
|
||||
async function offOne(rider, name, root) {
|
||||
const id = rider.id;
|
||||
let current;
|
||||
try { current = await rider.call('plugins'); } catch { return { id, outcome: 'bus-down' }; }
|
||||
const on = harnessList(current);
|
||||
if (on === null || typeof current?.revision !== 'string' || !/^[a-f0-9]{64}$/.test(current.revision)) return { id, outcome: 'unreadable' };
|
||||
if (!on.includes(name)) return { id, outcome: 'already-off' };
|
||||
let routes = null;
|
||||
try { routes = await rider.call('routes'); } catch (error) { if (error?.code !== 'launch-not-configured') return { id, outcome: 'unreadable' }; }
|
||||
let file;
|
||||
try { file = typeof rider.file === 'function' ? await rider.file() : null; } catch { file = null; }
|
||||
const named = harnessesNamed(file?.document);
|
||||
const fromRoot = root === undefined ? null : rootHarnessesNamed(root, id);
|
||||
if (named === null || fromRoot === null || typeof file?.revision !== 'string') return { id, outcome: 'unreadable' };
|
||||
for (const harness of fromRoot) named.add(harness);
|
||||
if (file.revision !== current.revision || (routes !== null && routes?.revision !== current.revision)) return { id, outcome: 'failed:revision-conflict' };
|
||||
// The routes view must be readable when there is one; each instance's route and fallback count.
|
||||
if (routes !== null) {
|
||||
if (!routes || typeof routes.instances !== 'object' || routes.instances === null) return { id, outcome: 'unreadable' };
|
||||
for (const route of Object.values(routes.instances)) {
|
||||
if (typeof route?.harness === 'string') named.add(route.harness);
|
||||
if (typeof route?.fallback?.harness === 'string') named.add(route.fallback.harness);
|
||||
}
|
||||
}
|
||||
// The harness rule: any route on this program, whatever account its model uses, keeps it on.
|
||||
if (named.has(name)) return { id, outcome: 'in-use' };
|
||||
if (on.length === 1) return { id, outcome: 'last-program' };
|
||||
try { await rider.call('plugins.set', { revision: current.revision, plugins: { harness: on.filter(n => n !== name) } }); }
|
||||
catch (error) { return { id, outcome: `failed:${failCode(error)}` }; }
|
||||
return { id, outcome: 'off' };
|
||||
}
|
||||
// Item 8, one organization: its proxy client keys by key id, from proxy.keys as the owner. Never
|
||||
// the usage queue. A key value never reaches here: proxy.keys answers ids, and every other field
|
||||
// is checked against a plain pattern before it is passed on.
|
||||
async function accessOne(rider) {
|
||||
const id = rider.id;
|
||||
if (typeof rider.fixture !== 'boolean' || typeof rider.cliproxy !== 'boolean') return { id, health: 'unreadable', reason: 'unreadable', keys: [] };
|
||||
if (rider.fixture || !rider.cliproxy) return { id, health: 'not-riding', keys: [] };
|
||||
let answer;
|
||||
try { answer = await rider.call('proxy.keys'); }
|
||||
catch (error) {
|
||||
const code = error?.code;
|
||||
if (code === 'proxy-uncertain') return { id, health: 'attention', reason: 'proxy-uncertain', keys: [] };
|
||||
const reason = code === 'not-configured' ? 'no-proxy-client' : code === 'outcome-unknown' || code === 'no-bus-host' ? 'bus-down' : 'unreadable';
|
||||
return { id, health: 'unreadable', reason, keys: [] };
|
||||
}
|
||||
if (!Array.isArray(answer?.keys)) return { id, health: 'unreadable', reason: 'unreadable', keys: [] };
|
||||
const keys = [];
|
||||
let other = 0, sentinel = 0;
|
||||
for (const key of answer.keys) {
|
||||
if (!key || typeof key !== 'object' || !KEY_ID.test(key.keyId ?? '')) return { id, health: 'unreadable', reason: 'unreadable', keys: [] };
|
||||
if (key.management === 'sentinel') sentinel++;
|
||||
else if (key.management === 'stack-managed') keys.push({ keyId: key.keyId, agent: plainLabel(key.agent), state: plainLabel(key.state), issuedAt: isoTime(key.issuedAt) });
|
||||
else other++;
|
||||
}
|
||||
const health = keys.length === 0 ? 'missing' : keys.every(k => k.state === 'active') ? 'ok' : 'attention';
|
||||
return { id, health, keys, other, sentinel };
|
||||
}
|
||||
function nextSteps({ release, init, unitState, fence, lock }) {
|
||||
if (!release.fetched || release.problem) return ['fetch'];
|
||||
if (lock.present) return lock.clearable ? ['init-lock-clear'] : ['init-lock-wait'];
|
||||
@@ -122,6 +258,7 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
let organizations = null;
|
||||
try { organizations = await orgs(); } catch { organizations = null; }
|
||||
const ride = await rideSummary();
|
||||
const programs = await programsSummary();
|
||||
const next = nextSteps({ release, init, unitState, fence, lock });
|
||||
// Ride all comes after the proxy runs with no fence, while an organization can still ride.
|
||||
if (next[0] === 'accounts' && ride?.some(r => r.state === 'ready' || r.state === 'fixture')) next.push('ride-all');
|
||||
@@ -143,6 +280,7 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
// needs a config format that does not exist yet.
|
||||
ownProxy: { available: false, state: 'not built', reason: 'This instance has one shared proxy. A separate proxy per organization is not built yet.' },
|
||||
ride,
|
||||
programs,
|
||||
next,
|
||||
};
|
||||
},
|
||||
@@ -254,6 +392,65 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
if (typeof org !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(org)) throw failure('proxy-org-invalid');
|
||||
return receipt({ op: 'ride-removed', org });
|
||||
},
|
||||
// #1873 settings-backend A1: three reads for the instance owner's setup page.
|
||||
// The proxy unit's last journal lines, redacted. A unit that is not installed is not-configured;
|
||||
// a unit state that cannot be read is outcome-unknown, as is a journal that cannot be read.
|
||||
async log({ lines = LOG_LINES.default } = {}) {
|
||||
if (typeof control.log !== 'function') throw failure('not-configured');
|
||||
const state = await unitStatus();
|
||||
if (state.error) throw failure('outcome-unknown');
|
||||
if (!state.installed) throw failure('not-configured');
|
||||
return control.log(lines, { secrets: secretValues(env) });
|
||||
},
|
||||
// When the service key was made, from proxy.json's mtime; never the key or any part of it.
|
||||
serviceKey() { return serviceKeyState(configDir); },
|
||||
// The setup history, a page at a time (receiptHistory).
|
||||
history({ limit, before = null } = {}) { return receiptHistory(dataRoot, { limit, before, secrets: secretValues(env) }); },
|
||||
// An account change made through /api/proxy/* or an organization's /api/orgs/<id>/proxy/*,
|
||||
// recorded after its bus call answered. Labels only: the provider id, a 12-hex account id, a
|
||||
// priority or a model count, the organization whose route carried it, and the signed-in name.
|
||||
// Never a key, a login state, a callback URL, a model list or a body. Answers null when the verb
|
||||
// is not an account change, true when recorded and false when the append failed.
|
||||
recordAccount({ verb, args = {}, result = null, user = null, org = null } = {}) {
|
||||
const label = value => (typeof value === 'string' && /^[a-z0-9][a-z0-9._-]{0,31}$/.test(value) ? value : null);
|
||||
const account = value => (typeof value === 'string' && /^[a-f0-9]{12}$/.test(value) ? value : null);
|
||||
let record;
|
||||
if (verb === 'proxy.apikey.add') record = { op: 'account.proxy.apikey.add', provider: label(args.provider) };
|
||||
else if (verb === 'proxy.login.status' && result?.status === 'ok') record = { op: 'account.proxy.login.complete' };
|
||||
else if (verb === 'proxy.priority.set') record = { op: 'account.proxy.priority.set', accountId: account(args.accountId), priority: Number.isSafeInteger(args.priority) ? args.priority : null };
|
||||
else if (verb === 'proxy.group.models.set') record = { op: 'account.proxy.group.models.set', accountId: account(args.accountId), models: Array.isArray(args.models) ? args.models.length : null };
|
||||
else return null;
|
||||
if (typeof org === 'string' && /^[a-z0-9][a-z0-9._-]{0,63}$/.test(org)) record.org = org;
|
||||
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
|
||||
try { receipt(record); return true; } catch { return false; }
|
||||
},
|
||||
// #1873 settings-backend A2, item 5: turns one harness program off for every current
|
||||
// organization that can do without it, through each organization's bus as the owner (the way
|
||||
// Ride all writes). Sequential, one organization at a time; one receipt names the outcome ids.
|
||||
async programsOff({ name, user = null } = {}) {
|
||||
return exclusive('programs', async () => {
|
||||
let installed;
|
||||
try { installed = harnesses(); } catch { installed = null; }
|
||||
if (typeof name !== 'string' || !Array.isArray(installed) || !installed.includes(name)) throw failure('invalid-request');
|
||||
const list = await riders();
|
||||
let root;
|
||||
try { root = typeof rootAgents === 'function' ? await rootAgents() : undefined; } catch { root = undefined; }
|
||||
const results = [];
|
||||
for (const rider of list) results.push(await offOne(rider, name, root));
|
||||
const record = { op: 'programs.off', name, results: results.map(r => ({ id: r.id, outcome: r.outcome })) };
|
||||
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
|
||||
// Each write is the organization's own change log entry, so a receipt that cannot be
|
||||
// written still answers what happened, with no receipt.
|
||||
let written = null;
|
||||
try { written = receipt(record); } catch { written = null; }
|
||||
return { name, results, receipt: written };
|
||||
});
|
||||
},
|
||||
// #1873 settings-backend A2, item 8: each organization's proxy client keys and their health.
|
||||
async access() {
|
||||
const list = await riders();
|
||||
return { organizations: await Promise.all(list.map(rider => accessOne(rider).catch(() => ({ id: rider.id, health: 'unreadable', reason: 'unreadable', keys: [] })))) };
|
||||
},
|
||||
async initLockClear() {
|
||||
return exclusive('init-lock-clear', async () => ({ receipt: clearInitLock({ configDir, dataRoot, buses: await buses(), now }) }));
|
||||
},
|
||||
|
||||
@@ -9,6 +9,7 @@ import { execFile } from 'node:child_process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { PIN, refuse } from './common.mjs';
|
||||
import { installPaths, loadProxy, verifyInstall } from './settings.mjs';
|
||||
import { holdsSecret, redactLine, redactValue } from './redact.mjs';
|
||||
|
||||
const REPO = fileURLToPath(new URL('../../../', import.meta.url)).replace(/\/$/, '');
|
||||
export const INSTANCE_NAME = /^[a-z][a-z0-9]{1,15}$/;
|
||||
@@ -142,6 +143,135 @@ export function recentReceipts(dataRoot, limit = 10) {
|
||||
} finally { fs.closeSync(fd); }
|
||||
}
|
||||
|
||||
// #1873 settings-backend A1 (item 9): the setup history, one page at a time, newest page first and
|
||||
// oldest first within a page. `before` is a byte offset the previous page answered as `next`: the
|
||||
// start of a line. The file is read the way ownedText reads (no symlink, no FIFO, owned by the
|
||||
// Console's user, no group or other bits) and backward from `before` in 64 KB chunks. A partial
|
||||
// last line (a write in progress) waits for a later read; a line that does not parse, or is longer
|
||||
// than 64 KB, is skipped. Each entry is redacted (redact.mjs) before it is answered, so a receipt
|
||||
// that ever held key text shows none. An entry whose redacted values read together, or whose keys
|
||||
// and values read together, still hold an environment secret split across them is answered as
|
||||
// { withheld: true }; pieces kept apart by another whole field are not seen (known limit). A missing
|
||||
// file is an empty history; any other failure to read refuses outcome-unknown. `uid` is a test seam.
|
||||
export const HISTORY_LIMIT = Object.freeze({ min: 1, max: 100, default: 25 });
|
||||
const HISTORY_CHUNK = 65536, HISTORY_LINE_MAX = 65536, HISTORY_SCAN_MAX = 2000;
|
||||
// A redacted entry's values joined in order, with each object key before its value when `keys`.
|
||||
const entryText = (value, keys) => (value !== null && typeof value === 'object'
|
||||
? Object.entries(value).map(([key, item]) => (keys && !Array.isArray(value) ? key : '') + entryText(item, keys)).join('')
|
||||
: String(value));
|
||||
export function receiptHistory(dataRoot, { limit = HISTORY_LIMIT.default, before = null, uid = process.getuid(), secrets = [] } = {}) {
|
||||
if (!Number.isSafeInteger(limit)) refuse('invalid-request');
|
||||
const n = Math.min(HISTORY_LIMIT.max, Math.max(HISTORY_LIMIT.min, limit));
|
||||
if (before !== null && !(Number.isSafeInteger(before) && before >= 0)) refuse('invalid-request');
|
||||
let fd;
|
||||
try { fd = fs.openSync(receiptFile(dataRoot), fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK); }
|
||||
catch (error) {
|
||||
if (error.code !== 'ENOENT') refuse('outcome-unknown');
|
||||
if (before !== null && before !== 0) refuse('invalid-request');
|
||||
return { entries: [], next: null };
|
||||
}
|
||||
try {
|
||||
let stat;
|
||||
try { stat = fs.fstatSync(fd); } catch { refuse('outcome-unknown'); }
|
||||
if (!stat.isFile() || stat.uid !== uid || (stat.mode & 0o077)) refuse('outcome-unknown');
|
||||
const size = stat.size;
|
||||
if (before !== null && before > size) refuse('invalid-request');
|
||||
// A window over the file: `buf` holds bytes [start, start + buf.length).
|
||||
let start = 0, buf = Buffer.alloc(0);
|
||||
const readAt = (offset, length) => {
|
||||
const out = Buffer.alloc(length);
|
||||
let got;
|
||||
try { got = fs.readSync(fd, out, 0, length, offset); } catch { refuse('outcome-unknown'); }
|
||||
if (got !== length) refuse('outcome-unknown');
|
||||
return out;
|
||||
};
|
||||
// The offset of the last newline in [0, from), or -1.
|
||||
const lastNewline = from => {
|
||||
let pos = from;
|
||||
while (pos > 0) {
|
||||
if (!(pos > start && pos <= start + buf.length)) { start = Math.max(0, pos - HISTORY_CHUNK); buf = readAt(start, pos - start); }
|
||||
const i = buf.lastIndexOf(0x0a, pos - start - 1);
|
||||
if (i >= 0) return start + i;
|
||||
pos = start;
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
const bytes = (from, to) => (from >= start && to <= start + buf.length ? buf.subarray(from - start, to - start) : readAt(from, to - from));
|
||||
// `cursor` is always 0 or the offset just after a newline: the end of the next line to read.
|
||||
let cursor;
|
||||
if (before === null) cursor = lastNewline(size) + 1;
|
||||
else if (before > 0 && bytes(before - 1, before)[0] !== 0x0a) refuse('invalid-request');
|
||||
else cursor = before;
|
||||
const entries = [];
|
||||
for (let scanned = 0; cursor > 0 && entries.length < n && scanned < HISTORY_SCAN_MAX; scanned++) {
|
||||
const lineStart = lastNewline(cursor - 1) + 1, lineEnd = cursor - 1;
|
||||
if (lineEnd - lineStart <= HISTORY_LINE_MAX) {
|
||||
let value = null;
|
||||
try { value = JSON.parse(bytes(lineStart, lineEnd).toString('utf8')); } catch { /* torn line */ }
|
||||
if (value && typeof value === 'object' && !Array.isArray(value) && typeof value.op === 'string') {
|
||||
const safe = redactValue(value, secrets);
|
||||
const split = holdsSecret(entryText(safe, false), secrets) || holdsSecret(entryText(safe, true), secrets);
|
||||
entries.push(split ? { withheld: true } : safe);
|
||||
}
|
||||
}
|
||||
cursor = lineStart;
|
||||
}
|
||||
entries.reverse();
|
||||
return { entries, next: cursor > 0 ? cursor : null };
|
||||
} finally { fs.closeSync(fd); }
|
||||
}
|
||||
|
||||
// #1873 settings-backend A1 (item 7): when the service key was made. proxy.json is created in the
|
||||
// same init step as the management key, with exclusive create, and is never rewritten, so its
|
||||
// mtime is the key's creation time. Only proxy.json is lstat'ed: the key file is never opened or
|
||||
// stat'ed, and nothing is read. A missing proxy.json is `missing`; a symlink, a non-file, one owned
|
||||
// by another user or one that cannot be examined is `unreadable`, with no date. There is no
|
||||
// renewal yet (proxy-scope: no rotation in the MVP), so renewedAt is null and renewal 'not-built'.
|
||||
export function serviceKeyState(configDir, { uid = process.getuid() } = {}) {
|
||||
const out = { madeAt: null, renewedAt: null, renewal: 'not-built', problem: null };
|
||||
let stat;
|
||||
try { stat = fs.lstatSync(path.join(configDir, 'proxy.json')); }
|
||||
catch (error) { out.problem = error.code === 'ENOENT' ? 'missing' : 'unreadable'; return out; }
|
||||
if (!stat.isFile() || stat.uid !== uid || !Number.isFinite(stat.mtimeMs) || stat.mtimeMs <= 0) { out.problem = 'unreadable'; return out; }
|
||||
out.madeAt = new Date(stat.mtimeMs).toISOString();
|
||||
return out;
|
||||
}
|
||||
|
||||
// #1873 settings-backend A1 (item 3): the proxy unit's last journal lines, made safe to show. The
|
||||
// unit name is unitName()'s, never the caller's; `lines` is clamped to 1..200. journalctl runs
|
||||
// through `run` (defaultRun: its timeout and 1 MB cap). A missing journalctl, a nonzero exit or a
|
||||
// timeout refuses outcome-unknown, with no lines and no other source tried. Each line is redacted
|
||||
// (redact.mjs) and cut to 300 characters; a line that cannot be shown is dropped, never shown raw.
|
||||
// A secret from `secrets` found across the shown lines, though in no one line, drops every line.
|
||||
export const LOG_LINES = Object.freeze({ min: 1, max: 200, default: 50 });
|
||||
export async function unitLog({ run = defaultRun, unit, lines = LOG_LINES.default, env, secrets = [] } = {}) {
|
||||
if (typeof unit !== 'string' || !/^mosaic-proxy(-[a-z][a-z0-9]{1,15})?\.service$/.test(unit)) refuse('proxy-unit-usage');
|
||||
if (!Number.isSafeInteger(lines)) refuse('invalid-request');
|
||||
const n = Math.min(LOG_LINES.max, Math.max(LOG_LINES.min, lines));
|
||||
let result;
|
||||
try { result = await run('journalctl', ['--user', '-u', unit, '-n', String(n), '-o', 'cat', '--no-pager', '-q'], { env, timeout: 15000 }); }
|
||||
catch { refuse('outcome-unknown'); }
|
||||
if (!result || result.code !== 0 || typeof result.stdout !== 'string') refuse('outcome-unknown');
|
||||
const raw = result.stdout.split('\n');
|
||||
if (raw.at(-1) === '') raw.pop();
|
||||
const kept = raw.slice(-n);
|
||||
const out = [];
|
||||
let redacted = 0, dropped = 0;
|
||||
try {
|
||||
for (const line of kept) {
|
||||
const safe = redactLine(line, secrets);
|
||||
if (safe.text === null) { if (line.trim()) dropped++; continue; }
|
||||
if (safe.redacted) redacted++;
|
||||
out.push(safe.text);
|
||||
}
|
||||
// The lines that will be shown, read together: a secret split across them drops them all.
|
||||
// Only environment secrets are checked this way. A key pattern broken across two journal
|
||||
// lines, each part too short for the long-run rules, is a known limit shared with chat-fail.
|
||||
if (holdsSecret(out.join(''), secrets)) { dropped += out.length; out.length = 0; }
|
||||
} catch { refuse('outcome-unknown'); }
|
||||
return { unit, lines: out, redacted, dropped };
|
||||
}
|
||||
|
||||
export function unitName(instance) {
|
||||
if (instance !== null && instance !== undefined && instance !== 'dev' && !INSTANCE_NAME.test(instance)) refuse('proxy-unit-usage');
|
||||
return instance && instance !== 'dev' ? `mosaic-proxy-${instance}.service` : 'mosaic-proxy.service';
|
||||
@@ -215,6 +345,8 @@ export function createUnitControl({ instance = null, port = () => null, unitDir
|
||||
return {
|
||||
unit,
|
||||
status,
|
||||
// settings-backend A1: the unit's last journal lines, redacted (unitLog).
|
||||
log(lines, { secrets = [] } = {}) { return unitLog({ run, unit, lines, env, secrets }); },
|
||||
async render() { return (await service('render')).stdout; },
|
||||
async install() { await service('install'); return status(); },
|
||||
async start() {
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
// #1873 settings-backend A1: redaction for the model service's log lines and the setup history,
|
||||
// read by the instance owner on the setup page.
|
||||
//
|
||||
// It is the redaction chat-fail landed for a failed turn's cause (packages/harness/src/failure-notice.mjs,
|
||||
// its REDACT list), with Mosaic's own key prefixes added to the key-prefix line: mso- (an organization's
|
||||
// model key), mst- (the Triage key) and msp-, beside msk- (the owner's). One line is added after it:
|
||||
// those four Mosaic prefixes with no word boundary, so a key glued to a word (xmso-...) is caught
|
||||
// too. Over-redaction there is the safe side. failure-notice.mjs does not
|
||||
// export its list, and packages/harness/ belongs to another lane now, so the list is copied here.
|
||||
// tests/redact.test.mjs fails when a copied line no longer matches failure-notice.mjs.
|
||||
//
|
||||
// A text is decoded (%XX, HTML references, NFKC) and cleaned (ANSI, controls, whitespace) with
|
||||
// failure-notice's own exported helpers before it is redacted. A text that holds one of the
|
||||
// Console's environment secrets (secretValues), even split by spaces or controls, is withheld
|
||||
// whole. A text that redacts to nothing but [redacted] says nothing and is dropped.
|
||||
import { cleanText, decodeText } from '../../harness/src/failure-notice.mjs';
|
||||
|
||||
export { secretValues } from '../../harness/src/failure-notice.mjs';
|
||||
|
||||
export const LINE_MAX = 300;
|
||||
export const REDACTED = '[redacted]';
|
||||
|
||||
// Copied from failure-notice.mjs. The key-prefix line adds mso, mst and msp, and the line after
|
||||
// it is Mosaic's own; every other line is failure-notice's.
|
||||
const KEY_WORD = "(?:\\w*[_-])?(?:api[_-]?key|key|token|secret|password|passwd|authorization|cookie|session)";
|
||||
const REDACT = [
|
||||
/(?<![a-z0-9+.-])[a-z0-9+.-]+:\/\/\S*/gi, // any link, its userinfo and query included, from the run's start
|
||||
/\bwww\.\S+/gi,
|
||||
/(?<!\S)(?=\S*\S@\S)\S+/g, // email addresses and user@host: the whole run holding the @
|
||||
/(?:^|(?<=[\s("'=:]))(?:~|\.{1,2})?\/\S+/g, // absolute and home paths
|
||||
/\b[A-Za-z]:\\\S*/g, // Windows paths
|
||||
/\\\\\S+/g, // UNC paths
|
||||
/\b(?:bearer|basic)\s+\S+/gi,
|
||||
new RegExp(`\\b${KEY_WORD}["']?\\s*[:=]\\s*(?:"[^"]*"?|'[^']*'?|[^\\s,;}]+)`, "gi"),
|
||||
new RegExp(`\\b${KEY_WORD}["']?\\s+(?:"[^"]*"?|'[^']*'?|(?=[^\\s"',;}]*\\d)[^\\s"',;}]+|[^\\s"',;}]{8,})`, "gi"),
|
||||
/\b(?:sk|msk|mso|mst|msp|rk|pk|ghp|gho|ghs|glpat|xox[a-z])[-_]\S+/gi,
|
||||
/ms[kotp][-_]\S+/gi, // Mosaic's prefixes glued to a word: added here, not in failure-notice.mjs
|
||||
/\beyJ\S+/g, // JWT
|
||||
/[A-Za-z0-9_\-+/=.]{32,}/g,
|
||||
/(?<![A-Za-z0-9_\-+/=.])(?=[A-Za-z0-9_\-+/=.]*\d)(?=[A-Za-z0-9_\-+/=.]*[A-Za-z])[A-Za-z0-9_\-+/=.]{16,}/g,
|
||||
];
|
||||
|
||||
// The text with every space, mark and separator gone, after cleanText, for the secret check.
|
||||
// cleanText has already removed ANSI, format, surrogate and private-use characters and turned
|
||||
// controls into spaces, so this removes the same set failure-notice's compactText does.
|
||||
const compact = clean => clean.replace(/[\p{M}\p{Z}\s]/gu, '');
|
||||
|
||||
function cap(text, n = LINE_MAX) {
|
||||
const chars = Array.from(text);
|
||||
return chars.length <= n ? text : chars.slice(0, n - 3).join('') + '...';
|
||||
}
|
||||
|
||||
// One text made safe to show. Answers { text, redacted } where text is null when nothing can be
|
||||
// shown: an empty text, one holding a secret (withheld: true), or one that redacts to only
|
||||
// [redacted]. Redaction runs over the whole text before the cap, so the cut never parts a key word
|
||||
// from its value. `secrets` is secretValues(env).
|
||||
export function redactLine(line, secrets = []) {
|
||||
const clean = cleanText(decodeText(String(line)));
|
||||
if (!clean) return { text: null, redacted: false };
|
||||
if (secrets.length) {
|
||||
const flat = compact(clean);
|
||||
if (secrets.some(value => flat.includes(value))) return { text: null, redacted: true, withheld: true };
|
||||
}
|
||||
let shown = clean;
|
||||
for (const re of REDACT) shown = shown.replace(re, REDACTED);
|
||||
shown = shown.replace(/(?:\[redacted\]\s*){2,}/g, `${REDACTED} `).trim();
|
||||
if (!shown || shown === REDACTED) return { text: null, redacted: true };
|
||||
return { text: cap(shown), redacted: shown !== clean };
|
||||
}
|
||||
|
||||
// True when the whole text, joined with no spaces, holds a secret: one split across lines is seen.
|
||||
export function holdsSecret(text, secrets = []) {
|
||||
if (!secrets.length) return false;
|
||||
const flat = compact(cleanText(decodeText(String(text))));
|
||||
return secrets.some(value => flat.includes(value));
|
||||
}
|
||||
|
||||
// A stored value made safe to show: strings are redacted, and a string that cannot be shown
|
||||
// becomes [redacted]. Keys that redaction would change are dropped with their value. Depth and
|
||||
// size are bounded; anything past the bound becomes null.
|
||||
export function redactValue(value, secrets = [], depth = 0) {
|
||||
if (value === null || typeof value === 'number' || typeof value === 'boolean') return value;
|
||||
if (typeof value === 'string') return value === '' ? '' : redactLine(value, secrets).text ?? REDACTED;
|
||||
if (depth >= 4 || typeof value !== 'object') return null;
|
||||
if (Array.isArray(value)) return value.slice(0, 64).map(item => redactValue(item, secrets, depth + 1));
|
||||
const out = {};
|
||||
for (const [key, item] of Object.entries(value).slice(0, 64)) {
|
||||
if (!/^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(key) || redactLine(key, secrets).text !== key) continue;
|
||||
out[key] = redactValue(item, secrets, depth + 1);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -7,7 +7,10 @@ import fs from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { createProxyAdmin } from '../src/admin-control.mjs';
|
||||
import { createProxyAdmin, installedHarnesses, harnessesNamed, rootHarnessesNamed } from '../src/admin-control.mjs';
|
||||
import { pluginHash } from '../../plugins/src/pinned.mjs';
|
||||
import { installPlugin } from '../../plugins/src/install.mjs';
|
||||
import { KINDS } from '../../plugins/src/kinds.mjs';
|
||||
import { initFiles, receiptFile } from '../src/admin.mjs';
|
||||
import { PIN } from '../src/common.mjs';
|
||||
|
||||
@@ -15,7 +18,7 @@ const code = name => error => error.code === name;
|
||||
const SENTINEL = 'pa-sentinel-management-key-0123456789';
|
||||
const stopped = async () => ({ stopped: true, running: [] });
|
||||
|
||||
function fixture(t, { unitState = { installed: false, active: 'inactive', healthy: false }, restart = null, fetch = null, pin = PIN, statusHook = null } = {}) {
|
||||
function fixture(t, { unitState = { installed: false, active: 'inactive', healthy: false }, restart = null, fetch = null, pin = PIN, statusHook = null, extra = {} } = {}) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pc-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
@@ -30,7 +33,7 @@ function fixture(t, { unitState = { installed: false, active: 'inactive', health
|
||||
stop: async () => { ops.push('stop'); return { ...unitState, active: 'inactive', pid: null }; },
|
||||
restart: async () => { ops.push('restart'); return restart ?? { state: unitState, evidence: { unit: 'mosaic-proxy-zz.service', confirmed: true, before: { pid: 10, since: 1, active: 'active' }, after: { pid: 11, since: 2, active: 'active', listening: true }, observedAt: new Date(0).toISOString() } }; },
|
||||
};
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: stopped, fetch: fetch ?? (async () => ({})), now: () => new Date(0), orgs: async () => [{ id: 'acme', catalogue: 'ready' }], pin });
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: stopped, fetch: fetch ?? (async () => ({})), now: () => new Date(0), orgs: async () => [{ id: 'acme', catalogue: 'ready' }], pin, ...extra });
|
||||
return { root, configDir, dataRoot, admin, ops, files: initFiles(configDir, dataRoot) };
|
||||
}
|
||||
const receipts = dataRoot => { try { return fs.readFileSync(receiptFile(dataRoot), 'utf8').trim().split('\n').filter(Boolean).map(JSON.parse); } catch { return []; } };
|
||||
@@ -227,3 +230,340 @@ test('status carries the last receipts from proxy-admin.jsonl, so the page shows
|
||||
assert.equal(receipts.length, 10);
|
||||
assert.ok(receipts.every(r => r.op === 'unit.stop'));
|
||||
});
|
||||
|
||||
// #1873 settings-backend A1: the controller's three reads and the account-change record.
|
||||
function a1Fixture(t, { unitState = { installed: true, active: 'active', healthy: true }, log = undefined, statusError = false, env = {} } = {}) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pc-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
fs.mkdirSync(configDir, { mode: 0o700 }); fs.mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const calls = [];
|
||||
const unit = {
|
||||
unit: 'mosaic-proxy-zz.service',
|
||||
status: async () => { if (statusError) throw Object.assign(new Error('systemctl gone'), { code: 'proxy-unit-failed' }); return { unit: 'mosaic-proxy-zz.service', ...unitState }; },
|
||||
...(log === undefined ? { log: async (lines, options) => { calls.push({ lines, options }); return { unit: 'mosaic-proxy-zz.service', lines: ['ok'], redacted: 0, dropped: 0 }; } } : log === null ? {} : { log }),
|
||||
};
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: stopped, now: () => new Date(0), env });
|
||||
return { admin, calls, configDir, dataRoot };
|
||||
}
|
||||
|
||||
test('log reads the unit journal with the env secrets; a missing unit or log reader is not-configured, an unreadable state outcome-unknown', async t => {
|
||||
const { admin, calls } = a1Fixture(t, { env: { MOSAIC_PROXY_KEY: 'plainwordsonly', PATH: '/usr/bin' } });
|
||||
assert.deepEqual(await admin.log(), { unit: 'mosaic-proxy-zz.service', lines: ['ok'], redacted: 0, dropped: 0 });
|
||||
assert.deepEqual(calls, [{ lines: 50, options: { secrets: ['plainwordsonly'] } }]);
|
||||
await admin.log({ lines: 7 });
|
||||
assert.equal(calls[1].lines, 7);
|
||||
await assert.rejects(a1Fixture(t, { unitState: { installed: false, active: 'inactive', healthy: false } }).admin.log(), code('not-configured'));
|
||||
await assert.rejects(a1Fixture(t, { log: null }).admin.log(), code('not-configured'));
|
||||
await assert.rejects(a1Fixture(t, { statusError: true }).admin.log(), code('outcome-unknown'));
|
||||
const failing = a1Fixture(t, { log: async () => { throw Object.assign(new Error('journal'), { code: 'outcome-unknown' }); } });
|
||||
await assert.rejects(failing.admin.log(), code('outcome-unknown'));
|
||||
});
|
||||
|
||||
test('serviceKey and history read the configured directories', async t => {
|
||||
const { admin, configDir, dataRoot } = a1Fixture(t);
|
||||
assert.equal(admin.serviceKey().problem, 'missing');
|
||||
fs.writeFileSync(join(configDir, 'proxy.json'), '{}', { mode: 0o600 });
|
||||
assert.equal(admin.serviceKey().problem, null);
|
||||
assert.match(admin.serviceKey().madeAt, /^\d{4}-\d\d-\d\dT/);
|
||||
assert.deepEqual(admin.history(), { entries: [], next: null });
|
||||
admin.recordAccount({ verb: 'proxy.login.status', result: { status: 'ok' } });
|
||||
assert.deepEqual(admin.history({ limit: 5 }), { entries: [{ time: new Date(0).toISOString(), op: 'account.proxy.login.complete' }], next: null });
|
||||
assert.equal(receipts(dataRoot).length, 1);
|
||||
});
|
||||
|
||||
test('history withholds an env secret a receipt holds', async t => {
|
||||
const { admin, dataRoot } = a1Fixture(t, { env: { PROXY_TOKEN: 'plainwordsonly' } });
|
||||
fs.mkdirSync(join(dataRoot, 'state'), { recursive: true, mode: 0o700 });
|
||||
fs.writeFileSync(receiptFile(dataRoot), JSON.stringify({ op: 'x', note: 'plain words only' }) + '\n', { mode: 0o600 });
|
||||
assert.deepEqual(admin.history().entries, [{ op: 'x', note: '[redacted]' }]);
|
||||
});
|
||||
|
||||
test('recordAccount records account changes by label only, never a key, login state, callback or model name', async t => {
|
||||
const { admin, dataRoot } = a1Fixture(t);
|
||||
const KEY = 'sk-proj-Ab3dE5fG7hJ9kL1mN3pQ5rS7tU9vW1xY';
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.apikey.add', args: { provider: 'openai', key: KEY }, result: { added: true }, user: 'jason', org: 'acme' }), true);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.apikey.add', args: { provider: KEY, key: KEY } }), true);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.login.status', args: { state: 'state-secret-123' }, result: { status: 'ok' } }), true);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.login.status', args: { state: 'state-secret-123' }, result: { status: 'wait' } }), null);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.priority.set', args: { accountId: '0123456789ab', priority: 2 } }), true);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.group.models.set', args: { accountId: 'not-hex', models: ['secret-model-name', 'b'] }, org: '../x' }), true);
|
||||
for (const verb of ['proxy.login.start', 'proxy.login.submit', 'proxy.login.cancel', 'plugins.set', 'proxy.accounts', undefined]) {
|
||||
assert.equal(admin.recordAccount({ verb, args: { redirect_url: 'https://cb.example/?code=abc', provider: 'x' } }), null, String(verb));
|
||||
}
|
||||
const all = receipts(dataRoot);
|
||||
const at = new Date(0).toISOString();
|
||||
assert.deepEqual(all, [
|
||||
{ time: at, op: 'account.proxy.apikey.add', provider: 'openai', org: 'acme', user: 'jason' },
|
||||
{ time: at, op: 'account.proxy.apikey.add', provider: null },
|
||||
{ time: at, op: 'account.proxy.login.complete' },
|
||||
{ time: at, op: 'account.proxy.priority.set', accountId: '0123456789ab', priority: 2 },
|
||||
{ time: at, op: 'account.proxy.group.models.set', accountId: null, models: 2 },
|
||||
]);
|
||||
const text = fs.readFileSync(receiptFile(dataRoot), 'utf8');
|
||||
for (const bit of ['Ab3dE5', 'state-secret', 'secret-model', 'cb.example', 'code=abc']) assert.equal(text.includes(bit), false, bit);
|
||||
});
|
||||
|
||||
test('recordAccount answers false, and throws nothing, when the history cannot be written', async t => {
|
||||
const { admin, dataRoot } = a1Fixture(t);
|
||||
fs.mkdirSync(join(dataRoot, 'state'), { mode: 0o700 });
|
||||
fs.mkdirSync(receiptFile(dataRoot));
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.apikey.add', args: { provider: 'openai', key: 'k' } }), false);
|
||||
});
|
||||
|
||||
// #1873 settings-backend A2 (items 5 and 8): fake organizations behind the riders. The harness names
|
||||
// are made up; every answer carries a planted key, which no result, receipt or history entry may hold.
|
||||
const REV = 'a'.repeat(64), REV2 = 'b'.repeat(64);
|
||||
const PLANT = `mso-zz-acme-key-${'Q'.repeat(43)}`;
|
||||
const KEY_TEXT = /ms[kotp][-_][A-Za-z0-9_-]{6,}/;
|
||||
const busError = name => Object.assign(new Error(`refused ${PLANT}`), { code: name, reason: PLANT });
|
||||
function fakeOrg(id, { on = ['alpha', 'beta'], revision = REV, fileRevision = null, instances = {}, routesError = null, routesRevision = null, document = null, fileError = false, down = false, setError = null, keys = [], keysError = null, fixture = false, cliproxy = true, plugins = undefined } = {}) {
|
||||
const calls = [];
|
||||
const rider = {
|
||||
id, fixture, cliproxy, changes: '',
|
||||
file: () => { if (fileError) throw new Error(PLANT); return { document: document ?? { roles: {}, plugins: { harness: on }, secretNote: PLANT }, revision: fileRevision ?? revision }; },
|
||||
call: async (verb, args) => {
|
||||
calls.push(args === undefined ? [verb] : [verb, args]);
|
||||
if (down) throw busError('outcome-unknown');
|
||||
if (verb === 'plugins') return { business: id, revision, plugins: plugins !== undefined ? plugins : { harness: on, 'model-provider': ['cliproxy'] }, available: { harness: on }, note: PLANT };
|
||||
if (verb === 'routes') { if (routesError) throw busError(routesError); return { business: id, revision: routesRevision ?? revision, harnesses: on, models: ['m1'], providerModels: {}, levels: {}, instances, note: PLANT }; }
|
||||
if (verb === 'plugins.set') { if (setError) throw busError(setError); return { business: id, revision: REV2, plugins: args.plugins }; }
|
||||
if (verb === 'proxy.keys') { if (keysError) throw busError(keysError); return { keys }; }
|
||||
throw busError('invalid-request');
|
||||
},
|
||||
};
|
||||
return { rider, calls };
|
||||
}
|
||||
const NO_ROOT = { state: 'absent', agents: {}, assistant: null };
|
||||
const programsFixture = (t, orgs, extra = {}) => fixture(t, { extra: { riders: async () => orgs.map(o => o.rider), harnesses: () => ['alpha', 'beta', 'gamma'], rootAgents: () => NO_ROOT, ...extra } });
|
||||
const rootAgent = (vars, { definition = 'coder', excludeOrgs = [] } = {}) => ({ definition, title: 'Coder', vars, excludeOrgs });
|
||||
const route = harness => ({ harness, model: 'm1', thinking: null, fallback: null });
|
||||
|
||||
test('A2 item 5: the installed list comes from the harness plugin tree, and status lists it with no organization', async t => {
|
||||
const tree = new URL('../../plugins/harness/', import.meta.url);
|
||||
const fromTree = fs.readdirSync(tree).filter(name => { try { return fs.lstatSync(new URL(`${name}/index.mjs`, tree)).isFile(); } catch { return false; } }).sort();
|
||||
assert.ok(fromTree.length >= 1);
|
||||
assert.deepEqual(installedHarnesses(), fromTree);
|
||||
const { admin } = fixture(t);
|
||||
assert.deepEqual((await admin.status()).programs, { installed: fromTree, organizations: [] });
|
||||
});
|
||||
|
||||
test('A2 item 5: status names the programs each organization has on; one bus down is null and leaves the others', async t => {
|
||||
const acme = fakeOrg('acme', { on: ['alpha'] }), bolt = fakeOrg('bolt', { down: true }), crux = fakeOrg('crux', { plugins: { harness: 'alpha' } });
|
||||
const { admin } = programsFixture(t, [acme, bolt, crux]);
|
||||
const status = await admin.status();
|
||||
assert.deepEqual(status.programs, { installed: ['alpha', 'beta', 'gamma'], organizations: [{ id: 'acme', on: ['alpha'] }, { id: 'bolt', on: null }, { id: 'crux', on: null }] });
|
||||
assert.doesNotMatch(JSON.stringify(status), KEY_TEXT);
|
||||
});
|
||||
|
||||
test('A2 item 5: off skips a program any route still uses, whatever its model, and writes nothing there', async t => {
|
||||
// A role route, a fallback route, a project agent only the file shows, and a route whose model has
|
||||
// no known account kind: each keeps alpha on (the harness rule).
|
||||
const role = fakeOrg('role', { instances: { chief: route('alpha') } });
|
||||
const fallback = fakeOrg('fallback', { instances: { chief: { ...route('beta'), fallback: { harness: 'alpha', model: 'm1', thinking: null } } } });
|
||||
const project = fakeOrg('project', { routesError: 'launch-not-configured', document: { roles: {}, plugins: { harness: ['alpha', 'beta'] }, projects: { site: { agents: { dev: { definition: 'coder', vars: { harness: 'alpha' } } } } } } });
|
||||
const unknownKind = fakeOrg('unknown-kind', { instances: { chief: { harness: 'alpha', model: 'mystery-account-model', thinking: null, fallback: null } } });
|
||||
const { admin } = programsFixture(t, [role, fallback, project, unknownKind]);
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results.map(r => [r.id, r.outcome]), [['role', 'in-use'], ['fallback', 'in-use'], ['project', 'in-use'], ['unknown-kind', 'in-use']]);
|
||||
for (const o of [role, fallback, project, unknownKind]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('A2 item 5: off turns the program off where nothing uses it, skips the last program, already off and unreadable, and reports bus down', async t => {
|
||||
const free = fakeOrg('free', { instances: { chief: route('beta') } });
|
||||
const last = fakeOrg('last', { on: ['alpha'] });
|
||||
const already = fakeOrg('already', { on: ['beta'] });
|
||||
const down = fakeOrg('down', { down: true });
|
||||
const unreadable = fakeOrg('unreadable', { fileError: true });
|
||||
const noFile = fakeOrg('no-file'); delete noFile.rider.file;
|
||||
const badRoutes = fakeOrg('bad-routes', { routesError: 'organization-invalid' });
|
||||
const { admin } = programsFixture(t, [free, last, already, down, unreadable, noFile, badRoutes]);
|
||||
const out = await admin.programsOff({ name: 'alpha', user: 'owner' });
|
||||
assert.equal(out.name, 'alpha');
|
||||
assert.deepEqual(out.results, [
|
||||
{ id: 'free', outcome: 'off' }, { id: 'last', outcome: 'last-program' }, { id: 'already', outcome: 'already-off' },
|
||||
{ id: 'down', outcome: 'bus-down' }, { id: 'unreadable', outcome: 'unreadable' }, { id: 'no-file', outcome: 'unreadable' }, { id: 'bad-routes', outcome: 'unreadable' },
|
||||
]);
|
||||
assert.deepEqual(free.calls.find(([verb]) => verb === 'plugins.set'), ['plugins.set', { revision: REV, plugins: { harness: ['beta'] } }]);
|
||||
for (const o of [last, already, down, unreadable, noFile, badRoutes]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('A2 item 5: a plugins.set refusal is reported per organization, and a revision change is reported once, never retried', async t => {
|
||||
const refused = fakeOrg('refused', { setError: 'unknown-plugin' });
|
||||
const raced = fakeOrg('raced', { setError: 'revision-conflict' });
|
||||
const moved = fakeOrg('moved', { fileRevision: REV2 });
|
||||
const ok = fakeOrg('ok');
|
||||
const { admin } = programsFixture(t, [refused, raced, moved, ok]);
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results.map(r => r.outcome), ['failed:unknown-plugin', 'failed:revision-conflict', 'failed:revision-conflict', 'off']);
|
||||
assert.equal(raced.calls.filter(([verb]) => verb === 'plugins.set').length, 1);
|
||||
assert.equal(moved.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('A2 item 5: off writes one receipt by name and outcome ids, and no answer, receipt or history entry holds a key', async t => {
|
||||
const a = fakeOrg('acme'), b = fakeOrg('bolt', { setError: 'invalid-plugins' });
|
||||
const { admin, dataRoot } = programsFixture(t, [a, b]);
|
||||
const out = await admin.programsOff({ name: 'beta', user: 'owner' });
|
||||
const record = { time: new Date(0).toISOString(), op: 'programs.off', name: 'beta', results: [{ id: 'acme', outcome: 'off' }, { id: 'bolt', outcome: 'failed:invalid-plugins' }], user: 'owner' };
|
||||
assert.deepEqual(out.receipt, record);
|
||||
assert.deepEqual(receipts(dataRoot), [record]);
|
||||
const history = admin.history({});
|
||||
assert.deepEqual(history.entries, [record]);
|
||||
for (const value of [out, receipts(dataRoot), history]) assert.doesNotMatch(JSON.stringify(value), KEY_TEXT);
|
||||
});
|
||||
|
||||
test('A2 item 5: an unknown name is invalid-request and calls no bus; two off calls at once give proxy-admin-busy for one', async t => {
|
||||
let release;
|
||||
const gate = new Promise(r => { release = r; });
|
||||
const slow = fakeOrg('slow');
|
||||
const original = slow.rider.call;
|
||||
// Blocks only from the busy part on, so a name check that lets a bad name through goes red, not hangs.
|
||||
let blocking = false;
|
||||
slow.rider.call = async (verb, args) => { if (verb === 'plugins' && blocking) await gate; return original(verb, args); };
|
||||
const { admin, dataRoot } = programsFixture(t, [slow]);
|
||||
await assert.rejects(admin.programsOff({ name: 'delta' }), code('invalid-request'));
|
||||
await assert.rejects(admin.programsOff({ name: 42 }), code('invalid-request'));
|
||||
assert.deepEqual(slow.calls, []);
|
||||
assert.deepEqual(receipts(dataRoot), []);
|
||||
blocking = true;
|
||||
const first = admin.programsOff({ name: 'alpha' });
|
||||
await new Promise(r => setImmediate(r));
|
||||
// Bounded: without the lock the second call would wait on the same gate, so it answers 'waited'.
|
||||
const second = admin.programsOff({ name: 'beta' }).then(() => 'answered', e => e?.code);
|
||||
assert.equal(await Promise.race([second, new Promise(r => setTimeout(() => r('waited'), 1000))]), 'proxy-admin-busy');
|
||||
release();
|
||||
await second;
|
||||
assert.deepEqual((await first).results, [{ id: 'slow', outcome: 'off' }]);
|
||||
assert.equal(receipts(dataRoot).length, 1);
|
||||
});
|
||||
|
||||
test('A2 item 5: an installed list that cannot be read refuses off as invalid-request', async t => {
|
||||
const a = fakeOrg('acme');
|
||||
const { admin } = programsFixture(t, [a], { harnesses: () => { throw new Error('unreadable tree'); } });
|
||||
await assert.rejects(admin.programsOff({ name: 'alpha' }), code('invalid-request'));
|
||||
assert.deepEqual(a.calls, []);
|
||||
assert.equal((await admin.status()).programs.installed, null);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P1-1): a root agent the organization takes keeps its program on; an excluded organization and the root assistant do not count', async t => {
|
||||
// Only the root agent file names alpha: nothing in either organization's file or routes does.
|
||||
const root = { state: 'ok', assistant: 'helper', agents: {
|
||||
writer: rootAgent({ harness: 'gamma', 'fallback.harness': 'alpha' }, { excludeOrgs: ['spare'] }),
|
||||
helper: rootAgent({ harness: 'beta' }, { definition: 'assistant' }),
|
||||
} };
|
||||
const takes = fakeOrg('takes', { on: ['alpha', 'beta', 'gamma'] });
|
||||
const spare = fakeOrg('spare', { on: ['alpha', 'beta', 'gamma'] });
|
||||
const { admin } = programsFixture(t, [takes, spare], { rootAgents: () => root });
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results, [{ id: 'takes', outcome: 'in-use' }, { id: 'spare', outcome: 'off' }]);
|
||||
assert.equal(takes.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
// The root assistant is not copied into an organization, so its harness does not keep beta on.
|
||||
const again = await admin.programsOff({ name: 'beta' });
|
||||
assert.deepEqual(again.results.map(r => r.outcome), ['off', 'off']);
|
||||
// The root file is read once per call, not once per organization.
|
||||
let reads = 0;
|
||||
const counted = programsFixture(t, [fakeOrg('a'), fakeOrg('b')], { rootAgents: () => { reads++; return NO_ROOT; } });
|
||||
await counted.admin.programsOff({ name: 'alpha' });
|
||||
assert.equal(reads, 1);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P1-1): a root agent file that cannot be read, or no reader, writes nothing where the program is on', async t => {
|
||||
const on = fakeOrg('on'), already = fakeOrg('already', { on: ['beta'] });
|
||||
const { admin } = programsFixture(t, [on, already], { rootAgents: () => { throw Object.assign(new Error(`root agent file refused ${PLANT}`), { name: 'BusinessError' }); } });
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results, [{ id: 'on', outcome: 'unreadable' }, { id: 'already', outcome: 'already-off' }]);
|
||||
assert.doesNotMatch(JSON.stringify(out), KEY_TEXT);
|
||||
const bare = fakeOrg('bare');
|
||||
const none = fixture(t, { extra: { riders: async () => [bare.rider], harnesses: () => ['alpha', 'beta'] } });
|
||||
assert.deepEqual((await none.admin.programsOff({ name: 'alpha' })).results, [{ id: 'bare', outcome: 'unreadable' }]);
|
||||
for (const o of [on, already, bare]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
// A root answer of the wrong shape is unreadable too.
|
||||
assert.equal(rootHarnessesNamed({ agents: { w: { vars: { harness: 'alpha' } } } }, 'acme'), null);
|
||||
assert.equal(rootHarnessesNamed(null, 'acme'), null);
|
||||
assert.deepEqual([...rootHarnessesNamed(NO_ROOT, 'acme')], []);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P3-1): a revision that is not 64 hex, routes from another revision, and routes with no instances write nothing; later organizations still go', async t => {
|
||||
const badRev = fakeOrg('bad-rev', { revision: 'not-a-revision' });
|
||||
const routesMoved = fakeOrg('routes-moved', { routesRevision: REV2 });
|
||||
const noInstances = fakeOrg('no-instances', { instances: null });
|
||||
const after = fakeOrg('after');
|
||||
const { admin, dataRoot } = programsFixture(t, [badRev, routesMoved, noInstances, after]);
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results, [{ id: 'bad-rev', outcome: 'unreadable' }, { id: 'routes-moved', outcome: 'failed:revision-conflict' }, { id: 'no-instances', outcome: 'unreadable' }, { id: 'after', outcome: 'off' }]);
|
||||
for (const o of [badRev, routesMoved, noInstances]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
assert.equal(receipts(dataRoot).length, 1);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P3-1): a harness program the instance plugin lock pins is listed as installed', t => {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pl-'));
|
||||
// The install leaves read-only directories; open them up before removing the tree.
|
||||
const thaw = p => { const st = fs.lstatSync(p); if (st.isSymbolicLink()) return; if (st.isDirectory()) { fs.chmodSync(p, 0o700); for (const n of fs.readdirSync(p)) thaw(join(p, n)); } };
|
||||
t.after(() => { thaw(root); fs.rmSync(root, { recursive: true, force: true }); });
|
||||
const src = join(root, 'src'), dataRoot = join(root, 'data');
|
||||
fs.mkdirSync(join(src, 'harness'), { recursive: true }); fs.mkdirSync(dataRoot);
|
||||
fs.writeFileSync(join(src, 'harness', 'index.mjs'), KINDS.harness.exports.map(name => `export function ${name}(){return ${name === 'levels' ? "['high']" : name === 'countsAccount' ? 'true' : 'null'};}`).join('\n') + '\nexport const interfaceVersion=1;');
|
||||
const manifest = { name: 'zetaplug', version: '1.0.0', kinds: ['harness'], interfaceVersions: { harness: 1 }, sha256: '0'.repeat(64), source: { path: 'fixture', version: '1.0.0' }, signature: null, tools: [], actions: [], services: [], roleTemplates: [] };
|
||||
fs.writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
manifest.sha256 = pluginHash(src);
|
||||
fs.writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
assert.equal(installedHarnesses(dataRoot).includes('zetaplug'), false);
|
||||
installPlugin({ dataRoot, source: src, expectedHash: manifest.sha256 });
|
||||
const listed = installedHarnesses(dataRoot);
|
||||
assert.ok(listed.includes('zetaplug'));
|
||||
assert.deepEqual(listed.filter(n => n !== 'zetaplug'), installedHarnesses());
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P3-3): an organization whose harness list holds anything but plugin names reads on: null', async t => {
|
||||
const odd = fakeOrg('odd', { plugins: { harness: ['alpha', `bad ${PLANT}`] } });
|
||||
const fine = fakeOrg('fine', { on: ['alpha'] });
|
||||
const { admin } = programsFixture(t, [odd, fine]);
|
||||
const status = await admin.status();
|
||||
assert.deepEqual(status.programs.organizations, [{ id: 'odd', on: null }, { id: 'fine', on: ['alpha'] }]);
|
||||
assert.doesNotMatch(JSON.stringify(status), KEY_TEXT);
|
||||
});
|
||||
|
||||
test('A2: harnessesNamed reads every harness and fallback.harness string outside the plugins list', () => {
|
||||
assert.deepEqual([...harnessesNamed({ plugins: { harness: ['alpha'] }, roles: { a: { vars: { harness: 'beta', 'fallback.harness': 'gamma' } } }, projects: { p: { agents: { x: { vars: { harness: 'delta' } } } } } })].sort(), ['beta', 'delta', 'gamma']);
|
||||
assert.equal(harnessesNamed(null), null);
|
||||
assert.equal(harnessesNamed([]), null);
|
||||
});
|
||||
|
||||
const key = (keyId, management, state, agent = 'dev') => ({ keyId, management, agent, issuedAt: '2026-11-01T10:00:00.000Z', state });
|
||||
test('A2 item 8: access gives each organization a health and key ids, never a key, and never reads the usage queue', async t => {
|
||||
const ok = fakeOrg('ok', { keys: [key('0123456789ab', 'stack-managed', 'active'), key('ba9876543210', 'not stack-managed', null, null), key('aaaaaaaaaaaa', 'sentinel', null, null)] });
|
||||
const attention = fakeOrg('attention', { keys: [key('0123456789ab', 'stack-managed', 'active'), key('bbbbbbbbbbbb', 'stack-managed', 'revoked')] });
|
||||
const missing = fakeOrg('missing', { keys: [key('cccccccccccc', 'not stack-managed', null, null)] });
|
||||
const fixtureOrg = fakeOrg('fixture', { fixture: true });
|
||||
const ownModels = fakeOrg('own', { cliproxy: false });
|
||||
const down = fakeOrg('down', { down: true });
|
||||
const noClient = fakeOrg('no-client', { keysError: 'not-configured' });
|
||||
const uncertain = fakeOrg('uncertain', { keysError: 'proxy-uncertain' });
|
||||
const proxyDown = fakeOrg('proxy-down', { keysError: 'proxy-down' });
|
||||
const badShape = fakeOrg('bad-shape', { keys: 'none' });
|
||||
const planted = fakeOrg('planted', { keys: [key('dddddddddddd', 'stack-managed', PLANT, PLANT), { ...key('eeeeeeeeeeee', 'stack-managed', 'active'), value: PLANT, issuedAt: PLANT }] });
|
||||
const plantedId = fakeOrg('planted-id', { keys: [key(PLANT, 'stack-managed', 'active')] });
|
||||
const unread = fakeOrg('unread'); unread.rider.fixture = undefined;
|
||||
const all = [ok, attention, missing, fixtureOrg, ownModels, down, noClient, uncertain, proxyDown, badShape, planted, plantedId, unread];
|
||||
const { admin } = programsFixture(t, all);
|
||||
const out = await admin.access();
|
||||
assert.deepEqual(out.organizations, [
|
||||
{ id: 'ok', health: 'ok', keys: [{ keyId: '0123456789ab', agent: 'dev', state: 'active', issuedAt: '2026-11-01T10:00:00.000Z' }], other: 1, sentinel: 1 },
|
||||
{ id: 'attention', health: 'attention', keys: [{ keyId: '0123456789ab', agent: 'dev', state: 'active', issuedAt: '2026-11-01T10:00:00.000Z' }, { keyId: 'bbbbbbbbbbbb', agent: 'dev', state: 'revoked', issuedAt: '2026-11-01T10:00:00.000Z' }], other: 0, sentinel: 0 },
|
||||
{ id: 'missing', health: 'missing', keys: [], other: 1, sentinel: 0 },
|
||||
{ id: 'fixture', health: 'not-riding', keys: [] },
|
||||
{ id: 'own', health: 'not-riding', keys: [] },
|
||||
{ id: 'down', health: 'unreadable', reason: 'bus-down', keys: [] },
|
||||
{ id: 'no-client', health: 'unreadable', reason: 'no-proxy-client', keys: [] },
|
||||
{ id: 'uncertain', health: 'attention', reason: 'proxy-uncertain', keys: [] },
|
||||
{ id: 'proxy-down', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'bad-shape', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'planted', health: 'attention', keys: [{ keyId: 'dddddddddddd', agent: null, state: null, issuedAt: '2026-11-01T10:00:00.000Z' }, { keyId: 'eeeeeeeeeeee', agent: 'dev', state: 'active', issuedAt: null }], other: 0, sentinel: 0 },
|
||||
{ id: 'planted-id', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'unread', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
]);
|
||||
assert.doesNotMatch(JSON.stringify(out), KEY_TEXT);
|
||||
assert.notEqual(out.organizations.find(o => o.id === 'uncertain').health, 'ok');
|
||||
// proxy.keys is the only verb access sends; the usage queue is never read.
|
||||
for (const o of all) for (const [verb] of o.calls) assert.equal(verb, 'proxy.keys');
|
||||
});
|
||||
@@ -6,12 +6,15 @@ import fs from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { PIN } from '../src/common.mjs';
|
||||
import { installPaths } from '../src/settings.mjs';
|
||||
import {
|
||||
processTree, pidListensOn, processStartMs, verifyRestart, initLockStatus, clearInitLock, initFiles, initState,
|
||||
fenceState, createUnitControl, releaseState, receiptFile, unitName,
|
||||
unitLog, serviceKeyState, receiptHistory, appendReceipt, LOG_LINES, HISTORY_LIMIT,
|
||||
} from '../src/admin.mjs';
|
||||
import { secretValues } from '../src/redact.mjs';
|
||||
|
||||
const code = name => error => error.code === name;
|
||||
const scratch = t => { const dir = fs.mkdtempSync(join(tmpdir(), 'pa-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true })); return dir; };
|
||||
@@ -193,3 +196,198 @@ test('unit actions map proxy-service refusals by code and refuse a missing unit'
|
||||
assert.equal(started.healthy, true); assert.deepEqual(verbs, ['start']);
|
||||
assert.throws(() => createUnitControl({ unitDir: 'relative' }), code('proxy-unit-path'));
|
||||
});
|
||||
|
||||
// #1873 settings-backend A1: the reads behind the setup page's log, service key and history.
|
||||
// Planted keys, one of each Mosaic prefix and short enough that only the prefix rule finds them,
|
||||
// plus long ones of the real shapes.
|
||||
const TAIL = 'Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE';
|
||||
const PLANTED = ['msk-tag-qwertyu', 'mso-tag-acme-qwerty', 'mst-tag-triage-zxcvb', 'msp-tag-plmokn',
|
||||
`msk-k1a2-${TAIL}`, `mso-k1a2-acme-${TAIL}`, `mst-k1a2-triage-${TAIL}`, `msp-k1a2-${TAIL}`];
|
||||
const ENV_SECRET = 'plainwordsonly';
|
||||
const leaks = text => [...PLANTED, ...PLANTED.map(k => k.slice(4)), TAIL.slice(0, 10), ENV_SECRET].filter(bit => text.includes(bit));
|
||||
|
||||
function journal(stdout, { code: exit = 0, calls = [] } = {}) {
|
||||
return async (command, args, options) => { calls.push({ command, args, options }); if (stdout instanceof Error) throw stdout; return { code: exit, stdout, stderr: 'raw stderr msk-tag-qwertyu' }; };
|
||||
}
|
||||
|
||||
test('unitLog asks journalctl for the named unit only, clamps the line count, and shows no planted key', async () => {
|
||||
const calls = [];
|
||||
const lines = ['proxy listening on 127.0.0.1:18432', ...PLANTED.map(k => `upstream refused ${k} for model x`), `auth with ${ENV_SECRET} ok`, 'msk-tag-qwertyu', '', 'last line'];
|
||||
const result = await unitLog({ run: journal(lines.join('\n') + '\n', { calls }), unit: 'mosaic-proxy-zz.service', lines: 10000, env: { X: '1' }, secrets: secretValues({ PROXY_KEY: ENV_SECRET }) });
|
||||
assert.deepEqual(calls[0].args, ['--user', '-u', 'mosaic-proxy-zz.service', '-n', String(LOG_LINES.max), '-o', 'cat', '--no-pager', '-q']);
|
||||
assert.equal(calls[0].command, 'journalctl');
|
||||
assert.deepEqual(calls[0].options.env, { X: '1' });
|
||||
assert.equal(result.unit, 'mosaic-proxy-zz.service');
|
||||
assert.equal(result.lines[0], 'proxy listening on 127.0.0.1:18432', 'a line with nothing secret shows as it is');
|
||||
assert.equal(result.lines.at(-1), 'last line');
|
||||
assert.equal(result.lines.filter(l => l === 'upstream refused [redacted] for model x').length, PLANTED.length);
|
||||
assert.equal(result.redacted, PLANTED.length, 'shown lines with a redaction; dropped lines count in dropped');
|
||||
assert.equal(result.dropped, 2, 'the env-secret line and the key-only line are dropped, the blank one is not counted');
|
||||
assert.deepEqual(leaks(JSON.stringify(result)), []);
|
||||
// At least one line, and a line count that is not an integer is refused.
|
||||
await unitLog({ run: journal('x\n', { calls }), unit: 'mosaic-proxy.service', lines: 0 });
|
||||
assert.equal(calls.at(-1).args[4], '1');
|
||||
await assert.rejects(unitLog({ run: journal('x\n'), unit: 'mosaic-proxy.service', lines: 2.5 }), code('invalid-request'));
|
||||
await assert.rejects(unitLog({ run: journal('x\n'), unit: 'mosaic-proxy.service', lines: '5' }), code('invalid-request'));
|
||||
});
|
||||
|
||||
test('unitLog keeps only the last lines asked for and never takes a unit name it was not given by unitName', async () => {
|
||||
const many = Array.from({ length: 30 }, (_, i) => `line ${i}`).join('\n');
|
||||
assert.deepEqual((await unitLog({ run: journal(many), unit: 'mosaic-proxy.service', lines: 3 })).lines, ['line 27', 'line 28', 'line 29']);
|
||||
for (const unit of ['sshd.service', 'mosaic-proxy.service; rm', '../mosaic-proxy.service', 'mosaic-proxy-A.service', undefined]) {
|
||||
await assert.rejects(unitLog({ run: journal('x'), unit, lines: 5 }), code('proxy-unit-usage'), String(unit));
|
||||
}
|
||||
});
|
||||
|
||||
test('a journal that cannot be read refuses outcome-unknown with no lines and no other source', async () => {
|
||||
for (const run of [journal(Object.assign(new Error('spawn journalctl ENOENT'), { code: 'ENOENT' })), journal('partial msk-tag-qwertyu', { code: 1 }), async () => ({ code: 0, stdout: null }), async () => null]) {
|
||||
await assert.rejects(unitLog({ run, unit: 'mosaic-proxy.service', lines: 5 }), error => error.code === 'outcome-unknown' && !leaks(String(error.message) + JSON.stringify(error)).length);
|
||||
}
|
||||
});
|
||||
|
||||
test('a secret split across journal lines drops every line', async () => {
|
||||
const result = await unitLog({ run: journal('ok one\nplain\nwordsonly\nok two\n'), unit: 'mosaic-proxy.service', lines: 10, secrets: [ENV_SECRET] });
|
||||
assert.deepEqual(result.lines, []);
|
||||
assert.equal(result.dropped, 4);
|
||||
});
|
||||
|
||||
test('createUnitControl.log runs unitLog for its own unit', async () => {
|
||||
const calls = [];
|
||||
const control = createUnitControl({ instance: 'zz', port: () => 1, run: journal('hello msk-tag-qwertyu\n', { calls }), probe: async () => true, waitMs: 0, pollMs: 0 });
|
||||
assert.deepEqual(await control.log(5), { unit: 'mosaic-proxy-zz.service', lines: ['hello [redacted]'], redacted: 1, dropped: 0 });
|
||||
assert.equal(calls[0].args[2], 'mosaic-proxy-zz.service');
|
||||
});
|
||||
|
||||
test('serviceKeyState answers proxy.json\'s mtime and never opens or stats the key file', t => {
|
||||
const dir = scratch(t);
|
||||
assert.deepEqual(serviceKeyState(dir), { madeAt: null, renewedAt: null, renewal: 'not-built', problem: 'missing' });
|
||||
const files = initFiles(dir, join(dir, 'data'));
|
||||
fs.writeFileSync(files.settings, '{}', { mode: 0o600 });
|
||||
fs.mkdirSync(join(files.key, '..'), { recursive: true, mode: 0o700 });
|
||||
fs.writeFileSync(files.key, 'msk-k1a2-' + TAIL, { mode: 0o600 });
|
||||
fs.utimesSync(join(dir, 'proxy.json'), new Date(Date.UTC(2026, 10, 1, 8)), new Date(Date.UTC(2026, 10, 1, 8)));
|
||||
const spies = ['openSync', 'readFileSync', 'statSync', 'lstatSync', 'accessSync', 'existsSync', 'realpathSync', 'readdirSync'].map(name => t.mock.method(fs, name));
|
||||
const state = serviceKeyState(dir);
|
||||
const seen = spies.flatMap(spy => spy.mock.calls.map(call => String(call.arguments[0])));
|
||||
spies.forEach(spy => spy.mock.restore());
|
||||
assert.deepEqual(state, { madeAt: '2026-11-01T08:00:00.000Z', renewedAt: null, renewal: 'not-built', problem: null });
|
||||
assert.deepEqual(seen, [join(dir, 'proxy.json')]);
|
||||
assert.deepEqual(leaks(JSON.stringify(state)), []);
|
||||
});
|
||||
|
||||
test('serviceKeyState gives no date for a proxy.json that is a symlink, a directory or another user\'s', t => {
|
||||
const dir = scratch(t), other = scratch(t);
|
||||
fs.writeFileSync(join(other, 'real.json'), '{}');
|
||||
fs.symlinkSync(join(other, 'real.json'), join(dir, 'proxy.json'));
|
||||
assert.deepEqual(serviceKeyState(dir), { madeAt: null, renewedAt: null, renewal: 'not-built', problem: 'unreadable' });
|
||||
fs.rmSync(join(dir, 'proxy.json')); fs.mkdirSync(join(dir, 'proxy.json'));
|
||||
assert.equal(serviceKeyState(dir).problem, 'unreadable');
|
||||
fs.rmSync(join(dir, 'proxy.json'), { recursive: true }); fs.writeFileSync(join(dir, 'proxy.json'), '{}');
|
||||
assert.equal(serviceKeyState(dir, { uid: process.getuid() + 1 }).problem, 'unreadable');
|
||||
assert.equal(serviceKeyState(dir).problem, null);
|
||||
});
|
||||
|
||||
// A receipt file with `count` entries; some lines padded so the file crosses several 64 KB chunks.
|
||||
function history(t, count, { pad = 0 } = {}) {
|
||||
const dataRoot = scratch(t);
|
||||
for (let i = 0; i < count; i++) appendReceipt(dataRoot, { time: new Date(i * 1000).toISOString(), op: 'fetch', n: i, ...(pad ? { note: 'word '.repeat(pad) } : {}) });
|
||||
return dataRoot;
|
||||
}
|
||||
const pages = (dataRoot, limit, extra = {}) => {
|
||||
const seen = []; let before = null, guard = 0;
|
||||
do {
|
||||
const page = receiptHistory(dataRoot, { limit, before, ...extra });
|
||||
seen.push(page.entries.map(e => e.n));
|
||||
before = page.next;
|
||||
} while (before !== null && ++guard < 1000);
|
||||
return seen;
|
||||
};
|
||||
|
||||
test('receiptHistory pages newest first, oldest first within a page, each entry exactly once across chunks', t => {
|
||||
const dataRoot = history(t, 300, { pad: 150 });
|
||||
assert.ok(fs.statSync(receiptFile(dataRoot)).size > 3 * 65536, 'the file spans several chunks');
|
||||
const seen = pages(dataRoot, 7);
|
||||
assert.deepEqual(seen[0], [293, 294, 295, 296, 297, 298, 299]);
|
||||
assert.deepEqual(seen.flat().sort((a, b) => a - b), Array.from({ length: 300 }, (_, i) => i));
|
||||
assert.equal(new Set(seen.flat()).size, 300);
|
||||
assert.deepEqual(seen.reverse().flat(), Array.from({ length: 300 }, (_, i) => i), 'pages read back to front are the whole history in order');
|
||||
const first = receiptHistory(dataRoot);
|
||||
assert.equal(first.entries.length, HISTORY_LIMIT.default);
|
||||
assert.equal(receiptHistory(dataRoot, { limit: 1000 }).entries.length, HISTORY_LIMIT.max, 'limit is clamped');
|
||||
assert.equal(receiptHistory(dataRoot, { limit: 0 }).entries.length, 1);
|
||||
assert.deepEqual(Object.keys(first.entries[0]).sort(), ['n', 'note', 'op', 'time']);
|
||||
});
|
||||
|
||||
test('receiptHistory skips a torn line and a partial last line, and a write after a page does not repeat or lose entries', t => {
|
||||
const dataRoot = history(t, 5);
|
||||
const file = receiptFile(dataRoot);
|
||||
const text = fs.readFileSync(file, 'utf8').split('\n');
|
||||
text.splice(2, 0, '{"op":"torn', '[1,2]', '{"no":"op"}', 'x'.repeat(70000));
|
||||
// The last line is a whole object whose newline is not written yet: it waits for the newline.
|
||||
fs.writeFileSync(file, text.join('\n') + '{"op":"partial","n":99}');
|
||||
const first = receiptHistory(dataRoot, { limit: 2 });
|
||||
assert.deepEqual(first.entries.map(e => e.n), [3, 4]);
|
||||
assert.deepEqual(receiptHistory(dataRoot, { limit: 1 }).entries.map(e => e.n), [4], 'not shown before its newline');
|
||||
fs.appendFileSync(file, '\n'); appendReceipt(dataRoot, { op: 'fetch', n: 100 });
|
||||
const rest = receiptHistory(dataRoot, { limit: 10, before: first.next });
|
||||
assert.deepEqual(rest.entries.map(e => e.n), [0, 1, 2]);
|
||||
assert.equal(rest.next, null);
|
||||
assert.deepEqual(receiptHistory(dataRoot, { limit: 3 }).entries.map(e => e.n), [4, 99, 100], 'the finished line shows on the next newest read');
|
||||
});
|
||||
|
||||
test('receiptHistory refuses a cursor that is not a line start, past the end, negative or not an integer', t => {
|
||||
const dataRoot = history(t, 3);
|
||||
const size = fs.statSync(receiptFile(dataRoot)).size;
|
||||
for (const before of [1, size + 1, -1, 1.5, '10', Number.MAX_SAFE_INTEGER + 2]) {
|
||||
assert.throws(() => receiptHistory(dataRoot, { before }), code('invalid-request'), String(before));
|
||||
}
|
||||
assert.throws(() => receiptHistory(dataRoot, { limit: 'all' }), code('invalid-request'));
|
||||
assert.deepEqual(receiptHistory(dataRoot, { before: 0 }), { entries: [], next: null });
|
||||
assert.equal(receiptHistory(dataRoot, { before: size }).entries.length, 3);
|
||||
});
|
||||
|
||||
test('receiptHistory withholds an entry whose fields hold an environment secret split across them', t => {
|
||||
const dataRoot = scratch(t);
|
||||
appendReceipt(dataRoot, { op: 'fetch', n: 1 });
|
||||
appendReceipt(dataRoot, { op: 'x', a: 'plainword', b: 'sonly' });
|
||||
appendReceipt(dataRoot, { op: 'x', plainw: 'ordsonly' });
|
||||
appendReceipt(dataRoot, { op: 'x', list: ['plainwo', 'rdsonly'] });
|
||||
appendReceipt(dataRoot, { op: 'fetch', n: 2 });
|
||||
const all = receiptHistory(dataRoot, { limit: 10, secrets: secretValues({ PROXY_TOKEN: ENV_SECRET }) });
|
||||
assert.deepEqual(all.entries, [{ op: 'fetch', n: 1 }, { withheld: true }, { withheld: true }, { withheld: true }, { op: 'fetch', n: 2 }]);
|
||||
assert.equal(receiptHistory(dataRoot, { limit: 10 }).entries[1].a, 'plainword', 'with no secrets nothing is withheld');
|
||||
});
|
||||
|
||||
test('receiptHistory: a missing file is an empty history; a symlink, FIFO, directory, group-readable or foreign file refuses', t => {
|
||||
const dataRoot = scratch(t);
|
||||
assert.deepEqual(receiptHistory(dataRoot), { entries: [], next: null });
|
||||
assert.throws(() => receiptHistory(dataRoot, { before: 5 }), code('invalid-request'));
|
||||
appendReceipt(dataRoot, { op: 'fetch' });
|
||||
const file = receiptFile(dataRoot);
|
||||
fs.chmodSync(file, 0o640);
|
||||
assert.throws(() => receiptHistory(dataRoot), code('outcome-unknown'));
|
||||
fs.chmodSync(file, 0o600);
|
||||
assert.throws(() => receiptHistory(dataRoot, { uid: process.getuid() + 1 }), code('outcome-unknown'));
|
||||
assert.equal(receiptHistory(dataRoot).entries.length, 1);
|
||||
const elsewhere = join(scratch(t), 'x.jsonl'); fs.writeFileSync(elsewhere, '{"op":"planted"}\n', { mode: 0o600 });
|
||||
fs.rmSync(file); fs.symlinkSync(elsewhere, file);
|
||||
assert.throws(() => receiptHistory(dataRoot), code('outcome-unknown'));
|
||||
fs.rmSync(file); fs.mkdirSync(file);
|
||||
assert.throws(() => receiptHistory(dataRoot), code('outcome-unknown'));
|
||||
fs.rmSync(file, { recursive: true });
|
||||
execFileSync('mkfifo', ['-m', '600', file]);
|
||||
assert.throws(() => receiptHistory(dataRoot), code('outcome-unknown'), 'a FIFO is refused without blocking');
|
||||
});
|
||||
|
||||
test('receiptHistory shows no planted key from any receipt field, nested value or key name', t => {
|
||||
const dataRoot = scratch(t);
|
||||
for (const key of PLANTED) appendReceipt(dataRoot, { time: new Date(0).toISOString(), op: 'init', note: `made ${key}`, nested: { list: [key, { deeper: key }] }, [key.replace(/-/g, '_')]: 1 });
|
||||
appendReceipt(dataRoot, { op: 'init', env: ENV_SECRET, [`x${ENV_SECRET}`]: 1 });
|
||||
appendReceipt(dataRoot, { op: PLANTED[1] });
|
||||
const all = receiptHistory(dataRoot, { limit: 100, secrets: secretValues({ PROXY_TOKEN: ENV_SECRET }) });
|
||||
assert.equal(all.entries.length, PLANTED.length + 2);
|
||||
assert.deepEqual(leaks(JSON.stringify(all)), []);
|
||||
assert.deepEqual(all.entries[0], { time: new Date(0).toISOString(), op: 'init', note: 'made [redacted]', nested: { list: ['[redacted]', { deeper: '[redacted]' }] } });
|
||||
assert.deepEqual(all.entries.at(-2), { op: 'init', env: '[redacted]' });
|
||||
assert.deepEqual(all.entries.at(-1), { op: '[redacted]' });
|
||||
});
|
||||
@@ -0,0 +1,122 @@
|
||||
// #1873 settings-backend A1: the redaction the setup page's log and history reads use. It is
|
||||
// chat-fail's list (packages/harness/src/failure-notice.mjs) with Mosaic's key prefixes added.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { redactLine, redactValue, holdsSecret, secretValues, REDACTED, LINE_MAX } from '../src/redact.mjs';
|
||||
|
||||
// Short keys of each Mosaic prefix, letters only, so no long-run or mixed-run rule catches them:
|
||||
// only the key-prefix line does.
|
||||
const SHORT = { msk: 'msk-tag-qwertyu', mso: 'mso-tag-acme-qwerty', mst: 'mst-tag-triage-zxcvb', msp: 'msp-tag-plmokn' };
|
||||
|
||||
for (const [prefix, key] of Object.entries(SHORT)) {
|
||||
test(`a short ${prefix}- key is redacted from a line, and the rest of the line stays`, () => {
|
||||
const safe = redactLine(`proxy said ${key} was refused`);
|
||||
assert.equal(safe.text, `proxy said ${REDACTED} was refused`);
|
||||
assert.equal(safe.redacted, true);
|
||||
assert.equal(safe.text.includes(key), false);
|
||||
// Upper case, and joined by _ instead of -, too.
|
||||
assert.equal(redactLine(`proxy said ${key.toUpperCase()} x`).text, `proxy said ${REDACTED} x`);
|
||||
assert.equal(redactLine(`proxy said ${key.replace('-', '_')} x`).text, `proxy said ${REDACTED} x`);
|
||||
});
|
||||
}
|
||||
|
||||
test('a short Mosaic key glued to a word or an underscore is redacted', () => {
|
||||
for (const key of Object.values(SHORT)) {
|
||||
for (const glued of [`x${key}`, `_${key}`, `id=x${key}`, `9${key.toUpperCase()}`]) {
|
||||
const safe = redactLine(`proxy said ${glued} was refused`);
|
||||
assert.equal(safe.redacted, true, glued);
|
||||
assert.equal(safe.text.toLowerCase().includes(key.slice(4)), false, glued);
|
||||
assert.match(safe.text, / was refused$/);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('the real key shapes of each prefix are redacted', () => {
|
||||
const tail = 'Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE';
|
||||
for (const key of [`msk-k1a2-${tail}`, `mso-k1a2-acme-${tail}`, `mst-k1a2-triage-${tail}`, `msp-k1a2-${tail}`, `sk-ant-${tail}`]) {
|
||||
const safe = redactLine(`key ${key} rejected by upstream`);
|
||||
assert.equal(safe.text.includes(key.slice(0, 12)), false, key.slice(0, 4));
|
||||
assert.equal(safe.text.includes(tail.slice(0, 8)), false, key.slice(0, 4));
|
||||
}
|
||||
});
|
||||
|
||||
test('bearer and basic tokens, key=value pairs, links, emails and paths never show', () => {
|
||||
const line = 'POST https://u:[email protected]/v1?key=abc Authorization: Bearer abcdefgh api_key=hunter2 token "quoted value" '
|
||||
+ 'by [email protected] from /home/jane/.config/mosaic-dev/proxy.json and ~/.mosaic/x C:\\Users\\j \\\\host\\share Basic dXNlcjpwYXNz eyJhbGciOi.x.y';
|
||||
const safe = redactLine(line);
|
||||
for (const bit of ['u:p@', 'api.example', 'abcdefgh', 'hunter2', 'quoted value', 'jane@', '/home/jane', '.config', '.mosaic', 'Users', 'share', 'dXNlcjpwYXNz', 'eyJhbGci']) {
|
||||
assert.equal(safe.text.includes(bit), false, bit);
|
||||
}
|
||||
assert.match(safe.text, /^POST /);
|
||||
});
|
||||
|
||||
test('a line holding an environment secret is withheld whole, even split by spaces, controls or encoding', () => {
|
||||
const secrets = secretValues({ MOSAIC_PROXY_KEY: 'plainwordsonly', HOME: '/home/x', SHORT_TOKEN: 'abc' });
|
||||
assert.deepEqual(secrets, ['plainwordsonly'], 'only secret-named values of 8 or more characters');
|
||||
for (const line of ['started with plainwordsonly', 'started with plain words only', 'started p\u0000lainwords\u200bonly', 'started %70lainwordsonly', 'started plainwordsonly']) {
|
||||
assert.deepEqual(redactLine(line, secrets), { text: null, redacted: true, withheld: true }, JSON.stringify(line));
|
||||
}
|
||||
assert.equal(holdsSecret('plain\nwords\nonly', secrets), true, 'split across lines');
|
||||
assert.equal(holdsSecret('nothing here', secrets), false);
|
||||
assert.equal(holdsSecret('plainwordsonly', []), false);
|
||||
});
|
||||
|
||||
test('a line that is empty, blank or only redactions is dropped; a long one is cut to the line cap', () => {
|
||||
assert.deepEqual(redactLine(''), { text: null, redacted: false });
|
||||
assert.deepEqual(redactLine(' \u0000\t '), { text: null, redacted: false });
|
||||
assert.deepEqual(redactLine(`${SHORT.mso} ${SHORT.mst}`), { text: null, redacted: true });
|
||||
assert.equal(redactLine(`a ${SHORT.mso} ${SHORT.mst} b`).text, `a ${REDACTED} b`, 'repeated redactions collapse');
|
||||
const long = redactLine('word '.repeat(200));
|
||||
assert.equal(Array.from(long.text).length, LINE_MAX);
|
||||
assert.match(long.text, /\.\.\.$/);
|
||||
assert.equal(redactLine('\u001b[31mred\u001b[0m text').text, 'red text', 'ANSI goes');
|
||||
});
|
||||
|
||||
test('a stored value is redacted all through: strings, nested strings, and keys that read as secrets', () => {
|
||||
const value = { op: 'account.proxy.apikey.add', time: '2026-11-19T12:34:56.789Z', provider: 'openai', note: `got ${SHORT.mso}`, deep: { list: [SHORT.msk, 3, true, null] }, [SHORT.mst.replace(/-/g, '_')]: 'x', 'bad key': 1 };
|
||||
const out = redactValue(value);
|
||||
assert.deepEqual(out, { op: 'account.proxy.apikey.add', time: '2026-11-19T12:34:56.789Z', provider: 'openai', note: `got ${REDACTED}`, deep: { list: [REDACTED, 3, true, null] } });
|
||||
assert.equal(JSON.stringify(out).includes('qwerty'), false);
|
||||
// Depth and size are bounded.
|
||||
assert.deepEqual(redactValue({ a: { b: { c: { d: { e: 1 } } } } }), { a: { b: { c: { d: null } } } });
|
||||
assert.equal(redactValue(Array.from({ length: 100 }, (_, i) => i)).length, 64);
|
||||
assert.equal(redactValue('plainwordsonly', ['plainwordsonly']), REDACTED);
|
||||
assert.equal(redactValue(undefined), null);
|
||||
});
|
||||
|
||||
// chat-fail's shape: redaction runs over a whole line before the cut, so a long run with no spaces
|
||||
// must stay linear.
|
||||
test('a 128000-character run with no spaces is redacted in well under a second', () => {
|
||||
const N = 128000;
|
||||
const secrets = secretValues({ API_KEY: 'plainwordsonly' });
|
||||
for (const run of ['a'.repeat(N), 'a.'.repeat(N / 2), 'a-'.repeat(N / 2), 'a/'.repeat(N / 2), 'a:'.repeat(N / 2), 'a"'.repeat(N / 2), 'mso-'.repeat(N / 4), 'a@'.repeat(N / 2)]) {
|
||||
const started = performance.now();
|
||||
const safe = redactLine(`${run} then the unit stopped`, secrets);
|
||||
const took = performance.now() - started;
|
||||
assert.ok(took < 1000, `${run.slice(0, 4)}... took ${Math.round(took)} ms`);
|
||||
assert.ok(safe.text === null || Array.from(safe.text).length <= LINE_MAX);
|
||||
}
|
||||
// A run that reads as a key is redacted whole, so the words after it show.
|
||||
assert.equal(redactLine(`${'a'.repeat(N)} then the unit stopped`).text, `${REDACTED} then the unit stopped`);
|
||||
});
|
||||
|
||||
// The list is a copy of failure-notice.mjs's. Every line must still match it, except the key-prefix
|
||||
// line, which is the original with mso, mst and msp added after msk, and Mosaic's glued-prefix line
|
||||
// right after it.
|
||||
test('the redaction list matches failure-notice.mjs line for line, with only the prefixes and one line added', () => {
|
||||
const source = file => fs.readFileSync(fileURLToPath(new URL(file, import.meta.url)), 'utf8');
|
||||
const block = text => {
|
||||
const body = /const REDACT = \[\n([\s\S]*?)\n\];/.exec(text)[1];
|
||||
return body.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('//')).map(line => line.replace(/,\s*\/\/ .*$/, ',').replace(/,$/, ''));
|
||||
};
|
||||
const keyWord = text => /const KEY_WORD = (.*);/.exec(text)[1];
|
||||
const theirs = source('../../harness/src/failure-notice.mjs'), ours = source('../src/redact.mjs');
|
||||
assert.equal(keyWord(ours), keyWord(theirs));
|
||||
const expected = block(theirs).map(line => line.replace('(?:sk|msk|rk|', '(?:sk|msk|mso|mst|msp|rk|'));
|
||||
assert.equal(expected.filter((line, i) => line !== block(theirs)[i]).length, 1, 'the original has one key-prefix line');
|
||||
const at = expected.findIndex(line => line.includes('(?:sk|msk|mso|mst|msp|rk|'));
|
||||
expected.splice(at + 1, 0, '/ms[kotp][-_]\\S+/gi');
|
||||
assert.deepEqual(block(ours), expected);
|
||||
});
|
||||
@@ -230,7 +230,9 @@ try {
|
||||
// own check (no symlink, a regular file this user owns, not group or world writable).
|
||||
const rideRiders = async ({ timeoutMs = 5000, deadline } = {}) => {
|
||||
const writer = organizationWriter({ dir: configDir(), rolesDir: rolesDir() });
|
||||
const local = id => readRider({ id, read: () => writer.read(id).document, readLog: () => ownedText(writer.logFile(id), { mask: 0o022 }), isFixture: orgUsesFixture });
|
||||
// settings-backend A2: `file()` is the organization file as the writer reads it ({document, revision}),
|
||||
// read again on each call, for turning a program off for every organization.
|
||||
const local = id => ({ ...readRider({ id, read: () => writer.read(id).document, readLog: () => ownedText(writer.logFile(id), { mask: 0o022 }), isFixture: orgUsesFixture }), file: () => writer.read(id) });
|
||||
const down = async () => { throw Object.assign(new Error('the bus is not reachable'), { code: 'no-bus-host' }); };
|
||||
// The single-business Console has no bus status here: its organization never reads as stopped.
|
||||
if (!organizations) return ownerCall && options.business ? [{ ...local(options.business), call: ownerCall }] : [];
|
||||
@@ -264,6 +266,8 @@ try {
|
||||
return out;
|
||||
},
|
||||
riders: rideRiders,
|
||||
// The root agents each organization takes count when a program is turned off (review P1-1).
|
||||
rootAgents: () => loadRootAgents(configDir(), { rolesDir: rolesDir() }),
|
||||
ownerModels,
|
||||
// A stopped fixture organization moves through the organization service's whole-file update, the
|
||||
// write `POST /api/orgs/ID/admin` makes, as the signed-in owner. The owner starts its bus after.
|
||||
|
||||
@@ -36,7 +36,28 @@ const ROUTES = Object.freeze({
|
||||
// #1873 ride-default: Ride all, and the confirmed move of one stopped fixture organization.
|
||||
'/api/proxy/admin/ride-all': ['POST', []],
|
||||
'/api/proxy/admin/ride-move': ['POST', ['org']],
|
||||
// #1873 settings-backend A1: reads for Model setup. The list names each read's query keys.
|
||||
'/api/proxy/admin/log': ['GET', ['lines']],
|
||||
'/api/proxy/admin/service-key': ['GET', []],
|
||||
'/api/proxy/admin/history': ['GET', ['limit', 'before']],
|
||||
// #1873 settings-backend A2: one harness program off for every organization, and each
|
||||
// organization's proxy client key health. The program list itself is in status.
|
||||
'/api/proxy/admin/programs/off': ['POST', ['name']],
|
||||
'/api/proxy/admin/access': ['GET', []],
|
||||
});
|
||||
// The A1 reads take a query; every other route ignores one, as before.
|
||||
const QUERY_ROUTES = new Set(['/api/proxy/admin/log', '/api/proxy/admin/service-key', '/api/proxy/admin/history']);
|
||||
// Each query key at most once, only the route's own keys, each a plain decimal number.
|
||||
function readQuery(req, allowed) {
|
||||
let params;
|
||||
try { params = new URL(req.url, 'http://localhost').searchParams; } catch { return null; }
|
||||
const out = {};
|
||||
for (const [key, value] of params) {
|
||||
if (!allowed.includes(key) || Object.hasOwn(out, key) || !/^[0-9]{1,15}$/.test(value)) return null;
|
||||
out[key] = Number(value);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
export const PROXY_ADMIN_PATHS = Object.freeze(Object.keys(ROUTES));
|
||||
const INIT_KEYS = ['generate', 'key', 'confirm', 'port', 'rotateGraceMinutes', 'firstOutputSeconds'];
|
||||
const json = (res, status, value) => {
|
||||
@@ -71,6 +92,8 @@ export async function proxyAdminHTTP(req, res, { path, admin, call, named, teste
|
||||
if (req.headersDistinct.origin?.length !== 1 || req.headers.origin !== origin) return done(403, { error: 'cross-origin request refused' });
|
||||
if (req.headersDistinct['sec-fetch-site']?.length !== 1 || req.headers['sec-fetch-site'] !== 'same-origin') return done(403, { error: 'cross-site request refused' });
|
||||
}
|
||||
let query = {};
|
||||
if (QUERY_ROUTES.has(path)) { query = readQuery(req, route[1]); if (!query) return refuse('invalid-request'); }
|
||||
if (!admin) return refuse('not-configured');
|
||||
let args = {};
|
||||
if (req.method === 'POST') {
|
||||
@@ -118,6 +141,13 @@ export async function proxyAdminHTTP(req, res, { path, admin, call, named, teste
|
||||
case '/api/proxy/admin/ride-move':
|
||||
if (typeof args.org !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(args.org)) return refuse('invalid-request');
|
||||
return done(200, await admin.rideMove({ org: args.org, principal }));
|
||||
case '/api/proxy/admin/log': return done(200, await admin.log(Object.hasOwn(query, 'lines') ? { lines: query.lines } : {}));
|
||||
case '/api/proxy/admin/service-key': return done(200, await admin.serviceKey());
|
||||
case '/api/proxy/admin/history': return done(200, await admin.history({ ...(Object.hasOwn(query, 'limit') ? { limit: query.limit } : {}), before: query.before ?? null }));
|
||||
case '/api/proxy/admin/programs/off':
|
||||
if (typeof args.name !== 'string' || !/^[a-z][a-z0-9-]{0,31}$/.test(args.name)) return refuse('invalid-request');
|
||||
return done(200, await admin.programsOff({ name: args.name, user: typeof user === 'string' ? user : null }));
|
||||
case '/api/proxy/admin/access': return done(200, await admin.access());
|
||||
}
|
||||
} catch (error) {
|
||||
const code = ['human-required', 'not-configured', 'invalid-request'].includes(error?.code) ? error.code : proxyCode(error, 'outcome-unknown');
|
||||
|
||||
@@ -18,7 +18,10 @@ const codes = ['not-configured', 'human-required', 'provider-not-allowed', 'logi
|
||||
const json = (res, status, value) => {
|
||||
res.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' }); res.end(JSON.stringify(value));
|
||||
};
|
||||
export async function proxyHTTP(req, res, { path, call, named, tester, granted, send, origin, user, log }) {
|
||||
// `record` is the proxy setup controller's recordAccount (settings-backend A1), or null where this
|
||||
// Console has no proxy setup: after a write's bus call answers, an account change is added to the
|
||||
// setup history. A record that fails leaves the bus answer as it is and adds recorded: false.
|
||||
export async function proxyHTTP(req, res, { path, call, named, tester, granted, send, origin, user, log, record = null }) {
|
||||
const route = routes[path];
|
||||
const done = (status, value) => {
|
||||
// No request body, query, login state, authorization URL or exception text.
|
||||
@@ -48,11 +51,19 @@ export async function proxyHTTP(req, res, { path, call, named, tester, granted,
|
||||
} catch { return done(400, { error: 'invalid-request' }); }
|
||||
}
|
||||
if (path === '/api/proxy/plugins/set' && (!args.plugins || typeof args.plugins !== 'object' || Array.isArray(args.plugins) || Object.keys(args.plugins).length !== 1 || !['harness', 'model-provider'].includes(Object.keys(args.plugins)[0]) || !Array.isArray(Object.values(args.plugins)[0]) || !Object.values(args.plugins)[0].every(name => typeof name === 'string'))) return done(400, { error: 'invalid-request' });
|
||||
try { return done(200, await call(route[1], args)); }
|
||||
let value;
|
||||
try { value = 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';
|
||||
// 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 });
|
||||
}
|
||||
if (req.method === 'POST' && typeof record === 'function') {
|
||||
let recorded;
|
||||
try { recorded = record({ verb: route[1], args, result: value, user: user ?? null }); } catch { recorded = false; }
|
||||
// All four recorded verbs answer a plain object; any other answer is passed on unmarked.
|
||||
if (recorded === false && value && typeof value === 'object' && !Array.isArray(value)) value = { ...value, recorded: false };
|
||||
}
|
||||
return done(200, value);
|
||||
}
|
||||
@@ -703,7 +703,14 @@
|
||||
<h2 id="b-for-you">Open decisions <span class="count">${rows.length}</span></h2>${body}
|
||||
<p class="small muted">Open a decision for its seen, DM and closing history.</p>`;
|
||||
}
|
||||
const messageText = m => m.from_role !== 'human' && typeof m.body === 'string' && m.body.startsWith('The turn failed: ') ? `The assistant couldn't reply. Try again, or check Agents.<span class="source">Details: ${txt(m.body.slice('The turn failed: '.length))}</span>` : txt(m.body);
|
||||
// #1873 settings slice 5: a failed turn whose body carries its own "\nDetails: " part (chat.js
|
||||
// messageBody) shows its cause and that part once.
|
||||
const messageText = m => {
|
||||
if (m.from_role === 'human' || typeof m.body !== 'string' || !m.body.startsWith('The turn failed: ')) return txt(m.body);
|
||||
const rest = m.body.slice('The turn failed: '.length), at = rest.indexOf('\nDetails: ');
|
||||
if (at > 0) return `The assistant couldn't reply: ${txt(rest.slice(0, at))}<span class="source">Details: ${txt(rest.slice(at + '\nDetails: '.length))}</span>`;
|
||||
return `The assistant couldn't reply. Try again, or check Agents.<span class="source">Details: ${txt(rest)}</span>`;
|
||||
};
|
||||
async function messagesView(get, q) {
|
||||
const after = q.get('after');
|
||||
if (after !== null && !SAFE.test(after)) return notFound('The message cursor is not a message id.');
|
||||
@@ -1299,6 +1306,9 @@
|
||||
}
|
||||
const routesApplyMarkup=()=>routesApply?.length?`<ul class="routes-apply" aria-label="How the change reaches each agent">${routesApply.map(a=>`<li><b>${txt(a.name)}</b>: ${txt(a.text)}${a.restart&&canSend&&!isTester?` ${window.MosaicAgents?.restartMarkup(a.name,a.thread)??''}`:''}</li>`).join('')}</ul>`:'';
|
||||
let harnessSettings=null, harnessDraft=null, harnessLoading=false, harnessReceipt='', harnessBusy=false;
|
||||
// #1873 settings slice 5: the organization's one choice, Use your models (the cliproxy module in
|
||||
// its model-provider list, from the plugins read above) or its own model service (Coming later).
|
||||
let modelsReceipt='', modelsReceiptKind='', modelsBusy=false, modelsFocus=false;
|
||||
function readHarnesses(){
|
||||
if(harnessLoading || harnessSettings !== null || !canSend || isTester)return; harnessLoading=true;const epoch=orgEpoch;
|
||||
fetch('/api/proxy/plugins',{cache:'no-store',redirect:'manual'}).then(async r=>{const v=await r.json();if(epoch!==orgEpoch)return;if(!r.ok || !/^[a-f0-9]{64}$/.test(v.revision??'') || !Array.isArray(v.available?.harness) || !v.available.harness.every(n=>typeof n==='string') || !Array.isArray(v.plugins?.harness??[]))throw Error();harnessSettings=v;}).catch(()=>{if(epoch!==orgEpoch)return;harnessSettings=false;harnessReceipt='Harnesses could not be read.';}).finally(()=>{if(epoch!==orgEpoch)return;harnessLoading=false;if(routeOf().split('?')[0]==='/settings/routing')render(false);});
|
||||
@@ -1345,11 +1355,48 @@
|
||||
if(owner&&serviceState==='not-running')return `<p class="small warn" id="routes-catalogue">${lead} The model service is not running. ${setupLink('Open Model setup')}. ${thinking}</p>${words}`;
|
||||
if(owner&&(serviceLoading||(serviceState===null&&triageOwner)))return `<p class="small warn" id="routes-catalogue">${lead} Checking the model service. ${thinking}</p>`;
|
||||
if(owner&&serviceState===false)return `<div class="banner routes-catalogue" id="routes-catalogue" role="status"><p>${lead}${plain} The model service's status could not be read. ${thinking}</p>${words}<p class="actions">${recheck}</p></div>`;
|
||||
return `<p class="small warn" id="routes-catalogue">${lead}${plain} ${thinking}${owner?' A new model needs the model service: ask the instance owner to set it up, or press Stop using the model service when this organization does not need it.':''}</p>${words}`;
|
||||
return `<p class="small warn" id="routes-catalogue">${lead}${plain} ${thinking}${owner?' A new model needs the model service: ask the instance owner to set it up, or press Stop using your models when this organization does not need it.':''}</p>${words}`;
|
||||
}
|
||||
// Check again rereads Routing; the bus looks for the model service at that read. A draft is kept.
|
||||
document.addEventListener('click',e=>{if(e.target.id!=='routes-recheck')return;e.target.disabled=true;serviceState=null;routesView=null;last.delete('routes');render(true);});
|
||||
document.addEventListener('mosaic-org-restarted',e=>{if(e.detail?.id!==window.MosaicOrg?.id)return;serviceState=null;routesView=null;last.delete('routes');const path=routeOf().split('?')[0];if(path==='/settings/routing'||document.querySelector('[data-org-down]'))render(true);});
|
||||
// The choice reads only the plugins read: before it answers the radios wait, and a failed read
|
||||
// says so with Read again. Use your models writes at once; Stop using your models is the way back.
|
||||
// While the organization needs a restart, its banner's Restart is the one primary and the choice waits.
|
||||
// A Console with no organizations says "this business": lowercase inside a sentence, capitalized
|
||||
// at its start (cap).
|
||||
const modelsWho=cap=>window.MosaicOrg?(window.MosaicOrg.label||window.MosaicOrg.id):cap?'This business':'this business';
|
||||
const modelsWhose=cap=>`${modelsWho(cap)}'s`;
|
||||
// settings-s5 review P2-3: a choice that is not in force says when the model service behind it is
|
||||
// not set up or not answering (the owner's status read, as on the catalogue line). The choice
|
||||
// still writes: riding before setup is the design's "Nothing set up" state.
|
||||
function modelsService(){
|
||||
const link=text=>`<a href="#/settings/model-setup">${text}</a>`;
|
||||
return serviceState==='not-set-up'?`<p class="small warn" id="models-service">Your model service isn't set up. ${link('Set it up in Model setup')}.</p>`
|
||||
:serviceState==='not-running'?`<p class="small warn" id="models-service">Your model service isn't answering. ${link('Restart it in Model setup')}.</p>`
|
||||
:serviceState==='fenced'?`<p class="small warn" id="models-service">Your model service is waiting for a restart to finish. ${link('Open Model setup to finish it')}.</p>`:'';
|
||||
}
|
||||
function modelsChoice(restartFirst){
|
||||
const who=txt(modelsWho()), Who=txt(modelsWho(true)), whose=txt(modelsWhose()), Whose=txt(modelsWhose(true));
|
||||
const names=harnessSettings&&Array.isArray(harnessSettings.plugins?.['model-provider'])?harnessSettings.plugins['model-provider']:[];
|
||||
const read=!!harnessSettings, on=read&&names.includes('cliproxy'), wait=!read||modelsBusy||restartFirst||out();
|
||||
if(triageOwner)readService();
|
||||
const now=harnessSettings===false?`Which models ${who} uses could not be read.`:!read?`Reading ${whose} models…`:on?`Now: ${who} uses your models.`:`Now: ${who} does not use your models. Its agents use only the models in its own organization file.`;
|
||||
const change=!read?'':restartFirst?`<p class="small" id="models-wait">You can change ${whose} models after it restarts.</p>`
|
||||
:on?`<p class="small muted" id="models-change">Its agents can use the models your accounts offer. To switch back, press Stop using your models. A route on one of your models then stops working until you change it.</p>`
|
||||
:`<p class="small muted" id="models-change">Choose Use your models to switch. ${Whose} agents can then use the models your accounts offer. While your model service is down, a launch that needs it is refused. A running agent keeps its model until it starts again.</p>${modelsService()}`;
|
||||
// P2-1: while the organization needs a restart every control here waits. Stop is left out, as in
|
||||
// the mockup, since a disabled button here looks no different from an enabled one.
|
||||
const stop=on&&!restartFirst?`<button type="button" id="models-stop"${modelsBusy||out()?' disabled':''}>${modelsBusy?'<span class="settings-spinner" aria-hidden="true"></span> Saving…':'Stop using your models'}</button>`:'';
|
||||
const again=harnessSettings===false?'<button type="button" id="models-read">Read again</button>':'';
|
||||
// P3-1: Use your models has no button of its own, so its write shows Saving in the status line.
|
||||
const status=modelsBusy&&!on?'<span class="settings-spinner" aria-hidden="true"></span> Saving…':txt(modelsReceipt);
|
||||
return `<fieldset class="models-source" id="models-source"><legend>Where ${who} gets its models</legend>`
|
||||
+`<label class="choice"><input type="radio" name="models-source" id="models-yours" value="yours"${on?' checked':''}${wait?' disabled':''}><strong>Use your models</strong><span class="badge badge-ok">Recommended</span><span class="small muted">${Who} uses the model service and accounts in Model setup. Nothing to sign in to.</span></label>`
|
||||
+`<label class="choice off"><input type="radio" name="models-source" id="models-own" value="own" disabled><strong>Use its own model service</strong><span class="badge badge-muted">Coming later</span><span class="small muted">${Who} would run a second model service with its own accounts. Planned, not built yet, so you can't choose it today.</span></label>`
|
||||
+`<p id="models-in-force">${now}</p>${change}<p class="actions">${stop}${again}</p>`
|
||||
+`<p role="status" id="models-receipt" class="${modelsBusy&&!on?'':modelsReceiptKind}" tabindex="-1" data-focus-key="models-receipt">${status}</p></fieldset>`;
|
||||
}
|
||||
function routesSection(view,instanceInfo){
|
||||
const title='<h2 id="routes-title">Routing</h2>';
|
||||
readHarnesses();
|
||||
@@ -1381,7 +1428,7 @@
|
||||
// settings-p3 (re-check 3 P3-5): a ready catalogue the bus picked up late says agents start on these
|
||||
// models after a restart; that notice gets the organization's Restart (not primary: Save routes is).
|
||||
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)}${n===window.MosaicConsole?.RESTART_TO_LAUNCH&&window.MosaicOrg?.id?` ${window.MosaicConsole.orgRestartButton(window.MosaicOrg.id,{name:window.MosaicOrg.label||window.MosaicOrg.id})}`:''}</p>`).join('')}`:v.catalogue?.status==='unavailable'?catalogueLine(proxyOn,v.catalogue):'<p class="small muted">Model catalogue not enabled.</p>';
|
||||
const providerControl=canSend&&!isTester?`<button type="button" id="provider-switch" data-enabled="${proxyOn}">${proxyOn?'Stop using the model service':'Use your models'}</button><p role="status" id="provider-switch-status"></p>`:'';
|
||||
const providerControl=canSend&&!isTester?modelsChoice(/ data-primary[ >]/.test(catalogueInfo)):'';
|
||||
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 for this organization, so every launch is refused, and so is a save that changes a harness. Switch one on under Harnesses.</p>';
|
||||
return `<section class="panel routing-panel" aria-labelledby="routes-title"><span class="sr-only">${title}</span>${catalogueInfo}${providerControl}${harnessControl}${off}<form id="routes-form"><div class="table-wrap" tabindex="0" role="region" aria-label="Routes, scroll for all columns"><table id="routes-table"><thead><tr><th scope="col" rowspan="2">Agent</th><th scope="colgroup" colspan="3">Primary</th><th scope="colgroup" colspan="3">Fallback</th></tr><tr>${ROUTE_CELLS.map(([,l])=>`<th scope="col">${l.replace('Fallback ','')}</th>`).join('')}</tr></thead><tbody>${rows}</tbody></table></div><p class="small muted">${canSend || isTester?'Changes are checked and logged with your identity.':'Saving requires signed-in send mode.'}</p><div class="settings-actions route-actions${routesDraft?' has-draft':''}"><button type="submit"${/ data-primary[ >]/.test(catalogueInfo)?' class="route-save-quiet"':''}${disabled}>${routesBusy?'<span class="settings-spinner" aria-hidden="true"></span> Saving…':'Save routes'}</button> <button type="button" id="routes-reload"${routesDraft?'':' hidden'}${routesBusy||!routesDraft?' disabled':''}>Discard draft</button><p role="status" id="routes-receipt" class="${routesReceipt.startsWith('Outcome unknown')?'banner ref':routesReceipt.includes('Draft kept')?'banner err':routesReceipt.includes('saved')?'settings-success':''}">${txt(routesReceipt)}</p></div>${routesApplyMarkup()}</form></section>`;
|
||||
@@ -1409,7 +1456,7 @@
|
||||
const moved=Object.entries(routes).flatMap(([name,r])=>{const was=routesView?.instances?.[name]??{};
|
||||
return [[r.harness,r.model,was.harness,was.model],[r.fallback?.harness,r.fallback?.model,was.fallback?.harness,was.fallback?.model]].filter(([h,m,wh,wm])=>m&&(h!==wh||m!==wm)).map(([,m])=>m);});
|
||||
const module=key=>routesView?.providerModels?.[key]?.module;
|
||||
return moved.length&&moved.every(key=>module(key)&&module(key)!=='cliproxy'&&names.includes(module(key)))?' This change does not need the model service: press Stop using the model service, then save again.':'';
|
||||
return moved.length&&moved.every(key=>module(key)&&module(key)!=='cliproxy'&&names.includes(module(key)))?' This change does not need your models: press Stop using your models, then save again.':'';
|
||||
}
|
||||
const routesAnswer=r=>/^[a-f0-9]{64}$/.test(r?.revision??'')&&Array.isArray(r.harnesses)&&Array.isArray(r.models)&&[r.levels,r.instances].every(o=>o&&typeof o==='object'&&!Array.isArray(o));
|
||||
for(const kind of ['input','change'])document.addEventListener(kind,e=>{const form=e.target?.closest?.('#routes-form');if(!form||!routesView)return;routesDraft=routesFields(form);routesEditVersion++;markSettingsDraft('routing',true);form.querySelector('.settings-actions')?.classList.add('has-draft');const discard=$('routes-reload');if(discard){discard.hidden=false;discard.disabled=false;}
|
||||
@@ -1429,17 +1476,32 @@
|
||||
catch{if(epoch!==orgEpoch)return;harnessReceipt='Outcome unknown. Read the harnesses again before trying.';harnessSettings=false;}
|
||||
finally{if(epoch===orgEpoch){harnessBusy=false;if(!out())render(false);}}
|
||||
});
|
||||
document.addEventListener('click',async e=>{if(e.target.id!=='provider-switch'||!canSend||isTester)return;
|
||||
const button=e.target,status=$('provider-switch-status');if(routesDraft){status.textContent='Save or discard the route draft first.';return;}button.disabled=true;const epoch=orgEpoch;
|
||||
try {const read=await fetch('/api/proxy/plugins',{cache:'no-store'});const settings=await read.json();if(epoch!==orgEpoch)return;if(!read.ok||!/^[a-f0-9]{64}$/.test(settings.revision??''))throw Error('Could not read provider settings.');
|
||||
const names=Array.isArray(settings.plugins?.['model-provider'])?settings.plugins['model-provider']:[];
|
||||
// The fresh read decides: a stale label never turns the proxy module on when it meant off.
|
||||
const on=names.includes('cliproxy');if(String(on)!==button.dataset.enabled){button.dataset.enabled=String(on);button.textContent=on?'Stop using the model service':'Use your models';button.disabled=false;status.textContent='The switch now shows the current setting. Press it again to change it.';return;}
|
||||
const next=on?names.filter(n=>n!=='cliproxy'):[...names,'cliproxy'];
|
||||
const response=await fetch('/api/proxy/plugins/set',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({revision:settings.revision,plugins:{'model-provider':next}}),cache:'no-store',redirect:'manual'});
|
||||
if(epoch!==orgEpoch)return;if(!response.ok)throw Error('Provider setting was refused.');harnessSettings=null;routesView=null;last.delete('routes');render(true);
|
||||
}catch(error){if(epoch!==orgEpoch)return;status.textContent=error.message;button.disabled=false;}
|
||||
});
|
||||
// The fresh read decides: a page that shows a stale choice is redrawn from it and nothing is written.
|
||||
async function setModels(want){
|
||||
if(!canSend||isTester||modelsBusy||!harnessSettings)return;
|
||||
const who=modelsWho(true), shown=(harnessSettings.plugins?.['model-provider']??[]).includes('cliproxy');
|
||||
let sent=false;
|
||||
if(routesDraft){modelsReceipt='Save or discard the route draft first.';modelsReceiptKind='banner err';modelsFocus=true;render(false);return;}
|
||||
modelsBusy=true;modelsReceipt='';modelsReceiptKind='';render(false);const epoch=orgEpoch;
|
||||
try{const read=await fetch('/api/proxy/plugins',{cache:'no-store',redirect:'manual'});const settings=await read.json();if(epoch!==orgEpoch)return;
|
||||
if(!read.ok||!/^[a-f0-9]{64}$/.test(settings.revision??'')||!Array.isArray(settings.available?.harness))throw Error('read');
|
||||
const names=Array.isArray(settings.plugins?.['model-provider'])?settings.plugins['model-provider']:[], on=names.includes('cliproxy');
|
||||
if(on!==shown){harnessSettings=settings;modelsReceipt=`${modelsWhose(true)} models changed since this page read it. The page now shows the current choice; choose again to change it.`;modelsReceiptKind='banner ref';return;}
|
||||
if(on===want)return;
|
||||
const next=want?[...names,'cliproxy']:names.filter(n=>n!=='cliproxy');
|
||||
sent=true;const response=await fetch('/api/proxy/plugins/set',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({revision:settings.revision,plugins:{'model-provider':next}}),cache:'no-store',redirect:'manual'});
|
||||
const result=await response.json().catch(()=>({}));if(epoch!==orgEpoch)return;
|
||||
if(!response.ok){modelsReceipt=`Not changed. ${result.message||result.error||'The change was refused.'}`;modelsReceiptKind='banner err';return;}
|
||||
modelsReceipt=want?`Saved. ${who} uses your models. An agent picks them up the next time it starts.`:`Saved. ${who} no longer uses your models. An agent on one of them keeps it until it starts again.`;modelsReceiptKind='settings-success';
|
||||
harnessSettings=null;routesView=null;last.delete('routes');window.MosaicModelDots?.read?.();
|
||||
// P3-2: a failed read before the write changed nothing and says so, with Read again.
|
||||
}catch{if(epoch!==orgEpoch)return;if(sent){modelsReceipt='Outcome unknown. Read the page again before trying.';modelsReceiptKind='banner ref';harnessSettings=null;}else{modelsReceipt='Not changed. Nothing was written.';modelsReceiptKind='banner err';harnessSettings=false;}}
|
||||
// P2-4: the render that shows the outcome moves focus to it (render() below).
|
||||
// Recheck N2: only while the page is still shown; a switch left mid-write does not take focus later.
|
||||
finally{if(epoch===orgEpoch){modelsBusy=false;modelsFocus=!!modelsReceipt&&routeOf().split('?')[0]==='/settings/routing';if(!out())render(harnessSettings===null);}}
|
||||
}
|
||||
document.addEventListener('change',e=>{if(e.target.id==='models-yours'&&e.target.checked)setModels(true);});
|
||||
document.addEventListener('click',e=>{if(e.target.closest?.('#models-stop'))setModels(false);else if(e.target.id==='models-read'){harnessSettings=null;harnessReceipt='';modelsReceipt='';modelsReceiptKind='';render(false);}});
|
||||
document.addEventListener('click',e=>{if(e.target.matches?.('.fallback-toggle'))e.target.closest('tr')?.classList.add('fallback-open');});
|
||||
document.addEventListener('click',e=>{if(e.target.id!=='routes-reload')return;routesDraft=null;routesView=null;routesReceipt='';routesApply=null;last.delete('routes');render(true);});
|
||||
document.addEventListener('submit',async e=>{
|
||||
@@ -1525,6 +1587,9 @@
|
||||
const notify = typeof n.binding==='string' ? `<code>${txt(n.binding)}</code>` : n.refused ? `Could not be read: <code>${txt(n.refused)}</code>` : '<span class="muted">None set</span>';
|
||||
const contents={limits:()=>launchForm(s.launch),routing:()=>routesSection(routes,instances),agents:()=>agentsSection(instances)+agentLevels,assistant:()=>assistantSection(s.launch,names),tokens:()=>tokenLimits(limitRows,limitsRefused)+tokenUsage(usage),names:()=>testerNames(names,instances),about:()=>`<section class="panel"><dl class="kv"><dt>Address</dt><dd><code>${txt(s.address)||'unknown'}</code></dd><dt>Release</dt><dd>${txt(s.release)||'unknown'}</dd><dt>Business</dt><dd>${txt(s.business)||none}</dd><dt>Board</dt><dd><code>${txt(s.board)||'unknown'}</code></dd><dt>Notifier binding</dt><dd>${notify}</dd></dl></section>`};
|
||||
const helpers={limits:'Leave a limit blank for no limit. Zero stops new launches. Running sessions keep running when limits or models change.',routing:"Each agent’s harness, model and thinking, and the fallback a launch takes when the primary harness fails its preflight. A fallback sets all three or none. Running sessions keep their route. The next launch reads the change. A harness is the program that runs the model. Preflight is the check a launch makes before it starts.",agents:'Each agent with its definition, launch membership and runs. A launch refuses until the agent has a route in Routing.',assistant:'The definition stays an assistant one. Holder and credentials stay hand edits.',tokens:'Token limits the bus checks, and token use read from session transcripts.'};
|
||||
// settings-s5 review P2-2: the header says where the models come from only from the plugins read.
|
||||
const riding=harnessSettings?(harnessSettings.plugins?.['model-provider']??[]).includes('cliproxy'):null;
|
||||
if(window.MosaicOrg&&!isTester)helpers.routing=`The models ${modelsWhose()} agents use.${riding===true?' They come from your accounts in Model setup.':riding===false?' They come from its own organization file.':''} ${helpers.routing}`;
|
||||
return settingsShell(page,contents[page](),`${get.oldest()?`Read ${when(get.oldest())}.`: 'Reading…'} ${helpers[page]||''}`);
|
||||
}
|
||||
let settingsFocusDisclosure=false;
|
||||
@@ -1911,6 +1976,11 @@
|
||||
if ((navigated || path !== '/settings/organizations') && orgCleanup) {orgCleanup();orgCleanup=null;}
|
||||
// Leaving a decision discards its confirmation immediately, including a preview in flight.
|
||||
if (answer && (navigated || path !== (answer.org ? '/inbox' : `/inbox/${answer.id}`))) answer = null;
|
||||
// Recheck N1: arriving at Models reads the model service again; a status read on an earlier visit may be out of date.
|
||||
if (navigated && path === '/settings/routing' && (typeof shown === 'string' ? shown.split('?')[0] : shown) !== path && !serviceLoading) serviceState = null;
|
||||
// settings-s5b: a focus request for the Models outcome line belongs to that page. Going anywhere else drops it,
|
||||
// including Assistant, Projects and the board, which return before the focus chain at the end of render.
|
||||
if (navigated && path !== '/settings/routing') modelsFocus = false;
|
||||
const g = ++gen, routeVersion = routesEditVersion;
|
||||
const current = () => g === gen && route === routeOf() && !out();
|
||||
// In organization mode #/ is the cross-organization dashboard; the processing controls stay on the board.
|
||||
@@ -1923,6 +1993,8 @@
|
||||
window.MosaicChat?.leave(chatPath);
|
||||
if (path === '/business') { location.replace('#/settings/business'); return; }
|
||||
if (path === '/settings/display') { location.replace('#/settings/appearance'); return; }
|
||||
// #1873 settings slice 5: Members is gone from the nav; its old addresses land on the organization's Agents page.
|
||||
if (path === '/settings/members') { location.replace(`#/settings/agents${window.MosaicOrg ? `?org=${enc(window.MosaicOrg.id)}` : ''}`); return; }
|
||||
// #1873 settings slice 4: the owner's page is Model setup; its old address lands there, query kept.
|
||||
if (path === '/settings/model-proxy') { location.replace(`#/settings/model-setup${query ? `?${query}` : ''}`); return; }
|
||||
// #1873 SP6: the Assistant page draws its own thread panes (assistant.js); every other page
|
||||
@@ -2046,7 +2118,11 @@
|
||||
sections(path);
|
||||
const h1 = $('bus-view').querySelector('h1');
|
||||
document.title = path === '/settings/business' && $('settings-page-title') ? `Settings · ${window.MosaicOrg ? 'Organization' : 'Business'} · Mosaic Console` : `${h1 ? h1.textContent : 'Console'} · Mosaic Console`;
|
||||
if (k) restore(k);
|
||||
// settings-s5 review P2-4: after a switch on Organization > Models, focus goes to its outcome line.
|
||||
// Recheck N2: a render that paints no outcome line drops the request instead of keeping it.
|
||||
const focusModels = modelsFocus && $('models-receipt'); modelsFocus = false;
|
||||
if (focusModels) { if (k) restore(k); focusModels.focus({ preventScroll: true }); }
|
||||
else if (k) restore(k);
|
||||
// Message on the Board (row 95) lands in the form it opened, addressed to the role.
|
||||
else if (focusSend && $('send-body')) { $('send-body').focus(); announce(`Message to ${draft.to}`); }
|
||||
// SP17: a step link on an organization's Workflows card lands in that step's title field.
|
||||
@@ -2261,6 +2337,7 @@
|
||||
instanceView = null; instanceDrafts = { add: null, change: null }; instanceReceipt = ''; instanceReceiptOp = null; instanceReceipts = Object.create(null);
|
||||
routesView = null; routesDraft = null; routesReceipt = ''; routesEditVersion++;
|
||||
harnessSettings = null; harnessDraft = null; harnessReceipt = '';
|
||||
modelsReceipt = ''; modelsReceiptKind = ''; modelsBusy = false; modelsFocus = false;
|
||||
serviceState = null; serviceLoading = false;
|
||||
// A file draft is keyed by project and path only; the page reload that used to drop it is gone.
|
||||
docKey = null; docStates.clear(); pkAll = []; pkShown = []; pkAt = 0;
|
||||
|
||||
@@ -593,7 +593,8 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
const service = '<div id="proxy-setup-next" class="proxy-setup-next" role="status" aria-live="polite"></div>'
|
||||
+ '<ol id="proxy-setup-progress" class="progress" aria-label="Setup progress" hidden></ol>'
|
||||
+ '<p id="proxy-setup-message" role="status" aria-live="polite"></p>'
|
||||
+ '<details id="proxy-setup-lines" hidden><summary>The service\'s last lines</summary><div class="inside"><p class="small muted">This page can\'t read the service\'s lines yet. What the service said last, when the host reported it:</p><pre id="proxy-setup-log" class="setup-log">Nothing reported.</pre></div></details>'
|
||||
// settings-s5b: the lines come from GET /api/proxy/admin/log, read when the box opens (readLog in the controller).
|
||||
+ '<details id="proxy-setup-lines" class="proxy-setup-lines" hidden><summary>The service\'s last lines</summary><div class="inside" data-log></div></details>'
|
||||
+ '<div id="proxy-setup-fixtures" class="banner mk-note" hidden></div>';
|
||||
const accounts = '<p id="proxy-message" role="status" aria-live="polite">Reading accounts…</p>'
|
||||
+ '<p id="proxy-readonly" class="muted" hidden>This Console is read-only. Sign-in and account changes are unavailable.</p>'
|
||||
@@ -637,12 +638,19 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
+ '<dl id="proxy-setup-settings" class="proxy-setup-state" hidden></dl><p id="proxy-setup-settings-note" class="small muted" hidden>Changing these after setup is not built yet.</p></div></details>';
|
||||
const tech = '<details id="proxy-setup-tech" class="proxy-setup-tech"><summary>Technical details: release, service, history</summary><div class="inside">'
|
||||
+ '<div data-admin hidden><dl id="proxy-setup-state" class="proxy-setup-state"></dl>'
|
||||
// settings-s5b: the setup history and the service's lines, read when they open (owner only). The service key is the last
|
||||
// row of the list above (paintState), and the history follows it, as models-advanced shows them.
|
||||
+ '<div class="proxy-setup-history"><h4 id="proxy-setup-history-title">History</h4><p id="proxy-setup-history-message" class="small muted" role="status">Not read yet.</p>'
|
||||
+ '<ol id="proxy-setup-history" aria-labelledby="proxy-setup-history-title"></ol>'
|
||||
+ '<p class="proxy-setup-history-foot"><button type="button" id="proxy-setup-history-all" aria-expanded="false" aria-controls="proxy-setup-history" hidden>Show all</button>'
|
||||
+ '<button type="button" id="proxy-setup-history-older" hidden>Show older</button><button type="button" id="proxy-setup-history-retry" hidden>Read again</button></p></div>'
|
||||
+ '<details id="proxy-setup-tech-lines" class="proxy-setup-lines"><summary>The service\'s last lines</summary><div class="inside" data-log></div></details>'
|
||||
+ '<div class="proxy-setup-unit"><h4>Service controls</h4><p class="small muted">Install renders the unit the same way as <code>mosaic proxy unit install</code>.</p>'
|
||||
+ '<div class="proxy-setup-buttons"><button type="button" data-unit="install" data-setup-write disabled>Install</button><button type="button" data-unit="start" data-setup-write disabled>Start</button><button type="button" data-unit="restart" data-setup-write disabled>Restart</button><button type="button" data-unit="stop" data-setup-write disabled>Stop</button><button type="button" id="proxy-setup-render">Show unit file</button></div>'
|
||||
+ '<pre id="proxy-setup-unit-file" hidden></pre></div>'
|
||||
+ '<div class="proxy-setup-orgs"><h4>Organizations</h4><div class="table-wrap" tabindex="0" role="region" aria-label="Organizations using the model service"><table><thead><tr><th>Organization</th><th>Proxy</th><th>Catalogue</th><th>Models</th></tr></thead><tbody id="proxy-setup-orgs"></tbody></table></div>'
|
||||
+ '<p id="proxy-setup-own" class="muted"></p></div>'
|
||||
+ '<div class="proxy-setup-receipts"><h4>History</h4><ol id="proxy-setup-receipts"><li class="muted">No setup actions on this page yet.</li></ol></div></div>'
|
||||
+ '</div>'
|
||||
+ '</div></details>';
|
||||
const markup = '<div id="proxy-setup" class="model-setup">'
|
||||
+ card('service', 1, 'Model service', service, true)
|
||||
@@ -957,29 +965,174 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
}
|
||||
const say = (text, kind) => receipt('proxy-setup-message', text, kind);
|
||||
const human = value => value === null || value === undefined ? '—' : typeof value === 'object' ? JSON.stringify(value) : String(value);
|
||||
function addReceipt(record) {
|
||||
if (!record || typeof record !== 'object') return;
|
||||
const list = $('proxy-setup-receipts'); list.querySelector('li.muted')?.remove();
|
||||
const li = document.createElement('li'), op = document.createElement('strong'); op.textContent = record.op ?? 'action';
|
||||
const details = Object.entries(record).filter(([k]) => k !== 'op' && k !== 'time').map(([k, v]) => `${k}: ${human(v)}`).join(', ');
|
||||
li.append(op, ` ${window.MosaicLocalTime(record.time)}${details ? ' — ' + details : ''}`);
|
||||
list.prepend(li);
|
||||
// settings-s5b: History, the service key and the service's lines come from their own reads
|
||||
// (GET /api/proxy/admin/history, service-key and log). Nothing reads them until their box opens,
|
||||
// and only the instance owner's page starts this controller. A write's receipt reads them again
|
||||
// when Technical details is open, so the page never shows a raw receipt.
|
||||
const techOpen = () => $('proxy-setup-tech')?.open === true;
|
||||
function addReceipt() { if (enabled && techOpen()) { readKey(); readHistory(true); } }
|
||||
const plural = (n, word) => `${n} ${word}${n === 1 ? '' : 's'}`;
|
||||
const count = n => Number.isInteger(n) && n > 0 ? n : 0;
|
||||
// A failed read says what the answer's code means here; a request that got no answer says so. A code
|
||||
// shaped like a Mosaic key is not shown, even though it passes admin()'s letters-and-hyphens check.
|
||||
const failText = (error, words) => !error?.code ? (error instanceof TypeError ? 'The Console did not answer.' : String(error?.message ?? 'The Console did not answer.'))
|
||||
: /ms[kotp][-_]/i.test(error.code) ? 'The Console refused the read.' : words[error.code] ? `${words[error.code]} (${error.code})` : error.message;
|
||||
const retryButton = (id, action) => { const button = el('button', 'Read again'); button.type = 'button'; if (id) button.id = id; button.addEventListener('click', action); return button; };
|
||||
// The service's last lines: one read paints every box. The lines show as the answer has them:
|
||||
// the route removes key-shaped text; this page adds nothing and takes nothing away.
|
||||
const LOG_WORDS = { 'not-configured': 'The model service is not installed, so it has no lines yet.', 'outcome-unknown': 'The host could not read the service\'s lines.', 'invalid-request': 'The Console refused the request.' };
|
||||
let logRound = 0, logAnswer = null;
|
||||
function paintLog() {
|
||||
for (const box of root.querySelectorAll('[data-log]')) {
|
||||
if (!logAnswer) { box.replaceChildren(el('p', 'Reading the service\'s lines…', 'small muted')); continue; }
|
||||
// An uninstalled service has no lines: a plain line, not a fault.
|
||||
if (logAnswer.none) { box.replaceChildren(el('p', LOG_WORDS['not-configured'], 'small muted'), retryButton(null, readLog)); continue; }
|
||||
if (logAnswer.error) { box.replaceChildren(el('p', `The service's lines could not be read. ${logAnswer.error}`, 'banner err'), retryButton(null, readLog)); continue; }
|
||||
const { lines, redacted, dropped } = logAnswer, notes = ['Mosaic removes anything that looks like a key.'];
|
||||
if (redacted) notes.push(`It removed something from ${plural(redacted, 'line')}.`);
|
||||
if (dropped) notes.push(`${plural(dropped, 'line')} ${dropped === 1 ? 'was' : 'were'} left out.`);
|
||||
const parts = [el('p', notes.join(' '), 'small muted')];
|
||||
parts.push(lines.length ? el('pre', lines.join('\n'), 'setup-log') : el('p', dropped ? 'No line is left to show.' : 'The service has written nothing yet.', 'small muted'));
|
||||
parts.push(retryButton(null, readLog));
|
||||
box.replaceChildren(...parts);
|
||||
}
|
||||
}
|
||||
// #1873: receipts survive a reload: status carries the last few from proxy-admin.jsonl.
|
||||
function paintReceipts() {
|
||||
if (!Array.isArray(state.receipts) || !state.receipts.length) return;
|
||||
$('proxy-setup-receipts').replaceChildren(); state.receipts.forEach(addReceipt);
|
||||
async function readLog() {
|
||||
if (!enabled) return;
|
||||
// A Read again pressed in a box keeps focus there once the answer is painted.
|
||||
const from = [...root.querySelectorAll('[data-log]')].find(box => box.contains(document.activeElement));
|
||||
const round = ++logRound; logAnswer = null; paintLog();
|
||||
let answer;
|
||||
try {
|
||||
const value = await admin('log?lines=20');
|
||||
if (!value || !Array.isArray(value.lines)) throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
answer = { lines: value.lines.filter(line => typeof line === 'string'), redacted: count(value.redacted), dropped: count(value.dropped) };
|
||||
} catch (error) { answer = error?.code === 'not-configured' ? { none: true } : { error: failText(error, LOG_WORDS) }; }
|
||||
if (round !== logRound || !enabled) return;
|
||||
logAnswer = answer; paintLog();
|
||||
from?.querySelector('button')?.focus();
|
||||
}
|
||||
// When the service key was made. Never the key: the answer has no key, and this shows only its time.
|
||||
// It is the last row of the state list; paintState redraws that list, so the row paints from keyView.
|
||||
const KEY_WORDS = { 'not-configured': 'Model service setup is not available on this Console.', 'outcome-unknown': 'The host could not check the key.' };
|
||||
let keyRound = 0, keyView = { text: 'Not read yet.', failed: false };
|
||||
function paintKey() {
|
||||
const dd = $('proxy-setup-keymade-text')?.parentElement; if (!dd) return;
|
||||
const line = el(keyView.failed ? 'p' : 'span', keyView.text, keyView.failed ? 'banner err' : keyView.reading ? 'muted' : undefined); line.id = 'proxy-setup-keymade-text';
|
||||
dd.replaceChildren(line, ...(keyView.failed ? [retryButton('proxy-setup-keymade-retry', readKey)] : []));
|
||||
}
|
||||
function keyRow(dl) {
|
||||
const dt = document.createElement('dt'), dd = document.createElement('dd'), span = el('span'); span.id = 'proxy-setup-keymade-text';
|
||||
dt.textContent = 'Service key'; dd.append(span); dl.append(dt, dd); paintKey();
|
||||
}
|
||||
async function readKey() {
|
||||
if (!enabled) return;
|
||||
const round = ++keyRound, refocus = document.activeElement?.id === 'proxy-setup-keymade-retry';
|
||||
keyView = { text: 'Reading…', failed: false, reading: true }; paintKey();
|
||||
let text, failed = false;
|
||||
try {
|
||||
const value = await admin('service-key');
|
||||
if (!value || typeof value !== 'object') throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
const renewed = value.renewal !== 'not-built' && Number.isFinite(Date.parse(value.renewedAt)) ? ` Last renewed ${window.MosaicLocalTime(value.renewedAt)}.` : ' Renewing it is not built yet.';
|
||||
if (value.problem === 'missing') text = 'Not made yet. Set up model service in step 1 makes it. You never see it.';
|
||||
else if (value.problem === 'unreadable') text = 'Mosaic can\'t tell when it was made: the service\'s settings file is not one it can check. You never see the key.';
|
||||
else if (Number.isFinite(Date.parse(value.madeAt))) text = `Mosaic made it on ${window.MosaicLocalTime(value.madeAt)}. You never see it.${renewed}`;
|
||||
else text = `When it was made is not known. You never see it.${renewed}`;
|
||||
} catch (error) { failed = true; text = `When it was made could not be read. ${failText(error, KEY_WORDS)}`; }
|
||||
if (round !== keyRound || !enabled) return;
|
||||
keyView = { text, failed }; paintKey();
|
||||
if (refocus) { if (failed) $('proxy-setup-keymade-retry')?.focus(); else { const line = $('proxy-setup-keymade-text'); line.tabIndex = -1; line.focus(); } }
|
||||
}
|
||||
// Setup history, a page at a time. The first page holds the newest entries, oldest first within
|
||||
// it; the list shows newest first. A page can be empty and still name an older one.
|
||||
const HISTORY_WORDS = { 'not-configured': 'Model service setup is not available on this Console.', 'outcome-unknown': 'The host could not read the history.', 'invalid-request': 'The Console refused that page.' };
|
||||
const FEW = 3;
|
||||
const hist = { entries: [], next: null, loading: false, expanded: false, error: null, emptyPage: false, round: 0, read: false };
|
||||
const orgName = id => { const label = typeof id === 'string' ? window.MosaicOrg?.row?.(id)?.label : null; return typeof label === 'string' && label ? label : null; };
|
||||
const providerOf = p => typeof p === 'string' && [...providerSelect.options].some(o => o.value === p) ? providerShort(p) : null;
|
||||
// A name shown as written, unless it holds text shaped like a Mosaic key.
|
||||
const shownName = v => typeof v === 'string' && v && v.length <= 64 && !/ms[kotp][-_]/i.test(v) ? v : null;
|
||||
function historyText(entry) {
|
||||
if (!entry || typeof entry !== 'object' || entry.withheld === true) return null;
|
||||
const org = orgName(entry.org), provider = providerOf(entry.provider), some = org ?? 'An organization';
|
||||
const n = (v, word) => Number.isInteger(v) && v >= 0 ? plural(v, word) : null;
|
||||
const labels = {
|
||||
'account.proxy.apikey.add': provider ? `${provider} account added` : 'Account added',
|
||||
'account.proxy.login.complete': 'Signed in to an account',
|
||||
'account.proxy.priority.set': Number.isInteger(entry.priority) ? `Account order changed, priority ${entry.priority}` : 'Account order changed',
|
||||
'account.proxy.group.models.set': n(entry.models, 'model') ? `Models chosen for an account, ${n(entry.models, 'model')}` : 'Models chosen for an account',
|
||||
fetch: 'Release downloaded and checked', init: 'Set up', 'init-lock.clear': 'Stale setup lock cleared', 'fence.clear': 'Key writes resumed',
|
||||
'unit.install': 'Service installed', 'unit.start': 'Started', 'unit.stop': 'Stopped', 'unit.restart': 'Restarted',
|
||||
'ride-all': 'Organizations switched to your models', 'ride-move': `${some} switched to your models`, 'ride-intent': `Switching ${org ?? 'an organization'} to your models`,
|
||||
'ride-not-added': `${some} was not switched to your models`, 'ride-removed': `${some} stopped using your models`,
|
||||
};
|
||||
const label = typeof entry.op === 'string' && Object.hasOwn(labels, entry.op) ? labels[entry.op] : 'Other setup action';
|
||||
const inOrg = org && String(entry.op).startsWith('account.') ? ` in ${org}` : '';
|
||||
const user = entry.user === '[redacted]' ? 'someone whose name Mosaic hid' : shownName(entry.user);
|
||||
return `${label}${inOrg} · ${window.MosaicLocalTime(entry.time)}${user ? ` · by ${user}` : ''}`;
|
||||
}
|
||||
function paintHistory() {
|
||||
const list = $('proxy-setup-history'), msg = $('proxy-setup-history-message'), all = $('proxy-setup-history-all'), older = $('proxy-setup-history-older'), retry = $('proxy-setup-history-retry');
|
||||
list.replaceChildren(...hist.entries.map((entry, i) => {
|
||||
const text = historyText(entry), li = el('li', text ?? 'Withheld. The Console kept this entry back because it may hold a secret.', text ? undefined : 'muted');
|
||||
if (!text) li.dataset.withheld = '';
|
||||
li.hidden = !hist.expanded && i >= FEW; return li;
|
||||
}));
|
||||
const more = hist.next !== null;
|
||||
// Show all only when some entries are folded away; Show older whenever there is an older page and
|
||||
// nothing read is folded away, so an empty page offers it at once. After an empty page it stays while
|
||||
// the list is folded, since the line below names it.
|
||||
all.hidden = !hist.read || hist.entries.length <= FEW;
|
||||
all.textContent = hist.expanded ? 'Show fewer' : 'Show all'; all.setAttribute('aria-expanded', String(hist.expanded));
|
||||
older.hidden = !more || Boolean(hist.error) || (!hist.expanded && hist.entries.length > FEW && !hist.emptyPage);
|
||||
older.disabled = hist.loading; older.textContent = hist.loading ? 'Reading…' : 'Show older';
|
||||
retry.hidden = !hist.error;
|
||||
msg.className = hist.error ? 'banner err' : 'small muted';
|
||||
msg.textContent = hist.error ? `History could not be read. ${hist.error}`
|
||||
: hist.loading && !hist.entries.length ? 'Reading…'
|
||||
: !hist.read ? 'Not read yet.'
|
||||
: hist.emptyPage && more ? 'That page of history had no entries to show. Show older reads further back.'
|
||||
: !hist.entries.length && !more ? 'No setup history yet.' : '';
|
||||
}
|
||||
async function readHistory(reset) {
|
||||
if (!enabled || (hist.loading && !reset)) return;
|
||||
if (reset) Object.assign(hist, { entries: [], next: null, expanded: false, read: false, emptyPage: false });
|
||||
const round = ++hist.round, before = hist.next, from = ['proxy-setup-history-older', 'proxy-setup-history-retry'].find(id => document.activeElement?.id === id);
|
||||
Object.assign(hist, { loading: true, error: null }); paintHistory();
|
||||
try {
|
||||
const value = await admin(`history?limit=25${before === null ? '' : `&before=${encodeURIComponent(before)}`}`);
|
||||
const next = value?.next;
|
||||
// An older page is further back than the one asked for; one that is not would repeat entries.
|
||||
if (!value || !Array.isArray(value.entries) || !(next === null || (Number.isInteger(next) && next >= 0 && (before === null || next < before)))) throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
if (round !== hist.round || !enabled) return;
|
||||
hist.entries.push(...value.entries.slice().reverse());
|
||||
Object.assign(hist, { next, read: true, emptyPage: value.entries.length === 0 });
|
||||
} catch (error) { if (round !== hist.round || !enabled) return; hist.error = failText(error, HISTORY_WORDS); }
|
||||
hist.loading = false; paintHistory();
|
||||
// The button pressed keeps focus while it stays; when it goes, focus lands on Read again, Show older,
|
||||
// Show fewer or the heading, in that order.
|
||||
if (from) { const back = [$(from), $('proxy-setup-history-retry'), $('proxy-setup-history-older'), $('proxy-setup-history-all')].find(b => !b.hidden); if (back) back.focus(); else { const h = $('proxy-setup-history-title'); h.tabIndex = -1; h.focus(); } }
|
||||
}
|
||||
$('proxy-setup-history-all').addEventListener('click', () => { hist.expanded = !hist.expanded; paintHistory(); });
|
||||
// Show older shows what it reads: the list stays open from then on.
|
||||
$('proxy-setup-history-older').addEventListener('click', () => { if (!hist.loading && hist.next !== null) { hist.expanded = true; readHistory(false); } });
|
||||
$('proxy-setup-history-retry').addEventListener('click', () => readHistory(!hist.read));
|
||||
$('proxy-setup-tech').addEventListener('toggle', () => { if (techOpen() && enabled) { readKey(); readHistory(true); } });
|
||||
$('proxy-setup-tech-lines').addEventListener('toggle', () => { if ($('proxy-setup-tech-lines').open && enabled) readLog(); });
|
||||
function row(dl, term, text) { const dt = document.createElement('dt'), dd = document.createElement('dd'); dt.textContent = term; dd.textContent = text; dl.append(dt, dd); }
|
||||
function paintState() {
|
||||
// #1873 settings slice 5: the timeouts init chose, from the same read. Null is not set in
|
||||
// proxy.json, so the model service uses its default.
|
||||
const timeouts = s => !s || typeof s !== 'object' ? '' : `, wait for a first answer${s.firstOutputSeconds == null ? ': the default' : ` ${human(s.firstOutputSeconds)} seconds`}, old key kept${s.rotateGraceMinutes == null ? ' after a new one: the default' : ` ${human(s.rotateGraceMinutes)} minutes after a new one`}`;
|
||||
const { release, init, unit, fence, initLock } = state, dl = $('proxy-setup-state'); dl.replaceChildren();
|
||||
row(dl, 'Release', !release.fetched ? `Not fetched (pinned ${release.version}, panel ${release.panelVersion})` : release.verified ? `Fetched ${release.version}, panel ${release.panelVersion}, hashes verified` : `Fetched ${release.version}, hashes not verified (${release.problem})`);
|
||||
const files = init.files.filter(f => f.present).map(f => f.label).join(', ') || 'none';
|
||||
row(dl, 'Initialized', init.initialized ? `Yes, port ${init.settings?.port ?? '—'}` : init.partial ? `Partly (${init.problem ?? 'incomplete'}). Present: ${files}` : init.problem ? `No (${init.problem})` : 'No');
|
||||
row(dl, 'Initialized', init.initialized ? `Yes, port ${init.settings?.port ?? '—'}${timeouts(init.settings)}` : init.partial ? `Partly (${init.problem ?? 'incomplete'}). Present: ${files}` : init.problem ? `No (${init.problem})` : 'No');
|
||||
row(dl, 'Service', `${unit.unit}: ${unit.installed ? 'installed' : 'not installed'}, ${unit.active}${unit.pid ? `, pid ${unit.pid}` : ''}${unit.port ? `, port ${unit.port} ${unit.listening ? 'answering' : 'not answering'}` : ''}${unit.error ? ` (${unit.error})` : ''}`);
|
||||
row(dl, 'Fence', !fence.set ? 'Clear' : fence.invalid ? 'Set (unreadable fence file)' : `Set after ${fence.op ?? 'a key write'}${fence.org ? ` for ${fence.org}` : ''}${fence.time ? ` at ${window.MosaicLocalTime(fence.time)}` : ''}`);
|
||||
row(dl, 'Init lock', !initLock.present ? 'None' : initLock.clearable ? 'Stale: the bus is stopped and no model service files exist' : `Held: ${initLock.blockers.join('; ')}`);
|
||||
if (state.restart?.confirmed) row(dl, 'Last restart', `Confirmed at ${window.MosaicLocalTime(state.restart.observedAt)}, new pid ${state.restart.pid}`);
|
||||
if (enabled) keyRow(dl);
|
||||
}
|
||||
// settings-p3 (re-check 3 P3-1): where the hand steps for a held init lock or a partial init are written.
|
||||
const README_STEPS = ['The hand steps for a stale init lock and a partial init are in ', 'packages/model-proxy/README.md'];
|
||||
@@ -1031,8 +1184,8 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
if (failed && (CHAIN.includes(next) || next === 'unit-restart')) {
|
||||
setCard('service', 'bad', `${failed.reason} The steps that finished are kept. No organization was touched.`, { badge: ['badge-danger', 'Setup stopped'] });
|
||||
box.replaceChildren(el('p', 'Try again starts from the step that failed.', 'small muted'), goButton('Try again', next, !sendOk || Boolean(state.busy), runChain));
|
||||
paintProgress(true); lines.hidden = false; lines.open = true;
|
||||
$('proxy-setup-log').textContent = state.unit?.error ? String(state.unit.error) : 'Nothing reported.';
|
||||
paintProgress(true);
|
||||
if (lines.hidden) { lines.hidden = false; lines.open = true; readLog(); }
|
||||
return;
|
||||
}
|
||||
lines.hidden = true;
|
||||
@@ -1118,7 +1271,16 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
function paintOrgsCard() {
|
||||
const line = $('setup-step-organizations').querySelector('[data-step-summary]'), go = $('proxy-ride-go');
|
||||
go.className = 'btn'; delete line.dataset.next;
|
||||
if (!running() || !accountCount) { setCard('organizations', 'wait', 'Opens after your first account.'); return; }
|
||||
// settings-s5: before the first account, an organization the sidebar already marks "can't start" is
|
||||
// named here, so the card and the mark agree.
|
||||
if (!running() || !accountCount) {
|
||||
const blocked = running() ? cantStart().map(f => nameOf(f.id)) : [];
|
||||
if (!blocked.length) { setCard('organizations', 'wait', 'Opens after your first account.'); return; }
|
||||
const one = blocked.length === 1;
|
||||
// The card stays closed (Move needs a model from an account), with the mark's word on its badge.
|
||||
setCard('organizations', 'wait', `Opens after your first account. ${listNames(blocked)} can't start now: ${one ? 'it uses' : 'they use'} stand-in models and ${one ? 'its bus is' : 'their buses are'} stopped. After your first account, move ${one ? 'it' : 'them'} to your models under Use your models in every organization, then start ${one ? 'its bus' : 'their buses'}.`, { badge: ['badge-danger', "Can't start"] });
|
||||
return;
|
||||
}
|
||||
const step = orgStep(), names = needRestart().filter(restartHelps).map(o => nameOf(o.id));
|
||||
// settings-p3 (rebase re-check P3-1, settings-s4 P3): the Ride all box still lists an organization,
|
||||
// so not "every". One that turned the model service off is not waiting on anything: it is named.
|
||||
@@ -1200,7 +1362,11 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
// After init, Advanced shows the settings init wrote, read-only.
|
||||
const dl = $('proxy-setup-settings'), s = init.initialized && init.settings && typeof init.settings === 'object' ? init.settings : null;
|
||||
dl.hidden = !s; $('proxy-setup-settings-note').hidden = !s; dl.replaceChildren();
|
||||
if (s) { row(dl, 'Port', human(s.port)); if (s.firstOutputSeconds !== undefined) row(dl, 'Wait for a first answer', `${human(s.firstOutputSeconds)} seconds`); }
|
||||
if (s) {
|
||||
row(dl, 'Port', human(s.port));
|
||||
if (s.firstOutputSeconds !== undefined) row(dl, 'Wait for a first answer', s.firstOutputSeconds === null ? 'The default' : `${human(s.firstOutputSeconds)} seconds`);
|
||||
if (s.rotateGraceMinutes !== undefined) row(dl, 'Old key kept after a new one', s.rotateGraceMinutes === null ? 'The default' : `${human(s.rotateGraceMinutes)} minutes`);
|
||||
}
|
||||
}
|
||||
// Programs that drive models: the harnesses this Console offers an organization, read on open.
|
||||
let programsRead = false;
|
||||
@@ -1255,15 +1421,15 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
+ 'Admission gets stricter there: every launch route is checked, direct Anthropic routes too, and while the model service is down or fenced every launch in that organization is refused. '
|
||||
+ (ready ? `${ready} ${ready === 1 ? 'organization can' : 'organizations can'} ride now.` : 'No organization is waiting to ride.');
|
||||
const go = $('proxy-ride-go'); go.disabled = !sendOk || Boolean(state.busy) || !ready;
|
||||
$('proxy-ride-skips').replaceChildren(...rideSkips().map(s => li(`Skipped ${s.id}: ${rideReason(s.reason)}`)));
|
||||
$('proxy-ride-skips').replaceChildren(...rideSkips().map(s => li(`Skipped ${nameOf(s.id)}: ${rideReason(s.reason)}`)));
|
||||
const listedFixtures = rideResult?.fixtures ?? fixtures.map(f => ({ id: f.id, running: f.running }));
|
||||
$('proxy-ride-fixtures').replaceChildren(...listedFixtures.map(f => {
|
||||
// running null: no bus status here (the single-business Console), so moving is not available.
|
||||
const name = nameOf(f.id);
|
||||
if (f.running === null) return li(`${name} uses stand-in models. Moving it is not available on this Console.`);
|
||||
// settings-s4 review F2: running false is what the sidebar marks "can't start"; say so, and the fix.
|
||||
const item = li(f.running === false ? `${name} can't start: it uses stand-in models and its bus is stopped. Move it to the owner's models, then start its bus. `
|
||||
: `${name} uses stand-in models, can be moved to the owner's models.${f.running === true ? ' Its bus is running: stop it first.' : ''} `);
|
||||
const item = li(f.running === false ? `${name} can't start: it uses stand-in models and its bus is stopped. Move it to your models under Use your models in every organization, then start its bus. `
|
||||
: `${name} uses stand-in models, can be moved to your models.${f.running === true ? ' Its bus is running: stop it first.' : ''} `);
|
||||
const move = document.createElement('button'); move.type = 'button'; move.className = 'btn'; move.textContent = `Move ${name}`;
|
||||
move.dataset.rideMove = f.id; move.disabled = !sendOk || Boolean(state.busy);
|
||||
move.addEventListener('click', () => { if (!move.disabled) rideMove(f.id); });
|
||||
@@ -1281,22 +1447,22 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
});
|
||||
};
|
||||
const rideMove = id => {
|
||||
if (!window.confirm(`Move ${id} to your models? Its stand-in models are replaced by a model your model service lists. After this, its agents spend real tokens on your account. Its bus stays stopped: start it when you are ready.`)) return;
|
||||
if (!window.confirm(`Move ${nameOf(id)} to your models? Its stand-in models are replaced by a model your model service lists. After this, its agents spend real tokens on your account. Its bus stays stopped: start it when you are ready.`)) return;
|
||||
return act('ride-move', async () => {
|
||||
const result = await admin('ride-move', { org: id });
|
||||
if (!result.moved) { rideSay(`${id} was not moved. ${rideReason(result.reason)}`, 'error'); return; }
|
||||
if (!result.moved) { rideSay(`${nameOf(id)} was not moved. ${rideReason(result.reason)}`, 'error'); return; }
|
||||
addReceipt(result.receipt);
|
||||
if (rideResult) rideResult = { ...rideResult, fixtures: rideResult.fixtures.filter(f => f.id !== id) };
|
||||
rideSay(`${id} now uses ${result.model}. Start its bus when you are ready. It spends real tokens from now on.`, 'success');
|
||||
rideSay(`${nameOf(id)} now uses ${result.model}. Start its bus when you are ready. It spends real tokens from now on.`, 'success');
|
||||
});
|
||||
};
|
||||
$('proxy-ride-go').addEventListener('click', () => { if (!$('proxy-ride-go').disabled) rideAll(); });
|
||||
function paint() {
|
||||
paintState(); paintControls(); paintReceipts(); paintService(); paintRestart(); paintOrgsCard(); paintTriageCard(); paintAccountCard();
|
||||
paintState(); paintControls(); paintService(); paintRestart(); paintOrgsCard(); paintTriageCard(); paintAccountCard();
|
||||
const job = state.fetch;
|
||||
if (job.state === 'running') say(`Downloading: ${STEPS[job.step] ?? job.step ?? 'starting'}…`);
|
||||
else if (job.state === 'failed' && seenFetch !== 'failed') { say(`Fetch failed. ${explain(job.error)}`, 'error'); seenFetch = 'failed'; }
|
||||
else if (job.state === 'done' && job.receipt && seenFetch !== job.receipt.time) { seenFetch = job.receipt.time; if (!state.receipts?.some(r => r.op === 'fetch' && r.time === job.receipt.time)) addReceipt(job.receipt); say(`Fetched ${job.receipt.version}; archive, panel and executable hashes verified.`, 'success'); }
|
||||
else if (job.state === 'done' && job.receipt && seenFetch !== job.receipt.time) { seenFetch = job.receipt.time; addReceipt(job.receipt); say(`Fetched ${job.receipt.version}; archive, panel and executable hashes verified.`, 'success'); }
|
||||
clearTimeout(timer); timer = null;
|
||||
if (enabled && !chain?.running && (job.state === 'running' || state.busy)) timer = setTimeout(load, 1000);
|
||||
// The sidebar's model marks read the same status (MosaicModelDots).
|
||||
@@ -1494,5 +1660,6 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
document.addEventListener('mosaic-org-restarted', () => { read(); });
|
||||
const begin = () => { (window.MosaicOrg?.ready ?? Promise.resolve()).then(read, () => {}); };
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', begin, { once: true }); else begin();
|
||||
window.MosaicModelDots = { get: id => marks.get(id) ?? null };
|
||||
// settings-s5: a page that changes what the status says (an organization's Models choice) asks for a fresh read.
|
||||
window.MosaicModelDots = { get: id => marks.get(id) ?? null, read };
|
||||
})();
|
||||
@@ -119,7 +119,10 @@ table.s1-table td code{font-size:12.5px;white-space:nowrap}
|
||||
.proxy-setup-next{display:flex;flex-wrap:wrap;align-items:center;gap:8px 16px;margin:0 0 12px}.proxy-setup-next p{margin:0;flex:1 1 280px}
|
||||
.proxy-setup-buttons{display:flex;flex-wrap:wrap;gap:8px}.proxy-setup-buttons button{min-height:44px}
|
||||
#proxy-setup pre{overflow-x:auto;max-width:100%;font-size:12px}#proxy-setup h3{margin:16px 0 4px}#proxy-setup details{margin:0 0 12px}#proxy-setup summary{min-height:44px;display:flex;align-items:center;cursor:pointer}
|
||||
#proxy-setup-receipts{padding-left:20px;overflow-wrap:anywhere;font-size:13px}
|
||||
/* settings-s5b: the setup history (GET /api/proxy/admin/history), the service key and the service's lines. */
|
||||
#proxy-setup-history{list-style:none;padding-left:0;overflow-wrap:anywhere;font-size:13px;margin:0 0 8px}#proxy-setup-history li{padding:2px 0}
|
||||
.proxy-setup-history-foot{display:flex;flex-wrap:wrap;gap:8px;margin:0}.proxy-setup-history-foot button,[data-log] button,#proxy-setup-keymade-retry{min-height:44px}
|
||||
[data-log] .setup-log{margin:8px 0}[data-log] p{margin:0 0 8px}#proxy-setup-history-message:empty{display:none}
|
||||
/* #1873 settings slice 1 (D3, D4): the kept restart list and Technical details. */
|
||||
.proxy-setup-restart{margin:0 0 12px}
|
||||
.proxy-setup-restart-list{list-style:none;margin:8px 0 0;padding:0;display:grid;gap:8px}
|
||||
@@ -339,6 +342,7 @@ button:disabled,[aria-disabled=true]{cursor:not-allowed}
|
||||
.rows li{display:grid;grid-template-columns:7rem minmax(0,1fr) auto;gap:6px var(--gap,12px);align-items:center;padding:10px 0;border-top:1px solid var(--line)}
|
||||
.rows li>span:first-child{font-weight:600;color:var(--strong)}
|
||||
.rows li>.account-reason{grid-column:2/-1;margin:0}
|
||||
.rows li.muted{display:block}
|
||||
.progress{list-style:none;margin:var(--gap,12px) 0 0;padding:0;display:grid;gap:6px}
|
||||
.progress li::before{display:inline-block;width:1.4em;color:var(--muted)}
|
||||
.progress .ok::before{content:"✓";color:var(--success)}.progress .run::before{content:"●";color:var(--action)}.progress .todo::before{content:"○"}.progress .bad::before{content:"✕";color:var(--danger)}
|
||||
@@ -375,6 +379,19 @@ button:disabled,[aria-disabled=true]{cursor:not-allowed}
|
||||
.settings-content fieldset.harness-control{display:flex;flex-wrap:wrap;align-items:center;gap:6px 12px}
|
||||
.settings-content fieldset.harness-control>legend{flex-basis:100%}
|
||||
.settings-content fieldset.harness-control>:is(button,p){flex-basis:100%}
|
||||
/* #1873 settings slice 5: an organization's one choice (mockup org-models). A choice not built yet is dashed and disabled. */
|
||||
/* settings-s5 review P3-4: the choice cards sit in the form column, as .settings-form does. */
|
||||
.settings-content fieldset.models-source{display:grid;gap:8px;max-width:28rem}
|
||||
.settings-content fieldset.models-source>legend{font-size:14px;color:var(--strong);margin-bottom:8px}
|
||||
.settings-content fieldset.models-source>p{margin:0}
|
||||
.choice{display:grid;grid-template-columns:auto 1fr auto;gap:4px 10px;align-items:start;padding:12px;border:1px solid var(--border);border-radius:var(--r);cursor:pointer}
|
||||
.choice input{margin-top:4px}
|
||||
.choice:has(input:checked){border-color:var(--action);background:var(--raised)}
|
||||
.choice:has(input:disabled){cursor:default}
|
||||
.choice.off{color:var(--muted);cursor:not-allowed;border-style:dashed;background:transparent}
|
||||
.choice strong{color:var(--strong)}.choice.off strong{color:var(--muted)}
|
||||
.choice .small{grid-column:2/4;margin:0}
|
||||
.choice:has(input:focus-visible){outline:2px solid var(--focus);outline-offset:2px}
|
||||
.settings-content :is(.instance-form,.level-form) textarea,.settings-content .assistant-form textarea{display:block;width:100%}
|
||||
.route-definition{display:none}.settings-spinner{display:inline-block;width:16px;height:16px;border:2px solid currentColor;border-right-color:transparent;border-radius:50%;vertical-align:middle;animation:settings-spin .8s linear infinite}@keyframes settings-spin{to{transform:rotate(360deg)}}@media(prefers-reduced-motion:reduce){.settings-spinner{animation:none}}
|
||||
@media(max-width:759px){#routes-table .route-definition{display:inline;float:right;color:var(--muted);font-size:12px;font-weight:400}}
|
||||
|
||||
@@ -1051,7 +1051,8 @@ export async function startServer({ host = '127.0.0.1', port = 7330, board = DEF
|
||||
origin: sso ? (localOrigin ?? sso.publicOrigin) : `https://${preview.host}`, user: signer?.user,
|
||||
principal: currentSession ? { provider: currentSession.provider, iss: currentSession.iss, sub: currentSession.sub } : null, log });
|
||||
if (path.startsWith('/api/proxy/')) return proxyHTTP(req, res, { path, call: proxyCall, named, tester, granted,
|
||||
send, origin: sso ? (localOrigin ?? sso.publicOrigin) : `https://${preview.host}`, user: signer?.user, log });
|
||||
send, origin: sso ? (localOrigin ?? sso.publicOrigin) : `https://${preview.host}`, user: signer?.user, log,
|
||||
record: proxyAdmin && typeof proxyAdmin.recordAccount === 'function' ? entry => proxyAdmin.recordAccount({ ...entry, org: orgId }) : null });
|
||||
}
|
||||
// #1873 triage-host: the Triage session's two routes answer only the instance owner's own session on
|
||||
// the public name, with no organization selected. Anyone else falls through to the answer an
|
||||
|
||||
@@ -185,6 +185,17 @@ test('dev owner resolves a decision through the real Console wrapper and bus', {
|
||||
const result=await post(`/api/orgs/acme/decisions/${decision.id}/resolve`,{choice:'yes',confirmation:confirmation.body.confirmation});
|
||||
assert.equal(result.status,200,JSON.stringify(result));
|
||||
assert.equal(result.body.choice,'yes');
|
||||
// settings-backend A2: the real Console's riders carry the organization file, so turning a program
|
||||
// off for every organization can read it; without it each organization answers unreadable.
|
||||
const get=path=>new Promise((resolve,reject)=>{const host=`127.0.0.1:${port}`;const req=request({host:'127.0.0.1',port,path,headers:{host}},res=>{let raw='';res.on('data',c=>raw+=c);res.on('end',()=>resolve({status:res.statusCode,body:JSON.parse(raw)}));});req.on('error',reject);req.end();});
|
||||
const status=await get('/api/orgs/acme/proxy/admin/status');
|
||||
assert.equal(status.status,200,JSON.stringify(status));
|
||||
const acmeOn=status.body.programs?.organizations?.find(o=>o.id==='acme')?.on;
|
||||
assert.ok(Array.isArray(acmeOn)&&acmeOn.length>0,JSON.stringify(status.body.programs));
|
||||
const off=await post('/api/orgs/acme/proxy/admin/programs/off',{name:acmeOn[0]});
|
||||
assert.equal(off.status,200,JSON.stringify(off));
|
||||
// The chief's route uses the program in both organizations, so neither file is written.
|
||||
assert.deepEqual(off.body.results,[{id:'acme',outcome:'in-use'},{id:'beta',outcome:'in-use'}],JSON.stringify(off.body));
|
||||
const stopped = new Promise(resolve => bus.once('exit', resolve));
|
||||
const stop = spawnSync(process.execPath,['packages/cli/src/cli.mjs','bus','stop','acme'],{env,encoding:'utf8'}); assert.equal(stop.status,0,stop.stderr); await stopped;
|
||||
busOut=''; bus=spawn(process.execPath,['packages/cli/src/cli.mjs','bus','start','acme','--no-launch'],{env,stdio:['ignore','pipe','pipe']});
|
||||
|
||||
@@ -20,7 +20,13 @@ const ROUTES = [
|
||||
['fence/clear', 'POST', {}], ['init-lock/clear', 'POST', {}],
|
||||
// #1873 ride-default: Ride all, and the move of one stopped fixture organization.
|
||||
['ride-all', 'POST', {}], ['ride-move', 'POST', { org: 'stand-in' }],
|
||||
// #1873 settings-backend A1: the model service log, the service key's made-at time, setup history.
|
||||
['log', 'GET'], ['service-key', 'GET'], ['history', 'GET'],
|
||||
// #1873 settings-backend A2: one program off for every organization, and access health.
|
||||
['programs/off', 'POST', { name: 'alpha' }], ['access', 'GET'],
|
||||
];
|
||||
const A2 = ROUTES.filter(([p]) => ['programs/off', 'access'].includes(p));
|
||||
const READS = ROUTES.filter(([p]) => ['log', 'service-key', 'history'].includes(p));
|
||||
|
||||
function fakeAdmin() {
|
||||
const calls = [];
|
||||
@@ -33,6 +39,12 @@ function fakeAdmin() {
|
||||
fetchStart: () => { calls.push(['fetchStart']); return { state: 'running' }; },
|
||||
init: async (call, args) => { calls.push(['init', args]); await call('proxy.init', args); return { settings: { port: 18432 }, receipt: { op: 'init' } }; },
|
||||
fenceClear: async call => { calls.push(['fenceClear']); return call('proxy.fence.clear', { evidence: {} }); },
|
||||
log: async args => { calls.push(['log', args]); return { unit: 'mosaic-proxy.service', lines: ['ok'], redacted: 0, dropped: 0 }; },
|
||||
serviceKey: () => { calls.push(['serviceKey']); return { madeAt: '2026-11-01T08:00:00.000Z', renewedAt: null, renewal: 'not-built', problem: null }; },
|
||||
history: args => { calls.push(['history', args]); return { entries: [], next: null }; },
|
||||
recordAccount: entry => { calls.push(['recordAccount', entry]); return true; },
|
||||
programsOff: async args => { calls.push(['programsOff', args]); return { name: args.name, results: [], receipt: null }; },
|
||||
access: async () => { calls.push(['access']); return { organizations: [] }; },
|
||||
};
|
||||
}
|
||||
const send = (port, path, { method = 'GET', headers = {}, body } = {}) => new Promise((resolve, reject) => {
|
||||
@@ -54,7 +66,7 @@ async function devServer(t, options = {}) {
|
||||
}
|
||||
const hit = (f, route, extra = {}) => send(f.port, `/api/proxy/admin/${route[0]}`, { method: route[1], headers: { ...f.headers(), ...extra }, body: route[2] });
|
||||
|
||||
test('the route table is exactly the brief: status, render, fetch, init, four unit verbs, fence clear, lock clear, ride all, ride move', () => {
|
||||
test('the route table is exactly the brief: status, render, fetch, init, four unit verbs, fence clear, lock clear, ride all, ride move, log, service key, history, programs off, access', () => {
|
||||
assert.deepEqual(PROXY_ADMIN_PATHS, ROUTES.map(([p]) => `/api/proxy/admin/${p}`));
|
||||
});
|
||||
|
||||
@@ -64,7 +76,8 @@ test('the dev local owner reaches every route; init and fence clear go to the bu
|
||||
const r = await hit(f, route);
|
||||
assert.equal(r.status, route[0] === 'fetch' ? 202 : 200, route[0]);
|
||||
}
|
||||
assert.deepEqual(f.admin.calls.map(c => c[0]), ['status', 'render', 'fetchStart', 'init', 'unitAction', 'unitAction', 'unitAction', 'unitAction', 'fenceClear', 'initLockClear', 'rideAll', 'rideMove']);
|
||||
assert.deepEqual(f.admin.calls.map(c => c[0]), ['status', 'render', 'fetchStart', 'init', 'unitAction', 'unitAction', 'unitAction', 'unitAction', 'fenceClear', 'initLockClear', 'rideAll', 'rideMove', 'log', 'serviceKey', 'history', 'programsOff', 'access']);
|
||||
assert.deepEqual(f.admin.calls.find(c => c[0] === 'programsOff')[1], { name: 'alpha', user: 'Local operator' });
|
||||
assert.deepEqual(f.admin.calls.filter(c => c[0] === 'unitAction').map(c => c[1]), ['install', 'start', 'stop', 'restart']);
|
||||
assert.deepEqual(f.busCalls.map(c => c[0]), ['proxy.init', 'proxy.fence.clear']);
|
||||
assert.deepEqual(f.busCalls[0][1], { generate: true });
|
||||
@@ -245,3 +258,251 @@ test('a signed-in member who is not the instance owner gets human-required for r
|
||||
}
|
||||
assert.deepEqual(member.admin.calls, []);
|
||||
});
|
||||
|
||||
// #1873 settings-backend A1: the three reads. Query keys are each read's own, once each, plain
|
||||
// decimal numbers; the controller clamps the values.
|
||||
test('the reads pass their query to the controller and refuse an unknown, repeated or non-number key', async t => {
|
||||
const f = await devServer(t);
|
||||
const get = path => send(f.port, path, { headers: f.headers() });
|
||||
assert.equal((await get('/api/proxy/admin/log')).status, 200);
|
||||
assert.equal((await get('/api/proxy/admin/log?lines=1000')).status, 200);
|
||||
assert.equal((await get('/api/proxy/admin/history')).status, 200);
|
||||
assert.equal((await get('/api/proxy/admin/history?limit=1000&before=4096')).status, 200);
|
||||
assert.equal((await get('/api/proxy/admin/service-key')).status, 200);
|
||||
assert.deepEqual(f.admin.calls, [['log', {}], ['log', { lines: 1000 }], ['history', { before: null }], ['history', { limit: 1000, before: 4096 }], ['serviceKey']]);
|
||||
f.admin.calls.length = 0;
|
||||
for (const path of ['/api/proxy/admin/log?lines=-1', '/api/proxy/admin/log?lines=1.5', '/api/proxy/admin/log?lines=', '/api/proxy/admin/log?lines=1e3', '/api/proxy/admin/log?lines=5&lines=6',
|
||||
'/api/proxy/admin/log?limit=5', '/api/proxy/admin/log?unit=sshd.service', '/api/proxy/admin/log?lines=0x10', '/api/proxy/admin/log?lines=1234567890123456',
|
||||
'/api/proxy/admin/history?before=abc', '/api/proxy/admin/history?lines=5', '/api/proxy/admin/history?limit=5&limit=6', '/api/proxy/admin/service-key?x=1', '/api/proxy/admin/service-key?key']) {
|
||||
const r = await get(path);
|
||||
assert.equal(r.status, 400, path); assert.deepEqual(r.json, { error: 'invalid-request' }, path);
|
||||
}
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
for (const route of READS) assert.equal((await send(f.port, `/api/proxy/admin/${route[0]}`, { method: 'POST', headers: f.headers(), body: {} })).status, 405, route[0]);
|
||||
});
|
||||
|
||||
test('a read the controller cannot answer reaches the page as its code only', async t => {
|
||||
const f = await devServer(t);
|
||||
f.admin.log = async () => { throw Object.assign(new Error(`${SENTINEL} journal said /home/owner/x`), { code: 'outcome-unknown' }); };
|
||||
f.admin.history = () => { throw Object.assign(new Error(SENTINEL), { code: 'invalid-request' }); };
|
||||
f.admin.serviceKey = () => { throw new Error(SENTINEL); };
|
||||
assert.deepEqual((await hit(f, ['log', 'GET'])).json, { error: 'outcome-unknown' });
|
||||
assert.equal((await hit(f, ['log', 'GET'])).status, 503);
|
||||
assert.deepEqual((await hit(f, ['history', 'GET'])).json, { error: 'invalid-request' });
|
||||
assert.deepEqual((await hit(f, ['service-key', 'GET'])).json, { error: 'outcome-unknown' });
|
||||
f.admin.log = async () => { throw Object.assign(new Error('x'), { code: 'not-configured' }); };
|
||||
assert.deepEqual((await hit(f, ['log', 'GET'])).json, { error: 'not-configured' });
|
||||
assert.equal(f.logs.join('\n').includes(SENTINEL), false);
|
||||
});
|
||||
|
||||
test('a member, an organization owner and a request on a name that is not the Console\'s get refused on every read', async t => {
|
||||
const organizations = { list: async () => [], resolve: async id => (id === 'x' ? { busHealthy: true } : null) };
|
||||
for (const session of [{ name: 'Bob', provider: 'local-dev', iss: 'local-dev', sub: 'bob', rootOwner: false, instanceRole: 'member' },
|
||||
{ name: 'Olive', provider: 'local-dev', iss: 'local-dev', sub: 'olive', rootOwner: false, instanceRole: 'operator', orgRole: 'owner' }]) {
|
||||
const f = await devServer(t, { organizations, sso: { ...createDevOwner(), session: () => session } });
|
||||
for (const route of READS) {
|
||||
const inOrg = await send(f.port, `/api/orgs/x/proxy/admin/${route[0]}?lines=5`.replace(/\?lines=5$/, ''), { headers: f.headers() });
|
||||
assert.equal(inOrg.status, 403, `${session.sub} ${route[0]}`); assert.deepEqual(inOrg.json, { error: 'human-required' }, `${session.sub} ${route[0]}`);
|
||||
const bad = await send(f.port, `/api/orgs/x/proxy/admin/${route[0]}?nope=1`, { headers: f.headers() });
|
||||
assert.deepEqual(bad.json, { error: 'human-required' }, 'the caller is checked before the query');
|
||||
const plain = await hit(f, route);
|
||||
assert.equal(plain.status >= 400 && plain.status < 500, true, `${route[0]} outside an organization is refused`);
|
||||
}
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
}
|
||||
const f = await devServer(t);
|
||||
for (const route of READS) {
|
||||
const r = await send(f.port, `/api/proxy/admin/${route[0]}`, { headers: { host: 'console.evil.example' } });
|
||||
assert.equal(r.status, 403, route[0]);
|
||||
}
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
});
|
||||
|
||||
// The real controller over a scratch config and data root with keys planted in proxy.json, the
|
||||
// management key file, the receipt history and the journal: none reaches an answer.
|
||||
test('end to end with the real controller, no planted key reaches any read', async t => {
|
||||
const { createProxyAdmin } = await import('../../model-proxy/src/admin-control.mjs');
|
||||
const { initFiles, appendReceipt } = await import('../../model-proxy/src/admin.mjs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'pa-a1-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
const files = initFiles(configDir, dataRoot);
|
||||
const TAIL = 'Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE';
|
||||
const planted = ['msk-tag-qwertyu', 'mso-tag-acme-qwerty', 'mst-tag-triage-zxcvb', 'msp-tag-plmokn', `msk-k1a2-${TAIL}`, `mso-k1a2-acme-${TAIL}`, `mst-k1a2-triage-${TAIL}`, `msp-k1a2-${TAIL}`];
|
||||
const { mkdirSync, writeFileSync } = await import('node:fs');
|
||||
mkdirSync(join(configDir, 'secrets'), { recursive: true, mode: 0o700 }); mkdirSync(dataRoot, { mode: 0o700 });
|
||||
writeFileSync(files.settings, JSON.stringify({ port: 18432, note: planted[0] }), { mode: 0o600 });
|
||||
writeFileSync(files.key, planted[4], { mode: 0o600 });
|
||||
for (const key of planted) appendReceipt(dataRoot, { time: new Date(0).toISOString(), op: 'init', note: `key ${key}`, [key.replace(/-/g, '_')]: key });
|
||||
const journalCalls = [];
|
||||
const unit = {
|
||||
unit: 'mosaic-proxy-zz.service',
|
||||
status: async () => ({ unit: 'mosaic-proxy-zz.service', installed: true, active: 'active', healthy: true }),
|
||||
log: async (lines, options) => {
|
||||
const { unitLog } = await import('../../model-proxy/src/admin.mjs');
|
||||
return unitLog({ run: async (command, args) => { journalCalls.push([command, ...args]); return { code: 0, stdout: [...planted.map(k => `refused ${k}`), 'auth plainwordsonly', 'served 3 requests'].join('\n') + '\n' }; }, unit: 'mosaic-proxy-zz.service', lines, secrets: options.secrets });
|
||||
},
|
||||
};
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: async () => ({ stopped: true, running: [] }), env: { MOSAIC_PROXY_TOKEN: 'plainwordsonly' } });
|
||||
const f = await devServer(t, { proxyAdmin: admin });
|
||||
const answers = [];
|
||||
for (const path of ['log', 'log?lines=1000', 'service-key', 'history', 'history?limit=2']) {
|
||||
const r = await send(f.port, `/api/proxy/admin/${path}`, { headers: f.headers() });
|
||||
assert.equal(r.status, 200, path);
|
||||
answers.push(r.text);
|
||||
}
|
||||
const all = answers.join('\n') + f.logs.join('\n');
|
||||
for (const bit of [...planted, ...planted.map(k => k.slice(4)), TAIL.slice(0, 10), 'plainwordsonly', root]) assert.equal(all.includes(bit), false, bit);
|
||||
const log = JSON.parse(answers[0]);
|
||||
assert.deepEqual(log.lines.slice(-1), ['served 3 requests']);
|
||||
assert.equal(log.lines.length, planted.length + 1);
|
||||
assert.equal(log.dropped, 1);
|
||||
assert.deepEqual(journalCalls[1].slice(4, 6), ['-n', '200']);
|
||||
const key = JSON.parse(answers[2]);
|
||||
assert.deepEqual(Object.keys(key), ['madeAt', 'renewedAt', 'renewal', 'problem']);
|
||||
assert.equal(key.problem, null); assert.equal(key.renewal, 'not-built');
|
||||
const first = JSON.parse(answers[4]);
|
||||
assert.equal(first.entries.length, 2);
|
||||
const rest = await send(f.port, `/api/proxy/admin/history?limit=100&before=${first.next}`, { headers: f.headers() });
|
||||
assert.equal(rest.json.entries.length, planted.length - 2);
|
||||
assert.equal(rest.json.next, null);
|
||||
assert.equal(rest.text.includes('qwerty'), false);
|
||||
});
|
||||
|
||||
// Account changes made outside the setup page join its history. The bus call's answer is unchanged;
|
||||
// a record that fails adds recorded: false and still answers the bus's result.
|
||||
test('an account write through /api/proxy/* records a labelled entry; reads and login steps record nothing', async t => {
|
||||
const f = await devServer(t);
|
||||
const post = (path, body) => send(f.port, path, { method: 'POST', headers: f.headers(), body });
|
||||
f.busCalls.length = 0;
|
||||
const added = await post('/api/proxy/apikey/add', { provider: 'openai', key: SENTINEL });
|
||||
assert.equal(added.status, 200);
|
||||
assert.deepEqual(added.json, { cleared: true }, 'the bus answer is passed through as it was');
|
||||
await post('/api/proxy/login/start', { provider: 'openai' });
|
||||
await send(f.port, '/api/proxy/accounts', { headers: f.headers() });
|
||||
const records = f.admin.calls.filter(c => c[0] === 'recordAccount').map(c => c[1]);
|
||||
assert.deepEqual(records.map(r => r.verb), ['proxy.apikey.add', 'proxy.login.start']);
|
||||
assert.deepEqual(records[0], { verb: 'proxy.apikey.add', args: { provider: 'openai', key: SENTINEL }, result: { cleared: true }, user: 'Local operator', org: null });
|
||||
f.admin.recordAccount = () => false;
|
||||
const unrecorded = await post('/api/proxy/apikey/add', { provider: 'openai', key: SENTINEL });
|
||||
assert.deepEqual(unrecorded.json, { cleared: true, recorded: false });
|
||||
f.admin.recordAccount = () => { throw new Error(SENTINEL); };
|
||||
const thrown = await post('/api/proxy/apikey/add', { provider: 'openai', key: SENTINEL });
|
||||
assert.deepEqual(thrown.json, { cleared: true, recorded: false });
|
||||
f.admin.recordAccount = () => null;
|
||||
assert.deepEqual((await post('/api/proxy/login/cancel', { state: 's' })).json, { cleared: true }, 'not an account change: nothing added');
|
||||
assert.equal(f.logs.join('\n').includes(SENTINEL), false);
|
||||
});
|
||||
|
||||
test('a refused or failed account write records nothing, and an organization route names its organization', async t => {
|
||||
const f = await devServer(t, { proxyCall: async () => { throw Object.assign(new Error('x'), { code: 'provider-not-allowed' }); } });
|
||||
const r = await send(f.port, '/api/proxy/apikey/add', { method: 'POST', headers: f.headers(), body: { provider: 'openai', key: SENTINEL } });
|
||||
assert.equal(r.status, 400);
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
const organizations = { list: async () => [], resolve: async id => (id === 'acme' ? { busHealthy: true, send: async () => ({}), proxyCall: async () => ({ added: true }) } : null) };
|
||||
const o = await devServer(t, { organizations });
|
||||
const inOrg = await send(o.port, '/api/orgs/acme/proxy/apikey/add', { method: 'POST', headers: o.headers(), body: { provider: 'openai', key: SENTINEL } });
|
||||
assert.deepEqual(inOrg.json, { added: true });
|
||||
assert.deepEqual(o.admin.calls, [['recordAccount', { verb: 'proxy.apikey.add', args: { provider: 'openai', key: SENTINEL }, result: { added: true }, user: 'Local operator', org: 'acme' }]]);
|
||||
});
|
||||
|
||||
test('a real controller records the account write without the key, and history shows it', async t => {
|
||||
const { createProxyAdmin } = await import('../../model-proxy/src/admin-control.mjs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'pa-a1r-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const { mkdirSync } = await import('node:fs');
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
mkdirSync(configDir, { mode: 0o700 }); mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit: { unit: 'mosaic-proxy-zz.service', status: async () => ({ installed: false }) }, buses: async () => ({ stopped: true, running: [] }), now: () => new Date(0), env: {} });
|
||||
const f = await devServer(t, { proxyAdmin: admin, proxyCall: async () => ({ added: true }) });
|
||||
const r = await send(f.port, '/api/proxy/apikey/add', { method: 'POST', headers: f.headers(), body: { provider: 'openai', key: SENTINEL } });
|
||||
assert.deepEqual(r.json, { added: true });
|
||||
const h = await send(f.port, '/api/proxy/admin/history', { headers: f.headers() });
|
||||
assert.deepEqual(h.json, { entries: [{ time: new Date(0).toISOString(), op: 'account.proxy.apikey.add', provider: 'openai', user: 'Local operator' }], next: null });
|
||||
assert.equal(readFileSync(join(dataRoot, 'state', 'proxy-admin.jsonl'), 'utf8').includes(SENTINEL), false);
|
||||
});
|
||||
|
||||
// #1873 settings-backend A2: programs off and access health.
|
||||
test('A2: a member and an organization owner get human-required on programs off and access, before any controller call', async t => {
|
||||
const organizations = { list: async () => [], resolve: async id => (id === 'x' ? { busHealthy: true } : null) };
|
||||
for (const session of [{ name: 'Bob', provider: 'local-dev', iss: 'local-dev', sub: 'bob', rootOwner: false, instanceRole: 'member' },
|
||||
{ name: 'Olive', provider: 'local-dev', iss: 'local-dev', sub: 'olive', rootOwner: false, instanceRole: 'operator', orgRole: 'owner' }]) {
|
||||
const f = await devServer(t, { organizations, sso: { ...createDevOwner(), session: () => session } });
|
||||
for (const route of A2) {
|
||||
const inOrg = await send(f.port, `/api/orgs/x/proxy/admin/${route[0]}`, { method: route[1], headers: f.headers(), body: route[2] });
|
||||
assert.equal(inOrg.status, 403, `${session.sub} ${route[0]}`); assert.deepEqual(inOrg.json, { error: 'human-required' }, `${session.sub} ${route[0]}`);
|
||||
const plain = await hit(f, route);
|
||||
assert.equal(plain.status >= 400 && plain.status < 500, true, `${session.sub} ${route[0]} outside an organization is refused`);
|
||||
}
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
}
|
||||
});
|
||||
|
||||
test('A2: programs off takes one plain program name and nothing else; access takes no body', async t => {
|
||||
const f = await devServer(t);
|
||||
for (const body of [{}, { name: '' }, { name: 'Alpha' }, { name: '../x' }, { name: 7 }, { name: 'a'.repeat(33) }, { name: 'alpha', extra: 1 }, { name: ['alpha'] }])
|
||||
assert.deepEqual((await hit(f, ['programs/off', 'POST', body])).json, { error: 'invalid-request' }, JSON.stringify(body));
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
assert.equal((await send(f.port, '/api/proxy/admin/access', { method: 'POST', headers: f.headers(), body: {} })).status, 405);
|
||||
assert.equal((await send(f.port, '/api/proxy/admin/programs/off', { headers: f.headers() })).status, 405);
|
||||
f.admin.programsOff = async () => { throw Object.assign(new Error(`${SENTINEL} busy`), { code: 'proxy-admin-busy' }); };
|
||||
const busy = await hit(f, ['programs/off', 'POST', { name: 'alpha' }]);
|
||||
assert.equal(busy.status, 409); assert.deepEqual(busy.json, { error: 'proxy-admin-busy' });
|
||||
f.admin.access = async () => { throw new Error(SENTINEL); };
|
||||
assert.deepEqual((await hit(f, ['access', 'GET'])).json, { error: 'outcome-unknown' });
|
||||
assert.equal(f.logs.join('\n').includes(SENTINEL), false);
|
||||
});
|
||||
|
||||
// The real controller behind the real routes, with fake organizations whose answers carry planted keys.
|
||||
test('A2 end to end: programs off writes one receipt, skips a down bus, and no answer, receipt or history holds a key; access never says ok for proxy-uncertain', async t => {
|
||||
const { createProxyAdmin } = await import('../../model-proxy/src/admin-control.mjs');
|
||||
const { mkdirSync } = await import('node:fs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'pa-a2-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
mkdirSync(configDir, { mode: 0o700 }); mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const TAIL = 'Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE';
|
||||
const planted = [`msk-k1a2-${TAIL}`, `mso-k1a2-acme-${TAIL}`, `mst-k1a2-triage-${TAIL}`, `msp-k1a2-${TAIL}`];
|
||||
const REV = 'c'.repeat(64);
|
||||
const sets = [];
|
||||
const org = (id, { down = false, keys = [], keysError = null } = {}) => ({
|
||||
id, fixture: false, cliproxy: true, changes: '',
|
||||
file: () => ({ document: { roles: {}, plugins: { harness: ['alpha', 'beta'] }, note: planted[1] }, revision: REV }),
|
||||
call: async (verb, args) => {
|
||||
if (down) throw Object.assign(new Error(planted[0]), { code: 'outcome-unknown' });
|
||||
if (verb === 'plugins') return { business: id, revision: REV, plugins: { harness: ['alpha', 'beta'] }, note: planted[0] };
|
||||
if (verb === 'routes') return { business: id, revision: REV, instances: {}, note: planted[2] };
|
||||
if (verb === 'plugins.set') { sets.push([id, args]); return { business: id, revision: 'd'.repeat(64) }; }
|
||||
if (verb === 'proxy.keys') { if (keysError) throw Object.assign(new Error(planted[3]), { code: keysError }); return { keys }; }
|
||||
throw Object.assign(new Error(planted[3]), { code: 'invalid-request' });
|
||||
},
|
||||
});
|
||||
const orgs = [
|
||||
org('acme', { keys: [{ keyId: '0123456789ab', management: 'stack-managed', agent: planted[1], issuedAt: planted[3], state: 'active' }, { keyId: 'abcdefabcdef', management: 'not stack-managed', agent: planted[0], issuedAt: null, state: null }] }),
|
||||
org('bolt', { down: true }),
|
||||
org('crux', { keysError: 'proxy-uncertain' }),
|
||||
];
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit: { unit: 'mosaic-proxy-zz.service', status: async () => ({ installed: false }) }, buses: async () => ({ stopped: true, running: [] }), now: () => new Date(0), env: {},
|
||||
riders: async () => orgs, harnesses: () => ['alpha', 'beta'], rootAgents: () => ({ state: 'absent', agents: {}, assistant: null }) });
|
||||
const f = await devServer(t, { proxyAdmin: admin });
|
||||
const unknown = await hit(f, ['programs/off', 'POST', { name: 'gamma' }]);
|
||||
assert.equal(unknown.status, 400); assert.deepEqual(unknown.json, { error: 'invalid-request' });
|
||||
const off = await hit(f, ['programs/off', 'POST', { name: 'alpha' }]);
|
||||
assert.equal(off.status, 200);
|
||||
assert.deepEqual(off.json.results, [{ id: 'acme', outcome: 'off' }, { id: 'bolt', outcome: 'bus-down' }, { id: 'crux', outcome: 'off' }]);
|
||||
assert.deepEqual(off.json.receipt, { time: new Date(0).toISOString(), op: 'programs.off', name: 'alpha', results: off.json.results, user: 'Local operator' });
|
||||
assert.deepEqual(sets.map(([id]) => id), ['acme', 'crux']);
|
||||
const lines = readFileSync(join(dataRoot, 'state', 'proxy-admin.jsonl'), 'utf8').trim().split('\n');
|
||||
assert.equal(lines.length, 1);
|
||||
const access = await hit(f, ['access', 'GET']);
|
||||
assert.equal(access.status, 200);
|
||||
assert.deepEqual(access.json.organizations.map(o => [o.id, o.health, o.reason ?? null]), [['acme', 'ok', null], ['bolt', 'unreadable', 'bus-down'], ['crux', 'attention', 'proxy-uncertain']]);
|
||||
assert.deepEqual(access.json.organizations[0].keys, [{ keyId: '0123456789ab', agent: null, state: 'active', issuedAt: null }]);
|
||||
const status = await hit(f, ['status', 'GET']);
|
||||
assert.deepEqual(status.json.programs, { installed: ['alpha', 'beta'], organizations: [{ id: 'acme', on: ['alpha', 'beta'] }, { id: 'bolt', on: null }, { id: 'crux', on: ['alpha', 'beta'] }] });
|
||||
const history = await hit(f, ['history', 'GET']);
|
||||
assert.deepEqual(history.json.entries, [off.json.receipt]);
|
||||
const all = [unknown.text, off.text, access.text, status.text, history.text, lines.join('\n'), f.logs.join('\n')].join('\n');
|
||||
assert.doesNotMatch(all, /ms[kotp]-[A-Za-z0-9_-]{4,}/);
|
||||
for (const bit of [TAIL, TAIL.slice(0, 10), root]) assert.equal(all.includes(bit), false, bit);
|
||||
});
|
||||
@@ -26,9 +26,12 @@ const scenario = (status, config = {}) => ({ '/api/config': { send: true, devOwn
|
||||
const writes = b => b.evaluate('window.__writes.filter(w=>w.path.startsWith("/api/proxy/admin/")).map(w=>({path:w.path,body:JSON.parse(w.body)}))');
|
||||
// Step 3's line names the step it holds in data-next.
|
||||
const STEP3 = '#setup-step-organizations [data-step-summary]';
|
||||
// settings-s5b: Technical details reads the setup history from GET /api/proxy/admin/history, not from receipts.
|
||||
const history = entries => ({ '/api/proxy/admin/history?limit=25': { entries, next: null }, '/api/proxy/admin/service-key': { madeAt: T, renewedAt: null, renewal: 'not-built', problem: null } });
|
||||
const historyItems = b => b.evaluate('[...document.querySelectorAll("#proxy-setup-history li")].map(li=>li.textContent.split(" · ")[0])');
|
||||
|
||||
test('a fresh instance shows Set up model service as one button, the not-built own proxy, and no terminal note', { timeout: 90000 }, async t => {
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(base), 'POST /api/proxy/admin/fetch': { status: 202, body: { state: 'running' } } } });
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(base), 'POST /api/proxy/admin/fetch': { status: 202, body: { state: 'running' } }, '/api/proxy/admin/log?lines=20': { unit: 'mosaic-proxy.service', lines: [], redacted: 0, dropped: 0 } } });
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-setup-go")?.dataset.next==="fetch"');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup").hidden'), false);
|
||||
@@ -46,6 +49,8 @@ test('a fresh instance shows Set up model service as one button, the not-built o
|
||||
await wait('document.querySelector("#setup-step-service").dataset.state==="bad"');
|
||||
assert.deepEqual(await writes(b), [{ path: '/api/proxy/admin/fetch', body: {} }]);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-go").textContent'), 'Try again');
|
||||
// settings-s5b: a failed step 1 opens the service's last lines, read from the log route.
|
||||
await wait('document.querySelector("#proxy-setup-lines").open && /written nothing yet/.test(document.querySelector("#proxy-setup-lines [data-log]").textContent)');
|
||||
assert.deepEqual(await b.evaluate('window.__unexpected'), []);
|
||||
});
|
||||
|
||||
@@ -59,7 +64,7 @@ test('without send the next-step button is disabled and nothing is posted', { ti
|
||||
});
|
||||
|
||||
test('init always sends generate; there is no pasted key, and Try again sends generate again', { timeout: 90000 }, async t => {
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(fetched), 'POST /api/proxy/admin/init': { body: { settings: { port: 18432 }, receipt: { op: 'init', time: T, port: 18432, generated: true } } } } });
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(fetched), ...history([{ op: 'init', time: T }]), 'POST /api/proxy/admin/init': { body: { settings: { port: 18432 }, receipt: { op: 'init', time: T, port: 18432, generated: true } } } } });
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-setup-go")?.dataset.next==="init"');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-go").textContent'), 'Continue setup');
|
||||
@@ -73,7 +78,11 @@ test('init always sends generate; there is no pasted key, and Try again sends ge
|
||||
await b.evaluate('(()=>{const f=document.querySelector("#proxy-setup-init");f.elements.port.value="18432";document.querySelector("#proxy-setup-go").click();})()');
|
||||
await wait('window.__writes.some(w=>w.path==="/api/proxy/admin/init")');
|
||||
assert.deepEqual(await writes(b), [{ path: '/api/proxy/admin/init', body: { generate: true, port: 18432 } }]);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-receipts").textContent'), /init/);
|
||||
// settings-s5b: the receipt is not shown raw; the history read under Technical details names it.
|
||||
assert.doesNotMatch(await b.evaluate('document.querySelector("#proxy-setup").textContent'), /generated/);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 1');
|
||||
assert.deepEqual(await historyItems(b), ['Set up']);
|
||||
// The status still names init, so the run stops there; Try again starts from it.
|
||||
await wait('document.querySelector("#proxy-setup-go")?.textContent==="Try again" && !document.querySelector("#proxy-setup-init [data-setup-write]").disabled');
|
||||
await b.evaluate('(()=>{const f=document.querySelector("#proxy-setup-init");f.elements.port.value="";document.querySelector("#proxy-setup-go").click();})()');
|
||||
@@ -82,14 +91,20 @@ test('init always sends generate; there is no pasted key, and Try again sends ge
|
||||
});
|
||||
|
||||
test('after a confirmed restart the next step clears the fence and shows the receipt', { timeout: 90000 }, async t => {
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(fenced), 'POST /api/proxy/admin/fence/clear': { body: { cleared: true, receipt: { op: 'fence.clear', time: T, pid: 4242 } } } } });
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(fenced), ...history([]), 'POST /api/proxy/admin/fence/clear': { body: { cleared: true, receipt: { op: 'fence.clear', time: T, pid: 4242 } } } } });
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-setup-go")?.dataset.next==="fence-clear"');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = true');
|
||||
await wait('document.querySelector("#proxy-setup-history-message").textContent === "No setup history yet."');
|
||||
await b.evaluate(`window.__scenario['/api/proxy/admin/history?limit=25'] = { entries: [{ op: 'fence.clear', time: '${T}' }], next: null }`);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-state").textContent'), /new pid 4242/);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-go").click()');
|
||||
await wait('document.querySelector("#proxy-setup-message").textContent.includes("Fence cleared")');
|
||||
assert.deepEqual(await writes(b), [{ path: '/api/proxy/admin/fence/clear', body: {} }]);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-receipts").textContent'), /fence\.clear.*pid: 4242/);
|
||||
// settings-s5b: with Technical details open, the write reads the history again.
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 1');
|
||||
assert.deepEqual(await historyItems(b), ['Key writes resumed']);
|
||||
assert.doesNotMatch(await b.evaluate('document.querySelector("#proxy-setup").textContent'), /pid: 4242/);
|
||||
});
|
||||
|
||||
test('a stopped proxy hides the account panels with a setup note, and starting the service brings them back', { timeout: 90000 }, async t => {
|
||||
@@ -143,13 +158,16 @@ test('a fence with no readable time names the --proxy-restarted path instead of
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-next details.proxy-setup-cli p").textContent'), 'Run this in a terminal on this machine: mosaic proxy fence clear --proxy-restarted');
|
||||
});
|
||||
|
||||
test('after a reload the receipts come from status, and an initialized proxy shows its settings read-only', { timeout: 90000 }, async t => {
|
||||
test('after a reload the history comes from the history read, not the status receipts, and an initialized proxy shows its settings read-only', { timeout: 90000 }, async t => {
|
||||
const initialized = { ...base, release: fetched.release, init: { initialized: true, partial: false, settings: { port: 18432 }, files: [] },
|
||||
receipts: [{ op: 'fetch', time: T, version: '8.0.42' }, { op: 'init', time: T, generated: true, port: 18432 }], next: ['unit-install'] };
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(initialized), 'POST /api/proxy/admin/unit/install': { body: { receipt: { op: 'unit.install', time: T } } } } });
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(initialized), ...history([{ op: 'fetch', time: T }, { op: 'init', time: T }]), 'POST /api/proxy/admin/unit/install': { body: { receipt: { op: 'unit.install', time: T } } } } });
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-setup-go")?.dataset.next==="unit-install"');
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-setup-receipts li strong")].map(s=>s.textContent)'), ['init', 'fetch'], 'newest first, from proxy-admin.jsonl');
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-setup-history li").length'), 0, 'nothing is read before Technical details opens');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 2');
|
||||
assert.deepEqual(await historyItems(b), ['Set up', 'Release downloaded and checked'], 'newest first, from the history read');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-init").hidden'), true, 'init is done, so its form is gone');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-settings").textContent'), 'Port18432');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-settings-note").textContent'), 'Changing these after setup is not built yet.');
|
||||
@@ -177,7 +195,7 @@ test('Ride all confirms the stricter admission, paints skips and stand-in organi
|
||||
await wait(`document.querySelector("#proxy-ride")?.checkVisibility() && document.querySelector(${JSON.stringify(STEP3)})?.dataset.next==="ride-all"`);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-ride-go").className'), 'btn primary');
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-ride-text").textContent'), /every launch route is checked, direct Anthropic routes too.*2 organizations can ride now/);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-ride-fixtures li").textContent'), /^west uses stand-in models, can be moved to the owner's models\./);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-ride-fixtures li").textContent'), /^west uses stand-in models, can be moved to your models\./);
|
||||
// The first Ride all's own reload paints, then waits on the accounts read for good.
|
||||
await b.evaluate('document.querySelector("#proxy-ride-go").click()');
|
||||
await wait('window.__writes.some(w=>w.path==="/api/proxy/admin/ride-all")');
|
||||
@@ -399,7 +417,7 @@ test('settings-p3: a stand-in organization with no bus status is not offered a M
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-ride-fixtures li")');
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-ride-fixtures li")].map(li => li.firstChild.textContent.trim())'),
|
||||
['west uses stand-in models. Moving it is not available on this Console.', "east uses stand-in models, can be moved to the owner's models."]);
|
||||
['west uses stand-in models. Moving it is not available on this Console.', "east uses stand-in models, can be moved to your models."]);
|
||||
assert.equal(await b.evaluate('document.querySelector("[data-ride-move=west]")'), null);
|
||||
assert.equal(await b.evaluate('document.querySelector("[data-ride-move=east]").disabled'), false);
|
||||
assert.deepEqual(await b.evaluate('window.__unexpected'), []);
|
||||
@@ -446,8 +464,8 @@ test('settings-s1 B4: only the next step has a button; done steps, Fence, Init l
|
||||
assert.deepEqual(await b.evaluate(`${visible('.settings-content .primary')}.map(e=>e.id||e.textContent)`), ['proxy-login-go'], 'one primary button on the page');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-go")'), null, 'step 1 is done, so it has no button');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-init").checkVisibility()'), false, 'init is done, so its form is gone');
|
||||
const tech = await b.evaluate('(()=>{const d=document.querySelector("#proxy-setup-tech");return {summary:d.querySelector("summary").textContent,open:d.open,terms:[...d.querySelectorAll("dt")].map(x=>x.textContent),units:d.querySelectorAll("[data-unit]").length,receipts:!!d.querySelector("#proxy-setup-receipts")}})()');
|
||||
assert.deepEqual(tech, { summary: 'Technical details: release, service, history', open: false, terms: ['Release', 'Initialized', 'Service', 'Fence', 'Init lock'], units: 4, receipts: true });
|
||||
const tech = await b.evaluate('(()=>{const d=document.querySelector("#proxy-setup-tech");return {summary:d.querySelector("summary").textContent,open:d.open,terms:[...d.querySelectorAll("dt")].map(x=>x.textContent),units:d.querySelectorAll("[data-unit]").length,history:!!d.querySelector("#proxy-setup-history")}})()');
|
||||
assert.deepEqual(tech, { summary: 'Technical details: release, service, history', open: false, terms: ['Release', 'Initialized', 'Service', 'Fence', 'Init lock', 'Service key'], units: 4, history: true });
|
||||
assert.equal(await b.evaluate(`${visible('#proxy-setup dt, #proxy-setup [data-unit]')}.length`), 0, 'nothing technical shows until it is opened');
|
||||
// Technical details opens by keyboard and shows the controls.
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech summary").focus()');
|
||||
|
||||
@@ -197,8 +197,9 @@ test('Routing switch writes the model-provider setting and keeps other modules',
|
||||
await b.call('Page.addScriptToEvaluateOnNewDocument',{source:`const original=window.fetch;window.posts=[];window.fetch=async(url,options={})=>{if(url==='/api/config')return new Response(JSON.stringify({send:true}));if(url==='/api/proxy/plugins')return new Response(JSON.stringify(${JSON.stringify(settings)}));if(url==='/api/proxy/plugins/set'){posts.push(JSON.parse(options.body));return new Promise(r=>window.answer=(status,body)=>r(new Response(JSON.stringify(body),{status})));}return original(url,options);};`});
|
||||
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("Use your models")');
|
||||
await b.evaluate('document.querySelector("#provider-switch").click();true');
|
||||
// settings-s5: the switch is the Use your models choice; choosing it writes at once.
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use your models")&&!document.querySelector("#models-yours").disabled');
|
||||
await b.evaluate('document.querySelector("#models-yours").click();true');
|
||||
await wait('posts.length===1');const payload=await b.evaluate('posts[0]');
|
||||
assert.deepEqual(payload.plugins,{'model-provider':['cliproxy']});
|
||||
const before=JSON.parse(readFileSync(file,'utf8')).plugins;
|
||||
@@ -271,7 +272,7 @@ test('quick fallback edits survive the harness redraw', {timeout:60000}, async t
|
||||
// organization's catalogue is ready with no proxy, and its switch reads Enable.
|
||||
test('D3 Routing: with cliproxy listed and no model proxy the switch reads Disable and removes cliproxy only; a fixture-only org reads Enable', {timeout:90000}, async t => {
|
||||
for (const [providers, label, line, next] of [
|
||||
[['fixture','cliproxy'], 'Stop using the model service', /can't see your models yet\. Your model service isn't set up yet\. A change to thinking alone still saves\. A new model needs the model service: ask the instance owner to set it up, or press Stop using the model service when this organization does not need it\./, ['fixture']],
|
||||
[['fixture','cliproxy'], 'Stop using your models', /can't see your models yet\. Your model service isn't set up yet\. A change to thinking alone still saves\. A new model needs the model service: ask the instance owner to set it up, or press Stop using your models when this organization does not need it\./, ['fixture']],
|
||||
[['fixture'], 'Use your models', /^Model catalogue, observed /, ['fixture','cliproxy']],
|
||||
]) {
|
||||
const root=tmp(t,'d3-switch-'), f=fixture(root,'acme',doc=>{doc.plugins['model-provider']=providers;return doc;});
|
||||
@@ -287,13 +288,17 @@ test('D3 Routing: with cliproxy listed and no model proxy the switch reads Disab
|
||||
await b.call('Page.addScriptToEvaluateOnNewDocument',{source:`const original=window.fetch;window.posts=[];window.fetch=async(url,options={})=>{if(url==='/api/config')return new Response(JSON.stringify({send:true}));if(url==='/api/proxy/plugins')return new Response(JSON.stringify(${JSON.stringify(settings)}));if(url==='/api/proxy/plugins/set'){posts.push(JSON.parse(options.body));return new Promise(r=>window.answer=(status,body)=>r(new Response(JSON.stringify(body),{status})));}return original(url,options);};`});
|
||||
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]')`);
|
||||
// settings-s5: in force, the way back is Stop using your models; not in force, the Use your models choice.
|
||||
const control=label==='Stop using your models'?'#models-stop':'#models-yours';
|
||||
await wait(`!!document.querySelector(${JSON.stringify(control)})&&!document.querySelector(${JSON.stringify(control)}).disabled&&!!document.querySelector('[data-harness]')`);
|
||||
// settings-s5 review P3-3: a Console with no organizations says "this business" mid-sentence.
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-source legend").textContent'),'Where this business gets its models');
|
||||
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): the bus's cause reads as a plain line; its own words sit under Technical details.
|
||||
assert.deepEqual(notices,[],providers.join(','));
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll(".routing-panel > details.routes-cause-detail p")].map(p=>p.textContent)'),label==='Stop using the model service'?['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');
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll(".routing-panel > details.routes-cause-detail p")].map(p=>p.textContent)'),label==='Stop using your models'?['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(${JSON.stringify(control)}).click();true`);
|
||||
await wait('posts.length===1');const payload=await b.evaluate('posts[0]');
|
||||
assert.deepEqual(payload.plugins,{'model-provider':next},providers.join(','));
|
||||
const result=await call('plugins.set',payload);await b.evaluate(`window.answer(200,${JSON.stringify(result)});true`);
|
||||
@@ -309,7 +314,7 @@ test('D3 Routing: with cliproxy listed and no model proxy the switch reads Disab
|
||||
test('D3 Routing: a catalogue refusal names Disable only when it can make the save apply, and sends a tester to the owner', {timeout:90000}, async t => {
|
||||
const refusal=code=>({error:code,message:`${code}: the reason. Nothing changed.`,sent:false});
|
||||
for (const [providers, tester, instance, code, expect] of [
|
||||
[['fixture','cliproxy'], false, 'coder', 'model-catalogue-unavailable', /Nothing changed\. This change does not need the model service: press Stop using the model service, then save again\. Draft kept\.$/],
|
||||
[['fixture','cliproxy'], false, 'coder', 'model-catalogue-unavailable', /Nothing changed\. This change does not need your models: press Stop using your models, then save again\. Draft kept\.$/],
|
||||
[['fixture','cliproxy'], false, 'reviewer', 'model-catalogue-unavailable', /Nothing changed\. Draft kept\.$/],
|
||||
[['fixture'], false, 'reviewer', 'model-provider-unavailable', /Nothing changed\. Draft kept\.$/],
|
||||
[['fixture','cliproxy'], true, 'coder', 'model-catalogue-unavailable', /Nothing changed\. Ask the organization's owner to set up the model service, or to stop using it when the change does not need it\. Draft kept\.$/],
|
||||
|
||||
@@ -187,7 +187,7 @@ test('settings-s4 review F2: a stand-in organization with its bus stopped reads
|
||||
assert.equal(await text(STEP3), "The model service is set up. Gamma Home can't start: it uses stand-in models and its bus is stopped. Move it to your models under Use your models in every organization, then start its bus.");
|
||||
assert.equal(await b.evaluate('document.querySelector("#setup-step-organizations").dataset.state'), 'bad');
|
||||
const item = await b.evaluate('document.querySelector("#proxy-ride-fixtures li").firstChild.textContent.trim()');
|
||||
assert.equal(item, "Gamma Home can't start: it uses stand-in models and its bus is stopped. Move it to the owner's models, then start its bus.");
|
||||
assert.equal(item, "Gamma Home can't start: it uses stand-in models and its bus is stopped. Move it to your models under Use your models in every organization, then start its bus.");
|
||||
assert.equal(await b.evaluate('document.querySelector("[data-ride-move=gamma]").textContent'), 'Move Gamma Home');
|
||||
assert.equal(await b.evaluate('document.querySelector("[data-ride-move=gamma]").checkVisibility()'), true);
|
||||
// The organizations that need nothing get no mark.
|
||||
|
||||
@@ -0,0 +1,455 @@
|
||||
// #1873 settings slice 5 (DESIGN.md 3.4, 12 slice 5, mockups org-models and org-models-restart): an
|
||||
// organization's Models page offers one choice, "Use your models" (the cliproxy model-provider module
|
||||
// through plugins.set) or "Use its own model service" (Coming later), says from the plugins read which
|
||||
// one is in force and what a switch changes, and switches both ways. Also the items slice 4 left: the
|
||||
// Ride all lines name an organization by its display name, step 3 and the "can't start" mark agree
|
||||
// before the first account, the sidebar marks follow a switch, a failed turn shows "Details:" once in
|
||||
// Messages, the old Members addresses land on Agents, and Technical details shows the port and
|
||||
// timeouts the status read carries.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { startServer } from '../src/serve.mjs';
|
||||
import { createDevOwner } from '../src/dev-owner.mjs';
|
||||
import { browser } from './browser.mjs';
|
||||
import { close } from './fixture.mjs';
|
||||
import { ReadError } from '../src/reads.mjs';
|
||||
|
||||
const LIST = [
|
||||
{ id: 'acme', label: 'Acme Studio', theme: 'cobalt', busHealthy: true },
|
||||
{ id: 'gamma', label: 'Gamma Home', theme: 'rosewood', busHealthy: false, busStopped: true },
|
||||
{ id: 'north', label: 'North Works', theme: 'cobalt', busHealthy: true },
|
||||
];
|
||||
const VIEW = {
|
||||
business: 'acme', revision: 'a'.repeat(64), harnesses: ['pi'], models: ['fixture-deterministic'],
|
||||
providerModels: { 'fixture-deterministic': { module: 'fixture', upstream: 'fixture', model: 'fixture-deterministic' } },
|
||||
levels: { pi: ['off', 'low'] }, instances: {}, catalogue: { status: 'ready', observedAt: new Date().toISOString(), models: [], notices: [] },
|
||||
};
|
||||
|
||||
async function orgFixture({ routes = {}, messages = [] } = {}) {
|
||||
const { checkRootAgents } = await import('../../organization/src/index.mjs');
|
||||
const rolesDir = fileURLToPath(new URL('../../../roles', import.meta.url));
|
||||
const root = { state: 'ok', ...checkRootAgents({ agentsVersion: 1, agents: {} }, { rolesDir }) };
|
||||
const empty = () => Promise.resolve([]);
|
||||
const member = org => ({
|
||||
id: org.id, doc: { appearance: { theme: org.theme }, roles: { chief: { definition: 'chief-of-staff' } }, projects: {} },
|
||||
status: async () => (org.busHealthy ? { healthy: true, pid: 4242, boot: null } : { healthy: false, boot: { ok: false, at: new Date().toISOString(), lastGood: null, reason: 'stopped by the owner' } }),
|
||||
options: () => ({ bus: { inbox: empty, agents: empty, 'thread.claims': empty, tasks: empty, usage: () => Promise.resolve({}), limits: () => Promise.resolve({}), decisions: empty }, chat: { threads: empty, projects: empty } }),
|
||||
});
|
||||
const organizations = {
|
||||
list: async () => LIST,
|
||||
members: async () => LIST.map(member),
|
||||
resolve: async id => {
|
||||
const org = LIST.find(o => o.id === id); if (!org) return null;
|
||||
if (!org.busHealthy) return { busHealthy: false, reads: { settings: async () => { throw new ReadError('bus-unavailable'); } }, bus: {}, chat: { projects: empty, threads: empty } };
|
||||
return {
|
||||
busHealthy: true,
|
||||
reads: { business: async () => ({ business: { id, human: 'jason', projects: [], appearance: { theme: org.theme } } }), settings: async () => ({ business: id, release: 'fixture', notifier: {} }), queue: async () => ({ rows: [] }), busStatus: async () => ({}) },
|
||||
bus: { messages: async () => ({ messages, unread: 0, total: messages.length }), agents: empty, 'thread.claims': empty, inbox: empty, tasks: empty, usage: async () => ({}), routes: async () => ({ ...VIEW, ...(routes[id] ?? {}), business: id }), instances: async () => ({ business: id, revision: 'a'.repeat(64), launch: true, noHire: [], retired: [], definitions: {}, instances: {} }) },
|
||||
chat: { projects: empty, threads: empty },
|
||||
};
|
||||
},
|
||||
};
|
||||
return { organizations, agentLevels: { loadRoot: () => root, rolesDir, ctx: { configDir: '/cfg', dataRoot: '/data', instance: 'test' } } };
|
||||
}
|
||||
|
||||
// The model service is set up and answering. init.settings is what the status read carries after init
|
||||
// (model-proxy admin.mjs initState).
|
||||
const SERVICE_UP = { next: ['accounts'], init: { initialized: true, partial: false, problem: null, settings: { port: 18432, rotateGraceMinutes: 30, firstOutputSeconds: 90 }, files: [] },
|
||||
release: { fetched: true, verified: true, version: '8.0.42', panelVersion: '1.26.7' },
|
||||
unit: { unit: 'mosaic-proxy.service', installed: true, active: 'active', port: 18432, listening: true, healthy: true }, fence: { set: false }, initLock: { present: false, blockers: [] }, fetch: { state: 'idle' }, organizations: [], ride: [] };
|
||||
const SERVICE_DOWN = { ...SERVICE_UP, next: ['unit-start'], unit: { ...SERVICE_UP.unit, active: 'inactive', listening: false, healthy: false } };
|
||||
const REV = n => String(n).repeat(64).slice(0, 64);
|
||||
const pluginsRead = providers => ({ revision: REV('b'), available: { harness: ['pi'], 'model-provider': ['fixture', 'cliproxy'] }, plugins: { harness: ['pi'], 'model-provider': providers } });
|
||||
|
||||
// The instance owner's flags and every model service read and write, by path suffix (display.js
|
||||
// scopes some reads under /api/orgs/<id>/). plugins.set merges the module lists the way the bus does
|
||||
// and may carry a new setup status (__s5.afterSet). window.confirm answers yes and records its text.
|
||||
const ownerStub = ({ service = SERVICE_UP, plugins = pluginsRead(['fixture']), accounts = [], posts = {}, afterSet = null, setStatus = 200 } = {}) => `(() => {
|
||||
const real = window.fetch.bind(window);
|
||||
window.__s5 = ${JSON.stringify({ service, plugins, accounts, posts, afterSet, setStatus })}; window.__sets = []; window.__writes = []; window.__confirms = [];
|
||||
window.confirm = text => { window.__confirms.push(text); return true; };
|
||||
const json = (body, status = 200) => new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
|
||||
window.fetch = async (input, init = {}) => {
|
||||
const url = new URL(typeof input === 'string' ? input : input.url, location.href), path = url.pathname, method = (init.method || 'GET').toUpperCase();
|
||||
if (method !== 'GET' && path.endsWith('/proxy/plugins/set')) {
|
||||
const body = JSON.parse(init.body); window.__sets.push({ path, body });
|
||||
if (__s5.setStatus !== 200) return json({ error: 'plugins-refused', message: 'The bus refused the change (plugins-refused).' }, __s5.setStatus);
|
||||
__s5.plugins = { ...__s5.plugins, revision: String(__s5.sets = (__s5.sets ?? 0) + 1).repeat(64).slice(0, 64), plugins: { ...__s5.plugins.plugins, ...body.plugins } };
|
||||
if (__s5.afterSet) __s5.service = __s5.afterSet;
|
||||
return json({ revision: __s5.plugins.revision, plugins: __s5.plugins.plugins });
|
||||
}
|
||||
if (method !== 'GET' && /\\/proxy\\//.test(path)) { window.__writes.push(path); const key = Object.keys(__s5.posts).find(k => path.endsWith(k)); const a = key ? __s5.posts[key] : { status: 500, body: { error: 'stub' } };
|
||||
if (a.service) __s5.service = a.service; return json(a.body ?? {}, a.status ?? 200); }
|
||||
if (method !== 'GET') return real(input, init);
|
||||
if (/\\/api\\/(orgs\\/[^/]+\\/)?config$/.test(path)) { const c = await (await real(input, init)).json(); return json({ ...c, orgSetup: true, proxyAdmin: true, proxyAdminSend: true, ...(/\\/orgs\\//.test(path) ? { settingsSend: true } : {}) }); }
|
||||
if (path.endsWith('/proxy/plugins')) return json(__s5.plugins);
|
||||
if (path.endsWith('/proxy/admin/status')) return json(__s5.service);
|
||||
if (path.endsWith('/proxy/accounts')) return json({ accounts: __s5.accounts });
|
||||
if (path.endsWith('/proxy/keys')) return json({ keys: [] });
|
||||
if (path.endsWith('/triage/route')) return json({ route: null });
|
||||
return real(input, init);
|
||||
};
|
||||
})();`;
|
||||
|
||||
async function setup(t, { path, viewport = [1440, 900], fixture = {}, stub = {} } = {}) {
|
||||
const { organizations, agentLevels } = await orgFixture(fixture);
|
||||
const server = await startServer({ port: 0, sso: createDevOwner(), organizations, agentLevels, triage: null, send: async () => ({}), log: () => {} });
|
||||
t.after(() => close(server));
|
||||
const b = await browser({ machine: false }); t.after(() => b.close());
|
||||
await b.viewport(...viewport);
|
||||
await b.call('Page.addScriptToEvaluateOnNewDocument', { source: ownerStub(stub) });
|
||||
await b.navigate(`http://127.0.0.1:${server.address().port}${path}`);
|
||||
const wait = expression => b.evaluate(`(async()=>{for(let i=0;i<250;i++){if(${expression})return true;await new Promise(r=>setTimeout(r,30))}throw Error("wait failed: "+${JSON.stringify(expression)}+" HASH "+location.hash)})()`);
|
||||
const text = sel => b.evaluate(`document.querySelector(${JSON.stringify(sel)})?.textContent ?? null`);
|
||||
const click = sel => b.evaluate(`(() => { const el = document.querySelector(${JSON.stringify(sel)}); if (!el) throw Error("no ${sel.replace(/"/g, '')}"); el.click(); return true; })()`);
|
||||
return { b, wait, text, click };
|
||||
}
|
||||
const mark = id => `document.querySelector('[data-workspace-org=${id}] .ws-dot')?.title`;
|
||||
const STEP3 = '#setup-step-organizations [data-step-summary]';
|
||||
|
||||
// ---- Organization > Models --------------------------------------------------------------------
|
||||
test('settings-s5: Acme Studio\'s models reads which choice is in force from the plugins read, switches to your models and back', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click } = await setup(t, { path: '/#/settings/routing?org=acme' });
|
||||
await wait('document.querySelector("#models-source") && document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
assert.equal(await text('#settings-page-title'), "Acme Studio's models");
|
||||
assert.equal(await text('#models-source legend'), 'Where Acme Studio gets its models');
|
||||
// Review P2-2: the header says where the models come from only from the plugins read.
|
||||
assert.match(await text('.settings-helper'), /The models Acme Studio's agents use\. They come from its own organization file\./);
|
||||
assert.doesNotMatch(await text('.settings-helper'), /your accounts/);
|
||||
// Not in force: the radio is not checked, and the line says what is in force and what a switch changes.
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-yours").checked'), false);
|
||||
assert.equal(await text('#models-in-force'), "Now: Acme Studio does not use your models. Its agents use only the models in its own organization file.");
|
||||
assert.match(await text('#models-change'), /^Choose Use your models to switch\. Acme Studio's agents can then use the models your accounts offer\./);
|
||||
// Use its own model service: dashed, disabled, Coming later.
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-own").disabled'), true);
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-own").closest("label").classList.contains("off")'), true);
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-own").closest("label").querySelector(".badge").textContent'), 'Coming later');
|
||||
assert.match(await b.evaluate('document.querySelector("#models-own").closest("label").textContent'), /Planned, not built yet, so you can't choose it today\./);
|
||||
assert.equal(await b.evaluate('getComputedStyle(document.querySelector("#models-own").closest("label")).borderTopStyle'), 'dashed');
|
||||
// The old button is gone.
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#provider-switch")'), false);
|
||||
|
||||
// Switch to your models: one write, the model-provider list only, with cliproxy added.
|
||||
await click('#models-yours');
|
||||
await wait('window.__sets.length === 1');
|
||||
assert.deepEqual(await b.evaluate('window.__sets[0].body'), { revision: REV_B, plugins: { 'model-provider': ['fixture', 'cliproxy'] } });
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.startsWith("Now: Acme Studio uses your models")');
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-yours").checked'), true);
|
||||
assert.match(await text('#models-receipt'), /^Saved\. Acme Studio uses your models\./);
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-receipt").classList.contains("settings-success")'), true);
|
||||
assert.match(await text('#models-change'), /Stop using your models/);
|
||||
// Review P2-4: focus moves to the Saved line.
|
||||
await wait('document.activeElement?.id === "models-receipt"');
|
||||
await wait('/They come from your accounts in Model setup\./.test(document.querySelector(".settings-helper")?.textContent ?? "")');
|
||||
|
||||
// And back: Stop using your models removes cliproxy and keeps the other module.
|
||||
await click('#models-stop');
|
||||
await wait('window.__sets.length === 2');
|
||||
assert.deepEqual(await b.evaluate('window.__sets[1].body.plugins'), { 'model-provider': ['fixture'] });
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use your models")');
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-yours").checked'), false);
|
||||
assert.match(await text('#models-receipt'), /^Saved\. Acme Studio no longer uses your models\./);
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#models-stop")'), false);
|
||||
await wait('document.activeElement?.id === "models-receipt"');
|
||||
});
|
||||
const REV_B = REV('b');
|
||||
|
||||
test('settings-s5: a refused switch says so and the page keeps the choice the read shows', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click } = await setup(t, { path: '/#/settings/routing?org=acme', stub: { setStatus: 409 } });
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
await click('#models-yours');
|
||||
await wait('window.__sets.length === 1 && /refused/.test(document.querySelector("#models-receipt")?.textContent ?? "")');
|
||||
assert.equal(await text('#models-receipt'), 'Not changed. The bus refused the change (plugins-refused).');
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-yours").checked'), false);
|
||||
assert.match(await text('#models-in-force'), /does not use your models/);
|
||||
});
|
||||
|
||||
test('settings-s5: a stale page does not write, it shows the current choice first', { timeout: 90000 }, async t => {
|
||||
const { b, wait, click } = await setup(t, { path: '/#/settings/routing?org=acme' });
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
// Someone else switched Acme Studio on since the page read it.
|
||||
await b.evaluate('__s5.plugins = { ...__s5.plugins, plugins: { ...__s5.plugins.plugins, "model-provider": ["fixture", "cliproxy"] } }; true');
|
||||
await click('#models-yours');
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.startsWith("Now: Acme Studio uses your models")');
|
||||
assert.equal(await b.evaluate('window.__sets.length'), 0);
|
||||
assert.match(await b.evaluate('document.querySelector("#models-receipt").textContent'), /changed since this page read it/);
|
||||
});
|
||||
|
||||
test('settings-s5: while the plugins read is out the choice waits, and a failed read says so with Read again', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text } = await setup(t, { path: '/#/settings/routing?org=acme', stub: { plugins: { broken: true } } });
|
||||
await wait('/could not be read/.test(document.querySelector("#models-in-force")?.textContent ?? "")');
|
||||
assert.equal(await text('#models-in-force'), "Which models Acme Studio uses could not be read.");
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-yours").disabled'), true);
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#models-read")'), true);
|
||||
});
|
||||
|
||||
test('settings-s5: an organization that needs a restart shows the banner with Restart as the one primary, and the choice waits', { timeout: 90000 }, async t => {
|
||||
const routes = { acme: { catalogue: { status: 'unavailable', notices: [], models: [] } } };
|
||||
const { b, wait, text } = await setup(t, { path: '/#/settings/routing?org=acme', fixture: { routes }, stub: { plugins: pluginsRead(['fixture', 'cliproxy']) } });
|
||||
await wait('document.querySelector("#routes-catalogue.banner [data-primary]") && document.querySelector("#models-in-force")?.textContent.startsWith("Now: Acme Studio uses")');
|
||||
assert.match(await text('#routes-catalogue'), /Acme Studio can't see your models yet\./);
|
||||
assert.match(await text('#routes-catalogue [data-primary]'), /Restart Acme Studio/);
|
||||
// The banner sits above the choice.
|
||||
assert.equal(await b.evaluate('!!(document.querySelector("#routes-catalogue").compareDocumentPosition(document.querySelector("#models-source")) & Node.DOCUMENT_POSITION_FOLLOWING)'), true);
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-yours").checked && document.querySelector("#models-yours").disabled'), true);
|
||||
assert.equal(await text('#models-wait'), "You can change Acme Studio's models after it restarts.");
|
||||
// Review P2-1: Stop using your models waits too: it is not offered until the restart.
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#models-stop")'), false);
|
||||
// One primary: the routes Save is quiet.
|
||||
assert.equal(await b.evaluate('document.querySelector("#routes-form [type=submit]").classList.contains("route-save-quiet")'), true);
|
||||
});
|
||||
|
||||
// Review P2-3: a choice not in force says when the model service behind it is not set up or not
|
||||
// answering, with a link to Model setup. The choice itself still writes.
|
||||
test('settings-s5: Use your models says when the model service is not set up or not answering', { timeout: 90000 }, async t => {
|
||||
for (const [service, line, link] of [
|
||||
[{ ...SERVICE_UP, init: { initialized: false, partial: false, problem: null, files: [] } }, "Your model service isn't set up. Set it up in Model setup.", 'Set it up in Model setup'],
|
||||
[SERVICE_DOWN, "Your model service isn't answering. Restart it in Model setup.", 'Restart it in Model setup'],
|
||||
]) {
|
||||
const { b, wait, text } = await setup(t, { path: '/#/settings/routing?org=acme', stub: { service } });
|
||||
await wait('document.querySelector("#models-service") && document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
assert.equal(await text('#models-service'), line);
|
||||
assert.deepEqual(await b.evaluate('[document.querySelector("#models-service a").textContent, document.querySelector("#models-service a").getAttribute("href")]'), [link, '#/settings/model-setup']);
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-yours").disabled'), false);
|
||||
}
|
||||
// Up and answering: no line.
|
||||
const { b, wait } = await setup(t, { path: '/#/settings/routing?org=acme' });
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#models-service")'), false);
|
||||
});
|
||||
|
||||
// Review P3-1: while Use your models writes, the status line says Saving.
|
||||
test('settings-s5: Use your models shows Saving while its write is out', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click } = await setup(t, { path: '/#/settings/routing?org=acme' });
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
await b.evaluate(`(() => { const f = window.fetch; window.fetch = async (input, init = {}) => { if ((init.method || 'GET') !== 'GET' && String(input).endsWith('/proxy/plugins/set')) await new Promise(r => { window.__release = r; }); return f(input, init); }; return true; })()`);
|
||||
await click('#models-yours');
|
||||
await wait('typeof window.__release === "function" && document.querySelector("#models-receipt")?.textContent.trim() === "Saving…"');
|
||||
assert.equal((await text('#models-receipt')).trim(), 'Saving…');
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#models-receipt .settings-spinner")'), true);
|
||||
await b.evaluate('window.__release(); true');
|
||||
await wait('/^Saved\./.test(document.querySelector("#models-receipt")?.textContent ?? "")');
|
||||
});
|
||||
|
||||
// Recheck N1: the not set up line goes once the owner sets the service up and comes back to Models.
|
||||
test('settings-s5: coming back to Models after setting the service up reads it again, and the not set up line goes', { timeout: 90000 }, async t => {
|
||||
const { b, wait } = await setup(t, { path: '/#/settings/routing?org=acme', stub: { service: { ...SERVICE_UP, init: { initialized: false, partial: false, problem: null, files: [] } } } });
|
||||
await wait('document.querySelector("#models-service") && document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
await b.evaluate(`window.__s5.service = ${JSON.stringify(SERVICE_UP)}; location.hash = '#/settings/agents?org=acme'; true`);
|
||||
await wait('location.hash.startsWith("#/settings/agents") && !document.querySelector("#models-in-force") && document.querySelector("#settings-page-title")');
|
||||
await b.evaluate(`location.hash = '#/settings/routing?org=acme'; true`);
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use") && !document.querySelector("#models-service")');
|
||||
});
|
||||
|
||||
// Recheck N2: a switch left while it writes does not move focus on the next visit to Models.
|
||||
test('settings-s5: leaving Models while a switch writes, the next visit focuses the page title, not the outcome line', { timeout: 90000 }, async t => {
|
||||
const { b, wait, click } = await setup(t, { path: '/#/settings/routing?org=acme' });
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
await b.evaluate(`(() => { const f = window.fetch; window.fetch = async (input, init = {}) => { if ((init.method || 'GET') !== 'GET' && String(input).endsWith('/proxy/plugins/set')) await new Promise(r => { window.__release = r; }); return f(input, init); }; return true; })()`);
|
||||
await click('#models-yours');
|
||||
await wait('typeof window.__release === "function"');
|
||||
await b.evaluate(`location.hash = '#/settings/agents?org=acme'; true`);
|
||||
await wait('location.hash.startsWith("#/settings/agents") && !document.querySelector("#models-in-force") && document.querySelector("#settings-page-title")');
|
||||
await b.evaluate('window.__release(); true');
|
||||
await wait('window.__sets.length === 1');
|
||||
await new Promise(r => setTimeout(r, 400));
|
||||
await b.evaluate(`location.hash = '#/settings/routing?org=acme'; true`);
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("uses your models") && /^Saved\./.test(document.querySelector("#models-receipt")?.textContent ?? "")');
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'settings-page-title');
|
||||
});
|
||||
|
||||
// Review P3-2: a failed read before the write says nothing was written, and offers Read again.
|
||||
test('settings-s5: a switch whose first read fails says Not changed, not Outcome unknown', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click } = await setup(t, { path: '/#/settings/routing?org=acme' });
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
await b.evaluate('__s5.plugins = { broken: true }; true');
|
||||
await click('#models-yours');
|
||||
await wait('/^Not changed/.test(document.querySelector("#models-receipt")?.textContent ?? "")');
|
||||
assert.equal(await text('#models-receipt'), 'Not changed. Nothing was written.');
|
||||
assert.equal(await b.evaluate('window.__sets.length'), 0);
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#models-read")'), true);
|
||||
assert.equal(await text('#models-in-force'), 'Which models Acme Studio uses could not be read.');
|
||||
});
|
||||
|
||||
test('settings-s5: at 390 the Models page does not scroll sideways and the choice takes a visible focus', { timeout: 90000 }, async t => {
|
||||
const { b, wait } = await setup(t, { path: '/#/settings/routing?org=acme', viewport: [390, 844] });
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
assert.equal(await b.evaluate('document.documentElement.scrollWidth <= document.documentElement.clientWidth'), true);
|
||||
await b.evaluate('document.querySelector("#models-yours").focus(); true');
|
||||
assert.notEqual(await b.evaluate('getComputedStyle(document.activeElement).outlineStyle'), 'none');
|
||||
});
|
||||
|
||||
// Review P3-4: at 1440 the choice cards sit in the form column, not the whole card width.
|
||||
test('settings-s5: at 1440 the choice sits in the form column', { timeout: 90000 }, async t => {
|
||||
const { b, wait } = await setup(t, { path: '/#/settings/routing?org=acme' });
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
const [choice, panel] = await b.evaluate('[document.querySelector("#models-source").getBoundingClientRect().width, document.querySelector(".routing-panel").getBoundingClientRect().width]');
|
||||
assert.ok(choice <= 448 + 1 && panel > choice + 200, `${choice} in ${panel}`);
|
||||
});
|
||||
|
||||
// ---- Sidebar marks follow a switch -------------------------------------------------------------
|
||||
test('settings-s5: the sidebar marks read the setup status again after an organization stops using your models', { timeout: 90000 }, async t => {
|
||||
const paused = { ...SERVICE_DOWN, ride: [{ id: 'acme', state: 'already' }] };
|
||||
const after = { ...SERVICE_DOWN, ride: [{ id: 'acme', state: 'removed' }] };
|
||||
const { wait, click } = await setup(t, { path: '/#/settings/routing?org=acme', stub: { service: paused, plugins: pluginsRead(['fixture', 'cliproxy']), afterSet: after } });
|
||||
await wait(`${mark('acme')} === 'paused' && document.querySelector("#models-stop")`);
|
||||
await click('#models-stop');
|
||||
await wait(`window.__sets.length === 1 && ${mark('acme')} === 'bus running'`);
|
||||
});
|
||||
|
||||
// ---- Model setup: display names, step 3 and the mark ------------------------------------------
|
||||
test('settings-s5: Move, its result and the skipped lines name the organization by its display name, in the Ride line\'s words', { timeout: 90000 }, async t => {
|
||||
const service = { ...SERVICE_UP, organizations: [{ id: 'acme', proxy: 'shared', catalogue: { status: 'ready' } }],
|
||||
ride: [{ id: 'acme', state: 'already' }, { id: 'north', state: 'bus-down' }, { id: 'gamma', state: 'fixture', running: false }] };
|
||||
const moved = { ...service, ride: [{ id: 'acme', state: 'already' }, { id: 'north', state: 'bus-down' }, { id: 'gamma', state: 'already' }] };
|
||||
const accounts = [{ accountId: 'a1', provider: 'claude', displayName: 'Claude work', status: 'active', priority: 1 }];
|
||||
const { b, wait, text, click } = await setup(t, { path: '/#/settings/model-setup', stub: { service, accounts, posts: { '/admin/ride-move': { body: { moved: true, model: 'Claude Sonnet 5.5', receipt: { op: 'ride-move', time: '2026-11-19T10:00:00.000Z' } }, service: moved } } } });
|
||||
await wait('document.querySelector("[data-ride-move=gamma]")');
|
||||
assert.equal(await text('#proxy-ride-skips li'), 'Skipped North Works: Its bus is not running. Start it, then ride again.');
|
||||
const item = await b.evaluate('document.querySelector("#proxy-ride-fixtures li").firstChild.textContent.trim()');
|
||||
assert.equal(item, "Gamma Home can't start: it uses stand-in models and its bus is stopped. Move it to your models under Use your models in every organization, then start its bus.");
|
||||
// Step 3 uses the same words.
|
||||
assert.match(await text(STEP3), /Gamma Home can't start: it uses stand-in models and its bus is stopped\. Move it to your models under Use your models in every organization, then start its bus\./);
|
||||
await click('[data-ride-move=gamma]');
|
||||
await wait('window.__confirms.length === 1');
|
||||
assert.match(await b.evaluate('window.__confirms[0]'), /^Move Gamma Home to your models\?/);
|
||||
await wait('/now uses/.test(document.querySelector("#proxy-ride-message")?.textContent ?? "")');
|
||||
assert.equal(await text('#proxy-ride-message'), 'Gamma Home now uses Claude Sonnet 5.5. Start its bus when you are ready. It spends real tokens from now on.');
|
||||
});
|
||||
|
||||
test('settings-s5: a Move that is refused names the organization by its display name', { timeout: 90000 }, async t => {
|
||||
const service = { ...SERVICE_UP, ride: [{ id: 'gamma', state: 'fixture', running: false }] };
|
||||
const accounts = [{ accountId: 'a1', provider: 'claude', displayName: 'Claude work', status: 'active', priority: 1 }];
|
||||
const { wait, text, click } = await setup(t, { path: '/#/settings/model-setup', stub: { service, accounts, posts: { '/admin/ride-move': { body: { moved: false, reason: 'bus-down' } } } } });
|
||||
await wait('document.querySelector("[data-ride-move=gamma]")');
|
||||
await click('[data-ride-move=gamma]');
|
||||
await wait('/not moved/.test(document.querySelector("#proxy-ride-message")?.textContent ?? "")');
|
||||
assert.equal(await text('#proxy-ride-message'), 'Gamma Home was not moved. Its bus is not running. Start it, then ride again.');
|
||||
});
|
||||
|
||||
test('settings-s5: before the first account, step 3 names the organization the sidebar marks "can\'t start"', { timeout: 90000 }, async t => {
|
||||
const service = { ...SERVICE_UP, ride: [{ id: 'gamma', state: 'fixture', running: false }] };
|
||||
const { b, wait, text } = await setup(t, { path: '/#/settings/model-setup', stub: { service, accounts: [] } });
|
||||
await wait(`${mark('gamma')} === "can't start" && document.querySelector("#setup-step-organizations")?.dataset.state`);
|
||||
await wait(`/Gamma Home/.test(document.querySelector(${JSON.stringify(STEP3)})?.textContent ?? "")`);
|
||||
assert.equal(await text(STEP3), "Opens after your first account. Gamma Home can't start now: it uses stand-in models and its bus is stopped. After your first account, move it to your models under Use your models in every organization, then start its bus.");
|
||||
// The card stays closed until there is an account to move it with, and its badge carries the mark's word.
|
||||
assert.equal(await b.evaluate('document.querySelector("#setup-step-organizations").dataset.state'), 'wait');
|
||||
assert.equal(await text('#setup-step-organizations [data-step-badge]'), "Can't start");
|
||||
assert.equal(await b.evaluate('document.querySelector("#setup-step-organizations [data-step-badge]").classList.contains("badge-danger")'), true); // The empty account list reads as one line, not squeezed into the name column.
|
||||
assert.equal(await b.evaluate('(() => { const li = document.querySelector("#proxy-accounts li.muted"); const r = document.createRange(); r.selectNodeContents(li); return li.textContent === "No accounts yet." && r.getClientRects().length === 1; })()'), true);
|
||||
});
|
||||
|
||||
test('settings-s5: Technical details and Advanced show the port and timeouts the status read carries', { timeout: 90000 }, async t => {
|
||||
const { b, wait } = await setup(t, { path: '/#/settings/model-setup' });
|
||||
await wait('document.querySelector("#proxy-setup-state dd")');
|
||||
const rows = await b.evaluate('Object.fromEntries([...document.querySelectorAll("#proxy-setup-state dt")].map(dt => [dt.textContent, dt.nextElementSibling.textContent]))');
|
||||
assert.equal(rows.Initialized, 'Yes, port 18432, wait for a first answer 90 seconds, old key kept 30 minutes after a new one');
|
||||
const advanced = await b.evaluate('Object.fromEntries([...document.querySelectorAll("#proxy-setup-settings dt")].map(dt => [dt.textContent, dt.nextElementSibling.textContent]))');
|
||||
assert.deepEqual(advanced, { Port: '18432', 'Wait for a first answer': '90 seconds', 'Old key kept after a new one': '30 minutes' });
|
||||
});
|
||||
|
||||
test('settings-s5: timeouts init left at the default read as the default', { timeout: 90000 }, async t => {
|
||||
const service = { ...SERVICE_UP, init: { ...SERVICE_UP.init, settings: { port: 18432, rotateGraceMinutes: null, firstOutputSeconds: null } } };
|
||||
const { b, wait } = await setup(t, { path: '/#/settings/model-setup', stub: { service } });
|
||||
await wait('document.querySelector("#proxy-setup-state dd")');
|
||||
assert.equal(await b.evaluate('[...document.querySelectorAll("#proxy-setup-state dt")].find(dt => dt.textContent === "Initialized").nextElementSibling.textContent'),
|
||||
'Yes, port 18432, wait for a first answer: the default, old key kept after a new one: the default');
|
||||
const advanced = await b.evaluate('Object.fromEntries([...document.querySelectorAll("#proxy-setup-settings dt")].map(dt => [dt.textContent, dt.nextElementSibling.textContent]))');
|
||||
assert.deepEqual(advanced, { Port: '18432', 'Wait for a first answer': 'The default', 'Old key kept after a new one': 'The default' });
|
||||
});
|
||||
|
||||
// ---- Messages and the old Members addresses ---------------------------------------------------
|
||||
test('settings-s5: a failed turn in Messages shows its cause, and "Details:" once', { timeout: 90000 }, async t => {
|
||||
const messages = [{ id: 'm1', from_role: 'chief', to_role: 'human', class: 'reply', at: '2026-11-19T10:00:00.000Z', read: true,
|
||||
body: 'The turn failed: Acme Studio can\'t see your models yet. Restart it in Settings.\nDetails: model-catalogue-unavailable' }];
|
||||
const { b, wait } = await setup(t, { path: '/#/orgs/acme/messages', fixture: { messages } });
|
||||
await wait('document.querySelector(".s1-message")');
|
||||
const shown = await b.evaluate('document.querySelector(".s1-message").textContent');
|
||||
assert.equal(shown.match(/Details:/g)?.length, 1, shown);
|
||||
assert.match(shown, /^The assistant couldn't reply: Acme Studio can't see your models yet\. Restart it in Settings\.Details: model-catalogue-unavailable$/);
|
||||
});
|
||||
|
||||
test('settings-s5: a failed turn without a Details part keeps the general line', { timeout: 90000 }, async t => {
|
||||
const messages = [{ id: 'm1', from_role: 'chief', to_role: 'human', class: 'reply', at: '2026-11-19T10:00:00.000Z', read: true, body: 'The turn failed: spawn-refused' }];
|
||||
const { b, wait } = await setup(t, { path: '/#/orgs/acme/messages', fixture: { messages } });
|
||||
await wait('document.querySelector(".s1-message")');
|
||||
assert.equal(await b.evaluate('document.querySelector(".s1-message").textContent'), "The assistant couldn't reply. Try again, or check Agents.Details: spawn-refused");
|
||||
});
|
||||
|
||||
test('settings-s5: #/settings/members and #/orgs/<id>/settings/members land on that organization\'s Agents page', { timeout: 90000 }, async t => {
|
||||
const { b, wait } = await setup(t, { path: '/#/orgs/north/settings/members' });
|
||||
await wait('location.hash === "#/settings/agents?org=north" && document.querySelector("#settings-nav a[href=\'#/settings/agents?org=north\'][aria-current=page]")');
|
||||
await b.evaluate('location.hash = "#/settings/members"; true');
|
||||
await wait('location.hash === "#/settings/agents?org=north" && document.querySelector("#settings-page-title")?.textContent === "Agents"');
|
||||
});
|
||||
|
||||
// Shots for the build report, beside the mockups: run with MOSAIC_UX_SHOTS=<dir>. Each page is shot whole
|
||||
// at 390 and 1440, light and dark, and measured: nothing scrolls sideways, and every control Tab reaches
|
||||
// shows a focus ring.
|
||||
test('shots', { timeout: 900000, skip: !process.env.MOSAIC_UX_SHOTS }, async t => {
|
||||
const { mkdir, writeFile } = await import('node:fs/promises');
|
||||
const { join } = await import('node:path');
|
||||
const dir = process.env.MOSAIC_UX_SHOTS; await mkdir(dir, { recursive: true });
|
||||
const settle = () => new Promise(r => setTimeout(r, 1000));
|
||||
const reasons = [
|
||||
{ accountId: 'a1', provider: 'claude', displayName: 'Claude work', status: 'unavailable', priority: 2, reason: 'The account is out of usage. It resets at 2026-11-20 05:00 UTC. Wait until then, or add another account.' },
|
||||
{ accountId: 'a2', provider: 'codex', displayName: 'Codex home', status: 'error', priority: 1, reason: 'The account needs to sign in again. Sign in again, or add another account.' },
|
||||
{ accountId: 'a3', provider: 'codex', displayName: 'Codex work', status: 'active', priority: 0 },
|
||||
];
|
||||
const failed = [{ id: 'm1', from_role: 'chief', to_role: 'human', class: 'reply', at: '2026-11-19T10:00:00.000Z', read: true,
|
||||
body: 'The turn failed: Acme Studio can\'t see your models yet. Restart it in Settings.\nDetails: model-catalogue-unavailable' }];
|
||||
const PAGES = [
|
||||
{ name: 'org-models', path: '/#/settings/routing?org=acme', ready: 'document.querySelector("#models-in-force")?.textContent.includes("does not use")' },
|
||||
{ name: 'org-models-riding', path: '/#/settings/routing?org=acme', ready: 'document.querySelector("#models-in-force")?.textContent.includes("does not use")',
|
||||
after: async (b, wait) => { await b.evaluate('document.querySelector("#models-yours").click(); true'); await wait('/^Saved/.test(document.querySelector("#models-receipt")?.textContent ?? "")'); } },
|
||||
{ name: 'org-models-not-set-up', path: '/#/settings/routing?org=acme', stub: { service: { ...SERVICE_UP, init: { initialized: false, partial: false, problem: null, files: [] } } },
|
||||
ready: 'document.querySelector("#models-service") && document.querySelector("#models-in-force")?.textContent.includes("does not use")' },
|
||||
{ name: 'org-models-restart', path: '/#/settings/routing?org=acme', fixture: { routes: { acme: { catalogue: { status: 'unavailable', notices: [], models: [] } } } }, stub: { plugins: pluginsRead(['fixture', 'cliproxy']) },
|
||||
ready: 'document.querySelector("#routes-catalogue.banner [data-primary]") && document.querySelector("#models-in-force")?.textContent.startsWith("Now: Acme Studio uses")' },
|
||||
{ name: 'account-reasons', path: '/#/settings/model-setup?step=accounts', stub: { accounts: reasons }, ready: 'document.querySelectorAll("#proxy-accounts .account-reason").length === 2' },
|
||||
{ name: 'step3-cant-start', path: '/#/settings/model-setup', stub: { service: { ...SERVICE_UP, ride: [{ id: 'gamma', state: 'fixture', running: false }] }, accounts: [] },
|
||||
ready: `/Gamma Home/.test(document.querySelector(${JSON.stringify(STEP3)})?.textContent ?? "")` },
|
||||
{ name: 'technical-details', path: '/#/settings/model-setup', ready: 'document.querySelector("#proxy-setup-state dd")',
|
||||
after: async b => { await b.evaluate('document.querySelector("#proxy-setup-advanced").open = true; document.querySelector("#proxy-setup-tech").open = true; true'); } },
|
||||
{ name: 'messages-details', path: '/#/orgs/acme/messages', fixture: { messages: failed }, ready: 'document.querySelector(".s1-message")' },
|
||||
];
|
||||
const report = [];
|
||||
for (const page of PAGES) for (const scheme of ['light', 'dark']) for (const width of [390, 1440]) {
|
||||
const { b, wait } = await setup(t, { path: page.path, fixture: page.fixture, stub: page.stub, viewport: [width, width === 390 ? 844 : 900] });
|
||||
await b.call('Emulation.setScrollbarsHidden', { hidden: true });
|
||||
await b.call('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: scheme }] });
|
||||
await wait(`(${page.ready}) && !!window.MosaicAppearance`);
|
||||
await b.evaluate(`window.MosaicAppearance.ready.then(() => window.MosaicAppearance.set({ mode: ${JSON.stringify(scheme)} }))`);
|
||||
if (page.after) await page.after(b, wait, width);
|
||||
await settle();
|
||||
const sideways = await b.evaluate('document.documentElement.scrollWidth > document.documentElement.clientWidth');
|
||||
const noRing = [], seen = new Set();
|
||||
for (let i = 0; i < 120; i++) {
|
||||
await b.call('Input.dispatchKeyEvent', { type: 'keyDown', key: 'Tab', code: 'Tab', windowsVirtualKeyCode: 9 });
|
||||
await b.call('Input.dispatchKeyEvent', { type: 'keyUp', key: 'Tab', code: 'Tab', windowsVirtualKeyCode: 9 });
|
||||
const r = await b.evaluate(`(() => { const e = document.activeElement; if (!e || e === document.body) return null; const cs = getComputedStyle(e);
|
||||
const ring = (cs.outlineStyle !== 'none' && parseFloat(cs.outlineWidth) > 0) || (cs.boxShadow && cs.boxShadow !== 'none');
|
||||
return { key: e.id || e.getAttribute('href') || e.tagName + ':' + (e.textContent || '').trim().slice(0, 30), ring }; })()`);
|
||||
if (!r || seen.has(r.key)) break; seen.add(r.key);
|
||||
if (!r.ring) noRing.push(r.key);
|
||||
}
|
||||
await b.evaluate('document.activeElement?.blur(); window.scrollTo(0, 0); true'); await settle();
|
||||
const shot = `${page.name}-${width}-${scheme}.png`;
|
||||
let metrics = await b.call('Page.getLayoutMetrics'), size = metrics.cssContentSize || metrics.contentSize;
|
||||
await b.viewport(width, Math.ceil(size.height)); await settle();
|
||||
metrics = await b.call('Page.getLayoutMetrics'); size = metrics.cssContentSize || metrics.contentSize;
|
||||
const { data } = await b.call('Page.captureScreenshot', { format: 'png', captureBeyondViewport: true, clip: { x: 0, y: 0, width: Math.ceil(size.width), height: Math.ceil(size.height), scale: 1 } });
|
||||
await writeFile(join(dir, shot), Buffer.from(data, 'base64'));
|
||||
report.push({ page: page.name, width, scheme, shot, sideways, noRing });
|
||||
await b.close();
|
||||
}
|
||||
await writeFile(join(dir, 'measure.json'), JSON.stringify(report, null, 1));
|
||||
for (const r of report) {
|
||||
assert.equal(r.sideways, false, `${r.shot} scrolls sideways`);
|
||||
assert.deepEqual(r.noRing, [], `${r.shot}: controls with no visible focus ring`);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,516 @@
|
||||
// #1873 settings-s5b (DESIGN.md 3.1, mockups models-setup-failed and models-advanced): Model setup
|
||||
// shows the service's last lines (GET /api/proxy/admin/log) on step 1 when setup stopped and under
|
||||
// Technical details, when the service key was made (GET /api/proxy/admin/service-key), and the setup
|
||||
// history a page at a time (GET /api/proxy/admin/history). All three are the instance owner's: a page
|
||||
// without proxyAdmin shows none of them and sends no /api/proxy/admin/ request. Also: a focus request
|
||||
// for an organization's Models outcome line is dropped when the owner goes to any other page.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { startServer } from '../src/serve.mjs';
|
||||
import { createDevOwner } from '../src/dev-owner.mjs';
|
||||
import { browser } from './browser.mjs';
|
||||
import { close } from './fixture.mjs';
|
||||
|
||||
const LIST = [
|
||||
{ id: 'acme', label: 'Acme Studio', theme: 'cobalt', busHealthy: true },
|
||||
{ id: 'gamma', label: 'Gamma Home', theme: 'rosewood', busHealthy: true },
|
||||
];
|
||||
const VIEW = {
|
||||
business: 'acme', revision: 'a'.repeat(64), harnesses: ['pi'], models: ['fixture-deterministic'],
|
||||
providerModels: { 'fixture-deterministic': { module: 'fixture', upstream: 'fixture', model: 'fixture-deterministic' } },
|
||||
levels: { pi: ['off', 'low'] }, instances: {}, catalogue: { status: 'ready', observedAt: new Date().toISOString(), models: [], notices: [] },
|
||||
};
|
||||
|
||||
async function orgFixture() {
|
||||
const { checkRootAgents } = await import('../../organization/src/index.mjs');
|
||||
const rolesDir = fileURLToPath(new URL('../../../roles', import.meta.url));
|
||||
const root = { state: 'ok', ...checkRootAgents({ agentsVersion: 1, agents: {} }, { rolesDir }) };
|
||||
const empty = () => Promise.resolve([]);
|
||||
const member = org => ({
|
||||
id: org.id, doc: { appearance: { theme: org.theme }, roles: { chief: { definition: 'chief-of-staff' } }, projects: {} },
|
||||
status: async () => ({ healthy: true, pid: 4242, boot: null }),
|
||||
options: () => ({ bus: { inbox: empty, agents: empty, 'thread.claims': empty, tasks: empty, usage: () => Promise.resolve({}), limits: () => Promise.resolve({}), decisions: empty }, chat: { threads: empty, projects: empty } }),
|
||||
});
|
||||
const organizations = {
|
||||
list: async () => LIST,
|
||||
members: async () => LIST.map(member),
|
||||
resolve: async id => {
|
||||
const org = LIST.find(o => o.id === id); if (!org) return null;
|
||||
return {
|
||||
busHealthy: true,
|
||||
reads: { business: async () => ({ business: { id, human: 'jason', projects: [], appearance: { theme: org.theme } } }), settings: async () => ({ business: id, release: 'fixture', notifier: {} }), queue: async () => ({ rows: [] }), busStatus: async () => ({}) },
|
||||
bus: { messages: async () => ({ messages: [], unread: 0, total: 0 }), agents: empty, 'thread.claims': empty, inbox: empty, tasks: empty, usage: async () => ({}), routes: async () => ({ ...VIEW, business: id }), instances: async () => ({ business: id, revision: 'a'.repeat(64), launch: true, noHire: [], retired: [], definitions: {}, instances: {} }) },
|
||||
chat: { projects: empty, threads: empty },
|
||||
};
|
||||
},
|
||||
};
|
||||
return { organizations, agentLevels: { loadRoot: () => root, rolesDir, ctx: { configDir: '/cfg', dataRoot: '/data', instance: 'test' } } };
|
||||
}
|
||||
|
||||
const T = n => `2026-11-19T10:0${n}:00.000Z`;
|
||||
const SERVICE_UP = { next: ['accounts'], init: { initialized: true, partial: false, problem: null, settings: { port: 18432, rotateGraceMinutes: 30, firstOutputSeconds: 90 }, files: [] },
|
||||
release: { fetched: true, verified: true, version: '8.0.42', panelVersion: '1.26.7' },
|
||||
unit: { unit: 'mosaic-proxy.service', installed: true, active: 'active', port: 18432, listening: true, healthy: true }, fence: { set: false }, initLock: { present: false, blockers: [] }, fetch: { state: 'idle' }, organizations: [], ride: [] };
|
||||
// Setup stopped: the download failed, so step 1 says so and opens the service's last lines.
|
||||
const SERVICE_FAILED = { ...SERVICE_UP, next: ['fetch'], release: { fetched: false, verified: false, version: '8.0.42', panelVersion: '1.26.7' },
|
||||
init: { initialized: false, partial: false, problem: null, files: [] }, unit: { unit: 'mosaic-proxy.service', installed: false, active: 'inactive' },
|
||||
fetch: { state: 'failed', error: 'proxy-install-invalid' } };
|
||||
// Setup stopped at the start step: the service is installed but did not answer, so the start was refused.
|
||||
const SERVICE_START = { ...SERVICE_UP, next: ['unit-start'], unit: { unit: 'mosaic-proxy.service', installed: true, active: 'inactive', port: 18432, listening: false, healthy: false } };
|
||||
const START_REFUSED = { 'POST unit/start': { status: 503, body: { error: 'proxy-unit-unhealthy' } } };
|
||||
const LOG_OK = { unit: 'mosaic-proxy.service', lines: ['Nov 19 10:00:01 host proxy[4242]: listening on 127.0.0.1:18432', 'Nov 19 10:00:02 host proxy[4242]: loaded key [redacted]'], redacted: 1, dropped: 2 };
|
||||
const KEY_OK = { madeAt: T(3), renewedAt: null, renewal: 'not-built', problem: null };
|
||||
// Three pages, newest first: the first holds four entries (one withheld), the second is empty but
|
||||
// names a third, and the third holds the oldest entry.
|
||||
const PAGE1 = { entries: [{ op: 'unit.start', time: T(4), user: 'jason' }, { op: 'init', time: T(5), user: '[redacted]' }, { withheld: true },
|
||||
{ op: 'account.proxy.apikey.add', time: T(7), org: 'acme', accountId: 'a1b2c3d4e5f6', user: 'jason' }], next: 3 };
|
||||
const PAGE2 = { entries: [], next: 1 };
|
||||
const PAGE3 = { entries: [{ op: 'ride-move', time: T(1), org: 'gamma', user: 'jason' }], next: null };
|
||||
const PAGES = { 'history?limit=25': PAGE1, 'history?limit=25&before=3': PAGE2, 'history?limit=25&before=1': PAGE3 };
|
||||
const LOG = 'log?lines=20';
|
||||
const OK = { [LOG]: LOG_OK, 'service-key': KEY_OK, ...PAGES };
|
||||
|
||||
// The instance owner's flags (or not), the setup status, and the three reads by name and query. An
|
||||
// answer may be a list: each read takes the next one and the last one stays. window.__admin records
|
||||
// every /api/proxy/admin/ request the page sends, window.__holds pauses a read by name, and window.__queue
|
||||
// pauses every request to a name, each until its own resolver runs. A write answers from admin['POST name'].
|
||||
const stub = ({ owner = true, service = SERVICE_UP, admin = OK } = {}) => `(() => {
|
||||
const real = window.fetch.bind(window);
|
||||
window.__s5b = ${JSON.stringify({ owner, service, admin })}; window.__admin = []; window.__writes = []; window.__holds = {}; window.__queue = {};
|
||||
const json = (body, status = 200) => new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
|
||||
window.fetch = async (input, init = {}) => {
|
||||
const url = new URL(typeof input === 'string' ? input : input.url, location.href), path = url.pathname, method = (init.method || 'GET').toUpperCase();
|
||||
const at = path.indexOf('/proxy/admin/');
|
||||
if (at >= 0) {
|
||||
const name = path.slice(at + 13) + url.search; window.__admin.push(method + ' ' + name);
|
||||
if (method !== 'GET') { window.__writes.push(path); const w = __s5b.admin[method + ' ' + name]; return w ? json(w.body, w.status) : json({ error: 'stub' }, 500); }
|
||||
if (name === 'status') return json(__s5b.service);
|
||||
if (window.__holds[name]) await new Promise(r => { window.__holds[name] = r; });
|
||||
if (window.__queue[name]) await new Promise(r => window.__queue[name].push(r));
|
||||
let a = __s5b.admin[name];
|
||||
if (Array.isArray(a)) a = a.length > 1 ? a.shift() : a[0];
|
||||
if (!a) return json({ error: 'fixture-unexpected-api' }, 403);
|
||||
return a.status ? json(a.body, a.status) : json(a);
|
||||
}
|
||||
if (method !== 'GET' && /\\/proxy\\//.test(path)) { window.__writes.push(path); return json({ error: 'stub' }, 500); }
|
||||
if (method !== 'GET') return real(input, init);
|
||||
if (/\\/api\\/(orgs\\/[^/]+\\/)?config$/.test(path)) { const c = await (await real(input, init)).json(); return json({ ...c, orgSetup: true, proxyAdmin: __s5b.owner, proxyAdminSend: __s5b.owner, ...(/\\/orgs\\//.test(path) ? { settingsSend: true } : {}) }); }
|
||||
if (path.endsWith('/proxy/plugins')) return json({ revision: 'b'.repeat(64), available: { harness: ['pi'], 'model-provider': ['fixture', 'cliproxy'] }, plugins: { harness: ['pi'], 'model-provider': ['fixture'] } });
|
||||
if (path.endsWith('/proxy/accounts')) return json({ accounts: [] });
|
||||
if (path.endsWith('/proxy/keys')) return json({ keys: [] });
|
||||
if (path.endsWith('/triage/route')) return json({ route: null });
|
||||
return real(input, init);
|
||||
};
|
||||
})();`;
|
||||
|
||||
async function setup(t, { path = '/#/settings/model-setup', viewport = [1440, 900], ...options } = {}) {
|
||||
const { organizations, agentLevels } = await orgFixture();
|
||||
const server = await startServer({ port: 0, sso: createDevOwner(), organizations, agentLevels, triage: null, send: async () => ({}), log: () => {} });
|
||||
t.after(() => close(server));
|
||||
const b = await browser({ machine: false }); t.after(() => b.close());
|
||||
await b.viewport(...viewport);
|
||||
await b.call('Page.addScriptToEvaluateOnNewDocument', { source: stub(options) });
|
||||
await b.navigate(`http://127.0.0.1:${server.address().port}${path}`);
|
||||
const wait = expression => b.evaluate(`(async()=>{for(let i=0;i<250;i++){if(${expression})return true;await new Promise(r=>setTimeout(r,30))}throw Error("wait failed: "+${JSON.stringify(expression)}+" HASH "+location.hash)})()`);
|
||||
const text = sel => b.evaluate(`document.querySelector(${JSON.stringify(sel)})?.textContent ?? null`);
|
||||
const click = sel => b.evaluate(`(() => { const el = document.querySelector(${JSON.stringify(sel)}); if (!el) throw Error("no ${sel.replace(/"/g, '')}"); el.click(); return true; })()`);
|
||||
const reads = () => b.evaluate('window.__admin.filter(r => r !== "GET status")');
|
||||
return { b, wait, text, click, reads };
|
||||
}
|
||||
const READY = 'document.querySelector("#proxy-setup-state dd") && !document.querySelector("#setup-step-service").hidden';
|
||||
const openTech = b => b.evaluate('(() => { const d = document.querySelector("#proxy-setup-tech"); d.open = true; return true; })()');
|
||||
const items = b => b.evaluate('[...document.querySelectorAll("#proxy-setup-history li")].map(li => ({ text: li.textContent, hidden: li.hidden, withheld: li.hasAttribute("data-withheld") }))');
|
||||
const KEY_TEXT = /ms[kotp]-[A-Za-z0-9_-]{4,}/i;
|
||||
|
||||
// ---- Each part renders from its read ---------------------------------------------------------
|
||||
test('settings-s5b: Technical details shows when the service key was made, the service\'s last lines and the newest history, and reads none of them until it opens', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click, reads } = await setup(t);
|
||||
await wait(READY);
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
assert.deepEqual(await reads(), [], 'nothing is read before Technical details opens');
|
||||
await openTech(b);
|
||||
await wait('/Mosaic made it on/.test(document.querySelector("#proxy-setup-keymade-text")?.textContent ?? "") && document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
// The service key: when it was made, that renewal is not built, never the key.
|
||||
// It is the last row of the state list, after Release and Service, as models-advanced shows it.
|
||||
assert.equal(await b.evaluate('[...document.querySelectorAll("#proxy-setup-state dt")].at(-1).textContent'), 'Service key');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-keymade-text").closest("dl").id'), 'proxy-setup-state');
|
||||
const key = await text('#proxy-setup-keymade-text');
|
||||
assert.match(key, /^Mosaic made it on .+\. You never see it\. Renewing it is not built yet\.$/);
|
||||
assert.equal(key.includes(await b.evaluate(`window.MosaicLocalTime(${JSON.stringify(T(3))})`)), true);
|
||||
// History, newest first, three shown with Show all; the withheld entry is a plain row.
|
||||
const list = await items(b);
|
||||
assert.deepEqual(list.map(i => i.hidden), [false, false, false, true]);
|
||||
assert.match(list[0].text, /^Account added in Acme Studio · .+ · by jason$/);
|
||||
assert.equal(list[1].text, 'Withheld. The Console kept this entry back because it may hold a secret.');
|
||||
assert.equal(list[1].withheld, true);
|
||||
assert.match(list[2].text, /^Set up · .+ · by someone whose name Mosaic hid$/);
|
||||
assert.match(list[3].text, /^Started · .+ · by jason$/);
|
||||
assert.equal(await text('#proxy-setup-history-all'), 'Show all');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), true);
|
||||
// The lines: read when their box opens, shown as the answer has them, with what was removed.
|
||||
assert.deepEqual(await reads(), ['GET service-key', 'GET history?limit=25']);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech-lines").open = true; true');
|
||||
await wait('document.querySelector("#proxy-setup-tech-lines pre.setup-log")');
|
||||
assert.equal(await text('#proxy-setup-tech-lines summary'), 'The service\'s last lines');
|
||||
assert.equal(await text('#proxy-setup-tech-lines pre.setup-log'), LOG_OK.lines.join('\n'));
|
||||
assert.equal(await text('#proxy-setup-tech-lines [data-log] p'), 'Mosaic removes anything that looks like a key. It removed something from 1 line. 2 lines were left out.');
|
||||
assert.deepEqual(await reads(), ['GET service-key', 'GET history?limit=25', 'GET log?lines=20']);
|
||||
// Read again reads the lines once more.
|
||||
await click('#proxy-setup-tech-lines [data-log] button');
|
||||
await wait('window.__admin.filter(r => r === "GET log?lines=20").length === 2 && document.querySelector("#proxy-setup-tech-lines pre.setup-log")');
|
||||
});
|
||||
|
||||
test('settings-s5b: when setup stops, step 1 opens the service\'s last lines from the log read', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, reads } = await setup(t, { service: SERVICE_START, admin: { ...OK, ...START_REFUSED } });
|
||||
await wait(READY + ' && document.querySelector("#proxy-setup-go") && !document.querySelector("#proxy-setup-go").hidden');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-go").click(); true');
|
||||
await wait('document.querySelector("#proxy-setup-lines pre.setup-log")');
|
||||
assert.deepEqual(await b.evaluate('window.__writes.map(p => p.slice(p.indexOf("/proxy/admin/") + 13))'), ['unit/start']);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-lines").hidden'), false);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-lines").open'), true);
|
||||
assert.equal(await text('#proxy-setup-lines pre.setup-log'), LOG_OK.lines.join('\n'));
|
||||
assert.match(await text('#proxy-setup-lines [data-log] p'), /^Mosaic removes anything that looks like a key\./);
|
||||
assert.doesNotMatch(await text('#proxy-setup-lines'), /can't read the service's lines yet|Nothing reported/);
|
||||
// The status read polls; the lines are read once, not on every paint.
|
||||
await new Promise(r => setTimeout(r, 1500));
|
||||
assert.deepEqual(await reads(), ['POST unit/start', 'GET log?lines=20']);
|
||||
});
|
||||
|
||||
test('settings-s5b: a service that is not installed has no lines, and step 1 says so plainly, not as a failed read', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text } = await setup(t, { service: SERVICE_FAILED, admin: { ...OK, [LOG]: { status: 503, body: { error: 'not-configured' } } } });
|
||||
await wait('document.querySelector("#proxy-setup-lines [data-log] p")');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-lines").open'), true);
|
||||
assert.equal(await text('#proxy-setup-lines [data-log] p'), 'The model service is not installed, so it has no lines yet.');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-lines [data-log] p").className'), 'small muted');
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#proxy-setup-lines .banner.err")'), false);
|
||||
assert.doesNotMatch(await text('#proxy-setup-lines'), /could not be read/);
|
||||
});
|
||||
|
||||
test('settings-s5b: a service key that is not made yet, or that cannot be checked, says so', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text } = await setup(t, { admin: { ...OK, 'service-key': [{ madeAt: null, renewedAt: null, renewal: 'not-built', problem: 'missing' }, { madeAt: null, renewedAt: null, renewal: 'not-built', problem: 'unreadable' }] } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('/Not made yet/.test(document.querySelector("#proxy-setup-keymade-text")?.textContent ?? "")');
|
||||
assert.equal(await text('#proxy-setup-keymade-text'), 'Not made yet. Set up model service in step 1 makes it. You never see it.');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = false; true');
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
await openTech(b);
|
||||
await wait('/can.t tell/.test(document.querySelector("#proxy-setup-keymade-text")?.textContent ?? "")');
|
||||
assert.match(await text('#proxy-setup-keymade-text'), /^Mosaic can't tell when it was made/);
|
||||
});
|
||||
|
||||
// ---- Failed reads -----------------------------------------------------------------------------
|
||||
test('settings-s5b: each failed read shows its line with Read again, and Read again works', { timeout: 90000 }, async t => {
|
||||
const fail = code => ({ status: 503, body: { error: code } });
|
||||
const { b, wait, text, click, reads } = await setup(t, { admin: { ...OK, [LOG]: [fail('outcome-unknown'), LOG_OK], 'service-key': [fail('not-configured'), KEY_OK], 'history?limit=25': [fail('outcome-unknown'), PAGE1] } });
|
||||
await wait(READY); await openTech(b);
|
||||
// The service key.
|
||||
await wait('document.querySelector("#proxy-setup-keymade-retry")');
|
||||
assert.equal(await text('#proxy-setup-keymade-text'), 'When it was made could not be read. Model service setup is not available on this Console. (not-configured)');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-keymade-text").className'), 'banner err', 'the same failure style as the history and the lines');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-keymade-retry").focus(); document.querySelector("#proxy-setup-keymade-retry").click(); true');
|
||||
await wait('/Mosaic made it on/.test(document.querySelector("#proxy-setup-keymade-text")?.textContent ?? "")');
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#proxy-setup-keymade-retry")'), false);
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-keymade-text', 'focus stays on the answer, not the page');
|
||||
// History.
|
||||
await wait('!document.querySelector("#proxy-setup-history-retry").hidden');
|
||||
assert.equal(await text('#proxy-setup-history-message'), 'History could not be read. The host could not read the history. (outcome-unknown)');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-message").className'), 'banner err');
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-setup-history li").length'), 0);
|
||||
await click('#proxy-setup-history-retry');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-retry").hidden'), true);
|
||||
assert.equal(await text('#proxy-setup-history-message'), '');
|
||||
// The lines.
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech-lines").open = true; true');
|
||||
await wait('document.querySelector("#proxy-setup-tech-lines [data-log] .banner.err")');
|
||||
assert.equal(await text('#proxy-setup-tech-lines [data-log] .banner.err'), 'The service\'s lines could not be read. The host could not read the service\'s lines. (outcome-unknown)');
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#proxy-setup-tech-lines pre")'), false);
|
||||
await b.evaluate('(() => { const r = document.querySelector("#proxy-setup-tech-lines [data-log] button"); r.focus(); r.click(); return true; })()');
|
||||
await wait('document.querySelector("#proxy-setup-tech-lines pre.setup-log")');
|
||||
assert.equal(await b.evaluate('document.activeElement?.closest("[data-log]") !== null && document.activeElement.textContent === "Read again"'), true);
|
||||
assert.deepEqual(await reads(), ['GET service-key', 'GET history?limit=25', 'GET service-key', 'GET history?limit=25', 'GET log?lines=20', 'GET log?lines=20']);
|
||||
});
|
||||
|
||||
// ---- Owner only -------------------------------------------------------------------------------
|
||||
test('settings-s5b: a page that is not the instance owner\'s shows none of the three and sends no /api/proxy/admin/ request', { timeout: 90000 }, async t => {
|
||||
// The owner's page, as the control: the three parts are there and read.
|
||||
{
|
||||
const { b, wait } = await setup(t, { service: SERVICE_FAILED });
|
||||
await wait('document.querySelector("#proxy-setup-lines pre.setup-log")');
|
||||
await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4 && /Mosaic made it/.test(document.querySelector("#proxy-setup-keymade-text").textContent)');
|
||||
}
|
||||
const { b, wait } = await setup(t, { owner: false, service: SERVICE_FAILED });
|
||||
await wait('window.MosaicProxy && document.querySelector("#proxy-setup") && document.querySelector("#settings-page-title")');
|
||||
await new Promise(r => setTimeout(r, 800));
|
||||
// Opening every box on the page, even the hidden ones, still reads nothing.
|
||||
await b.evaluate('document.querySelectorAll("#proxy-setup details").forEach(d => { d.open = true; }); true');
|
||||
await new Promise(r => setTimeout(r, 800));
|
||||
assert.deepEqual(await b.evaluate('window.__admin'), []);
|
||||
const shown = sel => b.evaluate(`(() => { const e = document.querySelector(${JSON.stringify(sel)}); return !!e && e.getClientRects().length > 0; })()`);
|
||||
assert.equal(await b.evaluate('[...document.querySelectorAll("#proxy-setup dt")].some(d => d.textContent === "Service key")'), false);
|
||||
for (const sel of ['#proxy-setup-keymade-text', '#proxy-setup-keymade-retry', '#proxy-setup-tech-lines', '#proxy-setup-lines', '#proxy-setup-history', '#proxy-setup-history-message', '#proxy-setup-history-all'])
|
||||
assert.equal(await shown(sel), false, `${sel} is not shown`);
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("[data-log] pre, [data-log] .banner, #proxy-setup-history li").length'), 0);
|
||||
});
|
||||
|
||||
// ---- History paging ---------------------------------------------------------------------------
|
||||
test('settings-s5b: history pages back one page at a time, shows each entry once, and an empty page with an older one still offers it', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click, reads } = await setup(t);
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
await click('#proxy-setup-history-all');
|
||||
assert.deepEqual((await items(b)).map(i => i.hidden), [false, false, false, false]);
|
||||
assert.equal(await text('#proxy-setup-history-all'), 'Show fewer');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-all").getAttribute("aria-expanded")'), 'true');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), false);
|
||||
// Two quick presses send one read.
|
||||
await b.evaluate('window.__holds["history?limit=25&before=3"] = true; true');
|
||||
await b.evaluate('(() => { const o = document.querySelector("#proxy-setup-history-older"); o.focus(); o.click(); o.click(); return true; })()');
|
||||
await wait('typeof window.__holds["history?limit=25&before=3"] === "function"');
|
||||
assert.equal(await text('#proxy-setup-history-older'), 'Reading…');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").disabled'), true);
|
||||
await b.evaluate('window.__holds["history?limit=25&before=3"](); true');
|
||||
// The empty page: no new entry, a line that says so, and Show older still there.
|
||||
await wait('/no entries to show/.test(document.querySelector("#proxy-setup-history-message").textContent)');
|
||||
assert.equal(await text('#proxy-setup-history-message'), 'That page of history had no entries to show. Show older reads further back.');
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-setup-history li").length'), 4);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), false);
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-history-older');
|
||||
// Folding the list keeps Show older, because the line above still names it.
|
||||
await click('#proxy-setup-history-all');
|
||||
assert.equal(await text('#proxy-setup-history-all'), 'Show all');
|
||||
assert.equal(await text('#proxy-setup-history-message'), 'That page of history had no entries to show. Show older reads further back.');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), false);
|
||||
await click('#proxy-setup-history-all');
|
||||
await click('#proxy-setup-history-older');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 5');
|
||||
const list = await items(b);
|
||||
assert.match(list[4].text, /^Gamma Home switched to your models · .+ · by jason$/);
|
||||
assert.equal(new Set(list.map(i => i.text)).size, list.length, 'each entry once');
|
||||
// The last page: no Show older, focus goes to Show fewer, no empty-page line.
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), true);
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-history-all');
|
||||
assert.equal(await text('#proxy-setup-history-message'), '');
|
||||
assert.deepEqual(await reads(), ['GET service-key', 'GET history?limit=25', 'GET history?limit=25&before=3', 'GET history?limit=25&before=1']);
|
||||
// Closing and opening Technical details starts from the newest page again.
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = false; true');
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
await openTech(b);
|
||||
await wait('window.__admin.filter(r => r === "GET history?limit=25").length === 2 && document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
assert.equal(await text('#proxy-setup-history-all'), 'Show all');
|
||||
});
|
||||
|
||||
test('settings-s5b: a history with no entries says so, and a first page that is empty but names an older one offers it', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click } = await setup(t, { admin: { ...OK, 'history?limit=25': [{ entries: [], next: null }, { entries: [], next: 1 }] } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelector("#proxy-setup-history-message").textContent === "No setup history yet."');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-all").hidden'), true);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = false; true');
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
await openTech(b);
|
||||
await wait('/no entries to show/.test(document.querySelector("#proxy-setup-history-message").textContent)');
|
||||
// Nothing is folded away, so there is no Show all, and Show older is offered at once.
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-all").hidden'), true);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), false);
|
||||
await click('#proxy-setup-history-older');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 1');
|
||||
assert.match((await items(b))[0].text, /^Gamma Home switched to your models/);
|
||||
assert.equal(await text('#proxy-setup-history-message'), '');
|
||||
});
|
||||
|
||||
test('settings-s5b: a short first page offers Show older at once, and what it reads stays open', { timeout: 90000 }, async t => {
|
||||
const e = (op, n) => ({ op, time: T(n), user: 'jason' });
|
||||
const { b, wait, click } = await setup(t, { admin: { ...OK, 'history?limit=25': { entries: [e('unit.start', 8), e('unit.stop', 9)], next: 7 }, 'history?limit=25&before=7': { entries: [e('init', 4), e('unit.install', 5), e('unit.restart', 6)], next: null } } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 2');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-all").hidden'), true);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), false);
|
||||
await click('#proxy-setup-history-older');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 5');
|
||||
assert.deepEqual((await items(b)).map(i => i.hidden), [false, false, false, false, false], 'the older entries it read are shown');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-all").textContent'), 'Show fewer');
|
||||
});
|
||||
|
||||
test('settings-s5b: an older page that is not further back than the one asked for is not shown', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click } = await setup(t, { admin: { ...OK, 'history?limit=25&before=3': [{ entries: [{ op: 'init', time: T(2), user: 'jason' }], next: 3 }, PAGE2] } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
await click('#proxy-setup-history-all');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-history-older").focus(); document.querySelector("#proxy-setup-history-older").click(); true');
|
||||
await wait('!document.querySelector("#proxy-setup-history-retry").hidden');
|
||||
assert.match(await text('#proxy-setup-history-message'), /^History could not be read\./);
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-setup-history li").length'), 4, 'the repeated page adds nothing');
|
||||
// The pressed button went away, so focus lands on Read again.
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-history-retry');
|
||||
// Read again answers; it goes away, and focus lands on Show older, not Show fewer.
|
||||
await b.evaluate('document.querySelector("#proxy-setup-history-retry").click(); true');
|
||||
await wait('document.querySelector("#proxy-setup-history-retry").hidden && /no entries to show/.test(document.querySelector("#proxy-setup-history-message").textContent)');
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-history-older');
|
||||
});
|
||||
|
||||
test('settings-s5b: a history answer from before Technical details was closed and opened again is dropped', { timeout: 90000 }, async t => {
|
||||
const STALE = { entries: [{ op: 'init', time: T(1), user: 'stale' }], next: null };
|
||||
const { b, wait } = await setup(t, { admin: { ...OK, 'history?limit=25': [PAGE1, STALE] } });
|
||||
await wait(READY);
|
||||
await b.evaluate('window.__queue["history?limit=25"] = []; true');
|
||||
await openTech(b);
|
||||
await wait('window.__queue["history?limit=25"].length === 1');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = false; true');
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
await openTech(b);
|
||||
await wait('window.__queue["history?limit=25"].length === 2');
|
||||
// The newer read answers first, then the older one.
|
||||
await b.evaluate('window.__queue["history?limit=25"][1](); true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
await b.evaluate('window.__queue["history?limit=25"][0](); true');
|
||||
await new Promise(r => setTimeout(r, 500));
|
||||
const list = await items(b);
|
||||
assert.equal(list.length, 4);
|
||||
assert.equal(list.some(i => /stale/.test(i.text)), false, 'the older answer is not painted');
|
||||
assert.equal(new Set(list.map(i => i.text)).size, 4, 'one copy of each entry');
|
||||
});
|
||||
|
||||
test('settings-s5b: a history entry whose action is not a known one reads Other setup action', { timeout: 90000 }, async t => {
|
||||
const { b, wait } = await setup(t, { admin: { ...OK, 'history?limit=25': { entries: [{ op: 'toString', time: T(1), user: 'jason' }, { op: 'constructor', time: T(2), user: 'jason' }, { op: 'something.new', time: T(3), user: 'jason' }], next: null } } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 3');
|
||||
for (const item of await items(b)) assert.match(item.text, /^Other setup action · .+ · by jason$/);
|
||||
});
|
||||
|
||||
test('settings-s5b: a withheld entry shows as withheld and nothing else from it', { timeout: 90000 }, async t => {
|
||||
const { b, wait } = await setup(t, { admin: { ...OK, 'history?limit=25': { entries: [{ withheld: true, op: 'init', time: T(2), user: 'jason', org: 'acme' }, { withheld: true }], next: null } } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 2');
|
||||
for (const item of await items(b)) {
|
||||
assert.equal(item.text, 'Withheld. The Console kept this entry back because it may hold a secret.');
|
||||
assert.equal(item.withheld, true);
|
||||
}
|
||||
assert.equal(await b.evaluate('[...document.querySelectorAll("#proxy-setup-history li")].every(li => li.classList.contains("muted"))'), true);
|
||||
});
|
||||
|
||||
// ---- No key text ------------------------------------------------------------------------------
|
||||
test('settings-s5b: key-shaped text in an unexpected field of any answer never reaches the page', { timeout: 90000 }, async t => {
|
||||
const k = p => `${p}-AAAAbbbbCCCCdddd0123456789`;
|
||||
const admin = {
|
||||
[LOG]: { unit: k('msk'), lines: ['plain line'], redacted: 0, dropped: 0, key: k('mso'), note: k('mst') },
|
||||
'service-key': { madeAt: k('msk'), renewedAt: k('mso'), renewal: k('mst'), problem: k('msp'), key: k('msk') },
|
||||
'history?limit=25': { entries: [
|
||||
{ op: k('msk'), time: k('mso'), user: k('mst'), org: k('msp'), provider: k('msk'), accountId: k('mso'), models: k('mst'), priority: k('msp'), note: k('msk') },
|
||||
{ op: 'account.proxy.apikey.add', time: T(2), user: `x${k('msk')}`, org: k('mso'), provider: k('mst') },
|
||||
{ op: 'ride-move', time: T(3), org: `${k('msp')}`, user: 'jason' },
|
||||
{ withheld: true, note: k('msk') },
|
||||
], next: null },
|
||||
};
|
||||
{
|
||||
const { b, wait } = await setup(t, { admin });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4 && !/Reading/.test(document.querySelector("#proxy-setup-keymade-text").textContent)');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-history-all").click(); document.querySelector("#proxy-setup-tech-lines").open = true; true');
|
||||
await wait('document.querySelector("#proxy-setup-tech-lines pre.setup-log")');
|
||||
const page = await b.evaluate('document.querySelector("#proxy-setup").textContent + "\\n" + document.body.innerText');
|
||||
assert.doesNotMatch(page, KEY_TEXT);
|
||||
const list = await items(b);
|
||||
assert.match(list[3].text, /^Other setup action · unknown$/);
|
||||
assert.match(list[2].text, /^Account added · /);
|
||||
assert.match(list[1].text, /^An organization switched to your models · /);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-keymade-text").textContent'), /^When it was made is not known\./);
|
||||
}
|
||||
// A refusal whose code is shaped like a key.
|
||||
const refused = { status: 400, body: { error: 'msk-abcdefghijklmnop' } };
|
||||
const { b, wait } = await setup(t, { admin: { [LOG]: refused, 'service-key': refused, 'history?limit=25': refused } });
|
||||
await wait(READY); await openTech(b);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech-lines").open = true; true');
|
||||
await wait('document.querySelector("#proxy-setup-keymade-retry") && !document.querySelector("#proxy-setup-history-retry").hidden && document.querySelector("#proxy-setup-tech-lines .banner.err")');
|
||||
const page = await b.evaluate('document.querySelector("#proxy-setup").textContent + "\\n" + document.body.innerText');
|
||||
assert.doesNotMatch(page, KEY_TEXT);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-history-message").textContent'), /^History could not be read\. The Console refused the read\.$/);
|
||||
});
|
||||
|
||||
// ---- Models focus follow-up -------------------------------------------------------------------
|
||||
// The settings-s5 re-check left one window: a switch whose follow-up read is still out when the owner
|
||||
// goes to a page that returns before render's focus chain (Assistant, Projects, the board) kept its focus request.
|
||||
for (const [name, away] of [['Assistant', '#/assistant'], ['the board', '#/board']])
|
||||
test(`settings-s5b: leaving Models for ${name} while a switch's follow-up read is out, the next visit does not focus the outcome line`, { timeout: 90000 }, async t => {
|
||||
const { b, wait, click } = await setup(t, { path: '/#/settings/routing?org=acme' });
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
// plugins.set succeeds; the routes read that the render after it awaits waits until released.
|
||||
await b.evaluate(`(() => { const f = window.fetch; window.__sets = 0;
|
||||
window.fetch = async (input, init = {}) => { const u = new URL(String(input?.url ?? input), location.href).pathname, m = (init.method || 'GET').toUpperCase();
|
||||
if (m !== 'GET' && u.endsWith('/proxy/plugins/set')) { window.__sets++; return new Response(JSON.stringify({ revision: 'c'.repeat(64) }), { status: 200, headers: { 'content-type': 'application/json' } }); }
|
||||
if (m === 'GET' && u.endsWith('/proxy/plugins') && window.__sets) return new Response(JSON.stringify({ revision: 'c'.repeat(64), available: { harness: ['pi'], 'model-provider': ['fixture', 'cliproxy'] }, plugins: { harness: ['pi'], 'model-provider': ['fixture', 'cliproxy'] } }), { status: 200, headers: { 'content-type': 'application/json' } });
|
||||
if (m === 'GET' && u.endsWith('/routes') && window.__sets && !window.__released) { await new Promise(r => { window.__release = r; }); window.__released = true; }
|
||||
return f(input, init); }; return true; })()`);
|
||||
await click('#models-yours');
|
||||
await wait('typeof window.__release === "function"');
|
||||
await b.evaluate(`location.hash = ${JSON.stringify(away)}; true`);
|
||||
await wait('!location.hash.startsWith("#/settings/") && !document.querySelector("#models-in-force")');
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
await b.evaluate('window.__release(); true');
|
||||
await new Promise(r => setTimeout(r, 400));
|
||||
await b.evaluate(`location.hash = '#/settings/routing?org=acme'; true`);
|
||||
await wait('/^Saved\\./.test(document.querySelector("#models-receipt")?.textContent ?? "") && document.querySelector("#models-in-force")?.textContent.includes("uses your models")');
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
assert.notEqual(await b.evaluate('document.activeElement?.id'), 'models-receipt');
|
||||
});
|
||||
|
||||
// ---- Shots ------------------------------------------------------------------------------------
|
||||
test('shots', { timeout: 900000, skip: !process.env.MOSAIC_UX_SHOTS }, async t => {
|
||||
const { mkdir, writeFile } = await import('node:fs/promises');
|
||||
const { join } = await import('node:path');
|
||||
const dir = process.env.MOSAIC_UX_SHOTS; await mkdir(dir, { recursive: true });
|
||||
const settle = () => new Promise(r => setTimeout(r, 1000));
|
||||
const fail = { status: 503, body: { error: 'outcome-unknown' } };
|
||||
const SHOTS = [
|
||||
{ name: 'setup-start-failed', options: { service: SERVICE_START, admin: { ...OK, ...START_REFUSED } }, ready: READY + ' && document.querySelector("#proxy-setup-go") && !document.querySelector("#proxy-setup-go").hidden',
|
||||
after: async (b, wait) => { await b.evaluate('document.querySelector("#proxy-setup-go").click(); true'); await wait('document.querySelector("#proxy-setup-lines pre.setup-log")'); } },
|
||||
{ name: 'setup-failed-no-lines', options: { service: SERVICE_FAILED, admin: { ...OK, [LOG]: { status: 503, body: { error: 'not-configured' } } } }, ready: 'document.querySelector("#proxy-setup-lines [data-log] p")' },
|
||||
{ name: 'technical-details', ready: READY,
|
||||
after: async (b, wait) => { await b.evaluate('document.querySelector("#proxy-setup-tech").open = true; true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4 && /Mosaic made it/.test(document.querySelector("#proxy-setup-keymade-text").textContent)'); } },
|
||||
{ name: 'technical-details-all', ready: READY,
|
||||
after: async (b, wait) => { await b.evaluate('document.querySelector("#proxy-setup-tech").open = true; true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-history-all").click(); document.querySelector("#proxy-setup-tech-lines").open = true; true');
|
||||
await wait('document.querySelector("#proxy-setup-tech-lines pre.setup-log")'); } },
|
||||
{ name: 'technical-details-failed', options: { admin: { [LOG]: fail, 'service-key': fail, 'history?limit=25': fail } }, ready: READY,
|
||||
after: async (b, wait) => { await b.evaluate('document.querySelector("#proxy-setup-tech").open = true; document.querySelector("#proxy-setup-tech-lines").open = true; true');
|
||||
await wait('document.querySelector("#proxy-setup-keymade-retry") && !document.querySelector("#proxy-setup-history-retry").hidden && document.querySelector("#proxy-setup-tech-lines .banner.err")'); } },
|
||||
];
|
||||
const report = [];
|
||||
for (const shot of SHOTS) for (const scheme of ['light', 'dark']) for (const width of [390, 1440]) {
|
||||
const { b, wait } = await setup(t, { ...(shot.options ?? {}), viewport: [width, width === 390 ? 844 : 900] });
|
||||
await b.call('Emulation.setScrollbarsHidden', { hidden: true });
|
||||
await b.call('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: scheme }] });
|
||||
await wait(`(${shot.ready}) && !!window.MosaicAppearance`);
|
||||
await b.evaluate(`window.MosaicAppearance.ready.then(() => window.MosaicAppearance.set({ mode: ${JSON.stringify(scheme)} }))`);
|
||||
if (shot.after) await shot.after(b, wait);
|
||||
await settle();
|
||||
const sideways = await b.evaluate('document.documentElement.scrollWidth > document.documentElement.clientWidth');
|
||||
const noRing = [], seen = new Set();
|
||||
for (let i = 0; i < 160; i++) {
|
||||
await b.call('Input.dispatchKeyEvent', { type: 'keyDown', key: 'Tab', code: 'Tab', windowsVirtualKeyCode: 9 });
|
||||
await b.call('Input.dispatchKeyEvent', { type: 'keyUp', key: 'Tab', code: 'Tab', windowsVirtualKeyCode: 9 });
|
||||
const r = await b.evaluate(`(() => { const e = document.activeElement; if (!e || e === document.body) return null; const cs = getComputedStyle(e);
|
||||
const ring = (cs.outlineStyle !== 'none' && parseFloat(cs.outlineWidth) > 0) || (cs.boxShadow && cs.boxShadow !== 'none');
|
||||
return { key: e.id || e.getAttribute('href') || e.tagName + ':' + (e.textContent || '').trim().slice(0, 30), ring }; })()`);
|
||||
if (!r || seen.has(r.key)) break; seen.add(r.key);
|
||||
if (!r.ring) noRing.push(r.key);
|
||||
}
|
||||
await b.evaluate('document.activeElement?.blur(); window.scrollTo(0, 0); true'); await settle();
|
||||
const file = `${shot.name}-${width}-${scheme}.png`;
|
||||
let metrics = await b.call('Page.getLayoutMetrics'), size = metrics.cssContentSize || metrics.contentSize;
|
||||
await b.viewport(width, Math.ceil(size.height)); await settle();
|
||||
metrics = await b.call('Page.getLayoutMetrics'); size = metrics.cssContentSize || metrics.contentSize;
|
||||
const { data } = await b.call('Page.captureScreenshot', { format: 'png', captureBeyondViewport: true, clip: { x: 0, y: 0, width: Math.ceil(size.width), height: Math.ceil(size.height), scale: 1 } });
|
||||
await writeFile(join(dir, file), Buffer.from(data, 'base64'));
|
||||
report.push({ page: shot.name, width, scheme, shot: file, sideways, noRing });
|
||||
await b.close();
|
||||
}
|
||||
await writeFile(join(dir, 'measure.json'), JSON.stringify(report, null, 1));
|
||||
for (const r of report) {
|
||||
assert.equal(r.sideways, false, `${r.shot} scrolls sideways`);
|
||||
assert.deepEqual(r.noRing, [], `${r.shot}: controls with no visible focus ring`);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user