fix(model-proxy): re-check 3: mosaic org update that takes cliproxy off an organization records the removal in the proxy's receipts first and saves nothing if it cannot; Ride all reads the receipts again before each add, so a removal saved during a run is honoured; the bus-running and unreadable skip lines say what to do (#1873, lane ride-default)
This commit was merged in pull request #1999.
This commit is contained in:
1 parent
9bfe101176
commit
2db4f24fd8
6 files changed
+142
-7
No files matched your search
@@ -5,6 +5,7 @@ import { listOrgs, orgLock, resolveOrg } from '../../organization/src/org-regist
|
||||
import { organizationWriter } from '../../organization/src/organization.mjs';
|
||||
import { templateRender, validateOrg, createOrg, showOrg, updateOrg, queueInit, queueShow, projectRepoInit, projectRepoShow, projectRepoSet, deleteOrg, archiveOrg, restoreOrg } from '../../organization/src/org-service.mjs';
|
||||
import { installedStarterPacks } from '../../plugins/src/starter-pack.mjs';
|
||||
import { appendReceipt } from '../../model-proxy/src/admin.mjs';
|
||||
import { DEV_OWNER, devOwnerInstance } from './dev-local-owner.mjs';
|
||||
import { loadSystem, orgPaths, rolesDir } from './config.mjs';
|
||||
import { CliError } from './errors.mjs';
|
||||
@@ -173,6 +174,18 @@ async function templateCommand(args, io, deps) {
|
||||
io.stdout.write(JSON.stringify(result, null, 2) + '\n');
|
||||
}
|
||||
const busStartVerb = id => `mosaic bus start ${id} --dev-local-owner --detach`;
|
||||
// #1873 ride-default: `mosaic org update` that takes cliproxy off an organization records the removal
|
||||
// in the instance's proxy receipts first, where Ride all reads it, as the Console's admin save does.
|
||||
// A record that cannot be written refuses the update and writes nothing. A stale revision records
|
||||
// nothing and is left to updateOrg's own refusal.
|
||||
const providerList = doc => (Array.isArray(doc?.plugins?.['model-provider']) ? doc.plugins['model-provider'] : []);
|
||||
export async function orgUpdate(ctx, id, doc, { ifRevision, record = org => appendReceipt(ctx.dataRoot, { time: new Date().toISOString(), op: 'ride-removed', org }) } = {}) {
|
||||
const shown = await showOrg(ctx, id);
|
||||
if (shown.revision === ifRevision && providerList(shown.doc).includes('cliproxy') && !providerList(doc).includes('cliproxy')) {
|
||||
try { await record(id); } catch { throw new CliError('the removal of the model proxy could not be recorded, so nothing was saved', 3); }
|
||||
}
|
||||
return updateOrg(ctx, id, doc, { ifRevision });
|
||||
}
|
||||
export async function orgCommand(args, io, deps = {}) {
|
||||
try {
|
||||
// #1873 T1: the definitions verbs take their own options.
|
||||
@@ -195,7 +208,7 @@ export async function orgCommand(args, io, deps = {}) {
|
||||
const instance = devOwnerInstance(io.env); const system = deps.system ?? loadSystem({ env: io.env, instance }); const ctx = { configDir: configDir(io.env), dataRoot: system.dataRoot, instance, principal: DEV_OWNER };
|
||||
if (words[0] === 'lock' && ['show', 'clear'].includes(words[1]) && words.length === 3) { allowed([], []); result = await orgLock(ctx, words[2], { clear: words[1] === 'clear' }); }
|
||||
else if (words[0] === 'create' && words.length === 2) { allowed([], ['--from']); result = await createOrg(ctx, words[1], await document(need('--from'))); }
|
||||
else if (words[0] === 'update' && words.length === 2) { allowed([], ['--from', '--if-revision']); const r = need('--if-revision'); if (!/^[1-9][0-9]*$/.test(r)) throw new CliError('invalid revision', 4); result = await updateOrg(ctx, words[1], await document(need('--from')), { ifRevision: Number(r) }); }
|
||||
else if (words[0] === 'update' && words.length === 2) { allowed([], ['--from', '--if-revision']); const r = need('--if-revision'); if (!/^[1-9][0-9]*$/.test(r)) throw new CliError('invalid revision', 4); result = await orgUpdate(ctx, words[1], await document(need('--from')), { ifRevision: Number(r) }); }
|
||||
else if (words[0] === 'archive' && words.length === 2) result = await archiveOrg(ctx, words[1], { ...lifecycle, busStop: busStopForDelete, copyRemote: await (deps.copyRemoteFor ?? copyRemoteFor)(ctx) });
|
||||
else if (words[0] === 'restore' && words.length === 2) {
|
||||
const startBus = !p.flags.has('--no-bus');
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
// #1873 ride-default, re-check 3 P1-1: `mosaic org update` that takes cliproxy off an organization
|
||||
// records the removal in the instance's proxy receipts before it writes, as the Console's admin save
|
||||
// does, so Ride all leaves the organization off.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { orgUpdate } from '../src/org.mjs';
|
||||
import * as orgService from '../../organization/src/org-service.mjs';
|
||||
import { createProxyAdmin } from '../../model-proxy/src/admin-control.mjs';
|
||||
import { receiptFile } from '../../model-proxy/src/admin.mjs';
|
||||
|
||||
const owner = { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' };
|
||||
function scratch(t) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'org-upd-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
fs.mkdirSync(configDir, { mode: 0o700 }); fs.mkdirSync(dataRoot, { mode: 0o700 });
|
||||
return { configDir, dataRoot, instance: 'test', principal: owner };
|
||||
}
|
||||
const receipts = dataRoot => { try { return fs.readFileSync(receiptFile(dataRoot), 'utf8').trim().split('\n').filter(Boolean).map(JSON.parse); } catch { return []; } };
|
||||
const withProviders = (doc, list) => ({ ...doc, plugins: { ...(doc.plugins ?? {}), 'model-provider': list } });
|
||||
async function cliproxyOrg(ctx, id) {
|
||||
const doc = withProviders(await orgService.templateRender(id), ['cliproxy']);
|
||||
await orgService.createOrg(ctx, id, doc);
|
||||
return orgService.showOrg(ctx, id);
|
||||
}
|
||||
|
||||
test('the reviewer probe: an organization taken off cliproxy by mosaic org update is skipped by Ride all as removed', async t => {
|
||||
const ctx = scratch(t);
|
||||
const shown = await cliproxyOrg(ctx, 'cli');
|
||||
await orgUpdate(ctx, 'cli', withProviders(shown.doc, []), { ifRevision: shown.revision });
|
||||
assert.deepEqual((await orgService.showOrg(ctx, 'cli')).doc.plugins['model-provider'], []);
|
||||
assert.deepEqual(receipts(ctx.dataRoot).map(r => [r.op, r.org]), [['ride-removed', 'cli']]);
|
||||
const writes = [];
|
||||
const call = async verb => {
|
||||
if (verb === 'plugins') return { revision: 'r', plugins: { 'model-provider': [] } };
|
||||
if (verb === 'proxy.accounts') return { accounts: [] };
|
||||
if (verb === 'plugins.set') { writes.push('cli'); return { revision: 'r2' }; }
|
||||
throw new Error(`unexpected ${verb}`);
|
||||
};
|
||||
const proxy = createProxyAdmin({ dataRoot: ctx.dataRoot, configDir: ctx.configDir, unit: { unit: 'u', status: async () => ({}) }, buses: async () => ({ stopped: true, running: [] }), fetch: async () => ({}),
|
||||
riders: async () => [{ id: 'cli', fixture: false, cliproxy: false, changes: '', call }] });
|
||||
const r = await proxy.rideAll();
|
||||
assert.deepEqual(r.added, []); assert.deepEqual(r.skipped, [{ id: 'cli', reason: 'removed' }]);
|
||||
assert.deepEqual(writes, []);
|
||||
});
|
||||
|
||||
test('mosaic org update records only a removal of cliproxy, and saves nothing when the record cannot be written', async t => {
|
||||
const ctx = scratch(t);
|
||||
const recorded = [];
|
||||
const record = async id => { recorded.push(id); };
|
||||
// Keeping cliproxy, or an organization that never had it, records nothing.
|
||||
let shown = await cliproxyOrg(ctx, 'keep');
|
||||
await orgUpdate(ctx, 'keep', { ...shown.doc }, { ifRevision: shown.revision, record });
|
||||
const plain = await orgService.templateRender('plain');
|
||||
await orgService.createOrg(ctx, 'plain', withProviders(plain, []));
|
||||
shown = await orgService.showOrg(ctx, 'plain');
|
||||
await orgUpdate(ctx, 'plain', { ...shown.doc }, { ifRevision: shown.revision, record });
|
||||
assert.deepEqual(recorded, []);
|
||||
// A stale revision records nothing and is refused by the service.
|
||||
shown = await cliproxyOrg(ctx, 'stale');
|
||||
await assert.rejects(orgUpdate(ctx, 'stale', withProviders(shown.doc, []), { ifRevision: shown.revision + 1, record }), { code: 'revision-conflict' });
|
||||
assert.deepEqual(recorded, []);
|
||||
// A record that fails refuses the update: the organization keeps cliproxy, at the same revision.
|
||||
await assert.rejects(orgUpdate(ctx, 'stale', withProviders(shown.doc, []), { ifRevision: shown.revision, record: async () => { throw Object.assign(new Error('disk full at /x/proxy-admin.jsonl'), { code: 'ENOSPC' }); } }),
|
||||
err => err.exitCode === 3 && err.message === 'the removal of the model proxy could not be recorded, so nothing was saved');
|
||||
const after = await orgService.showOrg(ctx, 'stale');
|
||||
assert.deepEqual(after.doc.plugins['model-provider'], ['cliproxy']); assert.equal(after.revision, shown.revision);
|
||||
// The removal itself records once, before the write.
|
||||
await orgUpdate(ctx, 'stale', withProviders(shown.doc, []), { ifRevision: shown.revision, record: async id => { recorded.push(id); assert.deepEqual((await orgService.showOrg(ctx, 'stale')).doc.plugins['model-provider'], ['cliproxy'], 'recorded before the write'); } });
|
||||
assert.deepEqual(recorded, ['stale']);
|
||||
});
|
||||
|
||||
test('the mosaic org update command records the removal in the instance\'s own proxy receipts', async t => {
|
||||
const { spawnSync } = await import('node:child_process');
|
||||
const { resolve } = await import('node:path');
|
||||
const repo = resolve(import.meta.dirname, '../../..');
|
||||
const home = fs.mkdtempSync(join(tmpdir(), 'org-upd-cli-'));
|
||||
t.after(() => fs.rmSync(home, { recursive: true, force: true }));
|
||||
const env = { ...process.env, HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-orgupd/config.json') };
|
||||
const run = (cmd, ...args) => { const r = spawnSync(join(repo, cmd), args, { cwd: repo, env, encoding: 'utf8' }); assert.equal(r.status, 0, r.stderr); return r.stdout; };
|
||||
run('scripts/bootstrap.sh', '--instance', 'orgupd');
|
||||
const on = join(home, 'on.json'), off = join(home, 'off.json');
|
||||
const doc = withProviders(await orgService.templateRender('cliw'), ['cliproxy']);
|
||||
fs.writeFileSync(on, JSON.stringify(doc)); fs.writeFileSync(off, JSON.stringify(withProviders(doc, [])));
|
||||
run('scripts/mosaic', 'org', 'create', 'cliw', '--from', on);
|
||||
const { revision } = JSON.parse(run('scripts/mosaic', 'org', 'show', 'cliw', '--json'));
|
||||
run('scripts/mosaic', 'org', 'update', 'cliw', '--from', off, '--if-revision', String(revision));
|
||||
assert.deepEqual(JSON.parse(run('scripts/mosaic', 'org', 'show', 'cliw', '--json')).doc.plugins['model-provider'], []);
|
||||
assert.deepEqual(receipts(join(home, '.mosaic-orgupd')).map(r => [r.op, r.org]), [['ride-removed', 'cliw']]);
|
||||
});
|
||||
@@ -67,6 +67,10 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
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' };
|
||||
// The receipts are read again here: a removal saved while this run worked on earlier
|
||||
// organizations is on file now, and the decision above came from the run's first read.
|
||||
const now = ridden()(id);
|
||||
if (now !== false) return { id, outcome: 'skipped', reason: now === null ? 'unreadable' : 'removed' };
|
||||
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) {
|
||||
|
||||
@@ -179,6 +179,28 @@ test('each add leaves its intent receipt before plugins.set, and an add whose ou
|
||||
assert.deepEqual(receipts(e.dataRoot).filter(r => r.org === 'l').map(r => r.op), ['ride-intent']);
|
||||
});
|
||||
|
||||
test('a removal saved while Ride all works on earlier organizations is read before the later add', async t => {
|
||||
// Re-check 3, P3-1: the run's first read of the receipts showed nothing for 'late'. While the run
|
||||
// reads 'first', the owner takes cliproxy off 'late' on its admin page (a ride-removed receipt).
|
||||
let a = null;
|
||||
const first = bus({ providers: [] }), late = bus({ providers: [] });
|
||||
const firstCall = async (verb, args) => { if (verb === 'plugins') a.rideRemoved('late'); return first.call(verb, args); };
|
||||
const f = admin(t, { riders: async () => [{ id: 'first', fixture: false, cliproxy: false, changes: '', call: firstCall }, { id: 'late', fixture: false, cliproxy: false, changes: '', call: late.call }] });
|
||||
a = f.admin;
|
||||
const r = await a.rideAll();
|
||||
assert.deepEqual(r.added, ['first']); assert.deepEqual(r.skipped, [{ id: 'late', reason: 'removed' }]);
|
||||
assert.equal(late.calls.some(([v]) => v === 'plugins.set'), false);
|
||||
assert.deepEqual(receipts(f.dataRoot).filter(x => x.org === 'late').map(x => x.op), ['ride-removed']);
|
||||
// Receipts that turn unreadable during the run skip the later add as unreadable.
|
||||
const g1 = bus({ providers: [] }), g2 = bus({ providers: [] });
|
||||
let g = null;
|
||||
const tear = async (verb, args) => { if (verb === 'plugins') { appendReceipt(g.dataRoot, { op: 'fetch' }); fs.appendFileSync(receiptFile(g.dataRoot), '{torn\n'); } return g1.call(verb, args); };
|
||||
g = admin(t, { riders: async () => [{ id: 'one', fixture: false, cliproxy: false, changes: '', call: tear }, { id: 'two', fixture: false, cliproxy: false, changes: '', call: g2.call }] });
|
||||
const s = await g.admin.rideAll();
|
||||
assert.deepEqual(s.skipped, [{ id: 'one', reason: 'unreadable' }, { id: 'two', reason: 'unreadable' }]);
|
||||
assert.equal(g2.calls.some(([v]) => v === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
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 }] });
|
||||
|
||||
@@ -769,9 +769,9 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
'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.',
|
||||
unreadable: 'Its organization file, its change log or the proxy\'s receipts could not be read.',
|
||||
unreadable: 'Its organization file, its change log or the proxy\'s receipts could not be read. A damaged line in the proxy\'s receipts must be repaired by hand before Ride all adds again.',
|
||||
failed: 'Its bus refused the change.',
|
||||
'bus-running': 'Its bus is running. Stop its bus first, then move it.',
|
||||
'bus-running': 'Its bus is running, or its state could not be read. Stop or recover its bus, then move it.',
|
||||
'no-proxy': 'Set up the model proxy first.',
|
||||
'no-riding-bus': 'No organization uses the proxy yet, so no model list was read. Ride all first.',
|
||||
'no-model': 'The proxy lists no model for the harness its agents use.',
|
||||
|
||||
@@ -184,7 +184,8 @@ test('Ride all confirms the stricter admission, paints skips and stand-in organi
|
||||
await wait('window.__writes.some(w=>w.path==="/api/proxy/admin/ride-move")');
|
||||
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.")');
|
||||
// Re-check 3 (P3-2): a running bus and a bus whose state cannot be read get the same refusal, in words that fit both.
|
||||
await wait(`document.querySelector("#proxy-ride-message").textContent===${JSON.stringify('west was not moved. Its bus is running, or its state could not be read. Stop or recover its bus, then move it.')}`);
|
||||
// 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).']]) {
|
||||
@@ -195,13 +196,15 @@ test('Ride all confirms the stricter admission, paints skips and stand-in organi
|
||||
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 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' }, { id: 'east', reason: 'unreadable' }], 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');
|
||||
await wait('document.querySelectorAll("#proxy-ride-skips li").length===3');
|
||||
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.']);
|
||||
'Skipped south: The proxy could not record the change first, so it was not made.',
|
||||
// Re-check 3 (P3-3): a damaged receipts line stops every add until someone repairs it.
|
||||
'Skipped east: Its organization file, its change log or the proxy\'s receipts could not be read. A damaged line in the proxy\'s receipts must be repaired by hand before Ride all adds again.']);
|
||||
assert.deepEqual(await b.evaluate('window.__unexpected'), []);
|
||||
});
|
||||
Reference in new issue
Block a user