fix(model-proxy): round 3: a Console save that takes cliproxy off an organization records the removal in the proxy's receipts first, and Ride all skips as removed any organization with any sign it was or may have been on cliproxy; a receipt line that does not parse fails closed; each add records an intent before it starts and an unknown outcome stays recorded; the fixture move and listing read stopped from the bus status and its process id; receipt and change-log reads check owner, mode and no symlink (#1873, lane ride-default)

This commit is contained in:
jason.woltje committed 2026-11-19 13:13:05 -06:00
1 parent 869d259036
commit 9bfe101176
10 files changed
+465 -72

No files matched your search

+39 -16
View File
@@ -7,7 +7,7 @@ import { homedir } from 'node:os';
import { fetchPinned } from './fetch.mjs';
import { PIN } from './common.mjs';
import { localRide, riddenOrgs } from './ride.mjs';
import { releaseState, initState, fenceState, initLockStatus, clearInitLock, appendReceipt, recentReceipts, receiptFile, createUnitControl, FILE_LABELS } from './admin.mjs';
import { releaseState, initState, fenceState, initLockStatus, clearInitLock, appendReceipt, recentReceipts, receiptsText, createUnitControl, FILE_LABELS } from './admin.mjs';
export const proxyCode = (error, fallback) => (/^proxy-[a-z-]+$/.test(error?.code ?? '') ? error.code : fallback);
const failure = code => Object.assign(new Error(code), { code });
@@ -16,8 +16,8 @@ const failure = code => Object.assign(new Error(code), { code });
// `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.
// #1873 ride-default: `riders()` lists the owner's active organizations for Ride all, read locally:
// [{id, fixture, cliproxy, changes, call}] where `changes` is the text of the organization's change
// log and `call(verb, args)` reaches its bus as the owner. `ownerModels()` answers
// [{id, fixture, cliproxy, changes, call, status}] where `changes` is the text of the organization's
// 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 }) {
@@ -37,31 +37,45 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
async function unitStatus() {
try { return await control.status(); } catch (error) { return { unit: control.unit, installed: false, active: 'unknown', healthy: false, error: proxyCode(error, 'proxy-unit-failed') }; }
}
// The organizations this proxy's own receipts show it put cliproxy on (riddenOrgs). No receipt
// file yet is none; a file that cannot be read is null, and localRide skips as unreadable.
// The organizations this proxy's own receipts show cliproxy was or may have been on (riddenOrgs).
// No receipt file yet is none; a file that fails receiptsText's check, or holds a line that does
// not parse, is null, and localRide skips every organization without cliproxy as unreadable.
function ridden() {
let text; try { text = fs.readFileSync(receiptFile(dataRoot), 'utf8'); } catch (error) { text = error?.code === 'ENOENT' ? '' : null; }
const ids = riddenOrgs(text);
const ids = riddenOrgs(receiptsText(dataRoot));
return id => (ids === null ? null : ids.has(id));
}
const decide = (rider, wasRidden) => localRide({ ...rider, ridden: wasRidden(rider.id) });
// Stopped is the organization admin page's test (org-admin bus stop): a bus status with no
// process id. A status that cannot be read, or a rider with no status, is not stopped.
async function stopped(rider) {
if (typeof rider.status !== 'function') return false;
try { return !Number.isSafeInteger((await rider.status())?.pid); } catch { return false; }
}
const failCode = error => (/^[a-z][a-z-]{0,63}$/.test(error?.code ?? '') ? error.code : 'failed');
// Ride all, one organization: the local rules first, then the organization's bus, which must
// answer and must hold the proxy client. The bus decides the write (plugins.set) as it would
// for the owner on the Settings page. The answer names a reason, never bus or upstream text.
// A ride-intent receipt is on disk before plugins.set is sent; with no receipt there is no write.
// When plugins.set fails and the bus then shows no cliproxy, a ride-not-added receipt clears that
// intent. Any other failure leaves it: the add may have happened.
async function rideOne(rider, wasRidden) {
const id = rider.id, local = decide(rider, wasRidden);
if (local === 'fixture') {
let running = true; try { await rider.call('plugins'); } catch { running = false; }
return { id, outcome: 'fixture', running };
}
if (local === 'fixture') return { id, outcome: 'fixture', running: !(await stopped(rider)) };
if (local !== 'ready') return { id, outcome: local === 'already' ? 'already' : 'skipped', reason: local };
let current;
try { current = await rider.call('plugins'); } catch { return { id, outcome: 'skipped', reason: 'bus-down' }; }
try { await rider.call('proxy.accounts'); } catch (error) { return { id, outcome: 'skipped', reason: error?.code === 'not-configured' ? 'no-proxy-client' : 'proxy-check-failed' }; }
const providers = Array.isArray(current?.plugins?.['model-provider']) ? current.plugins['model-provider'] : [];
if (providers.includes('cliproxy')) return { id, outcome: 'already' };
try { receipt({ op: 'ride-intent', org: id }); } catch { return { id, outcome: 'skipped', reason: 'not-recorded' }; }
try { await rider.call('plugins.set', { revision: current.revision, plugins: { 'model-provider': [...providers, 'cliproxy'] } }); }
catch (error) { return { id, outcome: 'failed', reason: /^[a-z][a-z-]{0,63}$/.test(error?.code ?? '') ? error.code : 'failed' }; }
catch (error) {
try {
const after = await rider.call('plugins');
if (Array.isArray(after?.plugins?.['model-provider']) && !after.plugins['model-provider'].includes('cliproxy')) receipt({ op: 'ride-not-added', org: id });
} catch { /* unknown outcome: the intent stands */ }
return { id, outcome: 'failed', reason: failCode(error) };
}
return { id, outcome: 'added' };
}
async function rideSummary() {
@@ -187,7 +201,11 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
const skipped = results.filter(r => r.outcome === 'skipped' || r.outcome === 'failed').map(r => ({ id: r.id, reason: r.reason }));
const fixtures = results.filter(r => r.outcome === 'fixture').map(r => ({ id: r.id, running: r.running }));
const already = results.filter(r => r.outcome === 'already').map(r => r.id);
return { added, already, skipped, fixtures, receipt: receipt({ op: 'ride-all', added, skipped, fixtures: fixtures.map(f => f.id) }) };
// Each add already left its ride-intent receipt, so a summary that cannot be written still
// answers what happened, with no receipt.
let summary = null;
try { summary = receipt({ op: 'ride-all', added, skipped, fixtures: fixtures.map(f => f.id) }); } catch { summary = null; }
return { added, already, skipped, fixtures, receipt: summary };
});
},
// Moves one stopped fixture organization to the owner's models. Its bus is refused at start
@@ -199,19 +217,24 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
if (!rider) return { org, moved: false, reason: 'unknown' };
const local = decide(rider, ridden());
if (local !== 'fixture') return { org, moved: false, reason: local };
let running = true; try { await rider.call('plugins'); } catch { running = false; }
if (running) return { org, moved: false, reason: 'bus-running' };
if (!(await stopped(rider))) return { org, moved: false, reason: 'bus-running' };
if (typeof move !== 'function') return { org, moved: false, reason: 'not-available' };
const owner = await ownerModels();
if (owner?.proxy !== true) return { org, moved: false, reason: 'no-proxy' };
if (owner.riding !== true) return { org, moved: false, reason: 'no-riding-bus' };
let result;
try { result = await move(org, owner.models, principal); }
catch (error) { result = { moved: false, reason: /^[a-z][a-z-]{0,63}$/.test(error?.code ?? '') ? error.code : 'failed' }; }
catch (error) { result = { moved: false, reason: failCode(error) }; }
if (!result?.moved) return { org, moved: false, reason: result?.reason ?? 'failed' };
return { org, moved: true, model: result.model, receipt: receipt({ op: 'ride-move', org, model: result.model }) };
});
},
// The organization admin page calls this before it saves a document that drops cliproxy from
// the model providers, and saves nothing when it throws. Ride all then leaves that organization off.
rideRemoved(org) {
if (typeof org !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(org)) throw failure('proxy-org-invalid');
return receipt({ op: 'ride-removed', org });
},
async initLockClear() {
return exclusive('init-lock-clear', async () => ({ receipt: clearInitLock({ configDir, dataRoot, buses: await buses(), now }) }));
},
+20
View File
@@ -107,6 +107,26 @@ export function appendReceipt(dataRoot, record) {
return record;
}
// A file's text, read the way fenceState checks the fence file: opened without following a symlink
// (and without blocking on a FIFO), a regular file the Console's own user owns, with none of the
// `mask` permission bits. A missing file throws its ENOENT; any other failure throws
// proxy-file-refused. `uid` is a test seam.
export function ownedText(file, { mask = 0o077, uid = process.getuid() } = {}) {
const fd = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK);
try {
const stat = fs.fstatSync(fd);
if (!stat.isFile() || stat.uid !== uid || (stat.mode & mask)) throw Object.assign(new Error('proxy-file-refused'), { code: 'proxy-file-refused' });
return fs.readFileSync(fd, 'utf8');
} finally { fs.closeSync(fd); }
}
// The whole receipt file's text for Ride all, which decides from it (ownedText, no group or other
// bits). No file yet answers ''; anything else that fails answers null, and the caller treats every
// organization without cliproxy as unreadable.
export function receiptsText(dataRoot, { uid = process.getuid() } = {}) {
try { return ownedText(receiptFile(dataRoot), { uid }); } catch (error) { return error?.code === 'ENOENT' ? '' : null; }
}
// The last few receipts, oldest first, for the page after a reload. Reads only the file's tail and
// skips a line that does not parse (a torn last write). Receipts never hold a key or a body.
export function recentReceipts(dataRoot, limit = 10) {
+50 -23
View File
@@ -6,54 +6,81 @@ import { usableModel } from '../../plugins/model-provider/cliproxy/index.mjs';
export { usableModel };
const has = (list, name) => Array.isArray(list) && list.includes(name);
// True when the organization's change log shows a committed write that removed cliproxy from
// its model providers: the owner took it off on purpose, so Ride all leaves it off. A log that
// cannot be read answers null, and the caller skips the organization as unreadable.
export function cliproxyRemoved(text) {
// The model providers one change-log change shows before and after, by the change's path: the
// list itself, the plugins object, or the whole document. Any other path shows none.
function providersOf(change) {
const at = value => change?.path === 'plugins.model-provider' ? value
: change?.path === 'plugins' ? value?.['model-provider']
: change?.path === '' ? value?.plugins?.['model-provider'] : undefined;
return [at(change?.old), at(change?.new)];
}
const touchesProviders = path => path === '' || path === 'plugins' || path === 'plugins.model-provider';
// What the organization's change log shows of cliproxy, from its committed writes: `removed` when
// a write took it off, `seen` when any write shows it on the organization before or after, or wrote
// the providers without logging their values (a paths-only entry). A log that cannot be read
// answers null, and the caller skips the organization as unreadable.
export function cliproxyHistory(text) {
if (typeof text !== 'string') return null;
let removed = false, seen = false;
try {
for (const line of text.split('\n')) {
if (!line.trim()) continue;
const event = JSON.parse(line);
if (event?.state !== 'committed' || !Array.isArray(event.changes)) continue;
for (const change of event.changes) {
const [before, after] = change?.path === 'plugins.model-provider' ? [change.old, change.new]
: change?.path === 'plugins' ? [change.old?.['model-provider'], change.new?.['model-provider']] : [null, null];
if (has(before, 'cliproxy') && !has(after, 'cliproxy')) return true;
if (!touchesProviders(change?.path)) continue;
if (!Object.hasOwn(change, 'old') && !Object.hasOwn(change, 'new')) { seen = true; continue; }
const [before, after] = providersOf(change);
if (has(before, 'cliproxy') && !has(after, 'cliproxy')) removed = true;
if (has(before, 'cliproxy') || has(after, 'cliproxy')) seen = true;
}
}
return false;
return { removed, seen };
} catch { return null; }
}
export function cliproxyRemoved(text) { const h = cliproxyHistory(text); return h === null ? null : h.removed; }
// The organizations the proxy itself once put cliproxy on, from the text of the instance's
// proxy-admin.jsonl: each ride-all receipt's added list and each ride-move receipt's org. The
// organization admin page saves a whole document and leaves no change-log entry, so an
// organization named here that lacks cliproxy now had it taken off by its owner. A torn line is
// skipped; text that is not a string answers null and the caller treats every organization as
// unreadable.
// The organizations the proxy's own receipts (proxy-admin.jsonl) show cliproxy was or may have
// been on: each ride-all receipt's added list, each ride-move's org, each ride-removed's org (the
// organization admin page writes one before it saves a document without cliproxy), and each
// ride-intent's org (Ride all writes one before each plugins.set) unless a later ride-not-added for
// that organization shows the bus did not take it. Any line that does not parse, and any ride
// record of the wrong shape, answers null: the caller then skips every organization without
// cliproxy as unreadable. Text that is not a string answers null too.
const RIDE_ORG = new Set(['ride-move', 'ride-removed', 'ride-intent', 'ride-not-added']);
export function riddenOrgs(text) {
if (typeof text !== 'string') return null;
const ids = new Set();
const ids = new Set(), pending = new Set();
for (const line of text.split('\n')) {
if (!line.trim()) continue;
let r; try { r = JSON.parse(line); } catch { continue; }
if (r?.op === 'ride-all' && Array.isArray(r.added)) for (const id of r.added) if (typeof id === 'string') ids.add(id);
if (r?.op === 'ride-move' && typeof r.org === 'string') ids.add(r.org);
let r; try { r = JSON.parse(line); } catch { return null; }
if (!r || typeof r !== 'object' || Array.isArray(r)) return null;
if (r.op === 'ride-all') {
if (!Array.isArray(r.added) || !r.added.every(id => typeof id === 'string')) return null;
for (const id of r.added) ids.add(id);
} else if (RIDE_ORG.has(r.op)) {
if (typeof r.org !== 'string') return null;
if (r.op === 'ride-intent') pending.add(r.org);
else if (r.op === 'ride-not-added') pending.delete(r.org);
else ids.add(r.org);
}
}
for (const id of pending) ids.add(id);
return ids;
}
// The local part of the decision, with no bus call: what Ride all does with one organization
// before it asks the organization's bus anything. An organization that uses cliproxy now (and is
// not a fixture organization) is already riding, whatever its history. `ridden` is true when the
// proxy's receipts show cliproxy was put on it, and null when those receipts cannot be read.
// not a fixture organization) is already riding, whatever its history. One without cliproxy now
// that had it, or may have had it, is removed: its change log or the proxy's receipts show it
// (`ridden` is true from the receipts, null when they cannot be read).
export function localRide({ fixture, cliproxy, changes, ridden = false }) {
if (typeof fixture !== 'boolean' || typeof cliproxy !== 'boolean') return 'unreadable';
if (cliproxy && !fixture) return 'already';
const removed = cliproxyRemoved(changes);
if (removed === null || (ridden === null && !cliproxy)) return 'unreadable';
if (removed || (ridden === true && !cliproxy)) return 'removed';
const history = cliproxyHistory(changes);
if (history === null || (ridden === null && !cliproxy)) return 'unreadable';
if (history.removed || (!cliproxy && (ridden === true || history.seen))) return 'removed';
if (fixture) return 'fixture';
return 'ready';
}
+176 -9
View File
@@ -8,7 +8,8 @@ import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { createHash } from 'node:crypto';
import { createProxyAdmin } from '../src/admin-control.mjs';
import { initFiles, receiptFile, appendReceipt } from '../src/admin.mjs';
import { initFiles, receiptFile, appendReceipt, receiptsText, ownedText } from '../src/admin.mjs';
import { execFileSync } from 'node:child_process';
import { PIN } from '../src/common.mjs';
import { cliproxyRemoved, localRide, riddenOrgs, pickModel, fixtureMove, moveFixtureOrg, readRider, ownerModelsFrom } from '../src/ride.mjs';
import * as orgService from '../../organization/src/org-service.mjs';
@@ -81,12 +82,169 @@ test('the local rule names unreadable, removed, fixture, already and ready in th
assert.equal(localRide({ fixture: false, cliproxy: true, changes: '', ridden: null }), 'already');
});
test('the proxy receipts name each organization ride all added or ride move moved', () => {
const text = [{ op: 'ride-all', added: ['a', 'b'], skipped: [{ id: 'c', reason: 'bus-down' }], fixtures: ['d'] }, { op: 'ride-move', org: 'e', model: 'm' },
{ op: 'fetch', added: ['f'] }, { op: 'ride-all', added: [7] }].map(r => JSON.stringify(r)).join('\n') + '\n{torn';
assert.deepEqual([...riddenOrgs(text)].sort(), ['a', 'b', 'e']);
test('the proxy receipts name each organization ride all added, tried to add, moved or saw removed', () => {
const lines = records => records.map(r => JSON.stringify(r)).join('\n') + '\n';
const text = lines([{ op: 'ride-all', added: ['a', 'b'], skipped: [{ id: 'c', reason: 'bus-down' }], fixtures: ['d'] }, { op: 'ride-move', org: 'e', model: 'm' },
{ op: 'fetch', added: ['f'] }, { op: 'ride-intent', org: 'g' }, { op: 'ride-removed', org: 'h' }]);
assert.deepEqual([...riddenOrgs(text)].sort(), ['a', 'b', 'e', 'g', 'h']);
assert.deepEqual([...riddenOrgs('')], []);
assert.equal(riddenOrgs(null), null);
// Round 3 (P2a): a line that does not parse, or a ride record of the wrong shape, fails closed.
assert.equal(riddenOrgs(text + '{torn'), null, 'a torn line');
assert.equal(riddenOrgs(lines([{ op: 'ride-all', added: [7] }])), null);
assert.equal(riddenOrgs(lines([{ op: 'ride-all', added: 'a' }])), null);
assert.equal(riddenOrgs(lines([{ op: 'ride-intent' }])), null);
assert.equal(riddenOrgs(lines([{ op: 'ride-removed', org: 3 }])), null);
assert.equal(riddenOrgs('7\n'), null, 'a line that is not a record');
// Round 3 (P2b): an intent the bus then showed was not applied is cleared, and only that intent.
assert.deepEqual([...riddenOrgs(lines([{ op: 'ride-intent', org: 'g' }, { op: 'ride-not-added', org: 'g' }]))], []);
assert.deepEqual([...riddenOrgs(lines([{ op: 'ride-all', added: ['g'] }, { op: 'ride-intent', org: 'g' }, { op: 'ride-not-added', org: 'g' }]))], ['g'], 'an earlier add stays');
assert.deepEqual([...riddenOrgs(lines([{ op: 'ride-removed', org: 'g' }, { op: 'ride-intent', org: 'g' }, { op: 'ride-not-added', org: 'g' }]))], ['g'], 'an earlier removal stays');
assert.deepEqual([...riddenOrgs(lines([{ op: 'ride-not-added', org: 'g' }, { op: 'ride-intent', org: 'g' }]))], ['g'], 'a later intent stands');
});
test('a change log that shows cliproxy on the organization at any time, or a write to its providers it cannot read, counts as removed once cliproxy is gone', () => {
// Round 3 (P1 case b): added on the Settings page, then removed on the admin page, which writes no log entry.
const added = committed([{ path: 'plugins.model-provider', old: [], new: ['cliproxy'] }]);
assert.equal(localRide({ fixture: false, cliproxy: false, changes: added }), 'removed');
assert.equal(localRide({ fixture: false, cliproxy: true, changes: added }), 'already');
assert.equal(localRide({ fixture: true, cliproxy: true, changes: added }), 'fixture', 'a fixture organization that lists cliproxy is still listed to move');
assert.equal(localRide({ fixture: false, cliproxy: false, changes: committed([{ path: 'plugins', old: null, new: { 'model-provider': ['cliproxy'] } }]) }), 'removed');
assert.equal(localRide({ fixture: false, cliproxy: false, changes: committed([{ path: '', old: null, new: { plugins: { 'model-provider': ['cliproxy'] } } }]) }), 'removed');
assert.equal(localRide({ fixture: false, cliproxy: false, changes: committed([{ path: 'plugins.model-provider', oldPresent: true, newPresent: true }]) }), 'removed', 'a paths-only write to the providers');
assert.equal(localRide({ fixture: false, cliproxy: false, changes: committed([{ path: 'roles.chief.vars', oldPresent: true, newPresent: true }]) }), 'ready', 'a paths-only write elsewhere');
assert.equal(localRide({ fixture: false, cliproxy: false, changes: committed([{ path: 'plugins.model-provider', old: ['fixture'], new: ['openai'] }]) }), 'ready');
assert.equal(localRide({ fixture: false, cliproxy: false, changes: JSON.stringify({ state: 'aborted', changes: [{ path: 'plugins.model-provider', old: [], new: ['cliproxy'] }] }) + '\n' }), 'ready', 'a write that did not commit');
});
test('the reviewer probe: an organization created from the cliproxy preset and saved without it on its admin page is skipped as removed', async t => {
// P1 case a. Nothing in the change log: the preset made it, the admin page took it off. The
// admin page leaves a ride-removed receipt (proxy admin rideRemoved) before it saves.
const b = bus({ providers: [] });
const { admin: a, dataRoot } = admin(t, { riders: async () => [{ id: 'p', fixture: false, cliproxy: false, changes: '', call: b.call }] });
assert.equal(a.rideRemoved('p').op, 'ride-removed');
const r = await a.rideAll();
assert.deepEqual(r.added, []); assert.deepEqual(r.skipped, [{ id: 'p', reason: 'removed' }]);
assert.equal(b.calls.some(([v]) => v === 'plugins.set'), false);
assert.deepEqual(receipts(dataRoot).filter(x => x.op === 'ride-removed').map(x => x.org), ['p']);
assert.throws(() => a.rideRemoved('../x'), /proxy-org-invalid|invalid/);
});
test('the reviewer probe: an organization added on the Settings page and removed on its admin page is skipped as removed', async t => {
// P1 case b, with no ride-removed receipt at all: the change log's add is enough.
const b = bus({ providers: [] });
const log = committed([{ path: 'plugins.model-provider', old: [], new: ['cliproxy'] }]);
const { admin: a } = admin(t, { riders: async () => [{ id: 's', fixture: false, cliproxy: false, changes: log, call: b.call }] });
const r = await a.rideAll();
assert.deepEqual(r.added, []); assert.deepEqual(r.skipped, [{ id: 's', reason: 'removed' }]);
assert.equal(b.calls.some(([v]) => v === 'plugins.set'), false);
});
test('each add leaves its intent receipt before plugins.set, and an add whose outcome is unknown stays recorded', async t => {
// P2b. The bus reads the receipts at the moment of the write.
let seen = null, dataRootOf = null;
const order = [];
const watching = id => { const b = bus({ providers: [] }); const call = async (verb, args) => { if (verb === 'plugins.set') { order.push(id); seen = receipts(dataRootOf()).map(r => `${r.op}:${r.org ?? ''}`); } return b.call(verb, args); }; return { ...b, call }; };
const one = watching('one'), two = watching('two');
const f = admin(t, { riders: async () => [{ id: 'one', fixture: false, cliproxy: false, changes: '', call: one.call }, { id: 'two', fixture: false, cliproxy: false, changes: '', call: two.call }] });
dataRootOf = () => f.dataRoot;
await f.admin.rideAll();
assert.deepEqual(order, ['one', 'two']);
assert.deepEqual(seen, ['ride-intent:one', 'ride-intent:two'], 'the first add is on record before the second starts');
// Probe C: plugins.set commits, then the call times out. The bus shows cliproxy, so the intent stands.
let providers = [];
const timeout = { calls: [], call: async (verb, args) => {
if (verb === 'plugins') return { revision: 'r1', plugins: { 'model-provider': providers } };
if (verb === 'proxy.accounts') return { accounts: [] };
if (verb === 'plugins.set') { providers = args.plugins['model-provider']; throw busError('timeout'); }
} };
let cp = false;
const c = admin(t, { riders: async () => [{ id: 't', fixture: false, cliproxy: cp, changes: '', call: timeout.call }] });
const first = await c.admin.rideAll();
assert.deepEqual(first.skipped, [{ id: 't', reason: 'timeout' }]);
assert.deepEqual(receipts(c.dataRoot).filter(r => r.org === 't').map(r => r.op), ['ride-intent']);
providers = []; // the owner took it off
assert.deepEqual((await c.admin.rideAll()).skipped, [{ id: 't', reason: 'removed' }]);
// A refusal the bus shows was not applied clears the intent, so the organization can ride later.
const refusedBus = bus({ providers: [], set: busError('revision-conflict') });
const d = admin(t, { riders: async () => [{ id: 'q', fixture: false, cliproxy: false, changes: '', call: refusedBus.call }] });
assert.deepEqual((await d.admin.rideAll()).skipped, [{ id: 'q', reason: 'revision-conflict' }]);
assert.deepEqual(receipts(d.dataRoot).filter(r => r.org === 'q').map(r => r.op), ['ride-intent', 'ride-not-added']);
assert.deepEqual((await d.admin.status()).ride, [{ id: 'q', state: 'ready' }]);
// A bus that does not answer the re-read leaves the intent: the add may have happened.
let gone = false;
const lost = { call: async (verb, args) => { if (gone) throw busError('no-bus-host'); if (verb === 'plugins') return { revision: 'r1', plugins: { 'model-provider': [] } }; if (verb === 'proxy.accounts') return { accounts: [] }; if (verb === 'plugins.set') { gone = true; throw busError('timeout'); } } };
const e = admin(t, { riders: async () => [{ id: 'l', fixture: false, cliproxy: false, changes: '', call: lost.call }] });
await e.admin.rideAll();
assert.deepEqual(receipts(e.dataRoot).filter(r => r.org === 'l').map(r => r.op), ['ride-intent']);
});
test('an intent receipt that cannot be written stops that add before plugins.set', async t => {
const b = bus({ providers: [] });
const f = admin(t, { riders: async () => [{ id: 'w', fixture: false, cliproxy: false, changes: '', call: b.call }] });
appendReceipt(f.dataRoot, { op: 'fetch' });
fs.chmodSync(receiptFile(f.dataRoot), 0o400); // readable by its owner only, not writable
const r = await f.admin.rideAll();
assert.deepEqual(r.skipped, [{ id: 'w', reason: 'not-recorded' }]);
assert.equal(b.calls.some(([v]) => v === 'plugins.set'), false);
});
test('receipts that are torn, a symlink, not the owner\'s or open to others are unreadable, and every organization without cliproxy is skipped', async t => {
// P2a and P3 (probes D and E).
const b = bus({ providers: [] });
const f = admin(t, { riders: async () => [{ id: 'z', fixture: false, cliproxy: false, changes: '', call: b.call }, { id: 'y', fixture: false, cliproxy: true, changes: '', call: b.call }] });
const file = receiptFile(f.dataRoot);
appendReceipt(f.dataRoot, { op: 'fetch' });
fs.appendFileSync(file, '{"op":"ride-all","added":["z"]');
let r = await f.admin.rideAll();
assert.deepEqual(r.skipped, [{ id: 'z', reason: 'unreadable' }], 'torn'); assert.deepEqual(r.already, ['y']);
fs.rmSync(file); fs.writeFileSync(join(f.dataRoot, 'state', 'empty'), '', { mode: 0o600 });
fs.symlinkSync(join(f.dataRoot, 'state', 'empty'), file);
assert.deepEqual((await f.admin.rideAll()).skipped, [{ id: 'z', reason: 'unreadable' }], 'symlink');
assert.equal(fs.lstatSync(file).isSymbolicLink(), true);
fs.rmSync(file); fs.writeFileSync(file, '', { mode: 0o600 }); fs.chmodSync(file, 0o640);
assert.deepEqual((await f.admin.rideAll()).skipped, [{ id: 'z', reason: 'unreadable' }], 'group-readable');
fs.rmSync(file); fs.mkdirSync(file);
assert.deepEqual((await f.admin.rideAll()).skipped, [{ id: 'z', reason: 'unreadable' }], 'not a file');
fs.rmSync(file, { recursive: true });
assert.equal(b.calls.some(([v]) => v === 'plugins.set'), false);
assert.deepEqual((await f.admin.rideAll()).added, ['z'], 'no receipt file is no history');
});
test('the receipts are read only when the Console\'s own user owns them', t => {
const f = admin(t);
appendReceipt(f.dataRoot, { op: 'ride-removed', org: 'p' });
assert.match(receiptsText(f.dataRoot), /"ride-removed"/);
assert.equal(receiptsText(f.dataRoot, { uid: process.getuid() + 1 }), null, 'another user\'s file');
});
test('a FIFO in place of the receipts is unreadable and never blocks the read', t => {
const f = admin(t);
fs.mkdirSync(join(f.dataRoot, 'state'), { mode: 0o700 });
execFileSync('mkfifo', ['-m', '600', receiptFile(f.dataRoot)]);
assert.equal(receiptsText(f.dataRoot), null);
});
test('the change log read takes the writer\'s rule: group readable is fine, group writable, a symlink or a missing file is not', t => {
const dir = fs.mkdtempSync(join(tmpdir(), 'rdo-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
const file = join(dir, 'changes.jsonl'); fs.writeFileSync(file, 'x\n', { mode: 0o600 }); fs.chmodSync(file, 0o640);
assert.equal(ownedText(file, { mask: 0o022 }), 'x\n');
fs.chmodSync(file, 0o660);
assert.throws(() => ownedText(file, { mask: 0o022 }), { code: 'proxy-file-refused' });
fs.chmodSync(file, 0o600); fs.symlinkSync(file, join(dir, 'link'));
assert.throws(() => ownedText(join(dir, 'link'), { mask: 0o022 }), { code: 'ELOOP' });
assert.throws(() => ownedText(join(dir, 'gone'), { mask: 0o022 }), { code: 'ENOENT' });
// readRider, as the Console wires it: a refused log is unreadable, a missing one is empty.
const doc = { plugins: { 'model-provider': [] } };
fs.chmodSync(file, 0o660);
assert.equal(readRider({ id: 'a', read: () => doc, readLog: () => ownedText(file, { mask: 0o022 }), isFixture: () => false }).changes, null);
assert.equal(readRider({ id: 'a', read: () => doc, readLog: () => ownedText(join(dir, 'gone'), { mask: 0o022 }), isFixture: () => false }).changes, '');
});
test('a fixture organization that uses cliproxy now, with a removal in its log, is removed, not movable', () => {
const log = committed([{ path: 'plugins.model-provider', old: ['fixture', 'cliproxy'], new: ['fixture'] }]) + committed([{ path: 'plugins.model-provider', old: ['fixture'], new: ['fixture', 'cliproxy'] }]);
assert.equal(localRide({ fixture: true, cliproxy: true, changes: log }), 'removed');
assert.equal(localRide({ fixture: true, cliproxy: true, changes: '' }), 'fixture');
});
test('an organization ride all once added and its owner then saved without cliproxy is skipped as removed', async t => {
@@ -136,7 +294,7 @@ test('ride all adds cliproxy through plugins.set and skips and names each organi
{ id: 'already', fixture: false, cliproxy: true, changes: '', call: alreadyBus.call },
{ id: 'raced', fixture: false, cliproxy: false, changes: '', call: raced.call },
{ id: 'unreadable', fixture: undefined, cliproxy: undefined, changes: '', call: unreadable.call },
{ id: 'stand-in', fixture: true, cliproxy: false, changes: '', call: fixtureBus.call },
{ id: 'stand-in', fixture: true, cliproxy: false, changes: '', call: fixtureBus.call, status: async () => ({ pid: null }) },
];
const { admin: a, dataRoot } = admin(t, { riders });
const result = await a.rideAll();
@@ -192,10 +350,15 @@ test('a fixture organization moves only when stopped, with a proxy, a riding bus
const moves = [];
const move = async (id, models, principal) => { moves.push({ id, models, principal }); return { moved: true, model: models[0].key }; };
let stoppedBus = bus({ down: true }), runningBus = bus();
// Round 3 (P2c): stopped is the organization admin page's test, a bus status with no process id.
const stopped = async () => ({ healthy: false, pid: null }), running = async () => ({ healthy: true, pid: 4242 });
const riders = async () => [
{ id: 'stand-in', fixture: true, cliproxy: false, changes: '', call: stoppedBus.call },
{ id: 'live', fixture: true, cliproxy: false, changes: '', call: runningBus.call },
{ id: 'real', fixture: false, cliproxy: false, changes: '', call: stoppedBus.call },
{ id: 'stand-in', fixture: true, cliproxy: false, changes: '', call: stoppedBus.call, status: stopped },
{ id: 'live', fixture: true, cliproxy: false, changes: '', call: runningBus.call, status: running },
{ id: 'busy', fixture: true, cliproxy: false, changes: '', call: stoppedBus.call, status: async () => ({ healthy: false, pid: 4343 }) },
{ id: 'unproven', fixture: true, cliproxy: false, changes: '', call: stoppedBus.call, status: async () => { throw busError('stale-host'); } },
{ id: 'no-status', fixture: true, cliproxy: false, changes: '', call: stoppedBus.call },
{ id: 'real', fixture: false, cliproxy: false, changes: '', call: stoppedBus.call, status: stopped },
];
let ownerNow = owner;
const f = admin(t, { riders, ownerModels: async () => ownerNow, move });
@@ -203,6 +366,10 @@ test('a fixture organization moves only when stopped, with a proxy, a riding bus
assert.deepEqual(await f.admin.rideMove({ org: 'nope', principal }), { org: 'nope', moved: false, reason: 'unknown' });
assert.deepEqual(await f.admin.rideMove({ org: 'real', principal }), { org: 'real', moved: false, reason: 'ready' });
assert.deepEqual(await f.admin.rideMove({ org: 'live', principal }), { org: 'live', moved: false, reason: 'bus-running' });
// A slow or busy bus that does not answer, a status that cannot be read, or no status at all is running.
for (const org of ['busy', 'unproven', 'no-status']) assert.deepEqual(await f.admin.rideMove({ org, principal }), { org, moved: false, reason: 'bus-running' }, org);
const listed = await f.admin.rideAll();
assert.deepEqual(listed.fixtures, [{ id: 'stand-in', running: false }, { id: 'live', running: true }, { id: 'busy', running: true }, { id: 'unproven', running: true }, { id: 'no-status', running: true }]);
ownerNow = { ...owner, proxy: false };
assert.deepEqual(await f.admin.rideMove({ org: 'stand-in', principal }), { org: 'stand-in', moved: false, reason: 'no-proxy' });
ownerNow = { ...owner, riding: false };
+8 -4
View File
@@ -22,6 +22,7 @@ import { showOrg } from '../../organization/src/org-service.mjs';
import * as orgService from '../../organization/src/org-service.mjs';
import { orgUsesFixture } from '../../cli/src/dev-fixture.mjs';
import { moveFixtureOrg, ownerModelsFrom, readRider } from '../../model-proxy/src/ride.mjs';
import { ownedText } from '../../model-proxy/src/admin.mjs';
import { configDir } from '../../organization/src/organization-config.mjs';
import { loadRootAgents } from '../../organization/src/agent-levels.mjs';
import { organizationWriter } from '../../organization/src/organization.mjs';
@@ -223,17 +224,20 @@ try {
// Instance-wide: one pinned release, one proxy.json, one unit. Each organization uses it (shared).
// #1873 ride-default: each active organization as Ride all reads it, from its file and change log
// (read only) and, through `call`, from its bus as the owner. A file that cannot be read gives
// undefined fields, which Ride all names as unreadable.
// undefined fields, which Ride all names as unreadable. The change log is read with the writer's
// own check (no symlink, a regular file this user owns, not group or world writable).
const rideRiders = async () => {
const writer = organizationWriter({ dir: configDir(), rolesDir: rolesDir() });
const local = id => readRider({ id, read: () => writer.read(id).document, readLog: () => readFileSync(writer.logFile(id), 'utf8'), isFixture: orgUsesFixture });
const local = id => readRider({ id, read: () => writer.read(id).document, readLog: () => ownedText(writer.logFile(id), { mask: 0o022 }), isFixture: orgUsesFixture });
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 }] : [];
const out = [];
for (const org of await listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance })) {
if (org.state !== 'active') continue;
let call = down; try { const paths = orgPaths(org.root); call = busCaller({ dataRoot: org.root, socket: paths.socket, business: org.id, timeoutMs: 5000 }); } catch {}
out.push({ ...local(org.id), call });
// The organization admin page's stopped test reads this bus status (its process id).
out.push({ ...local(org.id), call, status: () => orgBusStatus({ configDir: configDir(), dataRoot: system.dataRoot, instance }, org.id) });
}
return out;
};
@@ -333,7 +337,7 @@ try {
},
})] : [];
try {
server = await startServer({ restart, trail, ...(lan ? { host: lan.bind, devAllowHosts: lan.allowHosts } : {}), board: options.board, port: options.port, bus, reads, preview: options.preview, sso, proof, send, settingsWrite, projectFiles, projectFile, libraryFile, libraryStatus, librarySync, libraryRevert, librarySave, projectSave, chat, nameWrite, assistantStop, instancesWrite, routesWrite, agentWrite, proxyCall, proxyAdmin, triage, processing, decisionWrite, requestRead, requestWrite, userOptions, federation: system?.federation ?? null, organizations, agentLevels, ownerModels, routes: [...agentLevelRoutes, ...triageRoutes], preselect: options.business ?? null });
server = await startServer({ restart, trail, ...(lan ? { host: lan.bind, devAllowHosts: lan.allowHosts } : {}), board: options.board, port: options.port, bus, reads, preview: options.preview, sso, proof, send, settingsWrite, projectFiles, projectFile, libraryFile, libraryStatus, librarySync, libraryRevert, librarySave, projectSave, chat, nameWrite, assistantStop, instancesWrite, routesWrite, agentWrite, proxyCall, proxyAdmin, triage, processing, decisionWrite, requestRead, requestWrite, userOptions, federation: system?.federation ?? null, organizations, agentLevels, ownerModels, rideRemoved: proxyAdmin ? id => proxyAdmin.rideRemoved(id) : null, routes: [...agentLevelRoutes, ...triageRoutes], preselect: options.business ?? null });
} catch (err) { sso?.close(); throw err; }
if (testerSocket) {
const call = busCaller({ dataRoot: system.dataRoot, socket: socketPath(system.dataRoot), business: options.business ?? null, cli: TESTER_CLI });
+37 -16
View File
@@ -286,37 +286,52 @@ function refusal(res, error, { step, id, unknownOk = false }) {
// Default service wiring: the real C1/C2/C3 modules, loaded on first use.
// A missing module (a branch without S1 yet) answers not-configured.
async function load(spec) { try { return await import(spec); } catch (e) { if (e?.code === 'ERR_MODULE_NOT_FOUND') return null; throw e; } }
export function defaultOrgAdmin(env = process.env, { ownerModels = null } = {}) {
export function defaultOrgAdmin(env = process.env, { ownerModels = null, rideRemoved = null } = {}) {
let system = null;
// The same context `mosaic org` builds (packages/cli/src/org.mjs): the named
// instance from MOSAIC_CONFIG, its data root and its config directory.
const context = async () => {
if (!system) {
const { loadSystem } = await import('../../cli/src/config.mjs');
const { configDir } = await import('../../organization/src/organization-config.mjs');
const { devOwnerInstance } = await import('../../cli/src/dev-local-owner.mjs');
let instance; try { instance = devOwnerInstance(env); } catch { return null; }
system = { configDir: configDir(env), dataRoot: loadSystem({ env, instance }).dataRoot, instance };
}
return system;
};
return createOrgAdmin({
ownerModels,
ownerModels, rideRemoved: rideRemoved ?? removalRecorder(context),
service: () => load('../../organization/src/org-service.mjs'),
registry: () => load('../../organization/src/org-registry.mjs'),
bus: () => load('../../cli/src/org-bus.mjs'),
// The same context `mosaic org` builds (packages/cli/src/org.mjs): the named
// instance from MOSAIC_CONFIG, its data root and its config directory.
context: async () => {
if (!system) {
const { loadSystem } = await import('../../cli/src/config.mjs');
const { configDir } = await import('../../organization/src/organization-config.mjs');
const { devOwnerInstance } = await import('../../cli/src/dev-local-owner.mjs');
let instance; try { instance = devOwnerInstance(env); } catch { return null; }
system = { configDir: configDir(env), dataRoot: loadSystem({ env, instance }).dataRoot, instance };
}
return system;
},
context,
});
}
// A Console without the proxy setup page still records a removal of cliproxy, in the instance's
// proxy receipts (model-proxy admin.mjs appendReceipt), where Ride all reads it. `context()` gives
// the instance's data root; with none, the record and the save are refused.
export function removalRecorder(context) {
return async id => {
const ctx = await context(), proxy = await load('../../model-proxy/src/admin.mjs');
if (!ctx?.dataRoot || !proxy) throw Object.assign(new Error('not-configured'), { code: 'not-configured' });
return proxy.appendReceipt(ctx.dataRoot, { time: new Date().toISOString(), op: 'ride-removed', org: id });
};
}
// `service`, `registry` and `bus` are module objects or functions returning
// them (or null when absent). `context()` gives { configDir, dataRoot, instance }.
// `ownerModels()` (#1873 ride-default) answers {proxy, riding, models} from the owner's model
// proxy, read through the riding buses; without it the cliproxy preset reads unavailable.
export function createOrgAdmin({ service, registry = null, bus = null, context, ownerModels = null }) {
// `rideRemoved(id)` records in the proxy's receipts that a save took cliproxy off the organization,
// so Ride all leaves it off; without it such a save is refused.
export function createOrgAdmin({ service, registry = null, bus = null, context, ownerModels = null, rideRemoved = null }) {
const get = async x => (typeof x === 'function' ? x() : x);
const owner = async () => { if (typeof ownerModels !== 'function') return { proxy: false, riding: false, models: [] }; try { return await ownerModels(); } catch { return { proxy: false, riding: false, models: [] }; } };
return { service: () => get(service), registry: () => get(registry), bus: () => get(bus), context: () => get(context), ownerModels: owner };
return { service: () => get(service), registry: () => get(registry), bus: () => get(bus), context: () => get(context), ownerModels: owner, rideRemoved };
}
const providersOf = doc => (Array.isArray(doc?.plugins?.['model-provider']) ? doc.plugins['model-provider'] : []);
const ownerOf = admin => (typeof admin.ownerModels === 'function' ? admin.ownerModels() : { proxy: false, riding: false, models: [] });
// The handler. Returns true when it answered. `owner` is the signed-in owner
@@ -524,6 +539,12 @@ async function route(req, res, { path, search, org, admin, principal, write, log
const doc = toCanonical(id, body.doc, shown.doc);
const checked = await service.validateOrg(doc);
if (!checked.ok) return json(res, 422, { error: 'invalid', step: 'update', errors: withField(checked.errors ?? []) });
// #1873 ride-default: a save that takes cliproxy off leaves a proxy receipt first, so Ride all
// does not put it back. No record, no save.
if (providersOf(shown.doc).includes('cliproxy') && !providersOf(doc).includes('cliproxy')) {
if (typeof admin.rideRemoved !== 'function') return json(res, 503, { error: 'not-configured', step: 'update', message: 'this Console cannot record that the model proxy was taken off, so nothing was saved' });
try { await admin.rideRemoved(id); } catch { return json(res, 500, { error: 'internal', step: 'update', message: 'the removal of the model proxy could not be recorded, so nothing was saved' }); }
}
let receipt;
try { receipt = await service.updateOrg(ctx, id, doc, { ifRevision: body.ifRevision }); } catch (e) { return refusal(res, e, { step: 'update', id, unknownOk: true }); }
return json(res, 200, { outcome: 'done', step: 'update', receipt: forReceipt(ctx, receipt), verb: verbFor('update', id, { revision: body.ifRevision }) });
+2 -1
View File
@@ -764,7 +764,8 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
// #1873 ride-default: Ride all and the fixture move. The server decides each organization again;
// this code only words the answer. Every write asks first, in plain words, what changes.
const RIDE_REASONS = {
removed: 'Its owner took the model proxy off. Ride all leaves it off.',
removed: 'The model proxy was on it, or Ride all may have added it, and it is off now. Ride all leaves it off. Add it on its Settings page to use it.',
'not-recorded': 'The proxy could not record the change first, so it was not made.',
'bus-down': 'Its bus is not running. Start it, then ride again.',
'no-proxy-client': 'Its bus started before the proxy was set up. Restart its bus, then ride again.',
'proxy-check-failed': 'Its bus could not check the proxy. Check the proxy, then ride again.',
+2 -2
View File
@@ -812,7 +812,7 @@ async function libraryStatusRead(res, read, search) {
// `log` takes one line per send attempt that got past sign-in; never the body. `fieldsFile` is for
// tests; the field table is otherwise the committed packages/tasks/fields.json.
// `agentWrite` (row 194, D7) is `(verb, args) => Promise` for the Agents section's verbs and instance.request.
export async function startServer({ host = '127.0.0.1', port = 7330, board = DEFAULT_BOARD, timeout = 20000, bus = null, reads = null, preview = null, sso = null, tester = false, testerSocket = null, testerAuthHeader = null, proof = null, testerProofHeader = null, send = null, settingsWrite = null, projectFiles = null, projectFile = null, projectSave = null, libraryStatus = null, libraryRevert = null, chat = null, nameWrite = null, assistantStop = null, instancesWrite = null, routesWrite = null, agentWrite = null, proxyCall = null, proxyAdmin = null, triage = null, processing = null, decisionWrite = null, requestRead = null, requestWrite = null, userOptions = null, federation = null, organizations = null, preselect = null, routes = [], orgAdmin = null, ownerModels = null, devAllowHosts = [], log = line => console.log(line), fieldsFile = undefined, restart = null, trail = null, boot = randomUUID(), workspaceTimeoutMs = undefined, agentLevels = null } = {}) {
export async function startServer({ host = '127.0.0.1', port = 7330, board = DEFAULT_BOARD, timeout = 20000, bus = null, reads = null, preview = null, sso = null, tester = false, testerSocket = null, testerAuthHeader = null, proof = null, testerProofHeader = null, send = null, settingsWrite = null, projectFiles = null, projectFile = null, projectSave = null, libraryStatus = null, libraryRevert = null, chat = null, nameWrite = null, assistantStop = null, instancesWrite = null, routesWrite = null, agentWrite = null, proxyCall = null, proxyAdmin = null, triage = null, processing = null, decisionWrite = null, requestRead = null, requestWrite = null, userOptions = null, federation = null, organizations = null, preselect = null, routes = [], orgAdmin = null, ownerModels = null, rideRemoved = null, devAllowHosts = [], log = line => console.log(line), fieldsFile = undefined, restart = null, trail = null, boot = randomUUID(), workspaceTimeoutMs = undefined, agentLevels = null } = {}) {
// org-lan-bind: the dev owner on the LAN is an explicit opt-in. `devAllowHosts` names the Host
// names (without a port) a remote browser may use; only the dev owner takes them, and only with
// them may it bind a non-loopback IPv4 address. SSO, preview and tester listeners stay loopback.
@@ -842,7 +842,7 @@ export async function startServer({ host = '127.0.0.1', port = 7330, board = DEF
const registered = routeTable();
for (const registerRoutes of routes) registerRoutes(registered);
registered.add('GET', '/api/themes', (req, res) => themesHTTP(req, res, { log }), { publicRead: true });
const adminRoute = (req, res, context) => orgAdminHTTP(req, res, { ...context, admin: orgAdmin ??= defaultOrgAdmin(process.env, { ownerModels }), log, trail });
const adminRoute = (req, res, context) => orgAdminHTTP(req, res, { ...context, admin: orgAdmin ??= defaultOrgAdmin(process.env, { ownerModels, rideRemoved }), log, trail });
for (const path of ['/api/org-template', '/api/org-presets', '/api/org-validate', '/api/org-admin', /^\/api\/orgs\/[^/]+\/(?:admin|setup|repo|delete|archive|restore|restart|stop)(?:\/(?:queue|bus|project\/[^/]+|records\/[^/]+))?$/]) {
for (const method of ['GET', 'POST']) registered.add(method, path, adminRoute);
}
+113 -1
View File
@@ -8,7 +8,7 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { startServer } from '../src/serve.mjs';
import { createDevOwner } from '../src/dev-owner.mjs';
import { createOrgAdmin, orgAdminHTTP, browserProblems, pathFromMessage, toCanonical, forBrowser, plainMessage } from '../src/org-admin.mjs';
import { createOrgAdmin, defaultOrgAdmin, removalRecorder, orgAdminHTTP, browserProblems, pathFromMessage, toCanonical, forBrowser, plainMessage } from '../src/org-admin.mjs';
import { actionTrail } from '../src/action-trail.mjs';
import { close } from './fixture.mjs';
@@ -1104,3 +1104,115 @@ test('the cliproxy preset uses the owner\'s catalogue and refuses a model it doe
owner = null; admin.ownerModels = createOrgAdmin({ service, context: async () => ({}), ownerModels: async () => { throw new Error('bus down'); } }).ownerModels;
assert.match((await presetOf()).available.reason, /Set up the model proxy first/, 'a failing catalogue read is no proxy');
});
// #1873 ride-default round 3 (P1): a save on the admin page that drops cliproxy from the model
// providers leaves a ride-removed record first, so the next Ride all leaves the organization off.
test('a save that takes cliproxy off records the removal before it writes, and saves nothing when it cannot', async t => {
const removed = [];
let fail = null;
const f = fakes();
const admin = createOrgAdmin({ service: f.service, registry: await f.admin.registry(), bus: null, context: async () => ({ configDir: '/cfg', dataRoot: '/data', instance: 'test' }), rideRemoved: async id => { if (fail) throw fail; removed.push(id); } });
const c = await console_(t, { orgAdmin: admin });
const d = { call: c.call, orgs: f.orgs, calls: f.calls };
await d.call('POST', '/api/orgs', { id: 'acme', doc: browserDoc('acme') });
d.orgs.get('acme').doc.plugins = { 'model-provider': ['cliproxy', 'openai'] };
const save = async providers => {
const shown = await d.call('GET', '/api/orgs/acme/admin');
const doc = shown.body.doc; doc.plugins = { 'model-provider': providers };
return d.call('POST', '/api/orgs/acme/admin', { doc, ifRevision: shown.body.revision });
};
// Keeping cliproxy records nothing.
let r = await save(['cliproxy']);
assert.equal(r.status, 200, r.raw); assert.deepEqual(removed, []);
// The hook fails: nothing is saved.
fail = new Error('disk full /data/state/proxy-admin.jsonl');
r = await save(['openai']);
assert.equal(r.status, 500, r.raw); assert.equal(r.body.error, 'internal');
assert.doesNotMatch(r.raw, /proxy-admin\.jsonl|disk full/);
assert.deepEqual(d.orgs.get('acme').doc.plugins['model-provider'], ['cliproxy']);
fail = null;
const updates = d.calls.filter(x => x[0] === 'updateOrg').length;
r = await save(['openai']);
assert.equal(r.status, 200, r.raw); assert.deepEqual(removed, ['acme']);
assert.equal(d.calls.filter(x => x[0] === 'updateOrg').length, updates + 1);
assert.deepEqual(d.orgs.get('acme').doc.plugins['model-provider'], ['openai']);
// A Console wired without the record refuses that save.
const plain = await console_(t);
await plain.call('POST', '/api/orgs', { id: 'acme', doc: browserDoc('acme') });
plain.orgs.get('acme').doc.plugins = { 'model-provider': ['cliproxy'] };
const shown = await plain.call('GET', '/api/orgs/acme/admin');
const doc = shown.body.doc; doc.plugins = { 'model-provider': [] };
r = await plain.call('POST', '/api/orgs/acme/admin', { doc, ifRevision: shown.body.revision });
assert.equal(r.status, 503, r.raw); assert.equal(r.body.error, 'not-configured');
assert.deepEqual(plain.orgs.get('acme').doc.plugins['model-provider'], ['cliproxy']);
assert.equal(plain.calls.some(x => x[0] === 'updateOrg'), false);
});
// The reviewer's two P1 probes end to end: the real organization service, the admin page route,
// the real proxy admin and its receipts, and the organization file and change log as Ride all reads them.
test('against the real C2 service: an organization saved without cliproxy on its admin page is skipped by Ride all as removed', async t => {
const service = await import('../../organization/src/org-service.mjs');
const registry = await import('../../organization/src/org-registry.mjs');
const { organizationWriter } = await import('../../organization/src/organization.mjs');
const { createProxyAdmin } = await import('../../model-proxy/src/admin-control.mjs');
const { readRider } = await import('../../model-proxy/src/ride.mjs');
const { orgUsesFixture } = await import('../../cli/src/dev-fixture.mjs');
const { mkdtempSync, mkdirSync, readFileSync, rmSync } = await import('node:fs');
const { join, resolve, dirname } = await import('node:path'); const { tmpdir } = await import('node:os'); const { fileURLToPath } = await import('node:url');
const root = mkdtempSync(join(tmpdir(), 'oar')); const configDir = join(root, 'config'), dataRoot = join(root, 'data');
t.after(() => rmSync(root, { recursive: true, force: true }));
mkdirSync(configDir, { mode: 0o700 }); mkdirSync(dataRoot, { mode: 0o700 });
const rolesDir = resolve(dirname(fileURLToPath(import.meta.url)), '../../../roles');
const writes = [];
const busFor = id => async (verb, args) => {
if (verb === 'plugins') return { revision: 'r', plugins: { 'model-provider': organizationWriter({ dir: configDir, rolesDir }).read(id).document.plugins?.['model-provider'] ?? [] } };
if (verb === 'proxy.accounts') return { accounts: [] };
if (verb === 'plugins.set') { writes.push(id); return { revision: 'r2' }; }
throw new Error(`unexpected ${verb}`);
};
const riders = async () => ['made', 'settings'].map(id => {
const writer = organizationWriter({ dir: configDir, rolesDir });
return { ...readRider({ id, read: () => writer.read(id).document, readLog: () => readFileSync(writer.logFile(id), 'utf8'), isFixture: orgUsesFixture }), call: busFor(id) };
});
const proxy = createProxyAdmin({ dataRoot, configDir, unit: { unit: 'u', status: async () => ({}) }, buses: async () => ({ stopped: true, running: [] }), fetch: async () => ({}), riders });
const admin = createOrgAdmin({ service, registry, bus: null, context: async () => ({ configDir, dataRoot, instance: 'test' }), rideRemoved: id => proxy.rideRemoved(id) });
const c = await console_(t, { orgAdmin: admin });
const tpl = (await c.call('GET', '/api/org-template?id=made')).body.doc;
// Probe a: made on cliproxy (as the preset makes it), then saved without it on the admin page.
const made = await c.call('POST', '/api/orgs', { id: 'made', doc: { ...tpl, plugins: { 'model-provider': ['cliproxy'] } } });
assert.equal(made.status, 201, made.raw);
// Probe b: cliproxy added through the organization writer (as the Settings page's bus plugins.set writes it, with a change-log entry), then removed on the admin page.
const tpl2 = (await c.call('GET', '/api/org-template?id=settings')).body.doc;
assert.equal((await c.call('POST', '/api/orgs', { id: 'settings', doc: tpl2 })).status, 201);
const writer = organizationWriter({ dir: configDir, rolesDir });
const current = writer.read('settings');
writer.write('settings', { ...current.document, plugins: { ...(current.document.plugins ?? {}), 'model-provider': ['cliproxy'] } }, { revision: current.revision, actor: current.document.human });
assert.match(readFileSync(writer.logFile('settings'), 'utf8'), /cliproxy/);
for (const id of ['made', 'settings']) {
const shown = await c.call('GET', `/api/orgs/${id}/admin`);
assert.deepEqual(shown.body.doc.plugins['model-provider'], ['cliproxy'], id);
const doc = shown.body.doc; doc.plugins = { 'model-provider': [] };
const r = await c.call('POST', `/api/orgs/${id}/admin`, { doc, ifRevision: shown.body.revision });
assert.equal(r.status, 200, r.raw);
}
const result = await proxy.rideAll();
assert.deepEqual(result.added, []);
assert.deepEqual(result.skipped, [{ id: 'made', reason: 'removed' }, { id: 'settings', reason: 'removed' }]);
assert.deepEqual(writes, [], 'Ride all put cliproxy back on neither');
});
test('a Console without the proxy setup page records the removal in the instance\'s proxy receipts, and refuses with no data root', async t => {
const { riddenOrgs } = await import('../../model-proxy/src/ride.mjs');
const { receiptsText } = await import('../../model-proxy/src/admin.mjs');
const dataRoot = mkdtempSync(join(tmpdir(), 'oarr')); t.after(() => rmSync(dataRoot, { recursive: true, force: true }));
const record = await removalRecorder(async () => ({ dataRoot }))('acme');
assert.equal(record.op, 'ride-removed'); assert.equal(record.org, 'acme');
assert.deepEqual([...riddenOrgs(receiptsText(dataRoot))], ['acme']);
await assert.rejects(removalRecorder(async () => null)('acme'), /not-configured/);
});
test('the default Console wiring always has a removal recorder, and keeps the one it is given', () => {
assert.equal(typeof defaultOrgAdmin({}).rideRemoved, 'function');
const given = async () => ({ op: 'ride-removed' });
assert.equal(defaultOrgAdmin({}, { rideRemoved: given }).rideRemoved, given);
});
@@ -185,5 +185,23 @@ test('Ride all confirms the stricter admission, paints skips and stand-in organi
assert.match(await b.evaluate('window.__confirms[2]'), /^Move west to your models\?.*its agents spend real tokens on your account/);
assert.deepEqual(JSON.parse(await b.evaluate('window.__writes.find(w=>w.path==="/api/proxy/admin/ride-move").body')), { org: 'west' });
await wait('document.querySelector("#proxy-ride-message").textContent.startsWith("west was not moved.")');
// Round 3 (P3): an SSO Console's org-forbidden in plain words, and the neutral fallback for a code the page does not know.
for (const [n, reason, text] of [[2, 'org-forbidden', 'west was not moved. Moving organizations is not available on this Console yet. It works on a local development Console.'],
[3, 'frobnicated', 'west was not moved. The change was refused (frobnicated).']]) {
await b.evaluate(`window.__scenario['POST /api/proxy/admin/ride-move'] = { body: ${JSON.stringify({ moved: false, reason })} }`);
await wait('document.querySelector("[data-ride-move=west]")?.disabled===false');
await b.evaluate('document.querySelector("[data-ride-move=west]").click()');
await wait(`window.__writes.filter(w=>w.path==="/api/proxy/admin/ride-move").length===${n}`);
await wait(`document.querySelector("#proxy-ride-message").textContent===${JSON.stringify(text)}`);
}
// Round 3: an organization the proxy was or may have been on, and an add the receipt could not record first.
await b.evaluate(`window.__scenario['POST /api/proxy/admin/ride-all'] = { body: ${JSON.stringify({ added: [], already: [], skipped: [{ id: 'north', reason: 'removed' }, { id: 'south', reason: 'not-recorded' }], fixtures: [], receipt: { op: 'ride.all', time: T } })} }`);
await wait('document.querySelector("#proxy-ride-go").disabled===false');
await b.evaluate('document.querySelector("#proxy-ride-go").click()');
await wait('window.__writes.filter(w=>w.path==="/api/proxy/admin/ride-all").length===3');
await wait('document.querySelectorAll("#proxy-ride-skips li").length===2');
assert.deepEqual(JSON.parse(await b.evaluate('JSON.stringify([...document.querySelectorAll("#proxy-ride-skips li")].map(li=>li.textContent))')), [
'Skipped north: The model proxy was on it, or Ride all may have added it, and it is off now. Ride all leaves it off. Add it on its Settings page to use it.',
'Skipped south: The proxy could not record the change first, so it was not made.']);
assert.deepEqual(await b.evaluate('window.__unexpected'), []);
});