fix(model-proxy,webui,bus,plugins): models-offered review round: an organization that rides the proxy but does not answer (bus down, org file unreadable, catalogue not ready) is listed as unanswered, and switching any model off then refuses models-in-use until confirmed, so a down organization is never treated as using nothing; a provider with no entry reads all present models on and a save above 256 that drops a model in use asks first; the read seeds the file only when a sending Console gets a same-origin request (seed:true), never on a cross-site, same-site or metadata-less read or on a read-only Console; the in-use body always carries usedBy and unanswered arrays and the ask names unanswered organizations; the receipt caps off, inUse and unanswered at 32 with a count of the rest; tests add a hard-linked and an extra-key file (damaged), Claude accounts of unknown, error, disabled and unavailable state keeping a key model claude-code only, the bus validator refusing an isNew other than absent or true, and a browser case for the unanswered ask (#1873, lane models-offered)

This commit is contained in:
jason.woltje committed 2026-11-20 14:34:28 -06:00
1 parent d01701a9ce
commit acb228d56f
8 files changed
+180 -39

No files matched your search

@@ -127,3 +127,13 @@ test('the catalogue reads the offered file from the plugin data root at every re
rmSync(file); mkdirSync(file);
assert.ok((await service.read('one')).models.every(m => m.available === false && m.reason === 'model-off'), 'a directory in place of the file is damaged');
});
test('a provider answer whose isNew is anything but absent or true makes the catalogue unavailable', async () => {
let isNew;
const model = () => ({ key: 'fixture-model', module: 'fixture-check', upstream: 'fixture', provider: 'fixture', model: 'fixture-model', label: 'Fixture', available: true, reason: null, harnesses: ['pi'], ...(isNew === undefined ? {} : { isNew }) });
const module = { catalogue: async () => ({ status: 'ready', models: [model()], notices: [] }) };
const makeLoader = () => ({ load: async () => ({ refused: [] }), snapshot: () => [{ name: 'fixture-check', module }] });
const service = createModelCatalogue({ client: () => null, settings: () => ({ 'model-provider': ['fixture-check'] }), makeLoader });
for (const good of [undefined, true]) { isNew = good; assert.equal((await service.read('one', { fresh: true })).status, 'ready', String(good)); }
for (const bad of [false, 'yes', 1, null, {}]) { isNew = bad; assert.equal((await service.read('one', { fresh: true })).status, 'unavailable', JSON.stringify(bad)); }
});
+32 -14
View File
@@ -102,6 +102,7 @@ export function writeOffered(dataRoot, offered) {
const failure = (code, extra = {}) => Object.assign(new Error(code), { code, ...extra });
const ORG_ID = /^[a-z0-9][a-z0-9._-]{0,63}$/;
const REVISION = /^[a-f0-9]{16}$/;
const RECEIPT_LIST = 32;
export function offeredControl({ dataRoot, riders, receipt, exclusive, uid = process.getuid() }) {
const readFile = () => {
@@ -111,18 +112,23 @@ export function offeredControl({ dataRoot, riders, receipt, exclusive, uid = pro
};
// Every provider's present models, in catalogue order, and who uses which. A model is in use when
// an instance's model or fallback model key resolves (the organization's model table, or the
// bus's legacy mapping) to that cliproxy upstream and model id.
// bus's legacy mapping) to that cliproxy upstream and model id. `unanswered` names each
// organization whose usage is unknown: its file could not be read, its bus did not answer, or its
// catalogue (and so its legacy mapping) was not ready. A write that switches a model off treats
// each one as a possible user.
async function survey() {
let list;
try { list = await riders(); } catch { throw failure('outcome-unknown'); }
const riding = (Array.isArray(list) ? list : []).filter(r => r?.cliproxy === true && typeof r.id === 'string' && ORG_ID.test(r.id));
const named = (Array.isArray(list) ? list : []).filter(r => typeof r?.id === 'string' && ORG_ID.test(r.id));
const riding = named.filter(r => r.cliproxy === true);
if (!riding.length) throw failure('not-configured');
const unanswered = new Set(named.filter(r => typeof r.cliproxy !== 'boolean').map(r => r.id));
const answers = await Promise.all(riding.map(async r => { try { return { id: r.id, routes: await r.call('routes') }; } catch { return { id: r.id, routes: null }; } }));
const providers = new Map(), usedBy = new Map();
let ready = false;
for (const { routes } of answers) {
for (const { id, routes } of answers) {
const c = routes?.catalogue;
if (c?.status !== 'ready' || !Array.isArray(c.models)) continue;
if (c?.status !== 'ready' || !Array.isArray(c.models)) { unanswered.add(id); continue; }
ready = true;
for (const m of c.models) {
if (m?.module !== 'cliproxy' || typeof m.upstream !== 'string' || typeof m.model !== 'string' || !MODEL_ID.test(m.model)) continue;
@@ -148,14 +154,15 @@ export function offeredControl({ dataRoot, riders, receipt, exclusive, uid = pro
}
}
}
return { providers, usedBy };
return { providers, usedBy, unanswered: [...unanswered].sort() };
}
const entryOf = (file, p) => (file.offered && Object.hasOwn(file.offered.providers, p) ? file.offered.providers[p] : undefined);
const usersOf = (s, provider, id) => s.usedBy.get(`${provider}\n${id}`) ?? [];
// A provider with no entry yet has every present model on (catalogue() reads it so too).
const onFor = (entry, present) => entry ? entry.on : present.slice(0, MAX_ON);
const onFor = (entry, present) => entry ? entry.on : present;
const view = (file, s) => ({
revision: file.revision,
unanswered: s.unanswered,
providers: [...s.providers].map(([provider, present]) => {
const entry = entryOf(file, provider);
const on = new Set(onFor(entry, present).filter(id => present.includes(id)));
@@ -168,18 +175,22 @@ export function offeredControl({ dataRoot, riders, receipt, exclusive, uid = pro
return {
// GET: the lists. A provider seen for the first time is written as all its present models on
// and seen (the plugin has no rule for "current"), so a model it adds later starts off as New.
// The seed waits when another admin action holds the lock; the view reads the same either way.
async models() {
// The seed changes nothing the catalogue offers: a provider with no entry is all on already.
// So it never seeds a provider with more than MAX_ON models (that would switch some off; the
// owner's first save does it, with the in-use check), and it seeds only when `seed` is true,
// which the Console sets only for a request that passes its write checks. The seed waits when
// another admin action holds the lock; the view reads the same either way.
async models({ seed = false } = {}) {
let file = readFile();
const s = await survey();
const fresh = [...s.providers.keys()].filter(p => !entryOf(file, p));
const fresh = seed === true ? [...s.providers.keys()].filter(p => !entryOf(file, p) && s.providers.get(p).length <= MAX_ON) : [];
if (fresh.length) {
try {
await exclusive('models', async () => {
const now = readFile();
if (now.revision !== file.revision) return;
const providers = { ...(now.offered?.providers ?? {}) };
for (const p of fresh) { const present = s.providers.get(p); providers[p] = { on: present.slice(0, MAX_ON), seen: [...present] }; }
for (const p of fresh) { const present = s.providers.get(p); providers[p] = { on: [...present], seen: [...present] }; }
writeOffered(dataRoot, { version: OFFERED_VERSION, providers });
receipt({ op: 'models.seed', providers: fresh.map(p => ({ provider: p, on: providers[p].on.length })) });
});
@@ -189,7 +200,9 @@ export function offeredControl({ dataRoot, riders, receipt, exclusive, uid = pro
return view(file, s);
},
// POST: one provider's on list, whole. `revision` is the one the read gave; a model in use
// that this switches off refuses unless `confirmInUse` is true, naming who uses it.
// that this switches off refuses unless `confirmInUse` is true, naming who uses it. When it
// switches any model off and an organization's usage is unknown, it refuses the same way and
// names that organization.
async modelsSet({ provider, on, revision, confirmInUse = false, user = null } = {}) {
if (typeof provider !== 'string' || !PROVIDER_ID.test(provider) || typeof revision !== 'string' || !REVISION.test(revision) || typeof confirmInUse !== 'boolean') throw failure('invalid-request');
if (!idList(on, MAX_ON)) throw failure('models-invalid');
@@ -205,13 +218,18 @@ export function offeredControl({ dataRoot, riders, receipt, exclusive, uid = pro
if (on.some(id => !present.includes(id) && !before.includes(id))) throw failure('models-invalid');
const off = before.filter(id => !on.includes(id));
const users = off.flatMap(id => usersOf(s, provider, id).map(u => ({ model: id, ...u })));
if (users.length && confirmInUse !== true) throw failure('models-in-use', { usedBy: users });
const unanswered = off.length ? s.unanswered : [];
if ((users.length || unanswered.length) && confirmInUse !== true) throw failure('models-in-use', { usedBy: users, unanswered });
const seen = [...new Set([...(entry?.seen ?? []), ...present, ...on])];
const providers = { ...(file.offered?.providers ?? {}), [provider]: { on: [...on], seen } };
if (seen.length > MAX_SEEN) throw failure('models-invalid');
const next = writeOffered(dataRoot, { version: OFFERED_VERSION, providers });
const record = { op: 'models.set', provider, on: on.filter(id => !before.includes(id)), off };
if (users.length) record.inUse = users.map(u => ({ model: u.model, org: u.org, instance: u.instance }));
const record = { op: 'models.set', provider, on: on.filter(id => !before.includes(id)), off: off.slice(0, MAX_ON) };
if (off.length > MAX_ON) record.offMore = off.length - MAX_ON;
// Bounded, so the receipt line stays well inside what receiptHistory reads.
if (users.length) record.inUse = users.slice(0, RECEIPT_LIST).map(u => ({ model: u.model, org: u.org, instance: u.instance }));
if (users.length > RECEIPT_LIST) record.inUseMore = users.length - RECEIPT_LIST;
if (unanswered.length) record.unanswered = unanswered.slice(0, RECEIPT_LIST);
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
let written = null;
try { written = receipt(record); } catch { written = null; }
@@ -1,7 +1,7 @@
// #1873 models-offered (A9): the owner's per-provider on/off file and the Console's read and write.
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync, writeFileSync, readFileSync, statSync, symlinkSync, mkdirSync, chmodSync, existsSync } from 'node:fs';
import { mkdtempSync, rmSync, writeFileSync, readFileSync, statSync, symlinkSync, linkSync, mkdirSync, chmodSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { readOffered, writeOffered, offeredControl, offeredFile, offeredRevision, MAX_ON } from '../src/models-offered.mjs';
@@ -31,34 +31,77 @@ test('readOffered: no file is absent; a written file reads ok with its revision;
writeFileSync(other, JSON.stringify(doc), { mode: 0o600 });
rmSync(offeredFile(root)); symlinkSync(other, offeredFile(root));
assert.equal(readOffered(root).state, 'damaged', 'a symlink');
rmSync(offeredFile(root)); linkSync(other, offeredFile(root));
assert.equal(readOffered(root).state, 'damaged', 'a second hard link');
rmSync(offeredFile(root)); writeFileSync(offeredFile(root), '{"version":1,"providers":{"z.ai":{"on":["glm-5"],"seen":[]}}}', { mode: 0o600 });
assert.equal(readOffered(root).state, 'damaged', 'on but never seen');
rmSync(offeredFile(root)); writeFileSync(offeredFile(root), '{"version":1,"providers":{"z.ai":{"on":[],"seen":[],"harness":"pi"}}}', { mode: 0o600 });
assert.equal(readOffered(root).state, 'damaged', 'an unknown key in an entry');
assert.throws(() => writeOffered(root, { ...doc, harness: 'pi' }), { code: 'models-invalid' });
});
test('the first read seeds every present model on and seen, once; a busy lock leaves the file and still answers', async t => {
test('the first read asked to seed writes every present model on and seen, once; a read not asked to, or a busy lock, leaves the file and still answers', async t => {
const models = [entry('zai', 'glm-5'), entry('zai', 'glm-4.6'), entry('codex', 'gpt-5'), entry('zai', 'glm-5'), { module: 'other', upstream: 'zai', model: 'x' }];
const quiet = control(t, { riders: [rider('acme', models)] });
for (const args of [undefined, {}, { seed: 'yes' }, { seed: 1 }]) assert.deepEqual((await quiet.models(args)).providers.map(p => [p.provider, p.on, p.total]), [['z.ai', 2, 2], ['codex', 1, 1]]);
assert.equal(existsSync(offeredFile(quiet.dataRoot)), false, 'no seed unless asked');
assert.deepEqual(quiet.receipts, []);
const busy = control(t, { riders: [rider('acme', models)], busy: true });
const view = await busy.models();
const view = await busy.models({ seed: true });
assert.deepEqual(view.providers.map(p => [p.provider, p.label, p.on, p.total]), [['z.ai', 'z.ai', 2, 2], ['codex', 'ChatGPT / Codex', 1, 1]]);
assert.equal(existsSync(offeredFile(busy.dataRoot)), false);
assert.deepEqual(busy.receipts, []);
const c = control(t, { riders: [rider('acme', models)] });
const first = await c.models();
const first = await c.models({ seed: true });
assert.deepEqual(readOffered(c.dataRoot).offered, { version: 1, providers: { 'z.ai': { on: ['glm-5', 'glm-4.6'], seen: ['glm-5', 'glm-4.6'] }, codex: { on: ['gpt-5'], seen: ['gpt-5'] } } });
assert.deepEqual(c.receipts, [{ op: 'models.seed', providers: [{ provider: 'z.ai', on: 2 }, { provider: 'codex', on: 1 }] }]);
await c.models();
await c.models({ seed: true });
assert.equal(c.receipts.length, 1);
assert.equal(first.revision, readOffered(c.dataRoot).revision);
});
test(`a provider with more than ${MAX_ON} models seeds the first ${MAX_ON} on and every one seen`, async t => {
test(`a provider with more than ${MAX_ON} models is never seeded; it reads all on, and the first save checks every model it switches off`, async t => {
const many = Array.from({ length: MAX_ON + 4 }, (_, i) => entry('zai', `glm-${i}`));
const c = control(t, { riders: [rider('acme', many)] });
const view = await c.models();
assert.deepEqual([view.providers[0].on, view.providers[0].total], [MAX_ON, MAX_ON + 4]);
const file = readOffered(c.dataRoot).offered.providers['z.ai'];
assert.deepEqual([file.on.length, file.seen.length], [MAX_ON, MAX_ON + 4]);
const last = `glm-${MAX_ON + 3}`;
const routes = { instances: { chief: { model: 'mine' } }, providerModels: { mine: entry('zai', last) } };
const c = control(t, { riders: [rider('acme', many, routes), rider('bolt', [entry('codex', 'gpt-5')])] });
const view = await c.models({ seed: true });
assert.deepEqual([view.providers[0].on, view.providers[0].total], [MAX_ON + 4, MAX_ON + 4]);
assert.equal(view.providers[0].models.at(-1).on, true, 'the model in use reads on, as the catalogue offers it');
assert.deepEqual(Object.keys(readOffered(c.dataRoot).offered.providers), ['codex'], 'only the small provider is seeded');
const first = many.slice(0, MAX_ON).map(m => m.model);
await assert.rejects(c.modelsSet({ provider: 'z.ai', on: first, revision: view.revision }), error => {
assert.equal(error.code, 'models-in-use');
assert.deepEqual(error.usedBy, [{ model: last, org: 'acme', instance: 'chief', fallback: false }]);
return true;
});
const saved = await c.modelsSet({ provider: 'z.ai', on: first, revision: view.revision, confirmInUse: true });
assert.equal(saved.on, MAX_ON);
const record = c.receipts.at(-1);
assert.deepEqual(record.off, many.slice(MAX_ON).map(m => m.model), 'the receipt names every model switched off');
assert.equal(readOffered(c.dataRoot).offered.providers['z.ai'].seen.length, MAX_ON + 4);
});
test('an organization whose usage is unknown (its file unreadable, its bus down, its catalogue not ready) counts as a possible user of any model switched off', async t => {
const models = [entry('zai', 'glm-5'), entry('zai', 'glm-4.6')];
const down = { id: 'bolt', cliproxy: true, call: async () => { throw new Error('down'); } };
const unreadable = { id: 'crux', cliproxy: null, call: async () => { throw new Error('not asked'); } };
const notReady = rider('dune', [], { instances: { coder: { model: 'old-glm' } }, catalogue: { status: 'unavailable', models: [], legacy: {} } });
const notRiding = { id: 'east', cliproxy: false, call: async () => { throw new Error('not asked'); } };
const c = control(t, { riders: [rider('acme', models), down, unreadable, notReady, notRiding] });
const view = await c.models({ seed: true });
assert.deepEqual(view.unanswered, ['bolt', 'crux', 'dune']);
await assert.rejects(c.modelsSet({ provider: 'z.ai', on: ['glm-5'], revision: view.revision }), error => {
assert.equal(error.code, 'models-in-use');
assert.deepEqual([error.usedBy, error.unanswered], [[], ['bolt', 'crux', 'dune']]);
return true;
});
assert.equal(readOffered(c.dataRoot).revision, view.revision, 'nothing written');
// Switching nothing off needs no ask.
const same = await c.modelsSet({ provider: 'z.ai', on: ['glm-5', 'glm-4.6'], revision: view.revision });
const saved = await c.modelsSet({ provider: 'z.ai', on: ['glm-5'], revision: same.revision, confirmInUse: true });
assert.equal(saved.on, 1);
assert.deepEqual(c.receipts.at(-1), { op: 'models.set', provider: 'z.ai', on: [], off: ['glm-4.6'], unanswered: ['bolt', 'crux', 'dune'] });
});
test('a damaged file: the read and the write answer outcome-unknown and write nothing', async t => {
@@ -92,7 +135,7 @@ test('in use counts the organization model table and the bus legacy mapping, pri
const legacy = { instances: { coder: { model: 'old-glm' } }, catalogue: { status: 'ready', models: [entry('zai', 'glm-5'), entry('zai', 'glm-4.6')], legacy: { 'old-glm': entry('zai', 'glm-4.6') } } };
const table = { instances: { assistant: { model: 'mine', fallback: { model: 'mine' } }, '../x': { model: 'mine' } }, providerModels: { mine: entry('zai', 'glm-5') }, catalogue: { status: 'ready', models: [] } };
const c = control(t, { riders: [rider('acme', [], legacy), rider('bolt', [], table)] });
const view = await c.models();
const view = await c.models({ seed: true });
assert.deepEqual(view.providers[0].models.map(m => [m.id, m.usedBy]), [
['glm-5', [{ org: 'bolt', instance: 'assistant', fallback: false }, { org: 'bolt', instance: 'assistant', fallback: true }]],
['glm-4.6', [{ org: 'acme', instance: 'coder', fallback: false }]]]);
@@ -234,6 +234,17 @@ test('models-offered: a key model whose name an OAuth Claude account offers stay
assert.deepEqual([zai.available, zai.harnesses], [true, ['claude-code']]);
// A selection of unknown account kind (no catalogue module) named as Claude is still held to claude-code.
assert.deepEqual(route({ selection: { provider: 'zai', model: 'fixture-claude-chat' }, harness: 'pi' }), { code: 'model-harness-incompatible' });
// A Claude account whose state is unknown, signed out or switched off is not listed, but its names
// still count as OAuth: switching the same-named key model on keeps it claude-code only.
for (const accountState of [{ status: 'unknown' }, { status: 'error' }, { status: 'active', disabled: true }, { status: 'active', unavailable: true }]) {
const u = offeredDeps();
u.listAccounts = async () => ({ files: [{ name: 'credential-1', provider: 'claude', ...accountState }] });
u.listGroups = async () => [{ 'base-url': ZAI, keys: [{ auth_index: 'k' }], models: [{ name: 'fixture-chat' }] }];
const cu = await catalogue({ ...u, offered: { version: 1, providers: { 'z.ai': { on: ['fixture-chat'], seen: ['fixture-chat'] } } } });
const label = JSON.stringify(accountState);
assert.deepEqual(cu.models.map(m => [m.upstream, m.model, m.available, m.harnesses]), [['zai', 'fixture-chat', true, ['claude-code']]], label);
assert.deepEqual(preflight({ selection: cu.models[0], harness: 'pi', catalogue: cu }), { allow: false, code: 'model-harness-incompatible' }, label);
}
});
test('models-offered: a damaged file switches every model off and never falls back to all on', async () => {
+10 -3
View File
@@ -114,16 +114,23 @@ export async function proxyAdminHTTP(req, res, { path, admin, call, named, teste
} catch { return refuse('invalid-request'); }
}
// #1873 models-offered (A9). A model in use that a save would switch off is refused with who
// uses it (organization and instance names only), so the page can ask in place.
// uses it (organization and instance names only, and each organization whose usage is unknown),
// so the page can ask in place. The read may write the first seed, so it seeds only when the
// request passes the write checks above: a sending Console, same-origin fetch metadata, and no
// other origin (a same-origin GET fetch sends none).
if (path === '/api/proxy/admin/models' || path === '/api/proxy/admin/models/set') {
try {
if (path === '/api/proxy/admin/models') return done(200, await admin.models());
if (path === '/api/proxy/admin/models') {
const seed = send === true && req.headersDistinct['sec-fetch-site']?.length === 1 && req.headers['sec-fetch-site'] === 'same-origin'
&& (req.headers.origin === undefined || (req.headersDistinct.origin?.length === 1 && req.headers.origin === origin));
return done(200, await admin.models({ seed }));
}
if (!Array.isArray(args.on) || (Object.hasOwn(args, 'confirmInUse') && typeof args.confirmInUse !== 'boolean')) return refuse('invalid-request');
return done(200, await admin.modelsSet({ provider: args.provider, on: args.on, revision: args.revision, confirmInUse: args.confirmInUse === true, user: typeof user === 'string' ? user : null }));
} catch (error) {
const code = ['models-offered-conflict', 'models-in-use', 'models-invalid', 'invalid-request', 'not-configured', 'proxy-down', 'proxy-admin-busy'].includes(error?.code) ? error.code : 'outcome-unknown';
const status = code === 'models-offered-conflict' || code === 'models-in-use' ? 409 : STATUS[code] ?? 400;
return done(status, { error: code, ...(code === 'models-in-use' && Array.isArray(error.usedBy) ? { usedBy: error.usedBy } : {}) });
return done(status, { error: code, ...(code === 'models-in-use' ? { usedBy: Array.isArray(error.usedBy) ? error.usedBy : [], unanswered: Array.isArray(error.unanswered) ? error.unanswered : [] } : {}) });
}
}
try {
+6 -2
View File
@@ -912,7 +912,7 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
...(args === undefined ? {} : { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(args) }) });
if (response.type === 'opaqueredirect' || response.status === 401) throw Object.assign(new Error(), { code: 'signed-out' });
let value = null; try { value = await response.json(); } catch { /* refused below */ }
if (!response.ok) throw Object.assign(new Error(), { code: typeof value?.error === 'string' && /^[a-z-]+$/.test(value.error) ? value.error : 'outcome-unknown', usedBy: Array.isArray(value?.usedBy) ? value.usedBy : [] });
if (!response.ok) throw Object.assign(new Error(), { code: typeof value?.error === 'string' && /^[a-z-]+$/.test(value.error) ? value.error : 'outcome-unknown', usedBy: Array.isArray(value?.usedBy) ? value.usedBy : [], unanswered: Array.isArray(value?.unanswered) ? value.unanswered : [] });
return value;
}
const OFFERED_SAYS = {
@@ -981,9 +981,13 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
if (error.code === 'models-in-use') {
said.textContent = ''; said.className = '';
const who = error.usedBy.map(u => `${u.model}: ${offeredWho(u)}`).join('; ');
const quiet = error.unanswered.map(org => window.MosaicOrg?.row?.(org)?.label || org).join(', ');
const yes = el('button', 'Switch off anyway', 'btn'), no = el('button', 'Keep them on', 'btn'), actions = el('div', undefined, 'actions');
yes.type = 'button'; no.type = 'button'; actions.append(yes, no);
ask.replaceChildren(el('p', `In use: ${who}. Switched off, a model is offered nowhere, so these can no longer use it. Give each one another model on its organization's Models page.`), actions);
const says = [];
if (who) says.push(el('p', `In use: ${who}. Switched off, a model is offered nowhere, so these can no longer use it. Give each one another model on its organization's Models page.`));
if (quiet) says.push(el('p', `Not answering now, so it is not known which models they use: ${quiet}. A model you switch off stops working for them too.`));
ask.replaceChildren(...says, actions);
ask.hidden = false;
yes.addEventListener('click', () => send(true));
no.addEventListener('click', () => { ask.hidden = true; save.focus(); });
+39 -8
View File
@@ -47,7 +47,7 @@ function fakeAdmin() {
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: [] }; },
models: async () => { calls.push(['models']); return { revision: null, providers: [] }; },
models: async args => { calls.push(['models', args]); return { revision: null, providers: [] }; },
modelsSet: async args => { calls.push(['modelsSet', args]); return { provider: args.provider, revision: 'r2', on: 0, total: 0, receipt: null }; },
};
}
@@ -542,9 +542,11 @@ test('models set takes provider, an on list, revision and a true or false confir
assert.equal((await hit(f, ['models/set', 'POST', { ...base, confirmInUse: true }])).status, 200);
assert.deepEqual(f.admin.calls.map(c => c[1]), [{ ...base, confirmInUse: false, user: 'Local operator' }, { ...base, confirmInUse: true, user: 'Local operator' }]);
const usedBy = [{ model: 'glm-5', org: 'acme', instance: 'assistant', fallback: false }];
f.admin.modelsSet = async () => { throw Object.assign(new Error(`${SENTINEL} in use`), { code: 'models-in-use', usedBy }); };
f.admin.modelsSet = async () => { throw Object.assign(new Error(`${SENTINEL} in use`), { code: 'models-in-use', usedBy, unanswered: ['bolt'] }); };
const inUse = await hit(f, ['models/set', 'POST', base]);
assert.equal(inUse.status, 409); assert.deepEqual(inUse.json, { error: 'models-in-use', usedBy });
assert.equal(inUse.status, 409); assert.deepEqual(inUse.json, { error: 'models-in-use', usedBy, unanswered: ['bolt'] });
f.admin.modelsSet = async () => { throw Object.assign(new Error(`${SENTINEL} in use`), { code: 'models-in-use', usedBy: 'x' }); };
assert.deepEqual((await hit(f, ['models/set', 'POST', base])).json, { error: 'models-in-use', usedBy: [], unanswered: [] });
for (const [code, status] of [['models-offered-conflict', 409], ['models-invalid', 400], ['proxy-admin-busy', 409], ['proxy-down', 503], ['not-configured', 503]]) {
f.admin.modelsSet = async () => { throw Object.assign(new Error(`${SENTINEL} /home/owner/x`), { code, usedBy }); };
const r = await hit(f, ['models/set', 'POST', base]);
@@ -559,10 +561,33 @@ test('models set takes provider, an on list, revision and a true or false confir
for (const code of ['models-offered-conflict', 'models-in-use', 'models-invalid']) assert.equal(typeof REFUSALS[code]?.nextStep, 'string', code);
});
// The read may write the first seed, so it asks for one only when the request passes the write checks.
test('models read: the seed is asked for only by a sending Console on a same-origin request', async t => {
const f = await devServer(t);
const seeds = async (headers, server = f) => {
server.admin.calls.length = 0;
const all = { ...server.headers(), ...headers };
for (const [k, v] of Object.entries(all)) if (v === undefined) delete all[k];
const r = await send(server.port, '/api/proxy/admin/models', { headers: all });
assert.equal(r.status, 200);
return server.admin.calls[0][1].seed;
};
assert.equal(await seeds({}), true, 'same origin, with the Origin header');
assert.equal(await seeds({ origin: undefined }), true, 'a same-origin GET fetch sends no Origin');
assert.equal(await seeds({ 'sec-fetch-site': 'cross-site' }), false, 'cross-site');
assert.equal(await seeds({ 'sec-fetch-site': 'same-site' }), false, 'same-site');
assert.equal(await seeds({ 'sec-fetch-site': undefined }), false, 'no fetch metadata');
f.admin.calls.length = 0;
assert.equal((await send(f.port, '/api/proxy/admin/models', { headers: { ...f.headers(), origin: 'http://evil.example' } })).status, 403, 'a foreign Origin never reaches the read');
assert.deepEqual(f.admin.calls, []);
const readOnly = await devServer(t, { send: null });
assert.equal(await seeds({}, readOnly), false, 'a read-only Console');
});
// The real controller behind the real routes, with fake organizations whose route answers carry a planted key.
test('models end to end: the first read seeds every present model on and seen; a stale revision and a model in use refuse; confirm saves; a new model reads New and off; receipts and the file hold labels only', async t => {
test('models end to end: the first read seeds every present model on and seen; a stale revision, a model in use and an organization that does not answer refuse; confirm saves; a new model reads New and off; receipts and the file hold labels only', async t => {
const { createProxyAdmin } = await import('../../model-proxy/src/admin-control.mjs');
const { mkdirSync, statSync } = await import('node:fs');
const { mkdirSync, statSync, existsSync } = await import('node:fs');
const root = mkdtempSync(join(tmpdir(), 'pa-mo-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
@@ -585,8 +610,12 @@ test('models end to end: the first read seeds every present model on and seen; a
riders: async () => orgs, harnesses: () => [], rootAgents: () => ({ state: 'absent', agents: {}, assistant: null }) });
const f = await devServer(t, { proxyAdmin: admin });
const file = join(dataRoot, 'state', 'models-offered.json');
const crossSite = await hit(f, ['models', 'GET'], { 'sec-fetch-site': 'cross-site' });
assert.equal(crossSite.status, 200);
assert.equal(existsSync(file), false, 'a cross-site read seeds nothing');
const first = await hit(f, ['models', 'GET']);
assert.equal(first.status, 200);
assert.deepEqual(first.json.unanswered, ['bolt'], 'bolt\'s bus is down, so its usage is unknown');
assert.match(first.json.revision, /^[a-f0-9]{16}$/);
assert.deepEqual(first.json.providers.map(p => [p.provider, p.on, p.total]), [['z.ai', 2, 2], ['codex', 1, 1]]);
assert.deepEqual(first.json.providers[0].models, [
@@ -601,9 +630,11 @@ test('models end to end: the first read seeds every present model on and seen; a
assert.equal(stale.status, 409); assert.deepEqual(stale.json, { error: 'models-offered-conflict' });
const inUse = await hit(f, ['models/set', 'POST', { provider: 'z.ai', on: ['glm-4.6'], revision: first.json.revision }]);
assert.equal(inUse.status, 409);
assert.deepEqual(inUse.json, { error: 'models-in-use', usedBy: [{ model: 'glm-5', org: 'acme', instance: 'assistant', fallback: false }] });
assert.deepEqual(inUse.json, { error: 'models-in-use', usedBy: [{ model: 'glm-5', org: 'acme', instance: 'assistant', fallback: false }], unanswered: ['bolt'] });
const fallback = await hit(f, ['models/set', 'POST', { provider: 'codex', on: [], revision: first.json.revision, confirmInUse: false }]);
assert.deepEqual(fallback.json, { error: 'models-in-use', usedBy: [{ model: 'gpt-5', org: 'acme', instance: 'assistant', fallback: true }] }, 'a fallback model counts as in use');
assert.deepEqual(fallback.json, { error: 'models-in-use', usedBy: [{ model: 'gpt-5', org: 'acme', instance: 'assistant', fallback: true }], unanswered: ['bolt'] }, 'a fallback model counts as in use');
const quiet = await hit(f, ['models/set', 'POST', { provider: 'z.ai', on: ['glm-5'], revision: first.json.revision }]);
assert.deepEqual(quiet.json, { error: 'models-in-use', usedBy: [], unanswered: ['bolt'] }, 'glm-4.6 has no known user, but bolt did not answer');
const unknown = await hit(f, ['models/set', 'POST', { provider: 'z.ai', on: ['glm-4.6', 'made-up'], revision: first.json.revision }]);
assert.equal(unknown.status, 400); assert.deepEqual(unknown.json, { error: 'models-invalid' }, 'a model never listed cannot be switched on');
assert.deepEqual((await hit(f, ['models/set', 'POST', { provider: 'claude', on: [], revision: first.json.revision }])).json, { error: 'models-invalid' }, 'a provider no organization lists');
@@ -612,7 +643,7 @@ test('models end to end: the first read seeds every present model on and seen; a
assert.equal(saved.status, 200);
assert.equal(saved.json.on, 1); assert.equal(saved.json.total, 2);
assert.notEqual(saved.json.revision, first.json.revision);
assert.deepEqual(saved.json.receipt, { time: new Date(0).toISOString(), op: 'models.set', provider: 'z.ai', on: [], off: ['glm-5'], inUse: [{ model: 'glm-5', org: 'acme', instance: 'assistant' }], user: 'Local operator' });
assert.deepEqual(saved.json.receipt, { time: new Date(0).toISOString(), op: 'models.set', provider: 'z.ai', on: [], off: ['glm-5'], inUse: [{ model: 'glm-5', org: 'acme', instance: 'assistant' }], unanswered: ['bolt'], user: 'Local operator' });
assert.deepEqual((await hit(f, ['models/set', 'POST', { provider: 'z.ai', on: ['glm-5', 'glm-4.6'], revision: first.json.revision }])).json, { error: 'models-offered-conflict' }, 'the old revision is stale now');
// A model the provider adds later reads New and off.
catalogue.push(['zai', 'glm-6']);
@@ -185,6 +185,23 @@ test('models-offered page: switching off a model in use asks in place, naming wh
assert.deepEqual((await writes())[2].body, { provider: 'claude', on: ['claude-opus-5-5'], revision: 'r1', confirmInUse: true });
});
test('models-offered page: an organization that is not answering is named in the ask, labelled where known, with no In use line when nobody is known to use the model', { timeout: 90000 }, async t => {
const quiet = { status: 409, body: { error: 'models-in-use', usedBy: [], unanswered: ['lab', 'zeta'] } };
const done = { provider: 'claude', revision: 'r2', on: 1, total: 3, receipt: { op: 'models.set' } };
const { b, wait, writes } = await setup(t, { admin: { models: VIEW, 'POST models/set': [quiet, done] } });
await openOffered(b, wait);
await toggle(b, 'claude-sonnet-5-5');
await b.evaluate(`${save(CLAUDE)}.click(); true`);
const ASK = CLAUDE + ' [role=alert]';
await wait(`document.querySelector(${JSON.stringify(ASK)}) && !document.querySelector(${JSON.stringify(ASK)}).hidden`);
const ask = await b.evaluate(`document.querySelector(${JSON.stringify(ASK)}).textContent`);
assert.match(ask, /Not answering now, so it is not known which models they use: Lab Bench, zeta\. A model you switch off stops working for them too\./);
assert.doesNotMatch(ask, /In use:/);
await b.evaluate(`[...document.querySelectorAll(${JSON.stringify(ASK + ' button')})].find(x => x.textContent === 'Switch off anyway').click(); true`);
await wait('window.__writes.length === 2');
assert.equal((await writes())[1].body.confirmInUse, true);
});
test('models-offered page: a save after another change is refused as a conflict, worded, with Read the list again; a busy or unknown answer is worded too', { timeout: 90000 }, async t => {
const conflict = { status: 409, body: { error: 'models-offered-conflict' } };
const busy = { status: 409, body: { error: 'proxy-admin-busy' } };