feat(model-proxy, webui): the instance owner lists installed programs, turns one off for every organization where nothing uses it, and reads access health per organization (#1873) #2009

Merged
jason.woltje merged 2 commits from settings-backend-a2 into refactor 2026-11-20 06:52:47 +00:00
6 changed files with 545 additions and 7 deletions

No files matched your search

+165 -1
View File
@@ -9,10 +9,64 @@ import { PIN } from './common.mjs';
import { localRide, riddenOrgs } from './ride.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.
@@ -21,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 };
@@ -98,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'];
@@ -123,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');
@@ -144,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,
};
},
@@ -287,6 +424,33 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
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 }) }));
},
@@ -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 []; } };
@@ -307,3 +310,260 @@ test('recordAccount answers false, and throws nothing, when the history cannot b
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');
});
+5 -1
View File
@@ -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.
+8
View File
@@ -40,6 +40,10 @@ const ROUTES = Object.freeze({
'/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']);
@@ -140,6 +144,10 @@ export async function proxyAdminHTTP(req, res, { path, admin, call, named, teste
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');
@@ -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']});
+93 -2
View File
@@ -22,7 +22,10 @@ const ROUTES = [
['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() {
@@ -40,6 +43,8 @@ function fakeAdmin() {
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) => {
@@ -61,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, log, service key, history', () => {
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}`));
});
@@ -71,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', 'log', 'serviceKey', 'history']);
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 });
@@ -415,3 +421,88 @@ test('a real controller records the account write without the key, and history s
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);
});