fix(model-proxy,webui): settings-backend-a2 review: turning a program off counts the root agents each organization takes (not excluded, not the root assistant, as materializeCopies copies them), so a program only a root agent uses stays on (in-use), and a root agent file that can't be read, or no root reader, writes nothing where the program is on (unreadable); the Console passes loadRootAgents and the root file is read once per call (P1-1); tests for a revision that is not 64 hex, routes from another revision, routes with no instances and a lock-pinned harness listed as installed (P3-1); the busy test no longer hangs when the name check is gone (P3-2); an organization's harness list with anything but plugin names reads on: null (P3-3) (#1873, lane settings-backend-a2)

This commit was merged in pull request #2009.
This commit is contained in:
jason.woltje committed 2026-11-20 00:01:05 -06:00
1 parent 4377941bd4
commit 88d08c8437
4 files changed
+124 -13

No files matched your search

+30 -7
View File
@@ -46,6 +46,21 @@ export function harnessesNamed(document) {
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}$/;
@@ -62,7 +77,9 @@ const isoTime = value => (typeof value === 'string' && /^[0-9]{4}-[0-9]{2}-[0-9]
// stopped fixture organization to the owner's models through the organization service.
// #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.
export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir = null, unit = null, fetch = fetchPinned, buses, orgs = async () => [], riders = async () => [], harnesses = () => installedHarnesses(dataRoot), ownerModels = async () => ({ proxy: false, riding: false, models: [] }), move = null, now = () => new Date(), env = process.env, pin = PIN }) {
// `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 };
@@ -140,8 +157,9 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
} 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 or its answer has no harness list.
const harnessList = value => (Array.isArray(value?.plugins?.harness) && value.plugins.harness.every(n => typeof n === 'string') ? [...value.plugins.harness] : null);
// 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; }
@@ -155,8 +173,9 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
// 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.
async function offOne(rider, name) {
// 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' }; }
@@ -168,7 +187,9 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
let file;
try { file = typeof rider.file === 'function' ? await rider.file() : null; } catch { file = null; }
const named = harnessesNamed(file?.document);
if (named === null || typeof file?.revision !== 'string') return { id, outcome: 'unreadable' };
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) {
@@ -412,8 +433,10 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
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));
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
@@ -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, installedHarnesses, harnessesNamed } 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';
@@ -314,7 +317,7 @@ 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, document = null, fileError = false, down = false, setError = null, keys = [], keysError = null, fixture = false, cliproxy = true, plugins = undefined } = {}) {
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: '',
@@ -323,7 +326,7 @@ function fakeOrg(id, { on = ['alpha', 'beta'], revision = REV, fileRevision = nu
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, harnesses: on, models: ['m1'], providerModels: {}, levels: {}, instances, 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');
@@ -331,7 +334,9 @@ function fakeOrg(id, { on = ['alpha', 'beta'], revision = REV, fileRevision = nu
};
return { rider, calls };
}
const programsFixture = (t, orgs, extra = {}) => fixture(t, { extra: { riders: async () => orgs.map(o => o.rider), harnesses: () => ['alpha', 'beta', 'gamma'], ...extra } });
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 => {
@@ -412,12 +417,15 @@ test('A2 item 5: an unknown name is invalid-request and calls no bus; two off ca
const gate = new Promise(r => { release = r; });
const slow = fakeOrg('slow');
const original = slow.rider.call;
slow.rider.call = async (verb, args) => { if (verb === 'plugins') await gate; return original(verb, args); };
// 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'.
@@ -437,6 +445,84 @@ test('A2 item 5: an installed list that cannot be read refuses off as invalid-re
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);
+2
View File
@@ -266,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.
@@ -483,7 +483,7 @@ test('A2 end to end: programs off writes one receipt, skips a down bus, and no a
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'] });
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' });