fix(model-proxy,webui): models-offered re-check round: in use now counts every model or fallback.model key the organization file names (project agents, definitions; not launch.models' own upstream ids) and the root agents the organization takes (not the root assistant, not one that excludes it), resolved through the organization's model table or legacy mapping; an organization file or root agent file that cannot be read leaves that usage unknown (unanswered); the seed waits while any usage is unknown and keeps a model in use whose account is signed out on; a provider with no entry counts such a model as on, and a save keeps a model that is on but not present (not on the page) on; receipt lists (on, off, inUse, unanswered) stop at 32 with a count of the rest; the ask reads a user with no instance as an agent in its organization; history reads models.seed and models.set by name; tests cover each, plus the seed's revision recheck under the lock (#1873, lane models-offered)

This commit is contained in:
jason.woltje committed 2026-11-20 14:34:28 -06:00
1 parent acb228d56f
commit dde3ee4140
7 files changed
+209 -57

No files matched your search

+1 -1
View File
@@ -249,7 +249,7 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
}
return {
// #1873 models-offered (A9): models() and modelsSet(), the owner's per-provider on/off lists.
...offeredControl({ dataRoot, riders, receipt, exclusive }),
...offeredControl({ dataRoot, riders, receipt, exclusive, rootAgents }),
unit: control.unit,
async status() {
// The job is read before the release: a fetch that ends during the awaits below then reads
+100 -41
View File
@@ -103,19 +103,55 @@ const failure = (code, extra = {}) => Object.assign(new Error(code), { code, ...
const ORG_ID = /^[a-z0-9][a-z0-9._-]{0,63}$/;
const REVISION = /^[a-f0-9]{16}$/;
const RECEIPT_LIST = 32;
const AGENT_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
export function offeredControl({ dataRoot, riders, receipt, exclusive, uid = process.getuid() }) {
// Every launch.models key a document names for an agent, with the agent it sits under: each `model`
// or `fallback.model` string anywhere in it outside `plugins` and `launch` (a launch.models entry's
// own `model` is the upstream id, not a key). Broader than the routes view on purpose, the way
// admin-control.mjs harnessesNamed is: role vars, project agents, definitions and any later place
// count, so a switch-off asks wherever the file still names the model. Null when not a document.
export function modelsNamed(document) {
if (document === null || typeof document !== 'object' || Array.isArray(document)) return null;
const found = [];
const walk = (value, depth, owner) => {
if (depth > 32 || value === null || typeof value !== 'object') return;
if (Array.isArray(value)) { for (const item of value) walk(item, depth + 1, owner); return; }
for (const [key, item] of Object.entries(value)) {
if ((key === 'model' || key === 'fallback.model') && typeof item === 'string') found.push({ instance: owner, key: item, fallback: key === 'fallback.model' });
else walk(item, depth + 1, key === 'vars' || key === 'define' || key === 'narrow' ? owner : (AGENT_NAME.test(key) ? key : null));
}
};
for (const [key, value] of Object.entries(document)) if (key !== 'plugins' && key !== 'launch') walk(value, 1, null);
return found;
}
// The same for the root agents an organization takes (every one but the root assistant and those
// that exclude it, as admin-control.mjs rootHarnessesNamed reads them). `root` is what
// loadRootAgents answered; anything not in that shape answers null (unreadable).
export function rootModelsNamed(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 modelsNamed({ agents: taken, definitions: root.definitions ?? {} });
}
export function offeredControl({ dataRoot, riders, receipt, exclusive, rootAgents = null, uid = process.getuid() }) {
const readFile = () => {
const file = readOffered(dataRoot, { uid });
if (file.state === 'damaged') throw failure('outcome-unknown');
return file;
};
// 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. `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.
// a model or fallback model key resolves (the organization's model table, or the bus's legacy
// mapping) to that cliproxy upstream and model id. The keys come from the routes view's instances,
// from every place the organization file names one (project agents too) and from the root agents
// the organization takes. `unanswered` names each organization whose usage is unknown: its file
// could not be read, its bus did not answer, its catalogue (and so its legacy mapping) was not
// ready, or the root agent file could not be read. 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'); }
@@ -123,7 +159,15 @@ export function offeredControl({ dataRoot, riders, receipt, exclusive, uid = pro
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 }; } }));
let root;
try { root = typeof rootAgents === 'function' ? await rootAgents() : undefined; } catch { root = undefined; }
const answers = await Promise.all(riding.map(async r => {
let routes = null, named = null;
try { routes = await r.call('routes'); } catch { routes = null; }
try { named = typeof r.file === 'function' ? modelsNamed((await r.file())?.document) : null; } catch { named = null; }
const fromRoot = root === undefined ? null : rootModelsNamed(root, r.id);
return { id: r.id, routes, named: named && fromRoot ? [...named, ...fromRoot] : null };
}));
const providers = new Map(), usedBy = new Map();
let ready = false;
for (const { id, routes } of answers) {
@@ -138,34 +182,42 @@ export function offeredControl({ dataRoot, riders, receipt, exclusive, uid = pro
}
}
if (!ready) throw failure('proxy-down');
for (const { id, routes } of answers) {
if (!routes || typeof routes.instances !== 'object' || routes.instances === null) continue;
for (const { id, routes, named } of answers) {
if (!routes || typeof routes.instances !== 'object' || routes.instances === null || named === null) { unanswered.add(id); continue; }
const table = routes.providerModels && typeof routes.providerModels === 'object' ? routes.providerModels : {};
const legacy = routes.catalogue?.legacy && typeof routes.catalogue.legacy === 'object' ? routes.catalogue.legacy : {};
const resolve = key => (typeof key === 'string' && (Object.hasOwn(table, key) ? table[key] : Object.hasOwn(legacy, key) ? legacy[key] : null)) || null;
const uses = [];
for (const [instance, route] of Object.entries(routes.instances)) {
if (!ORG_ID.test(instance)) continue;
for (const [key, fallback] of [[route?.model, false], [route?.fallback?.model, true]]) {
const entry = resolve(key);
if (entry?.module !== 'cliproxy' || typeof entry.upstream !== 'string' || typeof entry.model !== 'string') continue;
const at = `${offeredProvider(entry.upstream)}\n${entry.model}`;
if (!usedBy.has(at)) usedBy.set(at, []);
usedBy.get(at).push({ org: id, instance, fallback });
}
uses.push({ instance, key: route?.model, fallback: false }, { instance, key: route?.fallback?.model, fallback: true });
}
const once = new Set();
for (const { instance, key, fallback } of [...uses, ...named]) {
const entry = resolve(key);
if (entry?.module !== 'cliproxy' || typeof entry.upstream !== 'string' || typeof entry.model !== 'string') continue;
const at = `${offeredProvider(entry.upstream)}\n${entry.model}`;
if (once.has(`${at}\n${instance}\n${fallback}`)) continue;
once.add(`${at}\n${instance}\n${fallback}`);
if (!usedBy.has(at)) usedBy.set(at, []);
usedBy.get(at).push({ org: id, instance, fallback });
}
}
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;
// The models of `provider` something uses, present or not (an account may be signed out).
const usedOf = (s, provider) => [...s.usedBy.keys()].filter(at => at.startsWith(`${provider}\n`)).map(at => at.slice(provider.length + 1)).filter(id => MODEL_ID.test(id));
// A provider with no entry yet has every model on (catalogue() reads it so): the present ones and
// any one in use whose account is signed out now.
const onFor = (entry, present, s, provider) => entry ? entry.on : [...new Set([...present, ...usedOf(s, provider)])];
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)));
const on = new Set(onFor(entry, present, s, provider).filter(id => present.includes(id)));
const seen = new Set(entry ? entry.seen : present);
return { provider, label: offeredLabel(provider), on: on.size, total: present.length,
models: present.map(id => ({ id, label: id, on: on.has(id), isNew: !seen.has(id), usedBy: usersOf(s, provider, id) })) };
@@ -174,23 +226,24 @@ 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 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.
// and seen, with any model in use whose account is signed out (the plugin has no rule for
// "current"), so a model it adds later starts off as New. The seed must change nothing anyone
// uses: a provider with no entry is all on already. So it never seeds a provider with more than
// MAX_ON such models, nor anything while an organization's usage is unknown (the owner's first
// save does it then, 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 = seed === true ? [...s.providers.keys()].filter(p => !entryOf(file, p) && s.providers.get(p).length <= MAX_ON) : [];
const fresh = seed === true && !s.unanswered.length ? [...s.providers.keys()].filter(p => !entryOf(file, p) && onFor(undefined, s.providers.get(p), s, 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], seen: [...present] }; }
for (const p of fresh) { const all = onFor(undefined, s.providers.get(p), s, p); providers[p] = { on: [...all], seen: [...all] }; }
writeOffered(dataRoot, { version: OFFERED_VERSION, providers });
receipt({ op: 'models.seed', providers: fresh.map(p => ({ provider: p, on: providers[p].on.length })) });
});
@@ -199,10 +252,12 @@ 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. When it
// switches any model off and an organization's usage is unknown, it refuses the same way and
// names that organization.
// POST: one provider's on list for its present models. `revision` is the one the read gave; a
// model in use 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. A model that is on but not present now (its account signed
// out) is not on the page, so it keeps its state: only a present model, or one named in `on`,
// changes.
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');
@@ -213,23 +268,27 @@ export function offeredControl({ dataRoot, riders, receipt, exclusive, uid = pro
const present = s.providers.get(provider);
if (!present) throw failure('models-invalid');
const entry = entryOf(file, provider);
const before = onFor(entry, present);
const before = onFor(entry, present, s, provider);
// A model may stay on while its account is signed out; a model never listed may not be switched on.
if (on.some(id => !present.includes(id) && !before.includes(id))) throw failure('models-invalid');
const off = before.filter(id => !on.includes(id));
const kept = before.filter(id => !present.includes(id) && !on.includes(id));
if (on.length + kept.length > MAX_ON) throw failure('models-invalid');
const off = before.filter(id => present.includes(id) && !on.includes(id));
const users = off.flatMap(id => usersOf(s, provider, id).map(u => ({ model: id, ...u })));
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 } };
const seen = [...new Set([...(entry?.seen ?? []), ...present, ...on, ...kept])];
const providers = { ...(file.offered?.providers ?? {}), [provider]: { on: [...on, ...kept], 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: 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);
// Every list is bounded, with a count of the rest, so the receipt line stays well inside what
// receiptHistory reads.
const record = { op: 'models.set', provider };
const list = (name, items) => { record[name] = items.slice(0, RECEIPT_LIST); if (items.length > RECEIPT_LIST) record[`${name}More`] = items.length - RECEIPT_LIST; };
list('on', on.filter(id => !before.includes(id)));
list('off', off);
if (users.length) list('inUse', users.map(u => ({ model: u.model, org: u.org, instance: u.instance })));
if (unanswered.length) list('unanswered', unanswered);
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
let written = null;
try { written = receipt(record); } catch { written = null; }
@@ -8,11 +8,12 @@ import { readOffered, writeOffered, offeredControl, offeredFile, offeredRevision
const tempRoot = t => { const root = mkdtempSync(join(tmpdir(), 'mo-')); t.after(() => rmSync(root, { recursive: true, force: true })); return root; };
const entry = (upstream, model) => ({ module: 'cliproxy', upstream, model });
const rider = (id, models, extra = {}) => ({ id, cliproxy: true, call: async verb => { if (verb !== 'routes') throw new Error('no'); return { instances: {}, catalogue: { status: 'ready', models }, ...extra }; } });
function control(t, { riders, busy = false } = {}) {
const rider = (id, models, extra = {}, document = { roles: {} }) => ({ id, cliproxy: true, file: async () => ({ document, revision: 'r' }), call: async verb => { if (verb !== 'routes') throw new Error('no'); return { instances: {}, catalogue: { status: 'ready', models }, ...extra }; } });
const NO_ROOT = async () => ({ state: 'absent', agents: {}, assistant: null });
function control(t, { riders, busy = false, rootAgents = NO_ROOT } = {}) {
const dataRoot = tempRoot(t), receipts = [];
const exclusive = async (name, action) => { if (busy) throw Object.assign(new Error('busy'), { code: 'proxy-admin-busy' }); return action(); };
return { dataRoot, receipts, ...offeredControl({ dataRoot, riders: async () => riders, receipt: r => { receipts.push(r); return r; }, exclusive }) };
return { dataRoot, receipts, ...offeredControl({ dataRoot, riders: async () => riders, receipt: r => { receipts.push(r); return r; }, exclusive, rootAgents }) };
}
test('readOffered: no file is absent; a written file reads ok with its revision; anything else reads damaged', t => {
@@ -88,12 +89,15 @@ test('an organization whose usage is unknown (its file unreadable, its bus 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 noFile = { ...rider('fern', models), file: async () => { throw new Error('unreadable'); } };
const noFileReader = { ...rider('gale', models), file: undefined };
const c = control(t, { riders: [rider('acme', models), down, unreadable, notReady, notRiding, noFile, noFileReader] });
const view = await c.models({ seed: true });
assert.deepEqual(view.unanswered, ['bolt', 'crux', 'dune']);
assert.deepEqual(view.unanswered, ['bolt', 'crux', 'dune', 'fern', 'gale']);
assert.equal(existsSync(offeredFile(c.dataRoot)), false, 'no seed while any usage is unknown');
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']]);
assert.deepEqual([error.usedBy, error.unanswered], [[], ['bolt', 'crux', 'dune', 'fern', 'gale']]);
return true;
});
assert.equal(readOffered(c.dataRoot).revision, view.revision, 'nothing written');
@@ -101,7 +105,80 @@ test('an organization whose usage is unknown (its file unreadable, its bus down,
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'] });
assert.deepEqual(c.receipts.at(-1), { op: 'models.set', provider: 'z.ai', on: [], off: ['glm-4.6'], unanswered: ['bolt', 'crux', 'dune', 'fern', 'gale'] });
// The root agent file unreadable (a throw, or not the loader's shape) leaves every organization's usage unknown.
for (const rootAgents of [async () => { throw new Error('unreadable'); }, async () => ({ state: 'ok', agents: { a: { vars: {} } } }), async () => null, null]) {
const r = control(t, { riders: [rider('acme', models)], rootAgents });
assert.deepEqual((await r.models({ seed: true })).unanswered, ['acme'], String(rootAgents));
assert.equal(existsSync(offeredFile(r.dataRoot)), false);
}
});
test('in use counts every place the organization file names a model and the root agents it takes: project agents, root copies; not an excluded or assistant root agent, not launch.models itself', async t => {
const models = [entry('zai', 'glm-5'), entry('zai', 'glm-4.6'), entry('codex', 'gpt-5'), entry('codex', 'gpt-5-mini')];
const routes = { instances: { chief: { model: 'g5' } }, providerModels: { g5: entry('zai', 'glm-5'), g46: entry('zai', 'glm-4.6'), gpt: entry('codex', 'gpt-5'), mini: entry('codex', 'gpt-5-mini') } };
const document = { roles: { chief: { vars: { model: 'g5' } } }, launch: { models: { g5: { module: 'cliproxy', upstream: 'zai', model: 'g46' } } },
projects: { site: { agents: { coder: { define: { definition: 'coder', vars: { model: 'g46', 'fallback.model': 'gpt' } } } } } } };
const root = { state: 'ok', assistant: null, agents: {
helper: { definition: 'coder', excludeOrgs: [], vars: { model: 'mini' } },
away: { definition: 'coder', excludeOrgs: ['acme'], vars: { model: 'gpt' } },
front: { definition: 'assistant', excludeOrgs: [], vars: { model: 'gpt' } } } };
const c = control(t, { riders: [rider('acme', models, routes, document)], rootAgents: async () => root });
const view = await c.models({ seed: true });
assert.deepEqual(view.providers.map(p => p.models.map(m => [m.id, m.usedBy])), [
[['glm-5', [{ org: 'acme', instance: 'chief', fallback: false }]], ['glm-4.6', [{ org: 'acme', instance: 'coder', fallback: false }]]],
[['gpt-5', [{ org: 'acme', instance: 'coder', fallback: true }]], ['gpt-5-mini', [{ org: 'acme', instance: 'helper', fallback: false }]]]]);
await assert.rejects(c.modelsSet({ provider: 'codex', on: [], revision: view.revision }), error => {
assert.deepEqual(error.usedBy.map(u => [u.model, u.instance, u.fallback]), [['gpt-5', 'coder', true], ['gpt-5-mini', 'helper', false]]);
return true;
});
});
test('a model in use whose account is signed out: the seed keeps it on, the first save with no entry keeps it on, and a save that leaves it off the page keeps it on', async t => {
let models = [entry('codex', 'gpt-5')];
const routes = { instances: { chief: { model: 'pro' } }, providerModels: { pro: entry('codex', 'gpt-5-pro') } };
const acme = { ...rider('acme', []), call: async () => ({ ...routes, catalogue: { status: 'ready', models } }) };
const c = control(t, { riders: [acme] });
const view = await c.models({ seed: true });
assert.deepEqual(readOffered(c.dataRoot).offered.providers.codex, { on: ['gpt-5', 'gpt-5-pro'], seen: ['gpt-5', 'gpt-5-pro'] }, 'the seed keeps the model in use on');
const saved = await c.modelsSet({ provider: 'codex', on: [], revision: view.revision });
assert.deepEqual(readOffered(c.dataRoot).offered.providers.codex.on, ['gpt-5-pro'], 'a model not on the page keeps its state');
assert.deepEqual(c.receipts.at(-1), { op: 'models.set', provider: 'codex', on: [], off: ['gpt-5'] });
models = [entry('codex', 'gpt-5'), entry('codex', 'gpt-5-pro')];
await assert.rejects(c.modelsSet({ provider: 'codex', on: [], revision: saved.revision }), { code: 'models-in-use' }, 'back again, it is on the page and asks');
// With no entry at all, the first save keeps an absent model in use on too.
models = [entry('codex', 'gpt-5')];
const fresh = control(t, { riders: [acme] });
const read = await fresh.models();
assert.equal(existsSync(offeredFile(fresh.dataRoot)), false);
await fresh.modelsSet({ provider: 'codex', on: ['gpt-5'], revision: read.revision });
assert.deepEqual(readOffered(fresh.dataRoot).offered.providers.codex, { on: ['gpt-5', 'gpt-5-pro'], seen: ['gpt-5', 'gpt-5-pro'] });
});
test('the receipt bounds every list at 32 with a count of the rest', async t => {
const many = Array.from({ length: 40 }, (_, i) => entry('zai', `glm-${i}`));
const instances = Object.fromEntries(many.map((m, i) => [`agent-${i}`, { model: `k${i}` }]));
const providerModels = Object.fromEntries(many.map((m, i) => [`k${i}`, m]));
const down = { id: 'zz-down', cliproxy: null, call: async () => { throw new Error('down'); } };
const quiet = Array.from({ length: 34 }, (_, i) => ({ ...down, id: `q-${String(i).padStart(2, '0')}` }));
const c = control(t, { riders: [rider('acme', many, { instances, providerModels }), ...quiet] });
const view = await c.models();
await c.modelsSet({ provider: 'z.ai', on: [], revision: view.revision, confirmInUse: true });
const r = c.receipts.at(-1);
assert.deepEqual([r.off.length, r.offMore, r.inUse.length, r.inUseMore, r.unanswered.length, r.unansweredMore], [32, 8, 32, 8, 32, 2]);
const back = await c.modelsSet({ provider: 'z.ai', on: many.map(m => m.model), revision: readOffered(c.dataRoot).revision });
assert.equal(back.on, 40);
assert.deepEqual([c.receipts.at(-1).on.length, c.receipts.at(-1).onMore], [32, 8]);
});
test('the seed rechecks the revision under the lock: a write between the read and the lock leaves the seed out', async t => {
const box = {};
const sneaky = { ...rider('acme', [entry('zai', 'glm-5')]), call: async () => { writeOffered(box.root, { version: 1, providers: { codex: { on: [], seen: [] } } }); return { instances: {}, catalogue: { status: 'ready', models: [entry('zai', 'glm-5')] } }; } };
const c = control(t, { riders: [sneaky] });
box.root = c.dataRoot;
await c.models({ seed: true });
assert.deepEqual(readOffered(c.dataRoot).offered.providers, { codex: { on: [], seen: [] } });
assert.deepEqual(c.receipts, []);
});
test('a damaged file: the read and the write answer outcome-unknown and write nothing', async t => {
+2 -1
View File
@@ -925,7 +925,7 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
'outcome-unknown': 'The outcome is unknown. Read the list again before you try again.',
};
const offeredSays = code => Object.hasOwn(OFFERED_SAYS, code) ? OFFERED_SAYS[code] : `The change was refused (${code}).`;
const offeredWho = u => `${window.MosaicOrg?.row?.(u.org)?.label || u.org}'s ${u.instance}${u.fallback ? ' fallback' : ''}`;
const offeredWho = u => { const org = window.MosaicOrg?.row?.(u.org)?.label || u.org; return `${u.instance ? `${org}'s ${u.instance}` : `an agent in ${org}`}${u.fallback ? ' fallback' : ''}`; };
let offeredSaved = null;
async function readOfferedOwner(round) {
const box = $('proxy-offered-list'), saved = offeredSaved;
@@ -1290,6 +1290,7 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
'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`,
'models.seed': 'Model lists recorded, every model on', 'models.set': provider ? `${provider} models switched on or off` : 'Models switched on or off',
};
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}` : '';
+10 -4
View File
@@ -596,9 +596,10 @@ test('models end to end: the first read seeds every present model on and seen; a
const catalogue = [['zai', 'glm-5'], ['zai', 'glm-4.6'], ['codex', 'gpt-5']];
const entry = ([upstream, model]) => ({ key: `${upstream}-${model}`.replace(/\./g, '-'), module: 'cliproxy', upstream, model, available: true, reason: null, harnesses: ['claude-code', 'pi'] });
const org = (id, { down = false, cliproxy = true, instances = {} } = {}) => ({
id, fixture: false, cliproxy, changes: '',
call: async verb => {
if (down) throw Object.assign(new Error(planted), { code: 'outcome-unknown' });
id, fixture: false, cliproxy, changes: '', down,
file: async () => ({ document: { roles: {} }, revision: 'c'.repeat(64) }),
async call(verb) {
if (this.down) throw Object.assign(new Error(planted), { code: 'outcome-unknown' });
if (verb === 'routes') return { business: id, revision: 'c'.repeat(64), note: planted, instances,
providerModels: { glm5: { module: 'cliproxy', upstream: 'zai', model: 'glm-5', provider: 'zai' }, gpt5: { module: 'cliproxy', upstream: 'codex', model: 'gpt-5', provider: 'codex' } },
catalogue: { status: 'ready', models: catalogue.map(entry), notices: [] } };
@@ -613,9 +614,14 @@ test('models end to end: the first read seeds every present model on and seen; a
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 waiting = await hit(f, ['models', 'GET']);
assert.deepEqual(waiting.json.unanswered, ['bolt'], 'bolt\'s bus is down, so its usage is unknown');
assert.equal(existsSync(file), false, 'no seed while an organization\'s usage is unknown');
orgs[1].down = false;
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.deepEqual(first.json.unanswered, []);
orgs[1].down = true;
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, [
@@ -185,8 +185,8 @@ 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'] } };
test('models-offered page: an organization that is not answering is named in the ask, labelled where known; a user named by the organization file with no instance reads as an agent in it', { timeout: 90000 }, async t => {
const quiet = { status: 409, body: { error: 'models-in-use', usedBy: [{ model: 'claude-sonnet-5-5', org: 'acme', instance: null, fallback: false }], 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);
@@ -196,7 +196,7 @@ test('models-offered page: an organization that is not answering is named in the
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:/);
assert.match(ask, /In use: claude-sonnet-5-5: an agent in Acme Studio\./);
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);
@@ -379,6 +379,15 @@ test('settings-s5b: a history entry whose action is not a known one reads Other
for (const item of await items(b)) assert.match(item.text, /^Other setup action · .+ · by jason$/);
});
test('settings-s5b: the models-offered entries read as what they did, never Other setup action', { timeout: 90000 }, async t => {
const { b, wait } = await setup(t, { admin: { ...OK, 'history?limit=25': { entries: [{ op: 'models.seed', time: T(1), user: 'jason' }, { op: 'models.set', provider: 'not-a-provider', time: T(2), user: 'jason' }], next: null } } });
await wait(READY); await openTech(b);
await wait('document.querySelectorAll("#proxy-setup-history li").length === 2');
const texts = (await items(b)).map(i => i.text).sort();
assert.match(texts[0], /^Model lists recorded, every model on · .+ · by jason$/);
assert.match(texts[1], /^Models switched on or off · .+ · 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);